ozdenui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
Data Table - v26.10
|
|
6
|
+
|
|
7
|
+
A table made from data: the columns from a list of objects, the rows from a list of arrays (or objects).
|
|
8
|
+
WHEN TO USE IT: the better looking table (style packages, sorting, search, selection, your own
|
|
9
|
+
cells), but the data it can hold has a limit: every row is a basic.js object (about 9 DOM nodes), all made at once.
|
|
10
|
+
Measured (headless Chrome, Apple M1, 4 columns), the time to the screen: 200 rows 0.07 s, 1 000 rows
|
|
11
|
+
0.13 s, 2 000 rows 0.27 s, 5 000 rows 0.65 s, 10 000 rows 1.4 s (a phone is slower). Up to about 2 000 rows. Very big
|
|
12
|
+
data (tens of thousands of rows, very many columns): comp/smart-table.js (SmartTable: it makes only the rows that are
|
|
13
|
+
seen; 100 000 rows in about 0.1 s in the Tauri template). A few rows inside a text: comp/tiny-table.js (TinyTable).
|
|
14
|
+
- Samples: 02-comp-examples/data-table.htm, data-table-2.htm ... data-table-5.htm (data: assets/data-table/).
|
|
15
|
+
- Sorting: a click on a sortable title (again: the other direction), the keyboard (Enter / Space on a title), sortBy().
|
|
16
|
+
- Search: showSearch: 1 puts a SearchInput (comp/search-input.js, load it first) in the header; search(). Escape
|
|
17
|
+
clears it (searchParams: { clearOnEscape: 0 } turns it off).
|
|
18
|
+
- Selection: a click, the arrow keys (Home / End), Enter runs onRowClick. select(id), getSelectedData().
|
|
19
|
+
- updateRows() adds, changes and removes rows by their id: the sorting, the search and the selection stay.
|
|
20
|
+
- Your own cell: column.createView(cell, value, row, self) makes objects in the cell (a chart, a button). It runs
|
|
21
|
+
again when the value of the row changes (updateRows, updateRow).
|
|
22
|
+
- The rows scroll with the ScrollBar of basic.js (basic/scroll-bar.js, load it first; useScrollBar: 0 turns it off).
|
|
23
|
+
- A table narrower than its columns (a phone): the rows scroll to the side and the titles go with them; an "auto"
|
|
24
|
+
column is never narrower than its minWidth.
|
|
25
|
+
- The rows off the screen are not laid out or drawn (CSS content-visibility: auto): 10 000 rows came on the
|
|
26
|
+
screen in 2.2 s before, 1.4 s now, a sort took 315 ms, now 210 ms.
|
|
27
|
+
- The values are shown as plain text (a value can not inject HTML).
|
|
28
|
+
- Style packages: "light" (default; the old name "classic" works too), "plain" (white, lines only), "dark",
|
|
29
|
+
"minimal", "minimalDark" (the minimal theme, _designs/minimal-theme.htm: as SmartTable "minimal"). (DataTable.styles)
|
|
30
|
+
|
|
31
|
+
A COLUMN:
|
|
32
|
+
{
|
|
33
|
+
key: "age", // its name (sortBy("age"), search(text, "age"))
|
|
34
|
+
title: "Age", // the text of the title ("": an empty title)
|
|
35
|
+
valueIndex: 3, // the index of the value in a row array (no value: the index of the column).
|
|
36
|
+
// A row object: row[key].
|
|
37
|
+
type: "number", // "text" (default) or "number": how it is sorted
|
|
38
|
+
width: 100, // a number or "auto" (default): the "auto" columns share the width that is left
|
|
39
|
+
minWidth: 100, // an "auto" column is never narrower (default 100); a narrower table scrolls to the side
|
|
40
|
+
align: "left", // "left" (default), "center", "right"
|
|
41
|
+
sortable: 1, // a click on the title sorts the rows
|
|
42
|
+
iconFile: "", // a small icon before the title
|
|
43
|
+
cellColor: "", // the color of the cells of the column ("": the color of the row)
|
|
44
|
+
cellStyle: {}, // props of the labels of the column (Ex: { fontSize: 12, bold: 1 })
|
|
45
|
+
createView: null, // function (cell, value, row, self) {}: your objects in the cell (cell.label is hidden)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
USAGE:
|
|
49
|
+
const table = DataTable({
|
|
50
|
+
width: 600,
|
|
51
|
+
height: 400,
|
|
52
|
+
titleText: "Customers",
|
|
53
|
+
showSearch: 1,
|
|
54
|
+
columns: [
|
|
55
|
+
{ key: "id", title: "#", valueIndex: 0, type: "number", width: 60, sortable: 1 },
|
|
56
|
+
{ key: "name", title: "Name", valueIndex: 1, sortable: 1 },
|
|
57
|
+
{ key: "city", title: "City", valueIndex: 2 },
|
|
58
|
+
],
|
|
59
|
+
rows: [[1, "John Smith", "New York"], [2, "Jane Doe", "Paris"]],
|
|
60
|
+
sortKey: "name",
|
|
61
|
+
onRowClick: function (self, row) { println(row.data); },
|
|
62
|
+
});
|
|
63
|
+
table.updateRows(newRows);
|
|
64
|
+
table.sortBy("id", "desc");
|
|
65
|
+
table.search("paris");
|
|
66
|
+
table.select(2);
|
|
67
|
+
DataTable({ styleName: "dark", ... });
|
|
68
|
+
|
|
69
|
+
Started Date: October 2026
|
|
70
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
71
|
+
|
|
72
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
73
|
+
Licensed under the Apache License, Version 2.0
|
|
74
|
+
SPDX-License-Identifier: Apache-2.0
|
|
75
|
+
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
"use strict";
|
|
79
|
+
|
|
80
|
+
// Default values:
|
|
81
|
+
const DataTableDefaults = {
|
|
82
|
+
width: 600,
|
|
83
|
+
height: 400,
|
|
84
|
+
columns: [], // See "A COLUMN" above.
|
|
85
|
+
rows: [], // [[1, "John", "Paris"], ...] or [{ id: 1, name: "John", city: "Paris" }, ...]
|
|
86
|
+
idIndex: 0, // The id of a row array: row[idIndex]
|
|
87
|
+
idKey: "id", // The id of a row object: row[idKey]
|
|
88
|
+
sortKey: "", // "": the order of the data
|
|
89
|
+
sortDirection: "asc", // "asc" or "desc"
|
|
90
|
+
canSelect: 1, // 0: a click runs onRowClick, no row is selected
|
|
91
|
+
rowHeight: "auto", // "auto": as high as its text (more lines); a number: one line, cut with "..."
|
|
92
|
+
titleText: "", // "": no title in the header
|
|
93
|
+
showSearch: 0, // 1: a search box in the header (comp/search-input.js SearchInput must be loaded)
|
|
94
|
+
searchParams: {}, // More params of the SearchInput. Ex: { placeholder: "Ara" }
|
|
95
|
+
emptyText: "There are no records.",
|
|
96
|
+
noResultText: "No records found.",
|
|
97
|
+
sortIconFile: "data:image/svg+xml;utf8," + encodeURIComponent('<?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" id="Outline" viewBox="0 0 24 24" width="512" height="512"><path d="M17.71,9.88l-4.3-4.29a2,2,0,0,0-2.82,0L6.29,9.88a1,1,0,0,0,0,1.41,1,1,0,0,0,1.42,0L11,8V19a1,1,0,0,0,2,0V8l3.29,3.29a1,1,0,1,0,1.42-1.41Z"/></svg>'),
|
|
98
|
+
useScrollBar: 1, // basic/scroll-bar.js ScrollBar
|
|
99
|
+
onRowClick: function (self, row) {}, // A click on a row, or Enter on the selected row
|
|
100
|
+
onSelect: function (self, row) {}, // The selected row changed (row: null when nothing is selected). Not with silent: 1
|
|
101
|
+
onSort: function (self) {}, // self.sortKey, self.sortDirection
|
|
102
|
+
styleName: "light", // "light" ("classic": its old name), "plain", "dark", "minimal", "minimalDark" or a name added to DataTable.styles
|
|
103
|
+
style: { // Light style package (default)
|
|
104
|
+
box: { color: "white", border: 1, borderColor: Black(0.15), round: 6 },
|
|
105
|
+
focus: { ringColor: "rgba(95, 158, 160, 0.6)" },
|
|
106
|
+
header: { height: 60, padding: [16, 0], gap: 12 },
|
|
107
|
+
headerTitle: { fontSize: 20, textColor: Black(0.75), fontFamily: "opensans-bold" },
|
|
108
|
+
search: { width: 220, height: 40, fontSize: 16, searchIconSize: 20 }, // + the params of SearchInput
|
|
109
|
+
searchStyleName: "ozden",
|
|
110
|
+
titleRow: { height: 50, color: "whitesmoke", lineColor: Black(0.2) },
|
|
111
|
+
titleCell: {
|
|
112
|
+
padding: 10,
|
|
113
|
+
gap: 6,
|
|
114
|
+
fontSize: 15,
|
|
115
|
+
textColor: Black(0.8),
|
|
116
|
+
fontFamily: "opensans-bold",
|
|
117
|
+
lineColor: Black(0.08), // between two titles
|
|
118
|
+
hoverColor: "white",
|
|
119
|
+
uppercase: 1,
|
|
120
|
+
},
|
|
121
|
+
titleIcon: { size: 16, opacity: 0.7, invert: 0 }, // invert: 1 -> a dark icon is drawn light (a dark page)
|
|
122
|
+
sortIcon: { size: 16, opacity: 0.25, activeOpacity: 1, invert: 0 },
|
|
123
|
+
row: {
|
|
124
|
+
padding: [10, 12], // the space around the text of a cell: [left / right, top / bottom]
|
|
125
|
+
fontSize: 15,
|
|
126
|
+
textColor: Black(0.7),
|
|
127
|
+
fontFamily: "opensans",
|
|
128
|
+
oddColor: "white",
|
|
129
|
+
evenColor: "#F7F7F7",
|
|
130
|
+
hoverColor: "rgba(254, 193, 8, 0.08)",
|
|
131
|
+
selectedColor: "rgba(254, 193, 8, 0.18)",
|
|
132
|
+
lineColor: Black(0.08),
|
|
133
|
+
},
|
|
134
|
+
empty: { fontSize: 15, textColor: Black(0.45), top: 40 },
|
|
135
|
+
scrollBar: { neverHide: 0, showDots: 0 },
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const DataTable = function (params = {}) {
|
|
140
|
+
|
|
141
|
+
// Merge style package: params.style > DataTable.styles[styleName] > DataTableDefaults.style (light)
|
|
142
|
+
basic.applyStylePackage(params, DataTable.styles, DataTableDefaults);
|
|
143
|
+
|
|
144
|
+
// Merge params:
|
|
145
|
+
mergeIntoIfMissing(params, DataTableDefaults);
|
|
146
|
+
|
|
147
|
+
// Edit params: copies, the caller's lists do not change
|
|
148
|
+
params.columns = DataTable.fitColumns(params.columns);
|
|
149
|
+
params.rows = Array.isArray(params.rows) ? params.rows.slice() : [];
|
|
150
|
+
|
|
151
|
+
// BOX: Component container
|
|
152
|
+
let box = startObject(params);
|
|
153
|
+
|
|
154
|
+
// *** PRIVATE VARIABLES:
|
|
155
|
+
const _s = box.style;
|
|
156
|
+
const ARROW_DOWN = 180; // deg: the sort icon of "desc"
|
|
157
|
+
const ROW_LINE_HEIGHT = 1.4; // the line of a text in a row, x its font size (only a guess: guessRowHeight)
|
|
158
|
+
let searchText = "";
|
|
159
|
+
let searchKey = "";
|
|
160
|
+
let shownRowCount = 0;
|
|
161
|
+
|
|
162
|
+
// *** PUBLIC VARIABLES:
|
|
163
|
+
box.rowList = []; // The row objects, in the order of the data. (row.data, row.id, row.cells)
|
|
164
|
+
box.shownRows = []; // The rows that are seen, in the order on the screen
|
|
165
|
+
box.selectedRow = null;
|
|
166
|
+
box.titleCells = []; // The title cells, in the order of the columns
|
|
167
|
+
// NOTE: Default values are also public variables. (box.columns, box.sortKey, box.sortDirection...)
|
|
168
|
+
|
|
169
|
+
// *** PRIVATE FUNCTIONS:
|
|
170
|
+
|
|
171
|
+
const getRowId = function (data) {
|
|
172
|
+
if (Array.isArray(data)) return data[box.idIndex];
|
|
173
|
+
return data ? data[box.idKey] : undefined;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const getValue = function (data, column) {
|
|
177
|
+
if (Array.isArray(data)) return data[column.valueIndex];
|
|
178
|
+
return data ? data[column.key] : undefined;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const findColumn = function (key) {
|
|
182
|
+
return box.columns.find(function (column) { return column.key == key; }) || null;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// The width the columns need: a narrower table scrolls to the side.
|
|
186
|
+
const findColumnsWidth = function () {
|
|
187
|
+
return box.columns.reduce(function (sum, column) {
|
|
188
|
+
return sum + ((column.width == "auto") ? column.minWidth : Number(column.width));
|
|
189
|
+
}, 0);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const findRow = function (id) {
|
|
193
|
+
return box.rowList.find(function (row) { return row.id == id; }) || null;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// The flex size of a column: a number is fixed, "auto" shares the free width, not under its minWidth.
|
|
197
|
+
// WHY the minimum: with a minimum of 0, a narrow table (a phone) gave its "auto" column no width, and its text
|
|
198
|
+
// broke after every letter, so every row was very tall.
|
|
199
|
+
const applyColumnWidth = function (cell, column) {
|
|
200
|
+
if (column.width == "auto") {
|
|
201
|
+
cell.width = 0;
|
|
202
|
+
cell.css = { flex: "1 1 0", minWidth: column.minWidth + "px" };
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
cell.css = { flex: "0 0 auto" };
|
|
206
|
+
cell.width = column.width;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// *** THE TITLES:
|
|
210
|
+
|
|
211
|
+
const createTitleCell = function (column, index) {
|
|
212
|
+
|
|
213
|
+
const isLast = (index == box.columns.length - 1);
|
|
214
|
+
|
|
215
|
+
// GROUP: Title cell
|
|
216
|
+
const titleCell = HGroup({
|
|
217
|
+
height: "100%",
|
|
218
|
+
align: DataTable.toAlign(column.align),
|
|
219
|
+
gap: _s.titleCell.gap,
|
|
220
|
+
padding: [_s.titleCell.padding, 0],
|
|
221
|
+
color: "transparent",
|
|
222
|
+
});
|
|
223
|
+
applyColumnWidth(titleCell, column);
|
|
224
|
+
if (!isLast) titleCell.css = { borderRight: "1px solid " + _s.titleCell.lineColor };
|
|
225
|
+
titleCell.column = column;
|
|
226
|
+
|
|
227
|
+
if (column.iconFile) {
|
|
228
|
+
// ICON: Title icon
|
|
229
|
+
titleCell.icon = Icon({ width: _s.titleIcon.size, height: _s.titleIcon.size, opacity: _s.titleIcon.opacity });
|
|
230
|
+
titleCell.icon.load(column.iconFile);
|
|
231
|
+
titleCell.icon.shrink = 0;
|
|
232
|
+
if (_s.titleIcon.invert) titleCell.icon.css = { filter: "invert(1)" };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// LABEL: Title text
|
|
236
|
+
titleCell.label = Label({
|
|
237
|
+
plainText: column.title,
|
|
238
|
+
fontSize: _s.titleCell.fontSize,
|
|
239
|
+
textColor: _s.titleCell.textColor,
|
|
240
|
+
fontFamily: _s.titleCell.fontFamily,
|
|
241
|
+
ellipsis: 1,
|
|
242
|
+
css: { minWidth: "0px", textTransform: _s.titleCell.uppercase ? "uppercase" : "none" },
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
if (column.sortable) {
|
|
246
|
+
// ICON: Sort direction
|
|
247
|
+
titleCell.sortIcon = Icon({ width: _s.sortIcon.size, height: _s.sortIcon.size, opacity: _s.sortIcon.opacity });
|
|
248
|
+
titleCell.sortIcon.load(box.sortIconFile);
|
|
249
|
+
titleCell.sortIcon.shrink = 0;
|
|
250
|
+
if (_s.sortIcon.invert) titleCell.sortIcon.css = { filter: "invert(1)" };
|
|
251
|
+
titleCell.sortIcon.setMotion("opacity 0.3s, transform 0.3s");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
endGroup();
|
|
255
|
+
|
|
256
|
+
if (column.sortable) makeTitleSortable(titleCell);
|
|
257
|
+
|
|
258
|
+
return titleCell;
|
|
259
|
+
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const makeTitleSortable = function (titleCell) {
|
|
263
|
+
|
|
264
|
+
titleCell.clickable = 1;
|
|
265
|
+
titleCell.cursor = "pointer";
|
|
266
|
+
titleCell.role = "button";
|
|
267
|
+
titleCell.tabIndex = 0;
|
|
268
|
+
titleCell.aria = { label: "Sort by " + (titleCell.column.title || titleCell.column.key) };
|
|
269
|
+
titleCell.setFocusRing(_s.focus.ringColor);
|
|
270
|
+
|
|
271
|
+
titleCell.onHoverChange(function (self, isOver) {
|
|
272
|
+
titleCell.color = isOver ? _s.titleCell.hoverColor : "transparent";
|
|
273
|
+
});
|
|
274
|
+
titleCell.on("click", function () {
|
|
275
|
+
toggleSort(titleCell.column);
|
|
276
|
+
});
|
|
277
|
+
titleCell.on("keydown", function (self, event) {
|
|
278
|
+
if (event.key != "Enter" && event.key != " ") return;
|
|
279
|
+
event.preventDefault();
|
|
280
|
+
toggleSort(titleCell.column);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// The same title again changes the direction; another title starts with "asc".
|
|
286
|
+
const toggleSort = function (column) {
|
|
287
|
+
const isSameColumn = (box.sortKey == column.key);
|
|
288
|
+
const direction = (isSameColumn && box.sortDirection == "asc") ? "desc" : "asc";
|
|
289
|
+
box.sortBy(column.key, direction);
|
|
290
|
+
box.onSort(box);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const paintSortIcons = function () {
|
|
294
|
+
box.titleCells.forEach(function (titleCell) {
|
|
295
|
+
if (!titleCell.sortIcon) return;
|
|
296
|
+
const isActive = (titleCell.column.key == box.sortKey);
|
|
297
|
+
titleCell.sortIcon.opacity = isActive ? _s.sortIcon.activeOpacity : _s.sortIcon.opacity;
|
|
298
|
+
titleCell.sortIcon.rotate = (isActive && box.sortDirection == "desc") ? ARROW_DOWN : 0;
|
|
299
|
+
titleCell.aria = { sort: isActive ? DataTable.toAriaSort(box.sortDirection) : "none" };
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// *** THE ROWS:
|
|
304
|
+
|
|
305
|
+
// A row: a group of cells. Its container must be set (createIn).
|
|
306
|
+
// The height of a row before the browser draws it (content-visibility). "auto" rows: one line of text.
|
|
307
|
+
const guessRowHeight = function () {
|
|
308
|
+
if (typeof box.rowHeight === "number") return box.rowHeight;
|
|
309
|
+
const padding = _s.row.padding;
|
|
310
|
+
const paddingY = Array.isArray(padding) ? padding[1] : (padding || 0);
|
|
311
|
+
return Math.round(_s.row.fontSize * ROW_LINE_HEIGHT + paddingY * 2 + 1);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const createRow = function (data) {
|
|
315
|
+
|
|
316
|
+
// GROUP: Row
|
|
317
|
+
// WHY content-visibility: the browser does not lay out and draw the rows off the screen. Measured
|
|
318
|
+
// with 10 000 rows: on the screen 2.2 s -> 1.5 s, a sort 315 -> 151 ms. Every row is still made (most of
|
|
319
|
+
// the time); the "auto" of contain-intrinsic-size keeps the real height of a row once it was drawn.
|
|
320
|
+
const row = HGroup({
|
|
321
|
+
width: "100%",
|
|
322
|
+
height: box.rowHeight,
|
|
323
|
+
align: "left top",
|
|
324
|
+
color: _s.row.oddColor,
|
|
325
|
+
clickable: 1,
|
|
326
|
+
cursor: "pointer",
|
|
327
|
+
role: "row",
|
|
328
|
+
css: {
|
|
329
|
+
borderBottom: "1px solid " + _s.row.lineColor,
|
|
330
|
+
flex: "0 0 auto",
|
|
331
|
+
contentVisibility: "auto",
|
|
332
|
+
containIntrinsicSize: "auto " + guessRowHeight() + "px",
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
row.data = data;
|
|
336
|
+
row.id = getRowId(data);
|
|
337
|
+
row.isHover = 0;
|
|
338
|
+
row.cells = [];
|
|
339
|
+
|
|
340
|
+
box.columns.forEach(function (column) {
|
|
341
|
+
row.cells.push(createCell(row, column));
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
endGroup();
|
|
345
|
+
|
|
346
|
+
row.onHoverChange(function (self, isOver) {
|
|
347
|
+
row.isHover = isOver;
|
|
348
|
+
paintRow(row);
|
|
349
|
+
});
|
|
350
|
+
row.on("click", function () {
|
|
351
|
+
clickRow(row);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
return row;
|
|
355
|
+
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const createCell = function (row, column) {
|
|
359
|
+
|
|
360
|
+
const isOneLine = (box.rowHeight != "auto");
|
|
361
|
+
|
|
362
|
+
// GROUP: Cell
|
|
363
|
+
const cell = HGroup({
|
|
364
|
+
height: "auto",
|
|
365
|
+
align: DataTable.toAlign(column.align),
|
|
366
|
+
padding: _s.row.padding,
|
|
367
|
+
color: column.cellColor || "transparent",
|
|
368
|
+
role: "cell",
|
|
369
|
+
css: { alignSelf: "stretch" },
|
|
370
|
+
});
|
|
371
|
+
applyColumnWidth(cell, column);
|
|
372
|
+
cell.column = column;
|
|
373
|
+
|
|
374
|
+
// LABEL: Value
|
|
375
|
+
cell.label = Label({
|
|
376
|
+
fontSize: _s.row.fontSize,
|
|
377
|
+
textColor: _s.row.textColor,
|
|
378
|
+
fontFamily: _s.row.fontFamily,
|
|
379
|
+
textAlign: column.align,
|
|
380
|
+
ellipsis: isOneLine ? 1 : 0,
|
|
381
|
+
css: isOneLine ? { minWidth: "0px" } : { minWidth: "0px", whiteSpace: "normal", overflowWrap: "anywhere" },
|
|
382
|
+
...column.cellStyle,
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
endGroup();
|
|
386
|
+
|
|
387
|
+
applyCellValue(cell, row);
|
|
388
|
+
return cell;
|
|
389
|
+
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// The value of the row in the cell: its text, or the objects of column.createView (made again).
|
|
393
|
+
const applyCellValue = function (cell, row) {
|
|
394
|
+
|
|
395
|
+
const column = cell.column;
|
|
396
|
+
const value = getValue(row.data, column);
|
|
397
|
+
|
|
398
|
+
if (!column.createView) {
|
|
399
|
+
cell.label.plainText = DataTable.toText(value);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
cell.label.visible = 0;
|
|
404
|
+
if (cell.content) cell.content.remove();
|
|
405
|
+
cell.content = createIn(cell, function () {
|
|
406
|
+
const content = HGroup({ width: "100%", height: "auto", align: DataTable.toAlign(column.align), clipContent: 0 });
|
|
407
|
+
column.createView(cell, value, row, box);
|
|
408
|
+
endGroup();
|
|
409
|
+
return content;
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
// A row that exists already gets its new data (its objects stay).
|
|
415
|
+
const refreshRow = function (row, data) {
|
|
416
|
+
row.data = data;
|
|
417
|
+
row.cells.forEach(function (cell) {
|
|
418
|
+
applyCellValue(cell, row);
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const addRowObject = function (data) {
|
|
423
|
+
const row = createIn(box.rowsGroup, function () { return createRow(data); });
|
|
424
|
+
box.rowList.push(row);
|
|
425
|
+
return row;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
const removeRowObject = function (row) {
|
|
429
|
+
if (row == box.selectedRow) box.selectedRow = null;
|
|
430
|
+
row.remove();
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
// The color of a row: selected > under the mouse > odd / even.
|
|
434
|
+
const paintRow = function (row) {
|
|
435
|
+
if (row == box.selectedRow) {
|
|
436
|
+
row.color = _s.row.selectedColor;
|
|
437
|
+
} else if (row.isHover) {
|
|
438
|
+
row.color = _s.row.hoverColor;
|
|
439
|
+
} else {
|
|
440
|
+
row.color = row.isEven ? _s.row.evenColor : _s.row.oddColor;
|
|
441
|
+
}
|
|
442
|
+
row.aria = { selected: (row == box.selectedRow) };
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
// *** SORT AND SEARCH: one function shows the rows (the order, the search, the colors, the empty text)
|
|
446
|
+
|
|
447
|
+
const compareRows = function (rowA, rowB, column) {
|
|
448
|
+
const a = getValue(rowA.data, column);
|
|
449
|
+
const b = getValue(rowB.data, column);
|
|
450
|
+
if (column.type == "number") return (parseFloat(a) || 0) - (parseFloat(b) || 0);
|
|
451
|
+
// WHY numeric: "10" comes after "9"; sensitivity base: "a" and "A" are sorted together
|
|
452
|
+
return DataTable.toText(a).localeCompare(DataTable.toText(b), undefined, { numeric: true, sensitivity: "base" });
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const getSortedRows = function () {
|
|
456
|
+
const column = findColumn(box.sortKey);
|
|
457
|
+
if (!column) return box.rowList.slice();
|
|
458
|
+
const direction = (box.sortDirection == "desc") ? -1 : 1;
|
|
459
|
+
return box.rowList.slice().sort(function (rowA, rowB) {
|
|
460
|
+
return compareRows(rowA, rowB, column) * direction;
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const isRowMatching = function (row) {
|
|
465
|
+
if (searchText == "") return true;
|
|
466
|
+
return box.columns.some(function (column) {
|
|
467
|
+
if (searchKey && searchKey != column.key) return false;
|
|
468
|
+
return DataTable.toSearchText(getValue(row.data, column)).includes(searchText);
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// WHY the CSS order: the rows are flex items; sorting moves no object and makes nothing again.
|
|
473
|
+
const showRows = function () {
|
|
474
|
+
|
|
475
|
+
box.shownRows = [];
|
|
476
|
+
|
|
477
|
+
getSortedRows().forEach(function (row, order) {
|
|
478
|
+
row.css = { order: String(order) };
|
|
479
|
+
row.visible = isRowMatching(row) ? 1 : 0;
|
|
480
|
+
if (!row.visible) return;
|
|
481
|
+
row.isEven = (box.shownRows.length % 2 == 1);
|
|
482
|
+
box.shownRows.push(row);
|
|
483
|
+
});
|
|
484
|
+
shownRowCount = box.shownRows.length;
|
|
485
|
+
|
|
486
|
+
box.rowList.forEach(paintRow);
|
|
487
|
+
paintSortIcons();
|
|
488
|
+
showEmptyText();
|
|
489
|
+
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const showEmptyText = function () {
|
|
493
|
+
box.lblEmpty.plainText = (box.rowList.length == 0) ? box.emptyText : box.noResultText;
|
|
494
|
+
box.lblEmpty.visible = (shownRowCount == 0) ? 1 : 0;
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// *** SELECTION:
|
|
498
|
+
|
|
499
|
+
const setSelectedRow = function (row, silent) {
|
|
500
|
+
if (row == box.selectedRow) return;
|
|
501
|
+
const oldRow = box.selectedRow;
|
|
502
|
+
box.selectedRow = row;
|
|
503
|
+
if (oldRow) paintRow(oldRow);
|
|
504
|
+
if (row) paintRow(row);
|
|
505
|
+
if (!silent) box.onSelect(box, row);
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
const clickRow = function (row) {
|
|
509
|
+
if (box.canSelect) setSelectedRow(row, 0);
|
|
510
|
+
box.onRowClick(box, row);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
// The arrow keys move the selection over the rows that are seen.
|
|
514
|
+
const moveSelection = function (step) {
|
|
515
|
+
if (box.shownRows.length == 0) return;
|
|
516
|
+
let index = box.shownRows.indexOf(box.selectedRow);
|
|
517
|
+
// Nothing selected: a step down (Home) starts before the first row, a step up (End) after the last one
|
|
518
|
+
if (index == -1) index = (step > 0) ? -1 : box.shownRows.length;
|
|
519
|
+
const next = Math.max(0, Math.min(box.shownRows.length - 1, index + step));
|
|
520
|
+
setSelectedRow(box.shownRows[next], 0);
|
|
521
|
+
scrollToRow(box.shownRows[next]);
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
// Scrolls only the rows (not the page around the table) to show a row.
|
|
525
|
+
// WHY not elem.scrollIntoView(): it scrolls every box around the row too; a table half out of a scrolling page
|
|
526
|
+
// moved the page at a selection (the same fault was found in FullScreenSelect and ItemList).
|
|
527
|
+
// NOTE: offsetTop is the place on the screen, also after a sort (the rows are ordered with their CSS order).
|
|
528
|
+
const scrollToRow = function (row) {
|
|
529
|
+
const area = box.scrollBox.elem;
|
|
530
|
+
const rowTop = row.elem.offsetTop + box.rowsGroup.elem.offsetTop;
|
|
531
|
+
const rowBottom = rowTop + row.elem.offsetHeight;
|
|
532
|
+
if (rowTop < area.scrollTop) area.scrollTop = rowTop;
|
|
533
|
+
else if (rowBottom > area.scrollTop + area.clientHeight) area.scrollTop = rowBottom - area.clientHeight;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const onKeyDown = function (self, event) {
|
|
537
|
+
if (event.target != box.elem) return; // (a key of the search box or of a title)
|
|
538
|
+
if (!box.canSelect) return;
|
|
539
|
+
const steps = { ArrowDown: 1, ArrowUp: -1, PageDown: 10, PageUp: -10, End: Infinity, Home: -Infinity };
|
|
540
|
+
if (steps[event.key] !== undefined) {
|
|
541
|
+
event.preventDefault();
|
|
542
|
+
moveSelection(steps[event.key]);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (event.key == "Enter" && box.selectedRow) {
|
|
546
|
+
event.preventDefault();
|
|
547
|
+
box.onRowClick(box, box.selectedRow);
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
// *** THE HEADER (a title and the search box)
|
|
552
|
+
|
|
553
|
+
const createHeader = function () {
|
|
554
|
+
|
|
555
|
+
if (!box.titleText && box.showSearch != 1) return 0;
|
|
556
|
+
|
|
557
|
+
// GROUP: Header
|
|
558
|
+
box.header = HGroup({
|
|
559
|
+
left: 0,
|
|
560
|
+
top: 0,
|
|
561
|
+
width: "100%",
|
|
562
|
+
height: _s.header.height,
|
|
563
|
+
align: "left center",
|
|
564
|
+
gap: _s.header.gap,
|
|
565
|
+
padding: _s.header.padding,
|
|
566
|
+
clipContent: 0,
|
|
567
|
+
position: "absolute",
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
// LABEL: The title of the table
|
|
571
|
+
box.lblTitle = Label({ ..._s.headerTitle, plainText: box.titleText, ellipsis: 1, grow: 1, css: { minWidth: "0px" } });
|
|
572
|
+
|
|
573
|
+
if (box.showSearch == 1) createSearch();
|
|
574
|
+
|
|
575
|
+
endGroup();
|
|
576
|
+
|
|
577
|
+
return _s.header.height;
|
|
578
|
+
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const createSearch = function () {
|
|
582
|
+
|
|
583
|
+
if (typeof SearchInput === "undefined") {
|
|
584
|
+
console.warn("DataTable: showSearch needs comp/search-input.js (SearchInput).");
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const searchParams = Object.assign(
|
|
589
|
+
{ styleName: _s.searchStyleName, clearOnEscape: 1 }, // (Escape clears the search, as in SmartTable)
|
|
590
|
+
_s.search,
|
|
591
|
+
box.searchParams,
|
|
592
|
+
);
|
|
593
|
+
searchParams.onChange = function (self) {
|
|
594
|
+
box.search(self.getSearchText());
|
|
595
|
+
};
|
|
596
|
+
box.searchInput = SearchInput(searchParams);
|
|
597
|
+
box.searchInput.shrink = 0;
|
|
598
|
+
box.searchInput.css = { maxWidth: "60%" }; // WHY: on a narrow table (a phone) the title keeps room
|
|
599
|
+
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
// *** PUBLIC FUNCTIONS:
|
|
603
|
+
|
|
604
|
+
// All the rows again. (The selection goes; the sorting and the search stay.)
|
|
605
|
+
box.setRows = function (rows) {
|
|
606
|
+
box.clearRows();
|
|
607
|
+
box.rows = Array.isArray(rows) ? rows.slice() : [];
|
|
608
|
+
box.rows.forEach(addRowObject);
|
|
609
|
+
showRows();
|
|
610
|
+
};
|
|
611
|
+
// USAGE: table.setRows([[1, "John"], [2, "Jane"]])
|
|
612
|
+
|
|
613
|
+
// Adds, changes and removes rows by their id. The objects of the rows that stay are kept.
|
|
614
|
+
box.updateRows = function (rows) {
|
|
615
|
+
|
|
616
|
+
// WHY a list for each id: rows with the same id (object rows with no id, a first column that repeats) kept
|
|
617
|
+
// only the last one in the map. The others stayed on the screen out of box.rowList, so clearRows() and
|
|
618
|
+
// setRows() could not remove them either ("ghost" rows).
|
|
619
|
+
const oldRows = new Map();
|
|
620
|
+
box.rowList.forEach(function (row) {
|
|
621
|
+
if (!oldRows.has(row.id)) oldRows.set(row.id, []);
|
|
622
|
+
oldRows.get(row.id).push(row);
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
box.rows = Array.isArray(rows) ? rows.slice() : [];
|
|
626
|
+
box.rowList = [];
|
|
627
|
+
|
|
628
|
+
box.rows.forEach(function (data) {
|
|
629
|
+
const sameId = oldRows.get(getRowId(data));
|
|
630
|
+
const row = (sameId && sameId.length) ? sameId.shift() : null;
|
|
631
|
+
if (!row) {
|
|
632
|
+
addRowObject(data);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
refreshRow(row, data);
|
|
636
|
+
box.rowList.push(row);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
const hadSelection = !!box.selectedRow;
|
|
640
|
+
oldRows.forEach(function (list) { list.forEach(function (row) { removeRowObject(row); }); });
|
|
641
|
+
showRows();
|
|
642
|
+
if (hadSelection && !box.selectedRow) box.onSelect(box, null);
|
|
643
|
+
|
|
644
|
+
};
|
|
645
|
+
// USAGE: data.push([101, "New"]); table.updateRows(data);
|
|
646
|
+
|
|
647
|
+
box.addRow = function (data) {
|
|
648
|
+
box.rows.push(data);
|
|
649
|
+
addRowObject(data);
|
|
650
|
+
showRows();
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// A row with this id gets the new data (its objects stay).
|
|
654
|
+
box.updateRow = function (data) {
|
|
655
|
+
const row = findRow(getRowId(data));
|
|
656
|
+
if (!row) return;
|
|
657
|
+
box.rows[box.rowList.indexOf(row)] = data;
|
|
658
|
+
refreshRow(row, data);
|
|
659
|
+
showRows();
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
box.removeRow = function (id) {
|
|
663
|
+
const row = findRow(id);
|
|
664
|
+
if (!row) return;
|
|
665
|
+
const index = box.rowList.indexOf(row);
|
|
666
|
+
const wasSelected = (row == box.selectedRow);
|
|
667
|
+
box.rows.splice(index, 1);
|
|
668
|
+
box.rowList.splice(index, 1);
|
|
669
|
+
removeRowObject(row);
|
|
670
|
+
showRows();
|
|
671
|
+
if (wasSelected) box.onSelect(box, null);
|
|
672
|
+
};
|
|
673
|
+
// USAGE: table.removeRow(5)
|
|
674
|
+
|
|
675
|
+
box.clearRows = function () {
|
|
676
|
+
box.rowList.forEach(removeRowObject);
|
|
677
|
+
box.rowList = [];
|
|
678
|
+
box.rows = [];
|
|
679
|
+
showRows();
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// A copy of the data of the rows (in the order of the data)
|
|
683
|
+
box.getRows = function () {
|
|
684
|
+
return box.rows.slice();
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
box.getRow = function (id) {
|
|
688
|
+
return findRow(id);
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
// key: the key of a column ("": the order of the data); direction: "asc" or "desc"
|
|
692
|
+
box.sortBy = function (key, direction = "asc") {
|
|
693
|
+
box.sortKey = findColumn(key) ? key : "";
|
|
694
|
+
box.sortDirection = (direction == "desc") ? "desc" : "asc";
|
|
695
|
+
box.scrollBox.elem.scrollTop = 0;
|
|
696
|
+
showRows();
|
|
697
|
+
};
|
|
698
|
+
// USAGE: table.sortBy("age", "desc")
|
|
699
|
+
|
|
700
|
+
box.clearSort = function () {
|
|
701
|
+
box.sortBy("");
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// Shows only the rows with the text (in the column key, or in every column). Returns the count of the rows seen.
|
|
705
|
+
box.search = function (text, key = "") {
|
|
706
|
+
searchText = DataTable.toSearchText(text).trim();
|
|
707
|
+
searchKey = key;
|
|
708
|
+
box.scrollBox.elem.scrollTop = 0;
|
|
709
|
+
showRows();
|
|
710
|
+
return shownRowCount;
|
|
711
|
+
};
|
|
712
|
+
// USAGE: table.search("paris") / table.search("") // all
|
|
713
|
+
|
|
714
|
+
// silent: 1 -> onSelect is not called.
|
|
715
|
+
box.select = function (id, silent = 0) {
|
|
716
|
+
if (!box.canSelect) return 0;
|
|
717
|
+
const row = findRow(id);
|
|
718
|
+
if (!row) return 0;
|
|
719
|
+
setSelectedRow(row, silent);
|
|
720
|
+
scrollToRow(row);
|
|
721
|
+
return 1;
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
box.clearSelection = function (silent = 0) {
|
|
725
|
+
setSelectedRow(null, silent);
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
box.getSelectedId = function () {
|
|
729
|
+
return box.selectedRow ? box.selectedRow.id : null;
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
box.getSelectedData = function () {
|
|
733
|
+
return box.selectedRow ? box.selectedRow.data : null;
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
box.setTitleText = function (text) {
|
|
737
|
+
box.titleText = text;
|
|
738
|
+
if (box.lblTitle) box.lblTitle.plainText = text;
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
// The biggest id of the rows (a new row gets the next one)
|
|
742
|
+
box.getBiggestId = function () {
|
|
743
|
+
let biggestId = 0;
|
|
744
|
+
box.rows.forEach(function (data) {
|
|
745
|
+
biggestId = Math.max(biggestId, basic.num(getRowId(data)) || 0);
|
|
746
|
+
});
|
|
747
|
+
return biggestId;
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
// *** OBJECT VIEW:
|
|
751
|
+
|
|
752
|
+
box.props(_s.box);
|
|
753
|
+
box.role = "grid";
|
|
754
|
+
box.tabIndex = 0;
|
|
755
|
+
box.aria = { label: box.titleText || "Table" };
|
|
756
|
+
box.setFocusRing(_s.focus.ringColor);
|
|
757
|
+
|
|
758
|
+
const headerHeight = createHeader();
|
|
759
|
+
const rowsTop = headerHeight + _s.titleRow.height;
|
|
760
|
+
|
|
761
|
+
// GROUP: Title row
|
|
762
|
+
box.titleRow = HGroup({
|
|
763
|
+
left: 0,
|
|
764
|
+
top: headerHeight,
|
|
765
|
+
width: "100%",
|
|
766
|
+
height: _s.titleRow.height,
|
|
767
|
+
align: "left center",
|
|
768
|
+
color: _s.titleRow.color,
|
|
769
|
+
position: "absolute",
|
|
770
|
+
role: "row",
|
|
771
|
+
css: { borderBottom: "1px solid " + _s.titleRow.lineColor, borderTop: headerHeight ? "1px solid " + _s.titleRow.lineColor : "none" },
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
box.columns.forEach(function (column, i) {
|
|
775
|
+
box.titleCells.push(createTitleCell(column, i));
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
endGroup();
|
|
779
|
+
|
|
780
|
+
// BOX: Scrolling area of the rows (to the side too, when the table is narrower than its columns)
|
|
781
|
+
box.scrollBox = startBox(0, rowsTop, "100%", "calc(100% - " + rowsTop + "px)", { color: "transparent", scrollY: 1, scrollX: 1 });
|
|
782
|
+
|
|
783
|
+
// GROUP: Rows (the order: their CSS order, showRows)
|
|
784
|
+
box.rowsGroup = VGroup({ width: "100%", height: "auto", align: "left top", css: { minWidth: findColumnsWidth() + "px" } });
|
|
785
|
+
endGroup();
|
|
786
|
+
|
|
787
|
+
endBox();
|
|
788
|
+
|
|
789
|
+
// WHY: the titles are not in the box that scrolls (they stay at the top); they go to the side with the rows.
|
|
790
|
+
box.scrollBox.on("scroll", function () {
|
|
791
|
+
box.titleRow.elem.scrollLeft = box.scrollBox.elem.scrollLeft;
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
// LABEL: No rows / no result
|
|
795
|
+
box.lblEmpty = Label({
|
|
796
|
+
left: 0,
|
|
797
|
+
top: rowsTop + _s.empty.top,
|
|
798
|
+
width: "100%",
|
|
799
|
+
fontSize: _s.empty.fontSize,
|
|
800
|
+
textColor: _s.empty.textColor,
|
|
801
|
+
textAlign: "center",
|
|
802
|
+
visible: 0,
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
if (box.useScrollBar == 1 && typeof ScrollBar !== "undefined") {
|
|
806
|
+
box.scrollBar = ScrollBar(Object.assign({ scrollableBox: box.scrollBox }, _s.scrollBar));
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// *** OBJECT INIT CODE:
|
|
810
|
+
|
|
811
|
+
box.on("keydown", onKeyDown);
|
|
812
|
+
const startRows = box.rows;
|
|
813
|
+
box.rows = [];
|
|
814
|
+
box.setRows(startRows);
|
|
815
|
+
|
|
816
|
+
return endObject(box);
|
|
817
|
+
|
|
818
|
+
};
|
|
819
|
+
|
|
820
|
+
// *** STATIC FUNCTIONS:
|
|
821
|
+
|
|
822
|
+
// The columns with their defaults (copies: the caller's objects do not change).
|
|
823
|
+
DataTable.fitColumns = function (columns) {
|
|
824
|
+
if (!Array.isArray(columns)) return [];
|
|
825
|
+
return columns.map(function (column, i) {
|
|
826
|
+
const fitted = Object.assign({
|
|
827
|
+
key: "column" + i,
|
|
828
|
+
title: "",
|
|
829
|
+
valueIndex: i,
|
|
830
|
+
type: "text",
|
|
831
|
+
width: "auto",
|
|
832
|
+
minWidth: 100, // px: the least width of an "auto" column (a narrower table scrolls to the side)
|
|
833
|
+
align: "left",
|
|
834
|
+
sortable: 0,
|
|
835
|
+
iconFile: "",
|
|
836
|
+
cellColor: "",
|
|
837
|
+
cellStyle: {},
|
|
838
|
+
createView: null,
|
|
839
|
+
}, column);
|
|
840
|
+
fitted.title = DataTable.toText(fitted.title);
|
|
841
|
+
return fitted;
|
|
842
|
+
});
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
DataTable.toText = function (value) {
|
|
846
|
+
if (value === undefined || value === null) return "";
|
|
847
|
+
return String(value);
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
// Lower case, the Turkish letters without their marks: "İstanbul" is found with "istanbul".
|
|
851
|
+
DataTable.toSearchText = function (value) {
|
|
852
|
+
return DataTable.toText(value).toLocaleLowerCase("tr").normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/ı/g, "i");
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
// "left" / "center" / "right" -> the align of a group
|
|
856
|
+
DataTable.toAlign = function (align) {
|
|
857
|
+
if (align == "center") return "center center";
|
|
858
|
+
if (align == "right") return "right center";
|
|
859
|
+
return "left center";
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
DataTable.toAriaSort = function (direction) {
|
|
863
|
+
return (direction == "desc") ? "descending" : "ascending";
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
// *** STYLE PACKAGES:
|
|
867
|
+
// USAGE: DataTable({ styleName: "plain" })
|
|
868
|
+
// USAGE: DataTable({ styleName: "dark", style: { row: { fontSize: 13 } } }) // Change only some keys.
|
|
869
|
+
// NOTE: A new package needs only the keys that differ from the light style.
|
|
870
|
+
DataTable.styles = {
|
|
871
|
+
|
|
872
|
+
// A light gray title row, striped rows, a soft yellow selection.
|
|
873
|
+
light: DataTableDefaults.style,
|
|
874
|
+
|
|
875
|
+
// White, no stripes, only lines: a table in a page.
|
|
876
|
+
plain: {
|
|
877
|
+
box: { color: "white", border: 0, round: 0 },
|
|
878
|
+
titleRow: { height: 50, color: "white", lineColor: Black(0.2) },
|
|
879
|
+
titleCell: { lineColor: "transparent", hoverColor: "whitesmoke", uppercase: 0 },
|
|
880
|
+
row: { evenColor: "white", hoverColor: Black(0.02), selectedColor: Black(0.05) },
|
|
881
|
+
},
|
|
882
|
+
|
|
883
|
+
// For a dark page.
|
|
884
|
+
dark: {
|
|
885
|
+
box: { color: "#232428", border: 1, borderColor: "rgba(255, 255, 255, 0.12)", round: 6 },
|
|
886
|
+
focus: { ringColor: "rgba(95, 179, 181, 0.7)" },
|
|
887
|
+
headerTitle: { fontSize: 20, textColor: "rgba(255, 255, 255, 0.9)", fontFamily: "opensans-bold" },
|
|
888
|
+
searchStyleName: "ozdenDark",
|
|
889
|
+
titleRow: { height: 50, color: "#2B2D31", lineColor: "rgba(255, 255, 255, 0.14)" },
|
|
890
|
+
titleCell: { textColor: "rgba(255, 255, 255, 0.85)", lineColor: "rgba(255, 255, 255, 0.08)", hoverColor: "#33353A" },
|
|
891
|
+
titleIcon: { size: 16, opacity: 0.8, invert: 1 },
|
|
892
|
+
sortIcon: { size: 16, opacity: 0.3, activeOpacity: 1, invert: 1 },
|
|
893
|
+
row: {
|
|
894
|
+
textColor: "rgba(255, 255, 255, 0.75)",
|
|
895
|
+
oddColor: "#232428",
|
|
896
|
+
evenColor: "#27292D",
|
|
897
|
+
hoverColor: "#2E3035",
|
|
898
|
+
selectedColor: "#24383A",
|
|
899
|
+
lineColor: "rgba(255, 255, 255, 0.06)",
|
|
900
|
+
},
|
|
901
|
+
empty: { fontSize: 15, textColor: "rgba(255, 255, 255, 0.45)", top: 40 },
|
|
902
|
+
scrollBar: { neverHide: 0, showDots: 0, bar_color: "#A0A0A0", bar_mouseOverColor: "#D0D0D0" },
|
|
903
|
+
},
|
|
904
|
+
|
|
905
|
+
};
|
|
906
|
+
DataTable.styles.classic = DataTable.styles.light; // the old name of light: the pages made with it keep their look
|
|
907
|
+
|
|
908
|
+
// The minimal theme (_designs/minimal-theme.htm), as SmartTable "minimal": small (30 px titles, rows of a 13 px text),
|
|
909
|
+
// on the ground of the window with a line around it; no stripes, thin lines between the rows, soft titles over a line,
|
|
910
|
+
// a faint row under the mouse and a stronger one for the selected row. minimalDark: the same on a dark window.
|
|
911
|
+
DataTable.createMinimalStyle = function (colors) {
|
|
912
|
+
return {
|
|
913
|
+
box: { color: colors.ground, border: 1, borderColor: colors.line, round: 4 },
|
|
914
|
+
focus: { ringColor: colors.focus },
|
|
915
|
+
header: { height: 40, padding: [12, 0], gap: 10 },
|
|
916
|
+
headerTitle: { fontSize: 14, textColor: colors.text, fontFamily: "opensans-bold" },
|
|
917
|
+
search: { width: 200, height: 28, fontSize: 13, searchIconSize: 14 },
|
|
918
|
+
searchStyleName: colors.minimalName,
|
|
919
|
+
titleRow: { height: 30, color: colors.ground, lineColor: colors.line },
|
|
920
|
+
titleCell: { padding: 10, gap: 6, fontSize: 12, textColor: colors.softText, fontFamily: "opensans-bold", lineColor: "transparent", hoverColor: colors.hover, uppercase: 0 },
|
|
921
|
+
titleIcon: { size: 14, opacity: 0.7, invert: colors.invertIcons },
|
|
922
|
+
sortIcon: { size: 14, opacity: 0.3, activeOpacity: 1, invert: colors.invertIcons },
|
|
923
|
+
row: { padding: [10, 5], fontSize: 13, textColor: colors.text, fontFamily: "opensans", oddColor: colors.ground, evenColor: colors.ground, hoverColor: colors.hover, selectedColor: colors.selected, lineColor: colors.track },
|
|
924
|
+
empty: { fontSize: 12, textColor: colors.softText, top: 30 },
|
|
925
|
+
scrollBar: { neverHide: 0, showDots: 0, bar_color: colors.scrollBar, bar_mouseOverColor: colors.scrollBar, bar_width: 3 },
|
|
926
|
+
};
|
|
927
|
+
};
|
|
928
|
+
DataTable.MINIMAL_COLORS = {
|
|
929
|
+
ground: "#FFFFFF", text: Black(0.85), softText: Black(0.55), line: Black(0.2), track: Black(0.08), hover: Black(0.03),
|
|
930
|
+
selected: Black(0.06), focus: "#3D8BD9", scrollBar: "#9A9A98", invertIcons: 0, minimalName: "minimal",
|
|
931
|
+
};
|
|
932
|
+
DataTable.styles.minimal = DataTable.createMinimalStyle(DataTable.MINIMAL_COLORS);
|
|
933
|
+
DataTable.MINIMAL_DARK_COLORS = {
|
|
934
|
+
ground: "#1A1A19", text: White(0.88), softText: White(0.55), line: White(0.18), track: White(0.08), hover: White(0.04),
|
|
935
|
+
selected: White(0.08), focus: "#7FB6DE", scrollBar: "#A0A0A0", invertIcons: 1, minimalName: "minimalDark",
|
|
936
|
+
};
|
|
937
|
+
DataTable.styles.minimalDark = DataTable.createMinimalStyle(DataTable.MINIMAL_DARK_COLORS);
|