mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-02 13:41:48 +01:00
♻️ Refactor av data structure
This commit is contained in:
parent
fbd26d1fd6
commit
b4068c5daf
9 changed files with 735 additions and 841 deletions
|
|
@ -31,14 +31,98 @@ import (
|
|||
|
||||
// AttributeView 描述了属性视图的结构。
|
||||
type AttributeView struct {
|
||||
Spec int `json:"spec"` // 格式版本
|
||||
ID string `json:"id"` // 属性视图 ID
|
||||
Name string `json:"name"` // 属性视图名称
|
||||
Columns []*Column `json:"columns"` // 列
|
||||
Rows []*Row `json:"rows"` // 行
|
||||
Spec int `json:"spec"` // 格式版本
|
||||
ID string `json:"id"` // 属性视图 ID
|
||||
Name string `json:"name"` // 属性视图名称
|
||||
KeyValues []*KeyValues `json:"keyValues"` // 属性视图属性列值
|
||||
CurrentViewID string `json:"currentViewID"` // 当前视图 ID
|
||||
Views []*View `json:"views"` // 视图
|
||||
}
|
||||
|
||||
CurrentViewID string `json:"currentViewID"` // 当前视图 ID
|
||||
Views []*View `json:"views"` // 视图
|
||||
// KeyValues 描述了属性视图属性列值的结构。
|
||||
type KeyValues struct {
|
||||
Key *Key `json:"key"` // 属性视图属性列
|
||||
Values []*Value `json:"values"` // 属性视图属性列值
|
||||
}
|
||||
|
||||
type KeyType string
|
||||
|
||||
const (
|
||||
KeyTypeBlock KeyType = "block"
|
||||
KeyTypeText KeyType = "text"
|
||||
KeyTypeNumber KeyType = "number"
|
||||
KeyTypeDate KeyType = "date"
|
||||
KeyTypeSelect KeyType = "select"
|
||||
KeyTypeMSelect KeyType = "mSelect"
|
||||
)
|
||||
|
||||
// Key 描述了属性视图属性列的基础结构。
|
||||
type Key struct {
|
||||
ID string `json:"id"` // 列 ID
|
||||
Name string `json:"name"` // 列名
|
||||
Type KeyType `json:"type"` // 列类型
|
||||
Icon string `json:"icon"` // 列图标
|
||||
|
||||
// 以下是某些列类型的特有属性
|
||||
|
||||
Options []*KeySelectOption `json:"options"` // 选项列表
|
||||
}
|
||||
|
||||
func NewKey(name string, keyType KeyType) *Key {
|
||||
return &Key{
|
||||
ID: ast.NewNodeID(),
|
||||
Name: name,
|
||||
Type: keyType,
|
||||
}
|
||||
}
|
||||
|
||||
type KeySelectOption struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
ID string `json:"id"`
|
||||
KeyID string `json:"keyID"`
|
||||
BlockID string `json:"blockID"`
|
||||
|
||||
Block *ValueBlock `json:"block,omitempty"`
|
||||
Text *ValueText `json:"text,omitempty"`
|
||||
Number *ValueNumber `json:"number,omitempty"`
|
||||
Date *ValueDate `json:"date,omitempty"`
|
||||
MSelect []*ValueSelect `json:"mSelect,omitempty"`
|
||||
}
|
||||
|
||||
func (value *Value) ToJSONString() string {
|
||||
data, err := gulu.JSON.MarshalJSON(value)
|
||||
if nil != err {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type ValueBlock struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type ValueText struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type ValueNumber struct {
|
||||
Content float64 `json:"content"`
|
||||
IsNotEmpty bool `json:"isNotEmpty"`
|
||||
}
|
||||
|
||||
type ValueDate struct {
|
||||
Content int64 `json:"content"`
|
||||
Content2 int64 `json:"content2"`
|
||||
}
|
||||
|
||||
type ValueSelect struct {
|
||||
Content string `json:"content"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
// View 描述了视图的结构。
|
||||
|
|
@ -91,8 +175,7 @@ func NewAttributeView(id string) *AttributeView {
|
|||
return &AttributeView{
|
||||
Spec: 0,
|
||||
ID: id,
|
||||
Columns: []*Column{{ID: ast.NewNodeID(), Name: "Block", Type: ColumnTypeBlock}},
|
||||
Rows: []*Row{},
|
||||
KeyValues: []*KeyValues{{Key: NewKey("Name", KeyTypeBlock)}},
|
||||
CurrentViewID: view.ID,
|
||||
Views: []*View{view},
|
||||
}
|
||||
|
|
@ -125,34 +208,6 @@ func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func ParseAttributeViewMap(avID string) (ret map[string]interface{}, err error) {
|
||||
ret = map[string]interface{}{}
|
||||
avJSONPath := getAttributeViewDataPath(avID)
|
||||
if !gulu.File.IsExist(avJSONPath) {
|
||||
av := NewAttributeView(avID)
|
||||
var data []byte
|
||||
data, err = gulu.JSON.MarshalJSON(av)
|
||||
if nil == err {
|
||||
return
|
||||
}
|
||||
|
||||
err = gulu.JSON.UnmarshalJSON(data, &ret)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := filelock.ReadFile(avJSONPath)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
|
||||
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SaveAttributeView(av *AttributeView) (err error) {
|
||||
data, err := gulu.JSON.MarshalIndentJSON(av, "", "\t")
|
||||
if nil != err {
|
||||
|
|
@ -179,6 +234,27 @@ func (av *AttributeView) GetView(viewID string) (ret *View, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (av *AttributeView) GetKey(keyID string) (ret *Key, err error) {
|
||||
for _, kv := range av.KeyValues {
|
||||
if kv.Key.ID == keyID {
|
||||
ret = kv.Key
|
||||
return
|
||||
}
|
||||
}
|
||||
err = ErrKeyNotFound
|
||||
return
|
||||
}
|
||||
|
||||
func (av *AttributeView) GetBlockKeyValues() (ret *KeyValues) {
|
||||
for _, kv := range av.KeyValues {
|
||||
if KeyTypeBlock == kv.Key.Type {
|
||||
ret = kv
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getAttributeViewDataPath(avID string) (ret string) {
|
||||
av := filepath.Join(util.DataDir, "storage", "av")
|
||||
ret = filepath.Join(av, avID+".json")
|
||||
|
|
@ -193,4 +269,5 @@ func getAttributeViewDataPath(avID string) (ret string) {
|
|||
|
||||
var (
|
||||
ErrViewNotFound = errors.New("view not found")
|
||||
ErrKeyNotFound = errors.New("key not found")
|
||||
)
|
||||
|
|
@ -1,237 +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
|
||||
|
||||
import (
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute/ast"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Cell struct {
|
||||
ID string `json:"id"`
|
||||
Value *Value `json:"value"`
|
||||
ValueType ColumnType `json:"valueType"`
|
||||
Color string `json:"color"`
|
||||
BgColor string `json:"bgColor"`
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
Block *ValueBlock `json:"block,omitempty"`
|
||||
Text *ValueText `json:"text,omitempty"`
|
||||
Number *ValueNumber `json:"number,omitempty"`
|
||||
Date *ValueDate `json:"date,omitempty"`
|
||||
MSelect []*ValueSelect `json:"mSelect,omitempty"`
|
||||
}
|
||||
|
||||
func (value *Value) ToJSONString() string {
|
||||
data, err := gulu.JSON.MarshalJSON(value)
|
||||
if nil != err {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (value *Value) Compare(other *Value) int {
|
||||
if nil == value {
|
||||
return -1
|
||||
}
|
||||
if nil == other {
|
||||
return 1
|
||||
}
|
||||
if nil != value.Block && nil != other.Block {
|
||||
return strings.Compare(value.Block.Content, other.Block.Content)
|
||||
}
|
||||
if nil != value.Text && nil != other.Text {
|
||||
return strings.Compare(value.Text.Content, other.Text.Content)
|
||||
}
|
||||
if nil != value.Number && nil != other.Number {
|
||||
if value.Number.Content > other.Number.Content {
|
||||
return 1
|
||||
} else if value.Number.Content < other.Number.Content {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
if nil != value.Date && nil != other.Date {
|
||||
if value.Date.Content > other.Date.Content {
|
||||
return 1
|
||||
} else if value.Date.Content < other.Date.Content {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.MSelect && nil != other.MSelect {
|
||||
var v1 string
|
||||
for _, v := range value.MSelect {
|
||||
v1 += v.Content
|
||||
}
|
||||
var v2 string
|
||||
for _, v := range other.MSelect {
|
||||
v2 += v.Content
|
||||
}
|
||||
return strings.Compare(v1, v2)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (value *Value) CompareOperator(other *Value, operator FilterOperator) bool {
|
||||
if nil == value || nil == other {
|
||||
return false
|
||||
}
|
||||
|
||||
if nil != value.Block && nil != other.Block {
|
||||
return strings.Contains(value.Block.Content, other.Block.Content)
|
||||
}
|
||||
|
||||
if nil != value.Text && nil != other.Text {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Text.Content == other.Text.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Text.Content != other.Text.Content
|
||||
case FilterOperatorContains:
|
||||
return strings.Contains(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorDoesNotContain:
|
||||
return !strings.Contains(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorStartsWith:
|
||||
return strings.HasPrefix(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorEndsWith:
|
||||
return strings.HasSuffix(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorIsEmpty:
|
||||
return "" == strings.TrimSpace(value.Text.Content)
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return "" != strings.TrimSpace(value.Text.Content)
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.Number && nil != other.Number {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Number.Content == other.Number.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Number.Content != other.Number.Content
|
||||
case FilterOperatorIsGreater:
|
||||
return value.Number.Content > other.Number.Content
|
||||
case FilterOperatorIsGreaterOrEqual:
|
||||
return value.Number.Content >= other.Number.Content
|
||||
case FilterOperatorIsLess:
|
||||
return value.Number.Content < other.Number.Content
|
||||
case FilterOperatorIsLessOrEqual:
|
||||
return value.Number.Content <= other.Number.Content
|
||||
case FilterOperatorIsEmpty:
|
||||
return !value.Number.IsNotEmpty
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return value.Number.IsNotEmpty
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.Date && nil != other.Date {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Date.Content == other.Date.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Date.Content != other.Date.Content
|
||||
case FilterOperatorIsGreater:
|
||||
return value.Date.Content > other.Date.Content
|
||||
case FilterOperatorIsGreaterOrEqual:
|
||||
return value.Date.Content >= other.Date.Content
|
||||
case FilterOperatorIsLess:
|
||||
return value.Date.Content < other.Date.Content
|
||||
case FilterOperatorIsLessOrEqual:
|
||||
return value.Date.Content <= other.Date.Content
|
||||
case FilterOperatorIsBetween:
|
||||
return value.Date.Content >= other.Date.Content && value.Date.Content <= other.Date.Content2
|
||||
case FilterOperatorIsEmpty:
|
||||
return 0 == value.Date.Content
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return 0 != value.Date.Content
|
||||
case FilterOperatorIsRelativeToToday:
|
||||
// TODO: date filter (relative to today)
|
||||
return value.Date.Content >= other.Date.Content && value.Date.Content <= other.Date.Content2
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.MSelect && nil != other.MSelect && 0 < len(value.MSelect) && 0 < len(other.MSelect) {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.MSelect[0].Content == other.MSelect[0].Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.MSelect[0].Content != other.MSelect[0].Content
|
||||
case FilterOperatorContains:
|
||||
for _, v := range value.MSelect {
|
||||
if v.Content == other.MSelect[0].Content {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case FilterOperatorDoesNotContain:
|
||||
for _, v := range value.MSelect {
|
||||
if v.Content == other.MSelect[0].Content {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
case FilterOperatorIsEmpty:
|
||||
return 0 == len(value.MSelect)
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return 0 != len(value.MSelect)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func NewCellBlock(blockID, blockContent string) *Cell {
|
||||
return &Cell{
|
||||
ID: ast.NewNodeID(),
|
||||
Value: &Value{Block: &ValueBlock{ID: blockID, Content: blockContent}},
|
||||
ValueType: ColumnTypeBlock,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCell(valueType ColumnType) *Cell {
|
||||
return &Cell{
|
||||
ID: ast.NewNodeID(),
|
||||
ValueType: valueType,
|
||||
}
|
||||
}
|
||||
|
||||
type ValueBlock struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type ValueText struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type ValueNumber struct {
|
||||
Content float64 `json:"content"`
|
||||
IsNotEmpty bool `json:"isNotEmpty"`
|
||||
}
|
||||
|
||||
type ValueDate struct {
|
||||
Content int64 `json:"content"`
|
||||
Content2 int64 `json:"content2"`
|
||||
}
|
||||
|
||||
type ValueSelect struct {
|
||||
Content string `json:"content"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
|
@ -1,57 +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
|
||||
|
||||
import (
|
||||
"github.com/88250/lute/ast"
|
||||
)
|
||||
|
||||
type ColumnType string
|
||||
|
||||
const (
|
||||
ColumnTypeBlock ColumnType = "block"
|
||||
ColumnTypeText ColumnType = "text"
|
||||
ColumnTypeNumber ColumnType = "number"
|
||||
ColumnTypeDate ColumnType = "date"
|
||||
ColumnTypeSelect ColumnType = "select"
|
||||
ColumnTypeMSelect ColumnType = "mSelect"
|
||||
)
|
||||
|
||||
// Column 描述了属性视图的基础结构。
|
||||
type Column struct {
|
||||
ID string `json:"id"` // 列 ID
|
||||
Name string `json:"name"` // 列名
|
||||
Type ColumnType `json:"type"` // 列类型
|
||||
Icon string `json:"icon"` // 列图标
|
||||
|
||||
// 以下是某些列类型的特有属性
|
||||
|
||||
Options []*ColumnSelectOption `json:"options"` // 选项列表
|
||||
}
|
||||
|
||||
type ColumnSelectOption struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
func NewColumn(name string, columnType ColumnType) *Column {
|
||||
return &Column{
|
||||
ID: ast.NewNodeID(),
|
||||
Name: name,
|
||||
Type: columnType,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +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
|
||||
|
||||
type Calculable interface {
|
||||
CalcCols()
|
||||
}
|
||||
|
||||
type ColumnCalc struct {
|
||||
Column string `json:"column"`
|
||||
Operator CalcOperator `json:"operator"`
|
||||
Result *Value `json:"result"`
|
||||
}
|
||||
|
||||
type CalcOperator string
|
||||
|
||||
const (
|
||||
CalcOperatorNone CalcOperator = ""
|
||||
CalcOperatorCountAll CalcOperator = "Count all"
|
||||
CalcOperatorCountValues CalcOperator = "Count values"
|
||||
CalcOperatorCountUniqueValues CalcOperator = "Count unique values"
|
||||
CalcOperatorCountEmpty CalcOperator = "Count empty"
|
||||
CalcOperatorCountNotEmpty CalcOperator = "Count not empty"
|
||||
CalcOperatorPercentEmpty CalcOperator = "Percent empty"
|
||||
CalcOperatorPercentNotEmpty CalcOperator = "Percent not empty"
|
||||
CalcOperatorSum CalcOperator = "Sum"
|
||||
CalcOperatorAverage CalcOperator = "Average"
|
||||
CalcOperatorMedian CalcOperator = "Median"
|
||||
CalcOperatorMin CalcOperator = "Min"
|
||||
CalcOperatorMax CalcOperator = "Max"
|
||||
CalcOperatorRange CalcOperator = "Range"
|
||||
CalcOperatorEarliest CalcOperator = "Earliest"
|
||||
CalcOperatorLatest CalcOperator = "Latest"
|
||||
)
|
||||
|
|
@ -1,37 +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
|
||||
|
||||
import "github.com/88250/lute/ast"
|
||||
|
||||
type Row struct {
|
||||
ID string `json:"id"`
|
||||
Cells []*Cell `json:"cells"`
|
||||
}
|
||||
|
||||
func NewRow() *Row {
|
||||
return &Row{ID: ast.NewNodeID()}
|
||||
}
|
||||
|
||||
func (row *Row) GetBlockCell() *Cell {
|
||||
for _, cell := range row.Cells {
|
||||
if ColumnTypeBlock == cell.ValueType {
|
||||
return cell
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -17,8 +17,10 @@
|
|||
package av
|
||||
|
||||
import (
|
||||
"github.com/88250/lute/ast"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LayoutTable 描述了表格布局的结构。
|
||||
|
|
@ -26,18 +28,14 @@ type LayoutTable struct {
|
|||
Spec int `json:"spec"` // 布局格式版本
|
||||
ID string `json:"id"` // 布局 ID
|
||||
|
||||
Columns []*TableColumn `json:"columns"` // 表格列
|
||||
ColIDs []string `json:"colIds"` // 列 ID,用于自定义排序
|
||||
RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
|
||||
Filters []*ViewFilter `json:"filters"` // 过滤规则
|
||||
Sorts []*ViewSort `json:"sorts"` // 排序规则
|
||||
Columns []*ViewTableColumn `json:"columns"` // 表格列
|
||||
RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
|
||||
Filters []*ViewFilter `json:"filters"` // 过滤规则
|
||||
Sorts []*ViewSort `json:"sorts"` // 排序规则
|
||||
}
|
||||
|
||||
type TableColumn struct {
|
||||
ID string `json:"id"` // 列 ID
|
||||
Name string `json:"name"` // 列名
|
||||
Type ColumnType `json:"type"` // 列类型
|
||||
Icon string `json:"icon"` // 列图标
|
||||
type ViewTableColumn struct {
|
||||
ID string `json:"id"` // 列 ID
|
||||
|
||||
Wrap bool `json:"wrap"` // 是否换行
|
||||
Hidden bool `json:"hidden"` // 是否隐藏
|
||||
|
|
@ -45,9 +43,194 @@ type TableColumn struct {
|
|||
Calc *ColumnCalc `json:"calc"` // 计算
|
||||
}
|
||||
|
||||
type TableRow struct {
|
||||
ID string `json:"id"`
|
||||
Cells []*Cell `json:"cells"`
|
||||
type Calculable interface {
|
||||
CalcCols()
|
||||
}
|
||||
|
||||
type ColumnCalc struct {
|
||||
Column string `json:"column"`
|
||||
Operator CalcOperator `json:"operator"`
|
||||
Result *Value `json:"result"`
|
||||
}
|
||||
|
||||
type CalcOperator string
|
||||
|
||||
const (
|
||||
CalcOperatorNone CalcOperator = ""
|
||||
CalcOperatorCountAll CalcOperator = "Count all"
|
||||
CalcOperatorCountValues CalcOperator = "Count values"
|
||||
CalcOperatorCountUniqueValues CalcOperator = "Count unique values"
|
||||
CalcOperatorCountEmpty CalcOperator = "Count empty"
|
||||
CalcOperatorCountNotEmpty CalcOperator = "Count not empty"
|
||||
CalcOperatorPercentEmpty CalcOperator = "Percent empty"
|
||||
CalcOperatorPercentNotEmpty CalcOperator = "Percent not empty"
|
||||
CalcOperatorSum CalcOperator = "Sum"
|
||||
CalcOperatorAverage CalcOperator = "Average"
|
||||
CalcOperatorMedian CalcOperator = "Median"
|
||||
CalcOperatorMin CalcOperator = "Min"
|
||||
CalcOperatorMax CalcOperator = "Max"
|
||||
CalcOperatorRange CalcOperator = "Range"
|
||||
CalcOperatorEarliest CalcOperator = "Earliest"
|
||||
CalcOperatorLatest CalcOperator = "Latest"
|
||||
)
|
||||
|
||||
type TableCell struct {
|
||||
ID string `json:"id"`
|
||||
Value *Value `json:"value"`
|
||||
ValueType KeyType `json:"valueType"`
|
||||
Color string `json:"color"`
|
||||
BgColor string `json:"bgColor"`
|
||||
}
|
||||
|
||||
func (value *Value) Compare(other *Value) int {
|
||||
if nil == value {
|
||||
return -1
|
||||
}
|
||||
if nil == other {
|
||||
return 1
|
||||
}
|
||||
if nil != value.Block && nil != other.Block {
|
||||
return strings.Compare(value.Block.Content, other.Block.Content)
|
||||
}
|
||||
if nil != value.Text && nil != other.Text {
|
||||
return strings.Compare(value.Text.Content, other.Text.Content)
|
||||
}
|
||||
if nil != value.Number && nil != other.Number {
|
||||
if value.Number.Content > other.Number.Content {
|
||||
return 1
|
||||
} else if value.Number.Content < other.Number.Content {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
if nil != value.Date && nil != other.Date {
|
||||
if value.Date.Content > other.Date.Content {
|
||||
return 1
|
||||
} else if value.Date.Content < other.Date.Content {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.MSelect && nil != other.MSelect {
|
||||
var v1 string
|
||||
for _, v := range value.MSelect {
|
||||
v1 += v.Content
|
||||
}
|
||||
var v2 string
|
||||
for _, v := range other.MSelect {
|
||||
v2 += v.Content
|
||||
}
|
||||
return strings.Compare(v1, v2)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (value *Value) CompareOperator(other *Value, operator FilterOperator) bool {
|
||||
if nil == value || nil == other {
|
||||
return false
|
||||
}
|
||||
|
||||
if nil != value.Block && nil != other.Block {
|
||||
return strings.Contains(value.Block.Content, other.Block.Content)
|
||||
}
|
||||
|
||||
if nil != value.Text && nil != other.Text {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Text.Content == other.Text.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Text.Content != other.Text.Content
|
||||
case FilterOperatorContains:
|
||||
return strings.Contains(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorDoesNotContain:
|
||||
return !strings.Contains(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorStartsWith:
|
||||
return strings.HasPrefix(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorEndsWith:
|
||||
return strings.HasSuffix(value.Text.Content, other.Text.Content)
|
||||
case FilterOperatorIsEmpty:
|
||||
return "" == strings.TrimSpace(value.Text.Content)
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return "" != strings.TrimSpace(value.Text.Content)
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.Number && nil != other.Number {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Number.Content == other.Number.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Number.Content != other.Number.Content
|
||||
case FilterOperatorIsGreater:
|
||||
return value.Number.Content > other.Number.Content
|
||||
case FilterOperatorIsGreaterOrEqual:
|
||||
return value.Number.Content >= other.Number.Content
|
||||
case FilterOperatorIsLess:
|
||||
return value.Number.Content < other.Number.Content
|
||||
case FilterOperatorIsLessOrEqual:
|
||||
return value.Number.Content <= other.Number.Content
|
||||
case FilterOperatorIsEmpty:
|
||||
return !value.Number.IsNotEmpty
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return value.Number.IsNotEmpty
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.Date && nil != other.Date {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.Date.Content == other.Date.Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.Date.Content != other.Date.Content
|
||||
case FilterOperatorIsGreater:
|
||||
return value.Date.Content > other.Date.Content
|
||||
case FilterOperatorIsGreaterOrEqual:
|
||||
return value.Date.Content >= other.Date.Content
|
||||
case FilterOperatorIsLess:
|
||||
return value.Date.Content < other.Date.Content
|
||||
case FilterOperatorIsLessOrEqual:
|
||||
return value.Date.Content <= other.Date.Content
|
||||
case FilterOperatorIsBetween:
|
||||
return value.Date.Content >= other.Date.Content && value.Date.Content <= other.Date.Content2
|
||||
case FilterOperatorIsEmpty:
|
||||
return 0 == value.Date.Content
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return 0 != value.Date.Content
|
||||
case FilterOperatorIsRelativeToToday:
|
||||
// TODO: date filter (relative to today)
|
||||
return value.Date.Content >= other.Date.Content && value.Date.Content <= other.Date.Content2
|
||||
}
|
||||
}
|
||||
|
||||
if nil != value.MSelect && nil != other.MSelect && 0 < len(value.MSelect) && 0 < len(other.MSelect) {
|
||||
switch operator {
|
||||
case FilterOperatorIsEqual:
|
||||
return value.MSelect[0].Content == other.MSelect[0].Content
|
||||
case FilterOperatorIsNotEqual:
|
||||
return value.MSelect[0].Content != other.MSelect[0].Content
|
||||
case FilterOperatorContains:
|
||||
for _, v := range value.MSelect {
|
||||
if v.Content == other.MSelect[0].Content {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case FilterOperatorDoesNotContain:
|
||||
for _, v := range value.MSelect {
|
||||
if v.Content == other.MSelect[0].Content {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
case FilterOperatorIsEmpty:
|
||||
return 0 == len(value.MSelect)
|
||||
case FilterOperatorIsNotEmpty:
|
||||
return 0 != len(value.MSelect)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Table 描述了表格实例的结构。
|
||||
|
|
@ -60,6 +243,29 @@ type Table struct {
|
|||
Rows []*TableRow `json:"rows"` // 表格行
|
||||
}
|
||||
|
||||
type TableColumn struct {
|
||||
ID string `json:"id"` // 列 ID
|
||||
Name string `json:"name"` // 列名
|
||||
Type KeyType `json:"type"` // 列类型
|
||||
Icon string `json:"icon"` // 列图标
|
||||
Wrap bool `json:"wrap"` // 是否换行
|
||||
Hidden bool `json:"hidden"` // 是否隐藏
|
||||
Width string `json:"width"` // 列宽度
|
||||
Calc *ColumnCalc `json:"calc"` // 计算
|
||||
}
|
||||
|
||||
type TableRow struct {
|
||||
ID string `json:"id"`
|
||||
Cells []*TableCell `json:"cells"`
|
||||
}
|
||||
|
||||
func NewTableRow() *TableRow {
|
||||
return &TableRow{
|
||||
ID: ast.NewNodeID(),
|
||||
Cells: []*TableCell{},
|
||||
}
|
||||
}
|
||||
|
||||
func (table *Table) GetType() LayoutType {
|
||||
return LayoutTypeTable
|
||||
}
|
||||
|
|
@ -91,7 +297,7 @@ func (table *Table) SortRows() {
|
|||
sort.Slice(table.Rows, func(i, j int) bool {
|
||||
for _, colIndexSort := range colIndexSorts {
|
||||
c := table.Columns[colIndexSort.Index]
|
||||
if c.Type == ColumnTypeBlock {
|
||||
if c.Type == KeyTypeBlock {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +335,7 @@ func (table *Table) FilterRows() {
|
|||
pass := true
|
||||
for j, index := range colIndexes {
|
||||
c := table.Columns[index]
|
||||
if c.Type == ColumnTypeBlock {
|
||||
if c.Type == KeyTypeBlock {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -156,15 +362,15 @@ func (table *Table) CalcCols() {
|
|||
}
|
||||
|
||||
switch col.Type {
|
||||
case ColumnTypeText:
|
||||
case KeyTypeText:
|
||||
table.calcColText(col, i)
|
||||
case ColumnTypeNumber:
|
||||
case KeyTypeNumber:
|
||||
table.calcColNumber(col, i)
|
||||
case ColumnTypeDate:
|
||||
case KeyTypeDate:
|
||||
table.calcColDate(col, i)
|
||||
case ColumnTypeSelect:
|
||||
case KeyTypeSelect:
|
||||
table.calcColSelect(col, i)
|
||||
case ColumnTypeMSelect:
|
||||
case KeyTypeMSelect:
|
||||
table.calcColMSelect(col, i)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue