Optimisation

This commit is contained in:
anoshenko 2023-05-04 16:45:03 +03:00
parent b6b5183f21
commit 4ec3fe2ff2
4 changed files with 142 additions and 99 deletions

54
data.go
View File

@ -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 {

View File

@ -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()
}
}
if params := value.ArrayAsParams(); len(params) > 0 {
style := new(simpleTableLineStyle)
style.params = params
table.properties[tag] = style
} else {
params[i][Style] = element.Value()
}
}
if style := newSimpleTableRowStyle(params); style != nil {
table.properties[RowStyle] = style
} else {
delete(table.properties, RowStyle)
}
} 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 {

View File

@ -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)

View File

@ -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)