diff --git a/kernel/av/av.go b/kernel/av/av.go index 5c370aeef..fbf581e07 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -22,7 +22,6 @@ import ( "database/sql" "os" "path/filepath" - "sort" "strings" "github.com/88250/gulu" @@ -190,117 +189,3 @@ func RebuildAttributeViewTable(tx *sql.Tx, av *AttributeView) (err error) { } return } - -type AttributeViewSort struct { - Column string `json:"column"` // 列 ID - Order SortOrder `json:"order"` // 排序顺序 -} - -type SortOrder string - -const ( - SortOrderAsc SortOrder = "ASC" - SortOrderDesc SortOrder = "DESC" -) - -func (av *AttributeView) SortRows() { - if 1 > len(av.Sorts) { - return - } - - type ColIndexSort struct { - Index int - Order SortOrder - } - - var colIndexSorts []*ColIndexSort - for _, s := range av.Sorts { - for i, c := range av.Columns { - if c.ID == s.Column { - colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order}) - break - } - } - } - - sort.Slice(av.Rows, func(i, j int) bool { - for _, colIndexSort := range colIndexSorts { - c := av.Columns[colIndexSort.Index] - if c.Type == ColumnTypeBlock { - continue - } - - result := av.Rows[i].Cells[colIndexSort.Index].Value.Compare(av.Rows[j].Cells[colIndexSort.Index].Value) - if 0 == result { - continue - } - - if colIndexSort.Order == SortOrderAsc { - return 0 > result - } - return 0 < result - } - return false - }) -} - -type AttributeViewFilter struct { - Column string `json:"column"` - Operator FilterOperator `json:"operator"` - Value *Value `json:"value"` -} - -type FilterOperator string - -const ( - FilterOperatorIsEqual FilterOperator = "=" - FilterOperatorIsNotEqual FilterOperator = "!=" - FilterOperatorIsGreater FilterOperator = ">" - FilterOperatorIsGreaterOrEqual FilterOperator = ">=" - FilterOperatorIsLess FilterOperator = "<" - FilterOperatorIsLessOrEqual FilterOperator = "<=" - FilterOperatorContains FilterOperator = "Contains" - FilterOperatorDoesNotContain FilterOperator = "Does not contains" - FilterOperatorIsEmpty FilterOperator = "Is empty" - FilterOperatorIsNotEmpty FilterOperator = "Is not empty" - FilterOperatorStartsWith FilterOperator = "Starts with" - FilterOperatorEndsWith FilterOperator = "Ends with" - FilterOperatorIsBetween FilterOperator = "Is between" - FilterOperatorIsRelativeToToday FilterOperator = "Is relative to today" -) - -func (av *AttributeView) FilterRows() { - if 1 > len(av.Filters) { - return - } - - var colIndexes []int - for _, f := range av.Filters { - for i, c := range av.Columns { - if c.ID == f.Column { - colIndexes = append(colIndexes, i) - break - } - } - } - - rows := []*Row{} - for _, row := range av.Rows { - pass := true - for j, index := range colIndexes { - c := av.Columns[index] - if c.Type == ColumnTypeBlock { - continue - } - - if !row.Cells[index].Value.CompareOperator(av.Filters[j].Value, av.Filters[j].Operator) { - pass = false - break - } - } - if pass { - rows = append(rows, row) - } - } - av.Rows = rows -} diff --git a/kernel/av/cell.go b/kernel/av/cell.go index 412cb598b..597f74b03 100644 --- a/kernel/av/cell.go +++ b/kernel/av/cell.go @@ -35,7 +35,6 @@ type Value struct { Text *ValueText `json:"text,omitempty"` Number *ValueNumber `json:"number,omitempty"` Date *ValueDate `json:"date,omitempty"` - Select *ValueSelect `json:"select,omitempty"` MSelect []*ValueSelect `json:"mSelect,omitempty"` } @@ -78,9 +77,7 @@ func (value *Value) Compare(other *Value) int { return 0 } } - if nil != value.Select && nil != other.Select { - return strings.Compare(value.Select.Content, other.Select.Content) - } + if nil != value.MSelect && nil != other.MSelect { var v1 string for _, v := range value.MSelect { @@ -172,21 +169,12 @@ func (value *Value) CompareOperator(other *Value, operator FilterOperator) bool } } - if nil != value.Select && nil != other.Select { + if nil != value.MSelect && nil != other.MSelect && 0 < len(value.MSelect) && 0 < len(other.MSelect) { switch operator { case FilterOperatorIsEqual: - return value.Select.Content == other.Select.Content + return value.MSelect[0].Content == other.MSelect[0].Content case FilterOperatorIsNotEqual: - return value.Select.Content != other.Select.Content - case FilterOperatorIsEmpty: - return "" == strings.TrimSpace(value.Select.Content) - case FilterOperatorIsNotEmpty: - return "" != strings.TrimSpace(value.Select.Content) - } - } - - if nil != value.MSelect && nil != other.MSelect { - switch operator { + return value.MSelect[0].Content != other.MSelect[0].Content case FilterOperatorContains: for _, v := range value.MSelect { if v.Content == other.MSelect[0].Content { diff --git a/kernel/av/filter.go b/kernel/av/filter.go new file mode 100644 index 000000000..89525bc17 --- /dev/null +++ b/kernel/av/filter.go @@ -0,0 +1,78 @@ +// 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 . + +package av + +type AttributeViewFilter struct { + Column string `json:"column"` + Operator FilterOperator `json:"operator"` + Value *Value `json:"value"` +} + +type FilterOperator string + +const ( + FilterOperatorIsEqual FilterOperator = "=" + FilterOperatorIsNotEqual FilterOperator = "!=" + FilterOperatorIsGreater FilterOperator = ">" + FilterOperatorIsGreaterOrEqual FilterOperator = ">=" + FilterOperatorIsLess FilterOperator = "<" + FilterOperatorIsLessOrEqual FilterOperator = "<=" + FilterOperatorContains FilterOperator = "Contains" + FilterOperatorDoesNotContain FilterOperator = "Does not contains" + FilterOperatorIsEmpty FilterOperator = "Is empty" + FilterOperatorIsNotEmpty FilterOperator = "Is not empty" + FilterOperatorStartsWith FilterOperator = "Starts with" + FilterOperatorEndsWith FilterOperator = "Ends with" + FilterOperatorIsBetween FilterOperator = "Is between" + FilterOperatorIsRelativeToToday FilterOperator = "Is relative to today" +) + +func (av *AttributeView) FilterRows() { + if 1 > len(av.Filters) { + return + } + + var colIndexes []int + for _, f := range av.Filters { + for i, c := range av.Columns { + if c.ID == f.Column { + colIndexes = append(colIndexes, i) + break + } + } + } + + rows := []*Row{} + for _, row := range av.Rows { + pass := true + for j, index := range colIndexes { + c := av.Columns[index] + if c.Type == ColumnTypeBlock { + continue + } + + if !row.Cells[index].Value.CompareOperator(av.Filters[j].Value, av.Filters[j].Operator) { + pass = false + break + } + } + if pass { + rows = append(rows, row) + } + } + av.Rows = rows +} diff --git a/kernel/av/sort.go b/kernel/av/sort.go new file mode 100644 index 000000000..fe0061a89 --- /dev/null +++ b/kernel/av/sort.go @@ -0,0 +1,72 @@ +// 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 . + +package av + +import "sort" + +type AttributeViewSort struct { + Column string `json:"column"` // 列 ID + Order SortOrder `json:"order"` // 排序顺序 +} + +type SortOrder string + +const ( + SortOrderAsc SortOrder = "ASC" + SortOrderDesc SortOrder = "DESC" +) + +func (av *AttributeView) SortRows() { + if 1 > len(av.Sorts) { + return + } + + type ColIndexSort struct { + Index int + Order SortOrder + } + + var colIndexSorts []*ColIndexSort + for _, s := range av.Sorts { + for i, c := range av.Columns { + if c.ID == s.Column { + colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order}) + break + } + } + } + + sort.Slice(av.Rows, func(i, j int) bool { + for _, colIndexSort := range colIndexSorts { + c := av.Columns[colIndexSort.Index] + if c.Type == ColumnTypeBlock { + continue + } + + result := av.Rows[i].Cells[colIndexSort.Index].Value.Compare(av.Rows[j].Cells[colIndexSort.Index].Value) + if 0 == result { + continue + } + + if colIndexSort.Order == SortOrderAsc { + return 0 > result + } + return 0 < result + } + return false + }) +}