A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1//
2// This program is free software: you can redistribute it and/or modify
3// it under the terms of the GNU Affero General Public License as published by
4// the Free Software Foundation, either version 3 of the License, or
5// (at your option) any later version.
6//
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10// GNU Affero General Public License for more details.
11//
12// You should have received a copy of the GNU Affero General Public License
13// along with this program. If not, see <https://www.gnu.org/licenses/>.
14
15package sql
16
17import (
18 "fmt"
19
20 "github.com/88250/lute/ast"
21 "github.com/siyuan-note/siyuan/kernel/av"
22 "github.com/siyuan-note/siyuan/kernel/util"
23)
24
25func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query string, depth *int, cachedAttrViews map[string]*av.AttributeView) (ret *av.Table) {
26 viewable := attrView.RenderedViewables[view.ID]
27 if nil != viewable {
28 ret = viewable.(*av.Table)
29 return
30 }
31
32 ret = &av.Table{
33 BaseInstance: av.NewViewBaseInstance(view),
34 Columns: []*av.TableColumn{},
35 Rows: []*av.TableRow{},
36 }
37
38 // 组装列
39 for _, col := range view.Table.Columns {
40 key, getErr := attrView.GetKey(col.ID)
41 if nil != getErr {
42 // 找不到字段则在视图中删除
43 removeMissingField(attrView, view, col.ID)
44 continue
45 }
46
47 ret.Columns = append(ret.Columns, &av.TableColumn{
48 BaseInstanceField: &av.BaseInstanceField{
49 ID: key.ID,
50 Name: key.Name,
51 Type: key.Type,
52 Icon: key.Icon,
53 Wrap: col.Wrap,
54 Hidden: col.Hidden,
55 Desc: key.Desc,
56 Calc: col.Calc,
57 Options: key.Options,
58 NumberFormat: key.NumberFormat,
59 Template: key.Template,
60 Relation: key.Relation,
61 Rollup: key.Rollup,
62 Date: key.Date,
63 },
64 Width: col.Width,
65 Pin: col.Pin,
66 })
67 }
68
69 rowsValues := generateAttrViewItems(attrView, view) // 生成行
70 filterNotFoundAttrViewItems(rowsValues) // 过滤掉不存在的行
71
72 // 生成行单元格
73 for rowID, rowValues := range rowsValues {
74 var tableRow av.TableRow
75 for _, col := range ret.Columns {
76 var tableCell *av.TableCell
77 for _, keyValues := range rowValues {
78 if keyValues.Key.ID == col.ID {
79 tableCell = &av.TableCell{
80 BaseValue: &av.BaseValue{
81 ID: keyValues.Values[0].ID,
82 Value: keyValues.Values[0],
83 ValueType: col.Type,
84 },
85 }
86 break
87 }
88 }
89 if nil == tableCell {
90 tableCell = &av.TableCell{
91 BaseValue: &av.BaseValue{
92 ID: rowID[:14] + ast.NewNodeID()[14:],
93 ValueType: col.Type,
94 },
95 }
96 }
97 tableRow.ID = rowID
98
99 filedDateIsTime := false
100 if nil != col.Date {
101 filedDateIsTime = col.Date.FillSpecificTime
102 }
103 fillAttributeViewBaseValue(tableCell.BaseValue, col.ID, rowID, col.NumberFormat, col.Template, filedDateIsTime)
104 tableRow.Cells = append(tableRow.Cells, tableCell)
105 }
106 ret.Rows = append(ret.Rows, &tableRow)
107 }
108
109 // 回填补全数据
110 fillAttributeViewKeyValues(attrView, ret)
111
112 // 批量获取块属性以提升性能
113 var ialIDs []string
114 for _, row := range ret.Rows {
115 blockVal := row.GetBlockValue()
116 if nil != blockVal && !blockVal.IsDetached {
117 ialIDs = append(ialIDs, blockVal.Block.ID)
118 }
119 }
120 ials := BatchGetBlockAttrs(ialIDs)
121
122 // 渲染自动生成的字段值,比如关联、汇总、创建时间和更新时间
123 fillAttributeViewAutoGeneratedValues(attrView, ret, ials, depth, cachedAttrViews)
124
125 // 最后渲染模板字段,这样模板就可以使用汇总、关联、创建时间和更新时间的值了
126 renderTemplateErr := fillAttributeViewTemplateValues(attrView, view, ret, ials)
127 if nil != renderTemplateErr {
128 util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
129 }
130
131 filterByQuery(query, ret)
132 manualSort(view, ret)
133 return
134}