Scoop API

scoop:scoop.ui.component:0.1.0

src/table/state.scoop

Selected production source · src/table/state.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.collections.HashSet
6/** Application-owned query, selection and presentation state; render only reads snapshots. */
7public class DataTableState<Row> public constructor(
8 data: TableData<Row>,
9 public val columns: Array<TableColumn<Row>>,
10 query: TableQuery = TableQuery(),
11 public val selectable: (Row) -> Boolean = { _ -> true },
12 public val onQueryChange: ((TableQuery) -> Unit)? = None
13) {
14 private val computed = state(tableInitial(data, columns, query))
15 private val selection = state(TableSelection([]))
16 private val hidden = state<Array<String>>([])
17 private val sizes = state<Array<TableColumnWidth>>([])
18 public val query: TableQuery get() = computed.value.query
19 public val result: TableResult<Row> get() = computed.value.result
20 public val selectedKeys: Array<ViewKey> get() = selection.value.keys
21 public val hiddenColumns: Array<String> get() = hidden.value
22 public val widths: Array<TableColumnWidth> get() = sizes.value
24 public fun replaceData(data: TableData<Row>) {
25 val old = computed.value
26 val next = tableCompute(data, columns, old.query, Some(old))
27 val selected = ArrayList<ViewKey>()
28 for (key in selection.value.keys) {
29 val keep =
30 when (data) {
31 case Local(val source) -> source.indexOfKey(key) != None;
32 case Provided(_, _) -> true
33 }
34 if (keep)
35 selected.add(key)
36 }
37 if (selected.size != selection.value.keys.size)
38 selection.value = TableSelection(selected.toArray())
39 computed.value = next
40 notify(old.query, next.query)
41 }
42 public fun setQuery(value: TableQuery) {
43 val old = computed.value
44 val nextQuery =
45 if (tableCriteriaEqual(old.query, value))
46 value
47 else
48 TableQuery(value.sorting, value.filters, 0L, value.pageSize)
49 if (old.query == nextQuery)
50 return
51 val next = tableCompute(old.data, columns, nextQuery, Some(old))
52 computed.value = next
53 notify(old.query, next.query)
54 }
55 public fun setPage(index: Long) {
56 setQuery(TableQuery(query.sorting, query.filters, index, query.pageSize))
57 }
58 public fun setPageSize(size: Long) {
59 setQuery(TableQuery(query.sorting, query.filters, 0L, size))
60 }
61 public fun toggleSort(id: String, additive: Boolean = false) {
62 requireComponent(tableColumn(columns, id).sortable, "table column is not sortable")
63 val next = ArrayList<TableSort>()
64 var found = false
65 for (sort in query.sorting) {
66 if (sort.column == id) {
67 found = true
68 if (sort.direction == TableSortDirection.Ascending)
69 next.add(TableSort(id, TableSortDirection.Descending))
70 } else
71 if (additive)
72 next.add(sort)
73 }
74 if (!found)
75 next.add(TableSort(id))
76 setQuery(TableQuery(next.toArray(), query.filters, 0L, query.pageSize))
77 }
78 public fun setFilter(id: String, text: String) {
79 tableColumn(columns, id)
80 val next = ArrayList<TableFilter>()
81 for (filter in query.filters)
82 if (filter.column != id)
83 next.add(filter)
84 if (text != "")
85 next.add(TableFilter(id, text))
86 setQuery(TableQuery(query.sorting, next.toArray(), 0L, query.pageSize))
87 }
88 private fun notify(previous: TableQuery, next: TableQuery) {
89 if (previous == next)
90 return
91 when (onQueryChange) {
92 case Some(val action) -> action(next);
93 case None -> {}
94 }
95 }
96 public fun isSelected(key: ViewKey): Boolean = selection.value.lookup.contains(key)
97 public fun setSelected(key: ViewKey, value: Boolean) {
98 if (value == isSelected(key))
99 return
100 if (value) {
101 val source = tableDataSource(computed.value.data)
102 val index = source.indexOfKey(key) ?: return
103 if (!selectable(source.rowAt(index)))
104 return
105 }
106 val next = ArrayList<ViewKey>()
107 for (selected in selection.value.keys)
108 if (selected != key)
109 next.add(selected)
110 if (value)
111 next.add(key)
112 selection.value = TableSelection(next.toArray())
113 }
114 public val pageSelectableCount: Long get() {
115 val source = result.rows
116 var count = 0L
117 for (index in 0L ..< source.count)
118 if (selectable(source.rowAt(index)))
119 count += 1L
120 return count
121 }
122 public val pageSelection: CheckState get() {
123 val source = result.rows
124 var total = 0L
125 var selected = 0L
126 for (index in 0L ..< source.count) {
127 if (selectable(source.rowAt(index))) {
128 total += 1L
129 if (isSelected(source.keyAt(index)))
130 selected += 1L
131 }
132 }
133 return if (selected == 0L)
134 CheckState.Unchecked
135 else
136 if (selected == total)
137 CheckState.Checked
138 else
139 CheckState.Indeterminate
140 }
141 public fun selectPage(value: Boolean) {
142 val source = result.rows
143 val next = ArrayList<ViewKey>()
144 for (key in selection.value.keys) {
145 val index = source.indexOfKey(key)
146 val targeted =
147 when (index) {
148 case Some(val at) -> selectable(source.rowAt(at));
149 case None -> false
150 }
151 if (!targeted || value)
152 next.add(key)
153 }
154 if (value)
155 for (index in 0L ..< source.count) {
156 val key = source.keyAt(index)
157 if (selectable(source.rowAt(index)) && !isSelected(key))
158 next.add(key)
159 }
160 selection.value = TableSelection(next.toArray())
161 }
162 public fun isColumnVisible(id: String): Boolean {
163 for (value in hidden.value)
164 if (value == id)
165 return false
166 return true
167 }
168 public fun showColumn(id: String, value: Boolean) {
169 val column = tableColumn(columns, id)
170 if (!column.hideable || isColumnVisible(id) == value)
171 return
172 // Keep one data column available for row content and actions.
173 if (!value && hidden.value.size == columns.size - 1L)
174 return
175 val next = ArrayList<String>()
176 for (item in hidden.value)
177 if (item != id)
178 next.add(item)
179 if (!value)
180 next.add(id)
181 hidden.value = next.toArray()
182 }
183 public fun columnWidth(id: String): Double? {
184 for (value in sizes.value)
185 if (value.column == id)
186 return Some(value.width)
187 return None
188 }
189 public fun resizeColumn(id: String, width: Double) {
190 val column = tableColumn(columns, id)
191 if (!column.resizable)
192 return
193 val nextWidth = column.size.constrain(width)
194 if (columnWidth(id) == Some(nextWidth))
195 return
196 setWidth(id, Some(nextWidth))
197 }
198 public fun resetColumnWidth(id: String) {
199 tableColumn(columns, id)
200 if (columnWidth(id) != None)
201 setWidth(id, None)
202 }
203 private fun setWidth(id: String, width: Double?) {
204 val next = ArrayList<TableColumnWidth>()
205 for (value in sizes.value)
206 if (value.column != id)
207 next.add(value)
208 when (width) {
209 case Some(val value) -> next.add(TableColumnWidth(id, value));
210 case None -> {}
211 }
212 sizes.value = next.toArray()
213 }
216private class TableSelection(val keys: Array<ViewKey>) {
217 val lookup = HashSet<ViewKey>()
218 init {
219 for (key in keys)
220 lookup.add(key)
221 }