From 32b6182dbf200d7e18eae3c45212b0f9077e95f6 Mon Sep 17 00:00:00 2001 From: Alexei Anoshenko Date: Fri, 14 Jan 2022 18:06:10 -0500 Subject: [PATCH] Added "allow-selection" property to TableView --- app_scripts.js | 4 +-- demo/tableDemo.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ tableAdapter.go | 8 ++++++ tableView.go | 51 ++++++++++++++++++++++++++++++++++-- 4 files changed, 125 insertions(+), 4 deletions(-) diff --git a/app_scripts.js b/app_scripts.js index 89f82e0..729de78 100644 --- a/app_scripts.js +++ b/app_scripts.js @@ -1402,7 +1402,7 @@ function tableViewBlurEvent(element, event) { function setTableCellCursor(element, row, column) { const cellID = element.id + "-" + row + "-" + column; var cell = document.getElementById(cellID); - if (!cell) { + if (!cell || cell.getAttribute("data-disabled")) { return false; } @@ -1573,7 +1573,7 @@ function tableViewCellKeyDownEvent(element, event) { function setTableRowCursor(element, row) { const tableRowID = element.id + "-" + row; var tableRow = document.getElementById(tableRowID); - if (!tableRow) { + if (!tableRow || tableRow.getAttribute("data-disabled")) { return false; } diff --git a/demo/tableDemo.go b/demo/tableDemo.go index 2e23c1a..2da8de3 100644 --- a/demo/tableDemo.go +++ b/demo/tableDemo.go @@ -34,6 +34,8 @@ GridLayout { Checkbox { row = 7, column = 0:1, id = tableColumnStyle, content = "Column style" }, TextView { row = 8, text = "Selection mode" }, DropDownList { row = 8, column = 1, id = tableSelectionMode, current = 0, items = ["none", "cell", "row"]}, + Checkbox { row = 9, column = 0:1, id = tableDisableHead, content = "Disable head selection" }, + Checkbox { row = 10, column = 0:1, id = tableDisableFoot, content = "Disable foot selection" }, ] } ] @@ -42,6 +44,31 @@ GridLayout { } ` +type demoTableAllowSelection struct { + index []int +} + +func (allow *demoTableAllowSelection) AllowCellSelection(row, column int) bool { + return allow.AllowRowSelection(row) +} + +func (allow *demoTableAllowSelection) AllowRowSelection(row int) bool { + if allow.index != nil { + for _, index := range allow.index { + if index == row { + return false + } + } + } + return true +} + +func newDemoTableAllowSelection(index []int) *demoTableAllowSelection { + result := new(demoTableAllowSelection) + result.index = index + return result +} + func createTableViewDemo(session rui.Session) rui.View { view := rui.CreateViewFromText(session, tableViewDemoText) if view == nil { @@ -97,6 +124,45 @@ func createTableViewDemo(session rui.Session) rui.View { rui.Set(view, "tableSelectionMode", rui.DropDownEvent, func(list rui.DropDownList, number int) { rui.Set(view, "demoTableView1", rui.SelectionMode, number) + switch rui.GetCurrent(view, "tableSelectionMode") { + case rui.CellSelection: + // TODO + + case rui.RowSelection: + // TODO + } + }) + + rui.Set(view, "tableDisableHead", rui.CheckboxChangedEvent, func(checked bool) { + if checked { + if rui.IsCheckboxChecked(view, "tableDisableFoot") { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1, 11})) + } else { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1})) + } + } else { + if rui.IsCheckboxChecked(view, "tableDisableFoot") { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{11})) + } else { + rui.Set(view, "demoTableView1", rui.AllowSelection, nil) + } + } + }) + + rui.Set(view, "tableDisableFoot", rui.CheckboxChangedEvent, func(checked bool) { + if checked { + if rui.IsCheckboxChecked(view, "tableDisableHead") { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1, 11})) + } else { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{11})) + } + } else { + if rui.IsCheckboxChecked(view, "tableDisableHead") { + rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1})) + } else { + rui.Set(view, "demoTableView1", rui.AllowSelection, nil) + } + } }) rui.Set(view, "tableCellGap", rui.DropDownEvent, func(list rui.DropDownList, number int) { diff --git a/tableAdapter.go b/tableAdapter.go index a32c36a..ee52f20 100644 --- a/tableAdapter.go +++ b/tableAdapter.go @@ -18,6 +18,14 @@ type TableCellStyle interface { CellStyle(row, column int) Params } +type TableAllowCellSelection interface { + AllowCellSelection(row, column int) bool +} + +type TableAllowRowSelection interface { + AllowRowSelection(row int) bool +} + type SimpleTableAdapter interface { TableAdapter TableCellStyle diff --git a/tableView.go b/tableView.go index 933739b..3562fb7 100644 --- a/tableView.go +++ b/tableView.go @@ -196,6 +196,12 @@ const ( // The main listener format: func(TableView, int), where the second argument is the row number. TableRowSelectedEvent = "table-row-selected" + // AllowSelection is the constant for the "allow-selection" property tag. + // The "allow-selection" property sets the adapter which specifies styles of each table row. + // This property can be assigned or by an implementation of TableAllowCellSelection + // or TableAllowRowSelection interface. + AllowSelection = "allow-selection" + // NoneSelection is the value of "selection-mode" property: the selection is forbidden. NoneSelection = 0 // CellSelection is the value of "selection-mode" property: the selection of a single cell only is enabled. @@ -293,7 +299,7 @@ func (table *tableViewData) remove(tag string) { table.propertyChanged(tag) case Gap, CellBorder, CellPadding, RowStyle, ColumnStyle, CellStyle, - HeadHeight, HeadStyle, FootHeight, FootStyle: + HeadHeight, HeadStyle, FootHeight, FootStyle, AllowSelection: if _, ok := table.properties[tag]; ok { delete(table.properties, tag) table.propertyChanged(tag) @@ -497,6 +503,19 @@ func (table *tableViewData) set(tag string, value interface{}) bool { return false } + case AllowSelection: + switch value.(type) { + case TableAllowCellSelection: + table.properties[tag] = value + + case TableAllowRowSelection: + table.properties[tag] = value + + default: + notCompatibleType(tag, value) + return false + } + case Current: switch value := value.(type) { case int: @@ -559,7 +578,8 @@ func (table *tableViewData) propertyChanged(tag string) { case Content, RowStyle, ColumnStyle, CellStyle, CellPadding, CellBorder, HeadHeight, HeadStyle, FootHeight, FootStyle, CellPaddingTop, CellPaddingRight, CellPaddingBottom, CellPaddingLeft, - TableCellClickedEvent, TableCellSelectedEvent, TableRowClickedEvent, TableRowSelectedEvent: + TableCellClickedEvent, TableCellSelectedEvent, TableRowClickedEvent, + TableRowSelectedEvent, AllowSelection: table.ReloadTableData() case Gap: @@ -861,6 +881,26 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) { ignorCells := []struct{ row, column int }{} selectionMode := GetSelectionMode(table, "") + var allowCellSelection TableAllowCellSelection = nil + if allow, ok := adapter.(TableAllowCellSelection); ok { + allowCellSelection = allow + } + if value := table.getRaw(AllowSelection); value != nil { + if style, ok := value.(TableAllowCellSelection); ok { + allowCellSelection = style + } + } + + var allowRowSelection TableAllowRowSelection = nil + if allow, ok := adapter.(TableAllowRowSelection); ok { + allowRowSelection = allow + } + if value := table.getRaw(AllowSelection); value != nil { + if style, ok := value.(TableAllowRowSelection); ok { + allowRowSelection = style + } + } + tableCSS := func(startRow, endRow int, cellTag string, cellBorder BorderProperty, cellPadding BoundsProperty) { for row := startRow; row < endRow; row++ { @@ -891,6 +931,10 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) { } buffer.WriteString(` onclick="tableRowClickEvent(this, event)"`) + + if allowRowSelection != nil && !allowRowSelection.AllowRowSelection(row) { + buffer.WriteString(` data-disabled="1"`) + } } if cssBuilder.buffer.Len() > 0 { @@ -982,6 +1026,9 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) { if selectionMode == CellSelection { buffer.WriteString(` onclick="tableCellClickEvent(this, event)"`) + if allowCellSelection != nil && !allowCellSelection.AllowCellSelection(row, column) { + buffer.WriteString(` data-disabled="1"`) + } } if columnSpan > 1 {