mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 Database gallery view https://github.com/siyuan-note/siyuan/issues/10414
This commit is contained in:
parent
e3f08bfafd
commit
0c3f06fa3e
3 changed files with 90 additions and 92 deletions
|
|
@ -44,6 +44,10 @@ type BaseInstance struct {
|
|||
PageSize int `json:"pageSize"` // 每页项目
|
||||
}
|
||||
|
||||
func (baseInstance *BaseInstance) GetFilters() []*ViewFilter {
|
||||
return baseInstance.Filters
|
||||
}
|
||||
|
||||
// BaseInstanceField 描述了实例字段的基础结构。
|
||||
type BaseInstanceField struct {
|
||||
ID string `json:"id"` // ID
|
||||
|
|
@ -63,6 +67,10 @@ type BaseInstanceField struct {
|
|||
Date *Date `json:"date,omitempty"` // 日期设置
|
||||
}
|
||||
|
||||
func (baseInstanceField *BaseInstanceField) GetID() string {
|
||||
return baseInstanceField.ID
|
||||
}
|
||||
|
||||
// CollectionLayout 描述了集合布局的接口。
|
||||
type CollectionLayout interface {
|
||||
|
||||
|
|
@ -79,6 +87,19 @@ type Collection interface {
|
|||
|
||||
// SetItems 设置集合中的项目。
|
||||
SetItems(items []Item)
|
||||
|
||||
// GetFields 返回集合的所有字段。
|
||||
GetFields() []Field
|
||||
|
||||
// GetFilters 返回集合的过滤规则。
|
||||
GetFilters() []*ViewFilter
|
||||
}
|
||||
|
||||
// Field 描述了一个字段的接口。
|
||||
type Field interface {
|
||||
|
||||
// GetID 返回字段的 ID。
|
||||
GetID() string
|
||||
}
|
||||
|
||||
// Item 描述了一个项目的接口。
|
||||
|
|
@ -97,3 +118,54 @@ type Item interface {
|
|||
// GetID 返回项目的 ID。
|
||||
GetID() string
|
||||
}
|
||||
|
||||
func filter(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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items := []Item{}
|
||||
attrViewCache := map[string]*AttributeView{}
|
||||
attrViewCache[attrView.ID] = attrView
|
||||
for _, row := range collection.GetItems() {
|
||||
pass := true
|
||||
values := row.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, row.GetID(), &attrViewCache) {
|
||||
pass = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if pass {
|
||||
items = append(items, row)
|
||||
}
|
||||
}
|
||||
collection.SetItems(items)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,6 +163,14 @@ func (gallery *Gallery) SetItems(items []Item) {
|
|||
}
|
||||
}
|
||||
|
||||
func (gallery *Gallery) GetFields() []Field {
|
||||
fields := []Field{}
|
||||
for _, field := range gallery.Fields {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
func (gallery *Gallery) GetType() LayoutType {
|
||||
return LayoutTypeGallery
|
||||
}
|
||||
|
|
@ -285,50 +293,5 @@ func (gallery *Gallery) Sort(attrView *AttributeView) {
|
|||
}
|
||||
|
||||
func (gallery *Gallery) Filter(attrView *AttributeView) {
|
||||
if 1 > len(gallery.Filters) {
|
||||
return
|
||||
}
|
||||
|
||||
var fieldIndexes []int
|
||||
for _, f := range gallery.Filters {
|
||||
for i, c := range gallery.Fields {
|
||||
if c.ID == f.Column {
|
||||
fieldIndexes = append(fieldIndexes, i)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cards := []*GalleryCard{}
|
||||
attrViewCache := map[string]*AttributeView{}
|
||||
attrViewCache[attrView.ID] = attrView
|
||||
for _, card := range gallery.Cards {
|
||||
pass := true
|
||||
for j, index := range fieldIndexes {
|
||||
operator := gallery.Filters[j].Operator
|
||||
|
||||
if nil == card.Values[index].Value {
|
||||
if FilterOperatorIsNotEmpty == operator {
|
||||
pass = false
|
||||
} else if FilterOperatorIsEmpty == operator {
|
||||
pass = true
|
||||
break
|
||||
}
|
||||
|
||||
if KeyTypeText != card.Values[index].ValueType {
|
||||
pass = false
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if !card.Values[index].Value.Filter(gallery.Filters[j], attrView, card.ID, &attrViewCache) {
|
||||
pass = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if pass {
|
||||
cards = append(cards, card)
|
||||
}
|
||||
}
|
||||
gallery.Cards = cards
|
||||
filter(gallery, attrView)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,6 +151,14 @@ func (table *Table) SetItems(items []Item) {
|
|||
}
|
||||
}
|
||||
|
||||
func (table *Table) GetFields() []Field {
|
||||
fields := []Field{}
|
||||
for _, column := range table.Columns {
|
||||
fields = append(fields, column)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
func (*Table) GetType() LayoutType {
|
||||
return LayoutTypeTable
|
||||
}
|
||||
|
|
@ -273,50 +281,5 @@ func (table *Table) Sort(attrView *AttributeView) {
|
|||
}
|
||||
|
||||
func (table *Table) Filter(attrView *AttributeView) {
|
||||
if 1 > len(table.Filters) {
|
||||
return
|
||||
}
|
||||
|
||||
var colIndexes []int
|
||||
for _, f := range table.Filters {
|
||||
for i, c := range table.Columns {
|
||||
if c.ID == f.Column {
|
||||
colIndexes = append(colIndexes, i)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rows := []*TableRow{}
|
||||
attrViewCache := map[string]*AttributeView{}
|
||||
attrViewCache[attrView.ID] = attrView
|
||||
for _, row := range table.Rows {
|
||||
pass := true
|
||||
for j, index := range colIndexes {
|
||||
operator := table.Filters[j].Operator
|
||||
|
||||
if nil == row.Cells[index].Value {
|
||||
if FilterOperatorIsNotEmpty == operator {
|
||||
pass = false
|
||||
} else if FilterOperatorIsEmpty == operator {
|
||||
pass = true
|
||||
break
|
||||
}
|
||||
|
||||
if KeyTypeText != row.Cells[index].ValueType {
|
||||
pass = false
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if !row.Cells[index].Value.Filter(table.Filters[j], attrView, row.ID, &attrViewCache) {
|
||||
pass = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if pass {
|
||||
rows = append(rows, row)
|
||||
}
|
||||
}
|
||||
table.Rows = rows
|
||||
filter(table, attrView)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue