mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 Database grouping by field https://github.com/siyuan-note/siyuan/issues/10964
This commit is contained in:
parent
2b753ac16f
commit
0341a1cb03
13 changed files with 2183 additions and 1939 deletions
|
|
@ -203,8 +203,8 @@ type View struct {
|
||||||
|
|
||||||
// GroupCalc 描述了分组计算规则和结果的结构。
|
// GroupCalc 描述了分组计算规则和结果的结构。
|
||||||
type GroupCalc struct {
|
type GroupCalc struct {
|
||||||
Field string `json:"field"` // 字段 ID
|
Field string `json:"field"` // 字段 ID
|
||||||
FieldCalc `json:"calc"` // 计算规则和结果
|
FieldCalc *FieldCalc `json:"calc"` // 计算规则和结果
|
||||||
}
|
}
|
||||||
|
|
||||||
// LayoutType 描述了视图布局类型。
|
// LayoutType 描述了视图布局类型。
|
||||||
|
|
@ -267,15 +267,6 @@ func NewGalleryView() (ret *View) {
|
||||||
// Viewable 描述了视图的接口。
|
// Viewable 描述了视图的接口。
|
||||||
type Viewable interface {
|
type Viewable interface {
|
||||||
|
|
||||||
// Filter 根据视图中设置的过滤器进行过滤。
|
|
||||||
Filter(attrView *AttributeView)
|
|
||||||
|
|
||||||
// Sort 根据视图中设置的排序规则进行排序。
|
|
||||||
Sort(attrView *AttributeView)
|
|
||||||
|
|
||||||
// Calc 根据视图中设置的计算规则进行计算。
|
|
||||||
Calc()
|
|
||||||
|
|
||||||
// GetType 获取视图的布局类型。
|
// GetType 获取视图的布局类型。
|
||||||
GetType() LayoutType
|
GetType() LayoutType
|
||||||
|
|
||||||
|
|
@ -284,6 +275,24 @@ type Viewable interface {
|
||||||
|
|
||||||
// SetGroups 设置视图分组列表。
|
// SetGroups 设置视图分组列表。
|
||||||
SetGroups(viewables []Viewable)
|
SetGroups(viewables []Viewable)
|
||||||
|
|
||||||
|
// SetGroupCalc 设置视图分组计算规则和结果。
|
||||||
|
SetGroupCalc(group *GroupCalc)
|
||||||
|
|
||||||
|
// GetGroupCalc 获取视图分组计算规则和结果。
|
||||||
|
GetGroupCalc() *GroupCalc
|
||||||
|
|
||||||
|
// SetGroupName 设置分组名称。
|
||||||
|
SetGroupName(name string)
|
||||||
|
|
||||||
|
// SetGroupFolded 设置分组是否折叠。
|
||||||
|
SetGroupFolded(folded bool)
|
||||||
|
|
||||||
|
// SetGroupHidden 设置分组是否隐藏。
|
||||||
|
SetGroupHidden(hidden bool)
|
||||||
|
|
||||||
|
// SetGroupDefault 设置分组是否为默认分组。
|
||||||
|
SetGroupDefault(defaulted bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAttributeView(id string) (ret *AttributeView) {
|
func NewAttributeView(id string) (ret *AttributeView) {
|
||||||
|
|
|
||||||
1798
kernel/av/calc.go
1798
kernel/av/calc.go
File diff suppressed because it is too large
Load diff
|
|
@ -76,6 +76,58 @@ const (
|
||||||
FilterOperatorIsFalse FilterOperator = "Is false"
|
FilterOperatorIsFalse FilterOperator = "Is false"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Filter(viewable Viewable, attrView *AttributeView) {
|
||||||
|
collection := viewable.(Collection)
|
||||||
|
filters := collection.GetFilters()
|
||||||
|
if 1 > len(filters) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var colIndexes []int
|
||||||
|
for _, f := range filters {
|
||||||
|
for i, c := range collection.GetFields() {
|
||||||
|
if c.GetID() == f.Column {
|
||||||
|
colIndexes = append(colIndexes, i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []Item
|
||||||
|
attrViewCache := map[string]*AttributeView{}
|
||||||
|
attrViewCache[attrView.ID] = attrView
|
||||||
|
for _, item := range collection.GetItems() {
|
||||||
|
pass := true
|
||||||
|
values := item.GetValues()
|
||||||
|
for j, index := range colIndexes {
|
||||||
|
operator := filters[j].Operator
|
||||||
|
|
||||||
|
if nil == values[index] {
|
||||||
|
if FilterOperatorIsNotEmpty == operator {
|
||||||
|
pass = false
|
||||||
|
} else if FilterOperatorIsEmpty == operator {
|
||||||
|
pass = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if KeyTypeText != values[index].Type {
|
||||||
|
pass = false
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !values[index].Filter(filters[j], attrView, item.GetID(), &attrViewCache) {
|
||||||
|
pass = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pass {
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
collection.SetItems(items)
|
||||||
|
}
|
||||||
|
|
||||||
func (value *Value) Filter(filter *ViewFilter, attrView *AttributeView, rowID string, attrViewCache *map[string]*AttributeView) bool {
|
func (value *Value) Filter(filter *ViewFilter, attrView *AttributeView, rowID string, attrViewCache *map[string]*AttributeView) bool {
|
||||||
if nil == filter || (nil == filter.Value && nil == filter.RelativeDate) {
|
if nil == filter || (nil == filter.Value && nil == filter.RelativeDate) {
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package av
|
package av
|
||||||
|
|
||||||
import "sort"
|
|
||||||
|
|
||||||
// BaseLayout 描述了布局的基础结构。
|
// BaseLayout 描述了布局的基础结构。
|
||||||
type BaseLayout struct {
|
type BaseLayout struct {
|
||||||
Spec int `json:"spec"` // 布局格式版本
|
Spec int `json:"spec"` // 布局格式版本
|
||||||
|
|
@ -38,10 +36,11 @@ type BaseLayout struct {
|
||||||
|
|
||||||
// BaseField 描述了字段的基础结构。
|
// BaseField 描述了字段的基础结构。
|
||||||
type BaseField struct {
|
type BaseField struct {
|
||||||
ID string `json:"id"` // 字段 ID
|
ID string `json:"id"` // 字段 ID
|
||||||
Wrap bool `json:"wrap"` // 是否换行
|
Wrap bool `json:"wrap"` // 是否换行
|
||||||
Hidden bool `json:"hidden"` // 是否隐藏
|
Hidden bool `json:"hidden"` // 是否隐藏
|
||||||
Desc string `json:"desc,omitempty"` // 字段描述
|
Desc string `json:"desc,omitempty"` // 字段描述
|
||||||
|
Calc *FieldCalc `json:"calc,omitempty"` // 计算规则
|
||||||
}
|
}
|
||||||
|
|
||||||
// BaseValue 描述了字段值的基础结构。
|
// BaseValue 描述了字段值的基础结构。
|
||||||
|
|
@ -67,8 +66,32 @@ type BaseInstance struct {
|
||||||
Folded bool `json:"folded,omitempty"` // 是否折叠
|
Folded bool `json:"folded,omitempty"` // 是否折叠
|
||||||
Hidden bool `json:"hidden,omitempty"` // 是否隐藏
|
Hidden bool `json:"hidden,omitempty"` // 是否隐藏
|
||||||
|
|
||||||
Groups []Viewable `json:"groups,omitempty"` // 分组实例列表
|
Groups []Viewable `json:"groups,omitempty"` // 分组实例列表
|
||||||
GroupCalc *GroupCalc `json:"groupCalc,omitempty"` // 分组计算规则和结果
|
GroupCalc *GroupCalc `json:"groupCalc,omitempty"` // 分组计算规则和结果
|
||||||
|
GroupName string `json:"groupName,omitempty"` // 分组名称
|
||||||
|
GroupFolded bool `json:"groupFolded,omitempty"` // 分组是否折叠
|
||||||
|
GroupHidden bool `json:"groupHidden,omitempty"` // 分组是否隐藏
|
||||||
|
GroupDefault bool `json:"groupDefault,omitempty"` // 是否为默认分组
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewViewBaseInstance(view *View) *BaseInstance {
|
||||||
|
return &BaseInstance{
|
||||||
|
ID: view.ID,
|
||||||
|
Icon: view.Icon,
|
||||||
|
Name: view.Name,
|
||||||
|
Desc: view.Desc,
|
||||||
|
HideAttrViewName: view.HideAttrViewName,
|
||||||
|
Filters: view.Filters,
|
||||||
|
Sorts: view.Sorts,
|
||||||
|
Group: view.Group,
|
||||||
|
GroupCalc: view.GroupCalc,
|
||||||
|
GroupName: view.GroupName,
|
||||||
|
GroupFolded: view.GroupFolded,
|
||||||
|
GroupHidden: view.GroupHidden,
|
||||||
|
GroupDefault: view.GroupDefault,
|
||||||
|
ShowIcon: view.Table.ShowIcon,
|
||||||
|
WrapField: view.Table.WrapField,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (baseInstance *BaseInstance) GetSorts() []*ViewSort {
|
func (baseInstance *BaseInstance) GetSorts() []*ViewSort {
|
||||||
|
|
@ -83,19 +106,44 @@ func (baseInstance *BaseInstance) SetGroups(viewables []Viewable) {
|
||||||
baseInstance.Groups = viewables
|
baseInstance.Groups = viewables
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) SetGroupCalc(group *GroupCalc) {
|
||||||
|
baseInstance.GroupCalc = group
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) GetGroupCalc() *GroupCalc {
|
||||||
|
return baseInstance.GroupCalc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) SetGroupName(name string) {
|
||||||
|
baseInstance.GroupName = name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) SetGroupFolded(folded bool) {
|
||||||
|
baseInstance.GroupFolded = folded
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) SetGroupHidden(hidden bool) {
|
||||||
|
baseInstance.GroupHidden = hidden
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstance *BaseInstance) SetGroupDefault(defaulted bool) {
|
||||||
|
baseInstance.GroupDefault = defaulted
|
||||||
|
}
|
||||||
|
|
||||||
func (baseInstance *BaseInstance) GetID() string {
|
func (baseInstance *BaseInstance) GetID() string {
|
||||||
return baseInstance.ID
|
return baseInstance.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// BaseInstanceField 描述了实例字段的基础结构。
|
// BaseInstanceField 描述了实例字段的基础结构。
|
||||||
type BaseInstanceField struct {
|
type BaseInstanceField struct {
|
||||||
ID string `json:"id"` // ID
|
ID string `json:"id"` // ID
|
||||||
Name string `json:"name"` // 名称
|
Name string `json:"name"` // 名称
|
||||||
Type KeyType `json:"type"` // 类型
|
Type KeyType `json:"type"` // 类型
|
||||||
Icon string `json:"icon"` // 图标
|
Icon string `json:"icon"` // 图标
|
||||||
Wrap bool `json:"wrap"` // 是否换行
|
Wrap bool `json:"wrap"` // 是否换行
|
||||||
Hidden bool `json:"hidden"` // 是否隐藏
|
Hidden bool `json:"hidden"` // 是否隐藏
|
||||||
Desc string `json:"desc"` // 描述
|
Desc string `json:"desc"` // 描述
|
||||||
|
Calc *FieldCalc `json:"calc"` // 计算规则和结果
|
||||||
|
|
||||||
// 以下是某些字段类型的特有属性
|
// 以下是某些字段类型的特有属性
|
||||||
|
|
||||||
|
|
@ -111,6 +159,22 @@ func (baseInstanceField *BaseInstanceField) GetID() string {
|
||||||
return baseInstanceField.ID
|
return baseInstanceField.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (baseInstanceField *BaseInstanceField) GetCalc() *FieldCalc {
|
||||||
|
return baseInstanceField.Calc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstanceField *BaseInstanceField) SetCalc(calc *FieldCalc) {
|
||||||
|
baseInstanceField.Calc = calc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstanceField *BaseInstanceField) GetType() KeyType {
|
||||||
|
return baseInstanceField.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseInstanceField *BaseInstanceField) GetNumberFormat() NumberFormat {
|
||||||
|
return baseInstanceField.NumberFormat
|
||||||
|
}
|
||||||
|
|
||||||
// Collection 描述了一个集合的接口。
|
// Collection 描述了一个集合的接口。
|
||||||
// 集合可以是表格、卡片等,包含多个项目。
|
// 集合可以是表格、卡片等,包含多个项目。
|
||||||
type Collection interface {
|
type Collection interface {
|
||||||
|
|
@ -124,6 +188,9 @@ type Collection interface {
|
||||||
// GetFields 返回集合的所有字段。
|
// GetFields 返回集合的所有字段。
|
||||||
GetFields() []Field
|
GetFields() []Field
|
||||||
|
|
||||||
|
// GetField 返回指定 ID 的字段。
|
||||||
|
GetField(id string) (ret Field)
|
||||||
|
|
||||||
// GetSorts 返回集合的排序规则。
|
// GetSorts 返回集合的排序规则。
|
||||||
GetSorts() []*ViewSort
|
GetSorts() []*ViewSort
|
||||||
|
|
||||||
|
|
@ -136,6 +203,18 @@ type Field interface {
|
||||||
|
|
||||||
// GetID 返回字段的 ID。
|
// GetID 返回字段的 ID。
|
||||||
GetID() string
|
GetID() string
|
||||||
|
|
||||||
|
// GetType 返回字段的类型。
|
||||||
|
GetType() KeyType
|
||||||
|
|
||||||
|
// GetCalc 返回字段的计算规则和结果。
|
||||||
|
GetCalc() *FieldCalc
|
||||||
|
|
||||||
|
// SetCalc 设置字段的计算规则和结果。
|
||||||
|
SetCalc(*FieldCalc)
|
||||||
|
|
||||||
|
// GetNumberFormat 返回数字字段的格式化设置。
|
||||||
|
GetNumberFormat() NumberFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
// Item 描述了一个项目的接口。
|
// Item 描述了一个项目的接口。
|
||||||
|
|
@ -154,169 +233,3 @@ type Item interface {
|
||||||
// GetID 返回项目的 ID。
|
// GetID 返回项目的 ID。
|
||||||
GetID() string
|
GetID() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func sort0(collection Collection, attrView *AttributeView) {
|
|
||||||
sorts := collection.GetSorts()
|
|
||||||
if 1 > len(sorts) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type FieldIndexSort struct {
|
|
||||||
Index int
|
|
||||||
Order SortOrder
|
|
||||||
}
|
|
||||||
|
|
||||||
var fieldIndexSorts []*FieldIndexSort
|
|
||||||
for _, s := range sorts {
|
|
||||||
for i, c := range collection.GetFields() {
|
|
||||||
if c.GetID() == s.Column {
|
|
||||||
fieldIndexSorts = append(fieldIndexSorts, &FieldIndexSort{Index: i, Order: s.Order})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
items := collection.GetItems()
|
|
||||||
editedValItems := map[string]bool{}
|
|
||||||
for i, item := range items {
|
|
||||||
for _, fieldIndexSort := range fieldIndexSorts {
|
|
||||||
val := items[i].GetValues()[fieldIndexSort.Index]
|
|
||||||
if KeyTypeCheckbox == val.Type {
|
|
||||||
if block := item.GetBlockValue(); nil != block && block.IsEdited() {
|
|
||||||
// 如果主键编辑过,则勾选框也算作编辑过,参与排序 https://github.com/siyuan-note/siyuan/issues/11016
|
|
||||||
editedValItems[item.GetID()] = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if val.IsEdited() {
|
|
||||||
// 如果该卡片某字段的值已经编辑过,则该卡片可参与排序
|
|
||||||
editedValItems[item.GetID()] = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将未编辑的卡片和已编辑的卡片分开排序
|
|
||||||
var uneditedItems, editedItems []Item
|
|
||||||
for _, item := range items {
|
|
||||||
if _, ok := editedValItems[item.GetID()]; ok {
|
|
||||||
editedItems = append(editedItems, item)
|
|
||||||
} else {
|
|
||||||
uneditedItems = append(uneditedItems, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(uneditedItems, func(i, j int) bool {
|
|
||||||
val1 := uneditedItems[i].GetBlockValue()
|
|
||||||
if nil == val1 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
val2 := uneditedItems[j].GetBlockValue()
|
|
||||||
if nil == val2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return val1.CreatedAt < val2.CreatedAt
|
|
||||||
})
|
|
||||||
|
|
||||||
sort.Slice(editedItems, func(i, j int) bool {
|
|
||||||
sorted := true
|
|
||||||
for _, fieldIndexSort := range fieldIndexSorts {
|
|
||||||
val1 := editedItems[i].GetValues()[fieldIndexSort.Index]
|
|
||||||
val2 := editedItems[j].GetValues()[fieldIndexSort.Index]
|
|
||||||
if nil == val1 || val1.IsEmpty() {
|
|
||||||
if nil != val2 && !val2.IsEmpty() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
sorted = false
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
if nil == val2 || val2.IsEmpty() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := val1.Compare(val2, attrView)
|
|
||||||
if 0 == result {
|
|
||||||
sorted = false
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sorted = true
|
|
||||||
|
|
||||||
if fieldIndexSort.Order == SortOrderAsc {
|
|
||||||
return 0 > result
|
|
||||||
}
|
|
||||||
return 0 < result
|
|
||||||
}
|
|
||||||
|
|
||||||
if !sorted {
|
|
||||||
key1 := editedItems[i].GetBlockValue()
|
|
||||||
if nil == key1 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
key2 := editedItems[j].GetBlockValue()
|
|
||||||
if nil == key2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return key1.CreatedAt < key2.CreatedAt
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
// 将包含未编辑的卡片放在最后
|
|
||||||
collection.SetItems(append(editedItems, uneditedItems...))
|
|
||||||
if 1 > len(collection.GetItems()) {
|
|
||||||
collection.SetItems([]Item{})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func filter0(collection Collection, attrView *AttributeView) {
|
|
||||||
filters := collection.GetFilters()
|
|
||||||
if 1 > len(filters) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var colIndexes []int
|
|
||||||
for _, f := range filters {
|
|
||||||
for i, c := range collection.GetFields() {
|
|
||||||
if c.GetID() == f.Column {
|
|
||||||
colIndexes = append(colIndexes, i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var items []Item
|
|
||||||
attrViewCache := map[string]*AttributeView{}
|
|
||||||
attrViewCache[attrView.ID] = attrView
|
|
||||||
for _, item := range collection.GetItems() {
|
|
||||||
pass := true
|
|
||||||
values := item.GetValues()
|
|
||||||
for j, index := range colIndexes {
|
|
||||||
operator := filters[j].Operator
|
|
||||||
|
|
||||||
if nil == values[index] {
|
|
||||||
if FilterOperatorIsNotEmpty == operator {
|
|
||||||
pass = false
|
|
||||||
} else if FilterOperatorIsEmpty == operator {
|
|
||||||
pass = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if KeyTypeText != values[index].Type {
|
|
||||||
pass = false
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if !values[index].Filter(filters[j], attrView, item.GetID(), &attrViewCache) {
|
|
||||||
pass = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if pass {
|
|
||||||
items = append(items, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
collection.SetItems(items)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -173,14 +173,15 @@ func (gallery *Gallery) GetFields() (ret []Field) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gallery *Gallery) GetField(id string) Field {
|
||||||
|
for _, field := range gallery.Fields {
|
||||||
|
if field.ID == id {
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (gallery *Gallery) GetType() LayoutType {
|
func (gallery *Gallery) GetType() LayoutType {
|
||||||
return LayoutTypeGallery
|
return LayoutTypeGallery
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gallery *Gallery) Sort(attrView *AttributeView) {
|
|
||||||
sort0(gallery, attrView)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gallery *Gallery) Filter(attrView *AttributeView) {
|
|
||||||
filter0(gallery, attrView)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
// SiYuan - Refactor your thinking
|
|
||||||
// Copyright (c) 2020-present, b3log.org
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package av
|
|
||||||
|
|
||||||
func (gallery *Gallery) Calc() {
|
|
||||||
// 卡片视图不支持计算
|
|
||||||
}
|
|
||||||
|
|
@ -63,9 +63,8 @@ type Table struct {
|
||||||
type TableColumn struct {
|
type TableColumn struct {
|
||||||
*BaseInstanceField
|
*BaseInstanceField
|
||||||
|
|
||||||
Pin bool `json:"pin"` // 是否固定
|
Pin bool `json:"pin"` // 是否固定
|
||||||
Width string `json:"width"` // 列宽度
|
Width string `json:"width"` // 列宽度
|
||||||
Calc *FieldCalc `json:"calc"` // 计算规则和结果
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableRow 描述了表格实例行的结构。
|
// TableRow 描述了表格实例行的结构。
|
||||||
|
|
@ -150,18 +149,15 @@ func (table *Table) GetFields() (ret []Field) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (table *Table) GetField(id string) Field {
|
||||||
|
for _, column := range table.Columns {
|
||||||
|
if column.ID == id {
|
||||||
|
return column
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (*Table) GetType() LayoutType {
|
func (*Table) GetType() LayoutType {
|
||||||
return LayoutTypeTable
|
return LayoutTypeTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) GetID() string {
|
|
||||||
return table.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) Sort(attrView *AttributeView) {
|
|
||||||
sort0(table, attrView)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) Filter(attrView *AttributeView) {
|
|
||||||
filter0(table, attrView)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -18,6 +18,7 @@ package av
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -37,6 +38,122 @@ const (
|
||||||
SortOrderDesc SortOrder = "DESC"
|
SortOrderDesc SortOrder = "DESC"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Sort(viewable Viewable, attrView *AttributeView) {
|
||||||
|
collection := viewable.(Collection)
|
||||||
|
sorts := collection.GetSorts()
|
||||||
|
if 1 > len(sorts) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type FieldIndexSort struct {
|
||||||
|
Index int
|
||||||
|
Order SortOrder
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldIndexSorts []*FieldIndexSort
|
||||||
|
for _, s := range sorts {
|
||||||
|
for i, c := range collection.GetFields() {
|
||||||
|
if c.GetID() == s.Column {
|
||||||
|
fieldIndexSorts = append(fieldIndexSorts, &FieldIndexSort{Index: i, Order: s.Order})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
items := collection.GetItems()
|
||||||
|
editedValItems := map[string]bool{}
|
||||||
|
for i, item := range items {
|
||||||
|
for _, fieldIndexSort := range fieldIndexSorts {
|
||||||
|
val := items[i].GetValues()[fieldIndexSort.Index]
|
||||||
|
if KeyTypeCheckbox == val.Type {
|
||||||
|
if block := item.GetBlockValue(); nil != block && block.IsEdited() {
|
||||||
|
// 如果主键编辑过,则勾选框也算作编辑过,参与排序 https://github.com/siyuan-note/siyuan/issues/11016
|
||||||
|
editedValItems[item.GetID()] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.IsEdited() {
|
||||||
|
// 如果该卡片某字段的值已经编辑过,则该卡片可参与排序
|
||||||
|
editedValItems[item.GetID()] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将未编辑的卡片和已编辑的卡片分开排序
|
||||||
|
var uneditedItems, editedItems []Item
|
||||||
|
for _, item := range items {
|
||||||
|
if _, ok := editedValItems[item.GetID()]; ok {
|
||||||
|
editedItems = append(editedItems, item)
|
||||||
|
} else {
|
||||||
|
uneditedItems = append(uneditedItems, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(uneditedItems, func(i, j int) bool {
|
||||||
|
val1 := uneditedItems[i].GetBlockValue()
|
||||||
|
if nil == val1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
val2 := uneditedItems[j].GetBlockValue()
|
||||||
|
if nil == val2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return val1.CreatedAt < val2.CreatedAt
|
||||||
|
})
|
||||||
|
|
||||||
|
sort.Slice(editedItems, func(i, j int) bool {
|
||||||
|
sorted := true
|
||||||
|
for _, fieldIndexSort := range fieldIndexSorts {
|
||||||
|
val1 := editedItems[i].GetValues()[fieldIndexSort.Index]
|
||||||
|
val2 := editedItems[j].GetValues()[fieldIndexSort.Index]
|
||||||
|
if nil == val1 || val1.IsEmpty() {
|
||||||
|
if nil != val2 && !val2.IsEmpty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
sorted = false
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
if nil == val2 || val2.IsEmpty() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := val1.Compare(val2, attrView)
|
||||||
|
if 0 == result {
|
||||||
|
sorted = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sorted = true
|
||||||
|
|
||||||
|
if fieldIndexSort.Order == SortOrderAsc {
|
||||||
|
return 0 > result
|
||||||
|
}
|
||||||
|
return 0 < result
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sorted {
|
||||||
|
key1 := editedItems[i].GetBlockValue()
|
||||||
|
if nil == key1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
key2 := editedItems[j].GetBlockValue()
|
||||||
|
if nil == key2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return key1.CreatedAt < key2.CreatedAt
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
// 将包含未编辑的卡片放在最后
|
||||||
|
collection.SetItems(append(editedItems, uneditedItems...))
|
||||||
|
if 1 > len(collection.GetItems()) {
|
||||||
|
collection.SetItems([]Item{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (value *Value) Compare(other *Value, attrView *AttributeView) int {
|
func (value *Value) Compare(other *Value, attrView *AttributeView) int {
|
||||||
switch value.Type {
|
switch value.Type {
|
||||||
case KeyTypeBlock:
|
case KeyTypeBlock:
|
||||||
|
|
|
||||||
|
|
@ -1316,9 +1316,74 @@ func renderViewableInstance(viewable av.Viewable, view *av.View, attrView *av.At
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
viewable.Filter(attrView)
|
av.Filter(viewable, attrView)
|
||||||
viewable.Sort(attrView)
|
av.Sort(viewable, attrView)
|
||||||
viewable.Calc()
|
av.Calc(viewable)
|
||||||
|
|
||||||
|
if groupCalc := viewable.GetGroupCalc(); nil != groupCalc {
|
||||||
|
if groupCalcKey, _ := attrView.GetKey(groupCalc.Field); nil != groupCalcKey {
|
||||||
|
collection := viewable.(av.Collection)
|
||||||
|
var calcResult *av.GroupCalc
|
||||||
|
field := collection.GetField(groupCalcKey.ID)
|
||||||
|
if nil != field {
|
||||||
|
if calc := field.GetCalc(); nil != calc && field.GetID() == groupCalcKey.ID {
|
||||||
|
// 直接使用字段计算结果
|
||||||
|
calcResult = &av.GroupCalc{Field: groupCalcKey.ID, FieldCalc: calc}
|
||||||
|
}
|
||||||
|
|
||||||
|
if nil == calcResult {
|
||||||
|
for i, f := range collection.GetFields() {
|
||||||
|
if f.GetID() != groupCalcKey.ID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
field.SetCalc(groupCalc.FieldCalc)
|
||||||
|
|
||||||
|
switch field.GetType() {
|
||||||
|
case av.KeyTypeBlock:
|
||||||
|
av.CalcFieldBlock(collection, field, i)
|
||||||
|
case av.KeyTypeText:
|
||||||
|
av.CalcFieldText(collection, field, i)
|
||||||
|
case av.KeyTypeNumber:
|
||||||
|
av.CalcFieldNumber(collection, field, i)
|
||||||
|
case av.KeyTypeDate:
|
||||||
|
av.CalcFieldDate(collection, field, i)
|
||||||
|
case av.KeyTypeSelect:
|
||||||
|
av.CalcFieldSelect(collection, field, i)
|
||||||
|
case av.KeyTypeMSelect:
|
||||||
|
av.CalcFieldMSelect(collection, field, i)
|
||||||
|
case av.KeyTypeURL:
|
||||||
|
av.CalcFieldURL(collection, field, i)
|
||||||
|
case av.KeyTypeEmail:
|
||||||
|
av.CalcFieldEmail(collection, field, i)
|
||||||
|
case av.KeyTypePhone:
|
||||||
|
av.CalcFieldPhone(collection, field, i)
|
||||||
|
case av.KeyTypeMAsset:
|
||||||
|
av.CalcFieldMAsset(collection, field, i)
|
||||||
|
case av.KeyTypeTemplate:
|
||||||
|
av.CalcFieldTemplate(collection, field, i)
|
||||||
|
case av.KeyTypeCreated:
|
||||||
|
av.CalcFieldCreated(collection, field, i)
|
||||||
|
case av.KeyTypeUpdated:
|
||||||
|
av.CalcFieldUpdated(collection, field, i)
|
||||||
|
case av.KeyTypeCheckbox:
|
||||||
|
av.CalcFieldCheckbox(collection, field, i)
|
||||||
|
case av.KeyTypeRelation:
|
||||||
|
av.CalcFieldRelation(collection, field, i)
|
||||||
|
case av.KeyTypeRollup:
|
||||||
|
av.CalcFieldRollup(collection, field, i)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
calcResult = &av.GroupCalc{Field: groupCalcKey.ID, FieldCalc: field.GetCalc()}
|
||||||
|
field.SetCalc(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
viewable.SetGroupCalc(calcResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 分页
|
// 分页
|
||||||
switch viewable.GetType() {
|
switch viewable.GetType() {
|
||||||
|
|
@ -1368,8 +1433,8 @@ func GetCurrentAttributeViewImages(avID, viewID, query string) (ret []string, er
|
||||||
}
|
}
|
||||||
|
|
||||||
table := getAttrViewTable(attrView, view, query)
|
table := getAttrViewTable(attrView, view, query)
|
||||||
table.Filter(attrView)
|
av.Filter(table, attrView)
|
||||||
table.Sort(attrView)
|
av.Sort(table, attrView)
|
||||||
|
|
||||||
for _, row := range table.Rows {
|
for _, row := range table.Rows {
|
||||||
for _, cell := range row.Cells {
|
for _, cell := range row.Cells {
|
||||||
|
|
@ -2480,8 +2545,8 @@ func addAttributeViewBlock(now int64, avID, blockID, previousBlockID, addingBloc
|
||||||
|
|
||||||
if nil != view && 0 < len(view.Filters) && !ignoreFillFilter {
|
if nil != view && 0 < len(view.Filters) && !ignoreFillFilter {
|
||||||
viewable := sql.RenderView(view, attrView, "")
|
viewable := sql.RenderView(view, attrView, "")
|
||||||
viewable.Filter(attrView)
|
av.Filter(viewable, attrView)
|
||||||
viewable.Sort(attrView)
|
av.Sort(viewable, attrView)
|
||||||
|
|
||||||
collection := viewable.(av.Collection)
|
collection := viewable.(av.Collection)
|
||||||
items := collection.GetItems()
|
items := collection.GetItems()
|
||||||
|
|
|
||||||
|
|
@ -83,8 +83,8 @@ func ExportAv2CSV(avID, blockID string) (zipPath string, err error) {
|
||||||
table := getAttrViewTable(attrView, view, "")
|
table := getAttrViewTable(attrView, view, "")
|
||||||
|
|
||||||
// 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
|
// 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
|
||||||
table.Filter(attrView)
|
av.Filter(table, attrView)
|
||||||
table.Sort(attrView)
|
av.Sort(table, attrView)
|
||||||
|
|
||||||
exportFolder := filepath.Join(util.TempDir, "export", "csv", name)
|
exportFolder := filepath.Join(util.TempDir, "export", "csv", name)
|
||||||
if err = os.MkdirAll(exportFolder, 0755); err != nil {
|
if err = os.MkdirAll(exportFolder, 0755); err != nil {
|
||||||
|
|
@ -2499,8 +2499,8 @@ func exportTree(tree *parse.Tree, wysiwyg, keepFold, avHiddenCol bool,
|
||||||
table := getAttrViewTable(attrView, view, "")
|
table := getAttrViewTable(attrView, view, "")
|
||||||
|
|
||||||
// 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
|
// 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
|
||||||
table.Filter(attrView)
|
av.Filter(table, attrView)
|
||||||
table.Sort(attrView)
|
av.Sort(table, attrView)
|
||||||
|
|
||||||
var aligns []int
|
var aligns []int
|
||||||
for range table.Columns {
|
for range table.Columns {
|
||||||
|
|
|
||||||
|
|
@ -20,18 +20,7 @@ import (
|
||||||
|
|
||||||
func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query string) (ret *av.Gallery) {
|
func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query string) (ret *av.Gallery) {
|
||||||
ret = &av.Gallery{
|
ret = &av.Gallery{
|
||||||
BaseInstance: &av.BaseInstance{
|
BaseInstance: av.NewViewBaseInstance(view),
|
||||||
ID: view.ID,
|
|
||||||
Icon: view.Icon,
|
|
||||||
Name: view.Name,
|
|
||||||
Desc: view.Desc,
|
|
||||||
HideAttrViewName: view.HideAttrViewName,
|
|
||||||
Filters: view.Filters,
|
|
||||||
Sorts: view.Sorts,
|
|
||||||
Group: view.Group,
|
|
||||||
ShowIcon: view.Gallery.ShowIcon,
|
|
||||||
WrapField: view.Gallery.WrapField,
|
|
||||||
},
|
|
||||||
CoverFrom: view.Gallery.CoverFrom,
|
CoverFrom: view.Gallery.CoverFrom,
|
||||||
CoverFromAssetKeyID: view.Gallery.CoverFromAssetKeyID,
|
CoverFromAssetKeyID: view.Gallery.CoverFromAssetKeyID,
|
||||||
CardAspectRatio: view.Gallery.CardAspectRatio,
|
CardAspectRatio: view.Gallery.CardAspectRatio,
|
||||||
|
|
@ -59,6 +48,7 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
|
||||||
Wrap: field.Wrap,
|
Wrap: field.Wrap,
|
||||||
Hidden: field.Hidden,
|
Hidden: field.Hidden,
|
||||||
Desc: key.Desc,
|
Desc: key.Desc,
|
||||||
|
Calc: field.Calc,
|
||||||
Options: key.Options,
|
Options: key.Options,
|
||||||
NumberFormat: key.NumberFormat,
|
NumberFormat: key.NumberFormat,
|
||||||
Template: key.Template,
|
Template: key.Template,
|
||||||
|
|
|
||||||
|
|
@ -24,20 +24,9 @@ import (
|
||||||
|
|
||||||
func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query string) (ret *av.Table) {
|
func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query string) (ret *av.Table) {
|
||||||
ret = &av.Table{
|
ret = &av.Table{
|
||||||
BaseInstance: &av.BaseInstance{
|
BaseInstance: av.NewViewBaseInstance(view),
|
||||||
ID: view.ID,
|
Columns: []*av.TableColumn{},
|
||||||
Icon: view.Icon,
|
Rows: []*av.TableRow{},
|
||||||
Name: view.Name,
|
|
||||||
Desc: view.Desc,
|
|
||||||
HideAttrViewName: view.HideAttrViewName,
|
|
||||||
Filters: view.Filters,
|
|
||||||
Sorts: view.Sorts,
|
|
||||||
Group: view.Group,
|
|
||||||
ShowIcon: view.Table.ShowIcon,
|
|
||||||
WrapField: view.Table.WrapField,
|
|
||||||
},
|
|
||||||
Columns: []*av.TableColumn{},
|
|
||||||
Rows: []*av.TableRow{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组装列
|
// 组装列
|
||||||
|
|
@ -58,6 +47,7 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
|
||||||
Wrap: col.Wrap,
|
Wrap: col.Wrap,
|
||||||
Hidden: col.Hidden,
|
Hidden: col.Hidden,
|
||||||
Desc: key.Desc,
|
Desc: key.Desc,
|
||||||
|
Calc: col.Calc,
|
||||||
Options: key.Options,
|
Options: key.Options,
|
||||||
NumberFormat: key.NumberFormat,
|
NumberFormat: key.NumberFormat,
|
||||||
Template: key.Template,
|
Template: key.Template,
|
||||||
|
|
@ -67,7 +57,6 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
|
||||||
},
|
},
|
||||||
Width: col.Width,
|
Width: col.Width,
|
||||||
Pin: col.Pin,
|
Pin: col.Pin,
|
||||||
Calc: col.Calc,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue