Scoop API

scoop:scoop.ui.component:0.1.0

src/table/data_controls.scoop

Selected production source · src/table/data_controls.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.window.accessibility.*
6import scoop.ui.style.*
7import scoop.ui.component.row as tableControlsRow
8import scoop.ui.component.select as tablePageSizeSelect
9import scoop.ui.component.pagination as tablePageNavigation
11internal fun <Row> dataTableHeader(input: DataTableProps<Row>, plan: DataTablePlan<Row>): View {
12 val model = input.state
13 val cells = ArrayList<TableCellProps>()
14 val views = ArrayList<View>()
15 if (input.selection) {
16 val cell =
17 TableCellProps(
18 PanelContent.Custom(
19 checkbox(
20 binding(
21 { model.pageSelection },
22 { value -> model.selectPage(value == CheckState.Checked) }
23 )
24 ) {
25 this.enabled = input.enabled && model.pageSelectableCount > 0L;
26 this.theme = input.theme
27 this.semantics =
28 ComponentSemantics(name = Some("Select all rows on this page"))
29 }
30 ),
31 header = true
32 )
33 cells.add(cell)
34 views.add(
35 plan.appearance.cell(
36 cell,
37 ViewKey.Text("selection"),
38 semantics =
39 Some(basicCellSemantics(cell, reference = Some(plan.semanticHeaders[0L])))
40 )
41 )
42 }
43 for (column in plan.columns) {
44 var title = column.header
45 var order = 0L
46 for (sort in model.query.sorting) {
47 order += 1L
48 if (sort.column == column.id)
49 title += if (sort.direction == TableSortDirection.Ascending)
50 f" ↑${order}"
51 else
52 f" ↓${order}"
53 }
54 val label =
55 if (column.sortable)
56 PanelContent.Custom(
57 button(title, { model.toggleSort(column.id, input.additiveSort) }) {
58 this.variant = ButtonVariant.Ghost
59 this.size = ButtonSize.Sm
60 this.enabled = input.enabled
61 this.fonts = input.fonts
62 this.theme = input.theme
63 this.style { this.surface { this.padding = Some(Insets.all(0.0)) } }
64 }
65 )
66 else
67 tableText(title)
68 val cell =
69 TableCellProps(
70 label,
71 alignment = column.alignment,
72 header = true,
73 semantics = ComponentSemantics(name = Some(title))
74 )
75 cells.add(cell)
76 val key = ViewKey.Text(f"column:${column.id}")
77 val content =
78 plan.appearance.cell(
79 cell,
80 key,
81 semantics =
82 Some(
83 basicCellSemantics(
84 cell,
85 reference = Some(plan.semanticHeaders[cells.size - 1L])
86 )
87 )
88 )
89 views.add(
90 if (column.resizable)
91 element(
92 TableResizeInput(
93 binding(
94 { model.columnWidth(column.id) },
95 {
96 width -> when (width) {
97 case Some(val value) -> model.resizeColumn(column.id, value);
98 case None -> model.resetColumnWidth(column.id)
99 }
100 }
101 ),
102 column.size,
103 plan.appearance.theme,
104 input.enabled,
105 ComponentSemantics(name = Some("Resize " + column.header))
106 ),
107 { props, scope -> TableResizeHeader(props, scope) },
108 [content],
109 key
110 )
111 else
112 content
113 )
114 }
115 return tableRowView(
116 TableRowProps(cells.toArray(), TableSection.Header),
117 plan.tracks,
118 plan.appearance,
119 ViewKey.Text("header"),
120 prepared = Some(views.toArray())
121 )
124internal fun <Row> dataTableColumnMenu(input: DataTableProps<Row>, open: Binding<Boolean>): View {
125 val model = input.state
126 val content =
127 menu {
128 for (column in model.columns)
129 if (column.hideable)
130 this.checkboxItem(
131 ViewKey.Text(column.id),
132 column.header,
133 binding(
134 { model.isColumnVisible(column.id) },
135 { value -> model.showColumn(column.id, value) }
136 )
137 ) {
138 this.enabled =
139 input.enabled &&
140 (
141 !model.isColumnVisible(column.id) ||
142 model.hiddenColumns.size <
143 model.columns.size -
144 1L
145 )
146 }
147 }
148 return dropdownMenu(open, content) {
149 this.trigger("Columns") {
150 this.variant = ButtonVariant.Outline;
151 this.size = ButtonSize.Sm
152 }
153 this.enabled = input.enabled && content.entries.size > 0L
154 this.fonts = input.fonts
155 this.theme = input.theme
156 }
159internal fun <Row> dataTablePagination(
160 input: DataTableProps<Row>,
161 appearance: TableAppearance
162): View {
163 val model = input.state
164 return tableControlsRow {
165 this.spacing = 12.0 * appearance.density
166 this.alignment = CrossAlignment.Center
167 this.item(
168 appearance.content(
169 tableText(
170 f"Selected: ${model.selectedKeys.size} · Rows: ${model.result.totalRows}"
171 ),
172 dim = true
173 )
174 ) { this.grow = 1.0 }
175 if (input.pageSizes.size > 0L)
176 this.add(
177 tablePageSizeSelect(
178 binding<Long?>(
179 { Some(model.query.pageSize) },
180 {
181 value -> when (value) {
182 case Some(val size) -> model.setPageSize(size);
183 case None -> {}
184 }
185 }
186 )
187 ) {
188 this.width = Some(80.0 * appearance.density)
189 this.size = SelectSize.Sm
190 this.enabled = input.enabled
191 this.theme = input.theme
192 this.fonts = input.fonts
193 for (size in input.pageSizes)
194 this.item(Choice(ViewKey.Number(size), size, f"${size}"))
195 }
196 )
197 this.add(
198 tablePageNavigation(
199 binding({ model.query.pageIndex + 1L }, { value -> model.setPage(value - 1L) }),
200 model.result.pageCount
201 ) {
202 this.compact = true
203 this.items =
204 Some(
205 if (model.result.pageCount == 0L)
206 []
207 else
208 [PaginationItem.Page(model.query.pageIndex + 1L)]
209 )
210 this.enabled = input.enabled
211 this.theme = input.theme
212 this.fonts = input.fonts
213 }
214 )
215 }