From 4ec3fe2ff28fe4fd3e569fd6b12bbf2dfcf76576 Mon Sep 17 00:00:00 2001 From: anoshenko Date: Thu, 4 May 2023 16:45:03 +0300 Subject: [PATCH] Optimisation --- data.go | 54 ++++++++++++++++++++ tableAdapter.go | 130 ++++++++++++------------------------------------ tableView.go | 3 +- viewStyleSet.go | 54 ++++++++++++++++++++ 4 files changed, 142 insertions(+), 99 deletions(-) diff --git a/data.go b/data.go index 60ce9a3..c0fd968 100644 --- a/data.go +++ b/data.go @@ -23,6 +23,7 @@ type DataObject interface { PropertyObject(tag string) DataObject SetPropertyValue(tag, value string) SetPropertyObject(tag string, object DataObject) + ToParams() Params } const ( @@ -43,6 +44,7 @@ type DataNode interface { ArraySize() int ArrayElement(index int) DataValue ArrayElements() []DataValue + ArrayAsParams() []Params } /******************************************************************************/ @@ -165,6 +167,42 @@ func (object *dataObject) SetPropertyObject(tag string, obj DataObject) { object.setNode(node) } +func (object *dataObject) ToParams() Params { + params := Params{} + for _, node := range object.property { + switch node.Type() { + case TextNode: + if text := node.Text(); text != "" { + params[node.Tag()] = text + } + + case ObjectNode: + if obj := node.Object(); obj != nil { + params[node.Tag()] = node.Object() + } + + case ArrayNode: + array := []any{} + for i := 0; i < node.ArraySize(); i++ { + if data := node.ArrayElement(i); data != nil { + if data.IsObject() { + if obj := data.Object(); obj != nil { + array = append(array, obj) + } + } else if text := data.Value(); text != "" { + array = append(array, text) + } + } + } + if len(array) > 0 { + params[node.Tag()] = array + } + } + } + + return params +} + /******************************************************************************/ type dataNode struct { tag string @@ -221,6 +259,22 @@ func (node *dataNode) ArrayElements() []DataValue { return []DataValue{} } +func (node *dataNode) ArrayAsParams() []Params { + result := []Params{} + if node.array != nil { + for _, data := range node.array { + if data.IsObject() { + if obj := data.Object(); obj != nil { + if params := obj.ToParams(); len(params) > 0 { + result = append(result, params) + } + } + } + } + } + return result +} + // ParseDataText - parse text and return DataNode func ParseDataText(text string) DataObject { diff --git a/tableAdapter.go b/tableAdapter.go index c3a773f..f482f4b 100644 --- a/tableAdapter.go +++ b/tableAdapter.go @@ -228,11 +228,21 @@ func (adapter *textTableAdapter) Cell(row, column int) any { return nil } -type simpleTableRowStyle struct { +type simpleTableLineStyle struct { params []Params } -func (style *simpleTableRowStyle) RowStyle(row int) Params { +func (style *simpleTableLineStyle) ColumnStyle(column int) Params { + if column < len(style.params) { + params := style.params[column] + if len(params) > 0 { + return params + } + } + return nil +} + +func (style *simpleTableLineStyle) RowStyle(row int) Params { if row < len(style.params) { params := style.params[row] if len(params) > 0 { @@ -242,50 +252,24 @@ func (style *simpleTableRowStyle) RowStyle(row int) Params { return nil } -func (table *tableViewData) setRowStyle(value any) bool { - newSimpleTableRowStyle := func(params []Params) TableRowStyle { - if len(params) == 0 { - return nil - } - result := new(simpleTableRowStyle) - result.params = params - return result - } - +func (table *tableViewData) setLineStyle(tag string, value any) bool { switch value := value.(type) { - case TableRowStyle: - table.properties[RowStyle] = value - case []Params: - if style := newSimpleTableRowStyle(value); style != nil { - table.properties[RowStyle] = style + if len(value) > 0 { + style := new(simpleTableLineStyle) + style.params = value + table.properties[tag] = style } else { - delete(table.properties, RowStyle) + delete(table.properties, tag) } case DataNode: - if value.Type() == ArrayNode { - params := make([]Params, value.ArraySize()) - for i, element := range value.ArrayElements() { - params[i] = Params{} - if element.IsObject() { - obj := element.Object() - for k := 0; k < obj.PropertyCount(); k++ { - if prop := obj.Property(k); prop != nil && prop.Type() == TextNode { - params[i][prop.Tag()] = prop.Text() - } - } - } else { - params[i][Style] = element.Value() - } - } - if style := newSimpleTableRowStyle(params); style != nil { - table.properties[RowStyle] = style - } else { - delete(table.properties, RowStyle) - } + if params := value.ArrayAsParams(); len(params) > 0 { + style := new(simpleTableLineStyle) + style.params = params + table.properties[tag] = style } else { - return false + delete(table.properties, tag) } default: @@ -294,6 +278,14 @@ func (table *tableViewData) setRowStyle(value any) bool { return true } +func (table *tableViewData) setRowStyle(value any) bool { + switch value := value.(type) { + case TableRowStyle: + table.properties[RowStyle] = value + } + return table.setLineStyle(RowStyle, value) +} + func (table *tableViewData) getRowStyle() TableRowStyle { for _, tag := range []string{RowStyle, Content} { if value := table.getRaw(tag); value != nil { @@ -305,70 +297,12 @@ func (table *tableViewData) getRowStyle() TableRowStyle { return nil } -type simpleTableColumnStyle struct { - params []Params -} - -func (style *simpleTableColumnStyle) ColumnStyle(row int) Params { - if row < len(style.params) { - params := style.params[row] - if len(params) > 0 { - return params - } - } - return nil -} - func (table *tableViewData) setColumnStyle(value any) bool { - newSimpleTableColumnStyle := func(params []Params) TableColumnStyle { - if len(params) == 0 { - return nil - } - result := new(simpleTableColumnStyle) - result.params = params - return result - } - switch value := value.(type) { case TableColumnStyle: table.properties[ColumnStyle] = value - - case []Params: - if style := newSimpleTableColumnStyle(value); style != nil { - table.properties[ColumnStyle] = style - } else { - delete(table.properties, ColumnStyle) - } - - case DataNode: - if value.Type() == ArrayNode { - params := make([]Params, value.ArraySize()) - for i, element := range value.ArrayElements() { - params[i] = Params{} - if element.IsObject() { - obj := element.Object() - for k := 0; k < obj.PropertyCount(); k++ { - if prop := obj.Property(k); prop != nil && prop.Type() == TextNode { - params[i][prop.Tag()] = prop.Text() - } - } - } else { - params[i][Style] = element.Value() - } - } - if style := newSimpleTableColumnStyle(params); style != nil { - table.properties[ColumnStyle] = style - } else { - delete(table.properties, ColumnStyle) - } - } else { - return false - } - - default: - return false } - return true + return table.setLineStyle(ColumnStyle, value) } func (table *tableViewData) getColumnStyle() TableColumnStyle { diff --git a/tableView.go b/tableView.go index 94a3871..0d935a0 100644 --- a/tableView.go +++ b/tableView.go @@ -1120,7 +1120,8 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) { footHeight := GetTableFootHeight(table) cellBorder := table.getCellBorder() cellPadding := table.boundsProperty(CellPadding) - if cellPadding == nil { + if cellPadding == nil || len(cellPadding.AllTags()) == 0 { + cellPadding = nil if style, ok := stringProperty(table, Style, table.Session()); ok { if style, ok := table.Session().resolveConstants(style); ok { cellPadding = table.cellPaddingFromStyle(style) diff --git a/viewStyleSet.go b/viewStyleSet.go index 4d1c8f6..ad499a8 100644 --- a/viewStyleSet.go +++ b/viewStyleSet.go @@ -202,6 +202,60 @@ func (style *viewStyle) set(tag string, value any) bool { case CellPaddingTop, CellPaddingRight, CellPaddingBottom, CellPaddingLeft: return style.setBoundsSide(CellPadding, tag, value) + case HeadStyle, FootStyle: + switch value := value.(type) { + case string: + style.properties[tag] = value + return true + + case Params: + style.properties[tag] = value + return true + + case DataObject: + if params := value.ToParams(); len(params) > 0 { + style.properties[tag] = params + } + return true + } + + case CellStyle, ColumnStyle, RowStyle: + switch value := value.(type) { + case string: + style.properties[tag] = value + return true + + case Params: + style.properties[tag] = value + return true + + case DataObject: + if params := value.ToParams(); len(params) > 0 { + style.properties[tag] = params + } + return true + + case DataNode: + switch value.Type() { + case TextNode: + if text := value.Text(); text != "" { + style.properties[tag] = text + } + return true + + case ObjectNode: + if obj := value.Object(); obj != nil { + if params := obj.ToParams(); len(params) > 0 { + style.properties[tag] = params + } + return true + } + + case ArrayNode: + // TODO + } + } + case Outline: return style.setOutline(value)