@parallelogram-js/dashboard 0.0.0-stage → 0.1.1
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.
- package/CHANGELOG.md +113 -0
- package/LICENSE +21 -0
- package/README.md +1390 -2
- package/dist/parallelogram-dashboard.css +9401 -0
- package/dist/parallelogram-dashboard.css.map +1 -0
- package/dist/parallelogram-dashboard.min.css +1 -0
- package/package.json +72 -4
- package/scss/_base.scss +403 -0
- package/scss/_datetime.scss +50 -0
- package/scss/_icon.scss +40 -0
- package/scss/_index.scss +60 -0
- package/scss/_mixins.scss +396 -0
- package/scss/_tokens.scss +340 -0
- package/scss/_utilities.scss +464 -0
- package/scss/_variables.scss +219 -0
- package/scss/components/_accordion.scss +297 -0
- package/scss/components/_actionbar.scss +333 -0
- package/scss/components/_alert.scss +183 -0
- package/scss/components/_avatar.scss +203 -0
- package/scss/components/_badge.scss +176 -0
- package/scss/components/_banner.scss +344 -0
- package/scss/components/_breadcrumb.scss +220 -0
- package/scss/components/_button.scss +264 -0
- package/scss/components/_card.scss +256 -0
- package/scss/components/_chart.scss +246 -0
- package/scss/components/_command.scss +267 -0
- package/scss/components/_dashboard.scss +681 -0
- package/scss/components/_data-list.scss +280 -0
- package/scss/components/_dropdown.scss +318 -0
- package/scss/components/_empty.scss +226 -0
- package/scss/components/_filebrowser.scss +906 -0
- package/scss/components/_form-layout.scss +332 -0
- package/scss/components/_input.scss +310 -0
- package/scss/components/_list.scss +130 -0
- package/scss/components/_media.scss +239 -0
- package/scss/components/_modal.scss +298 -0
- package/scss/components/_navbar.scss +312 -0
- package/scss/components/_notification.scss +59 -0
- package/scss/components/_pageheader.scss +99 -0
- package/scss/components/_pagination.scss +165 -0
- package/scss/components/_progress.scss +333 -0
- package/scss/components/_separator.scss +129 -0
- package/scss/components/_sheet.scss +286 -0
- package/scss/components/_sidebar.scss +324 -0
- package/scss/components/_skeleton.scss +246 -0
- package/scss/components/_stat.scss +218 -0
- package/scss/components/_table.scss +589 -0
- package/scss/components/_tabs.scss +266 -0
- package/scss/components/_timeline.scss +339 -0
- package/scss/components/_toast.scss +259 -0
- package/scss/components/_toolbar.scss +301 -0
- package/scss/components/_tooltip.scss +250 -0
- package/scss/main.scss +6 -0
- package/src/js/behaviours/accordion.js +58 -0
- package/src/js/behaviours/action-bar-list.js +406 -0
- package/src/js/behaviours/action-bar.js +29 -0
- package/src/js/behaviours/chart.js +339 -0
- package/src/js/behaviours/collapsible.js +34 -0
- package/src/js/behaviours/editable-table.js +91 -0
- package/src/js/behaviours/expandable.js +80 -0
- package/src/js/behaviours/overflow-actions.js +137 -0
- package/src/js/behaviours/publish-schedule.js +91 -0
- package/src/js/behaviours/search-clear.js +19 -0
- package/src/js/behaviours/segmented-control.js +43 -0
- package/src/js/behaviours/submenu.js +73 -0
- package/src/js/behaviours/table-columns.js +77 -0
- package/src/js/behaviours/table-paste.js +305 -0
- package/src/js/behaviours/table-reorder.js +252 -0
- package/src/js/behaviours/table-sort.js +101 -0
- package/src/js/behaviours/tabs.js +148 -0
- package/src/js/behaviours/toast-manager.js +106 -0
- package/src/js/behaviours/toggle.js +186 -0
- package/src/js/behaviours/unsaved-guard.js +88 -0
- package/src/js/core-loader.js +26 -0
- package/src/js/index.js +85 -0
- package/src/js/polyfills/dialog-commands.js +20 -0
- package/src/js/util/dom.js +39 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { TableSort } from './table-sort.js';
|
|
2
|
+
import { leadingEdge } from '../util/dom.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Drag-to-reorder rows in a table with data-table-reorder.
|
|
6
|
+
*
|
|
7
|
+
* Each row's .table__handle cell holds a button. Drag it and the row moves into its landing
|
|
8
|
+
* place as the pointer moves while the other rows slide out of the way; near the top or bottom of
|
|
9
|
+
* the scrolling area the table scrolls. Releasing settles the row; Escape or a cancelled pointer
|
|
10
|
+
* puts it back. With the keyboard, focus a handle and press Space to pick the row up, the up and
|
|
11
|
+
* down arrows to move it, Space to drop it and Escape to cancel. The row being moved carries
|
|
12
|
+
* data-dragging, moves are announced to screen readers, and the table dispatches a bubbling
|
|
13
|
+
* "table:reorder" event with { row, from, to }. A reorderable table has no column sorting.
|
|
14
|
+
*/
|
|
15
|
+
export class TableReorder {
|
|
16
|
+
static elements = new WeakMap();
|
|
17
|
+
|
|
18
|
+
mount(table) {
|
|
19
|
+
if (TableReorder.elements.has(table) || !table.tBodies[0]) return;
|
|
20
|
+
const status = document.createElement('div');
|
|
21
|
+
status.className = 'sr-only';
|
|
22
|
+
status.setAttribute('aria-live', 'assertive');
|
|
23
|
+
table.after(status);
|
|
24
|
+
const state = { table, body: table.tBodies[0], status, grabbed: null, origin: null };
|
|
25
|
+
TableReorder.elements.set(table, state);
|
|
26
|
+
state.body.addEventListener('pointerdown', e => this._pointerDown(e, state));
|
|
27
|
+
state.body.addEventListener('keydown', e => this._keyDown(e, state));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_rowFor(target, state) {
|
|
31
|
+
const handle = target.closest('.table__handle button');
|
|
32
|
+
const row = handle?.closest('tr');
|
|
33
|
+
return row?.parentElement === state.body ? { handle, row } : {};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_rows(state, except) {
|
|
37
|
+
return [...state.body.rows].filter(row => row !== except && row.offsetParent !== null);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_name(row) {
|
|
41
|
+
const cell = [...row.cells].find(c => !c.matches('.table__handle, .table__checkbox'));
|
|
42
|
+
return (cell && TableSort.value(cell)) || 'Row';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_describe(row, state) {
|
|
46
|
+
const rows = this._rows(state);
|
|
47
|
+
return `position ${rows.indexOf(row) + 1} of ${rows.length}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_announce(state, message) {
|
|
51
|
+
state.status.textContent = message;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_dispatch(state, row) {
|
|
55
|
+
const to = [...state.body.rows].indexOf(row);
|
|
56
|
+
if (to === state.origin) return;
|
|
57
|
+
state.table.dispatchEvent(new CustomEvent('table:reorder', { bubbles: true, detail: { row, from: state.origin, to } }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_restore(row, state) {
|
|
61
|
+
const others = [...state.body.rows].filter(other => other !== row);
|
|
62
|
+
state.body.insertBefore(row, others[state.origin] || null);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
_reducedMotion() {
|
|
66
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The row's box without any slide animation still running on it, so targets don't flicker. */
|
|
70
|
+
_layoutRect(row) {
|
|
71
|
+
const rect = row.getBoundingClientRect();
|
|
72
|
+
const shift = new DOMMatrixReadOnly(getComputedStyle(row).transform).m42;
|
|
73
|
+
return { top: rect.top - shift, bottom: rect.bottom - shift, height: rect.height };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Run a DOM move, then slide the other rows from where they were to where they now are. */
|
|
77
|
+
_animate(state, row, mutate) {
|
|
78
|
+
const rows = this._rows(state, row);
|
|
79
|
+
const before = new Map(rows.map(other => [other, other.getBoundingClientRect().top]));
|
|
80
|
+
mutate();
|
|
81
|
+
if (this._reducedMotion()) return;
|
|
82
|
+
for (const [other, top] of before) {
|
|
83
|
+
const delta = top - other.getBoundingClientRect().top;
|
|
84
|
+
if (delta) other.animate([{ transform: `translateY(${delta}px)` }, { transform: 'none' }], { duration: 150, easing: 'ease-out' });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Drop the pointer offset and glide the row into its slot, then run done. */
|
|
89
|
+
_settle(row, done) {
|
|
90
|
+
const from = row.getBoundingClientRect().top;
|
|
91
|
+
row.style.transform = '';
|
|
92
|
+
const delta = from - row.getBoundingClientRect().top;
|
|
93
|
+
if (!delta || this._reducedMotion()) {
|
|
94
|
+
done();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
row.animate([{ transform: `translateY(${delta}px)` }, { transform: 'none' }], { duration: 150, easing: 'ease-out' })
|
|
98
|
+
.finished.then(done, done);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Where the row lands for its leading edge at y: a neighbour gives way once half covered. */
|
|
102
|
+
_dropTarget(state, row, y) {
|
|
103
|
+
let best = null;
|
|
104
|
+
for (const candidate of this._rows(state, row)) {
|
|
105
|
+
const rect = this._layoutRect(candidate);
|
|
106
|
+
const distance = y < rect.top ? rect.top - y : y > rect.bottom ? y - rect.bottom : 0;
|
|
107
|
+
if (!best || distance < best.distance) best = { target: candidate, rect, distance };
|
|
108
|
+
}
|
|
109
|
+
if (!best) return null;
|
|
110
|
+
return { target: best.target, where: (y - best.rect.top) / best.rect.height < 0.5 ? 'before' : 'after' };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The nearest ancestor that scrolls vertically, or the page. */
|
|
114
|
+
_scroller(table) {
|
|
115
|
+
for (let el = table.parentElement; el && el !== document.body; el = el.parentElement) {
|
|
116
|
+
const { overflowY } = getComputedStyle(el);
|
|
117
|
+
if (/(auto|scroll)/.test(overflowY) && el.scrollHeight > el.clientHeight) return el;
|
|
118
|
+
}
|
|
119
|
+
return document.scrollingElement;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_pointerDown(event, state) {
|
|
123
|
+
const { handle, row } = this._rowFor(event.target, state);
|
|
124
|
+
if (!row || event.button !== 0) return;
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
handle.focus();
|
|
127
|
+
|
|
128
|
+
const grabOffset = event.clientY - row.getBoundingClientRect().top;
|
|
129
|
+
const scroller = this._scroller(state.table);
|
|
130
|
+
let pointerY = event.clientY;
|
|
131
|
+
let frame = null;
|
|
132
|
+
state.origin = [...state.body.rows].indexOf(row);
|
|
133
|
+
row.setAttribute('data-dragging', '');
|
|
134
|
+
|
|
135
|
+
const follow = () => {
|
|
136
|
+
row.style.transform = '';
|
|
137
|
+
row.style.transform = `translateY(${pointerY - grabOffset - row.getBoundingClientRect().top}px)`;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const update = () => {
|
|
141
|
+
const next = this._dropTarget(state, row, leadingEdge(this._layoutRect(row).top, pointerY - grabOffset, row.offsetHeight));
|
|
142
|
+
const at = next && (next.where === 'before' ? row.nextElementSibling === next.target : next.target.nextElementSibling === row);
|
|
143
|
+
if (next && !at) {
|
|
144
|
+
this._animate(state, row, () => state.body.insertBefore(row, next.where === 'before' ? next.target : next.target.nextElementSibling));
|
|
145
|
+
}
|
|
146
|
+
follow();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// Scrolls while the pointer is near the top or bottom of the scrolling area
|
|
150
|
+
const edgeScroll = () => {
|
|
151
|
+
const page = scroller === document.scrollingElement;
|
|
152
|
+
const top = page ? 0 : scroller.getBoundingClientRect().top;
|
|
153
|
+
const bottom = page ? window.innerHeight : scroller.getBoundingClientRect().bottom;
|
|
154
|
+
const zone = 48;
|
|
155
|
+
const speed = pointerY < top + zone ? -(top + zone - pointerY) : pointerY > bottom - zone ? pointerY - (bottom - zone) : 0;
|
|
156
|
+
if (speed) {
|
|
157
|
+
scroller.scrollTop += Math.max(-20, Math.min(20, speed / 2));
|
|
158
|
+
update();
|
|
159
|
+
}
|
|
160
|
+
frame = requestAnimationFrame(edgeScroll);
|
|
161
|
+
};
|
|
162
|
+
frame = requestAnimationFrame(edgeScroll);
|
|
163
|
+
|
|
164
|
+
const move = (e) => {
|
|
165
|
+
pointerY = e.clientY;
|
|
166
|
+
update();
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const finish = (commit) => {
|
|
170
|
+
cancelAnimationFrame(frame);
|
|
171
|
+
document.removeEventListener('pointermove', move);
|
|
172
|
+
document.removeEventListener('pointerup', release);
|
|
173
|
+
document.removeEventListener('pointercancel', cancel);
|
|
174
|
+
document.removeEventListener('keydown', escape, true);
|
|
175
|
+
if (!commit) this._restore(row, state);
|
|
176
|
+
this._settle(row, () => row.removeAttribute('data-dragging'));
|
|
177
|
+
if (commit) {
|
|
178
|
+
this._announce(state, `${this._name(row)} moved to ${this._describe(row, state)}.`);
|
|
179
|
+
this._dispatch(state, row);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const release = () => finish(true);
|
|
183
|
+
const cancel = () => finish(false);
|
|
184
|
+
const escape = (e) => {
|
|
185
|
+
if (e.key !== 'Escape') return;
|
|
186
|
+
// Handled here so a full-screen dialog doesn't also close
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
e.stopPropagation();
|
|
189
|
+
finish(false);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
document.addEventListener('pointermove', move);
|
|
193
|
+
document.addEventListener('pointerup', release);
|
|
194
|
+
document.addEventListener('pointercancel', cancel);
|
|
195
|
+
document.addEventListener('keydown', escape, true);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
_keyDown(event, state) {
|
|
199
|
+
const { handle, row } = this._rowFor(event.target, state);
|
|
200
|
+
if (!row) return;
|
|
201
|
+
const name = this._name(row);
|
|
202
|
+
|
|
203
|
+
if (event.key === ' ' || event.key === 'Enter') {
|
|
204
|
+
event.preventDefault();
|
|
205
|
+
if (state.grabbed === row) {
|
|
206
|
+
state.grabbed = null;
|
|
207
|
+
handle.setAttribute('aria-pressed', 'false');
|
|
208
|
+
row.removeAttribute('data-dragging');
|
|
209
|
+
this._announce(state, `${name} dropped at ${this._describe(row, state)}.`);
|
|
210
|
+
this._dispatch(state, row);
|
|
211
|
+
} else {
|
|
212
|
+
state.grabbed = row;
|
|
213
|
+
state.origin = [...state.body.rows].indexOf(row);
|
|
214
|
+
handle.setAttribute('aria-pressed', 'true');
|
|
215
|
+
row.setAttribute('data-dragging', '');
|
|
216
|
+
this._announce(state, `${name} grabbed. Use the up and down arrow keys to move it. Space drops, Escape cancels.`);
|
|
217
|
+
}
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (state.grabbed !== row) return;
|
|
222
|
+
|
|
223
|
+
if (event.key === 'Escape') {
|
|
224
|
+
event.preventDefault();
|
|
225
|
+
this._restore(row, state);
|
|
226
|
+
state.grabbed = null;
|
|
227
|
+
handle.setAttribute('aria-pressed', 'false');
|
|
228
|
+
row.removeAttribute('data-dragging');
|
|
229
|
+
handle.focus();
|
|
230
|
+
this._announce(state, `Move cancelled. ${name} is back at ${this._describe(row, state)}.`);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return;
|
|
235
|
+
const rows = this._rows(state);
|
|
236
|
+
const up = event.key === 'ArrowUp';
|
|
237
|
+
const neighbour = rows[rows.indexOf(row) + (up ? -1 : 1)];
|
|
238
|
+
if (!neighbour) return;
|
|
239
|
+
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
this._animate(state, row, () => state.body.insertBefore(row, up ? neighbour : neighbour.nextElementSibling));
|
|
242
|
+
handle.focus();
|
|
243
|
+
handle.scrollIntoView({ block: 'nearest' });
|
|
244
|
+
this._announce(state, `${name} moved to ${this._describe(row, state)}.`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static enhanceAll(selector = '[data-table-reorder]', root = document) {
|
|
248
|
+
const instance = new TableReorder();
|
|
249
|
+
root.querySelectorAll(selector).forEach(el => instance.mount(el));
|
|
250
|
+
return instance;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Click-to-sort headers that only change the view.
|
|
3
|
+
*
|
|
4
|
+
* In a table with data-table-sort, each .table__head--sortable sorts the rows by its column:
|
|
5
|
+
* ascending, then descending, then back to the natural order. Cells sort by their control's value
|
|
6
|
+
* (a select's chosen text), their data-sort-value, or their text, comparing numbers as numbers and
|
|
7
|
+
* leaving blanks last. The table carries data-sorted while sorted.
|
|
8
|
+
*
|
|
9
|
+
* Every row keeps its natural position while the view is sorted: a row added at the end joins
|
|
10
|
+
* the end, a row inserted after another (a duplicate) follows it, and a row put back keeps its
|
|
11
|
+
* place. Values edited while sorted stay put until the next sort, so rows don't move under the
|
|
12
|
+
* cursor. The table fires "table:sort" with { column, direction }.
|
|
13
|
+
*/
|
|
14
|
+
export class TableSort {
|
|
15
|
+
static elements = new WeakSet();
|
|
16
|
+
|
|
17
|
+
static collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
|
|
18
|
+
|
|
19
|
+
mount(table) {
|
|
20
|
+
if (TableSort.elements.has(table) || !table.tBodies[0] || table.hasAttribute('data-table-reorder')) return;
|
|
21
|
+
TableSort.elements.add(table);
|
|
22
|
+
const body = table.tBodies[0];
|
|
23
|
+
const ranks = new WeakMap();
|
|
24
|
+
const state = { head: null, direction: null };
|
|
25
|
+
|
|
26
|
+
const rerank = () => {
|
|
27
|
+
const rows = [...body.rows];
|
|
28
|
+
if (!state.head) {
|
|
29
|
+
rows.forEach((row, i) => ranks.set(row, i));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const ranked = rows.filter(row => ranks.has(row)).map(row => ranks.get(row)).sort((a, b) => a - b);
|
|
33
|
+
rows.forEach((row) => {
|
|
34
|
+
if (ranks.has(row)) return;
|
|
35
|
+
const before = row.previousElementSibling;
|
|
36
|
+
if (!row.nextElementSibling || !before || !ranks.has(before)) {
|
|
37
|
+
ranks.set(row, (ranked.at(-1) ?? 0) + 1);
|
|
38
|
+
} else {
|
|
39
|
+
const after = ranked.find(rank => rank > ranks.get(before));
|
|
40
|
+
ranks.set(row, after === undefined ? ranks.get(before) + 1 : (ranks.get(before) + after) / 2);
|
|
41
|
+
}
|
|
42
|
+
ranked.push(ranks.get(row));
|
|
43
|
+
ranked.sort((a, b) => a - b);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const sort = (head, direction) => {
|
|
48
|
+
table.querySelectorAll('.table__head--sortable[aria-sort]').forEach(other => other.removeAttribute('aria-sort'));
|
|
49
|
+
state.head = direction ? head : null;
|
|
50
|
+
state.direction = direction;
|
|
51
|
+
if (direction) head.setAttribute('aria-sort', direction);
|
|
52
|
+
table.toggleAttribute('data-sorted', !!direction);
|
|
53
|
+
|
|
54
|
+
const order = direction === 'descending' ? -1 : 1;
|
|
55
|
+
const rows = [...body.rows].sort((a, b) => {
|
|
56
|
+
if (!direction) return ranks.get(a) - ranks.get(b);
|
|
57
|
+
const x = TableSort.value(a.cells[head.cellIndex]);
|
|
58
|
+
const y = TableSort.value(b.cells[head.cellIndex]);
|
|
59
|
+
if (!x || !y) return (!x - !y) || ranks.get(a) - ranks.get(b);
|
|
60
|
+
return TableSort.collator.compare(x, y) * order || ranks.get(a) - ranks.get(b);
|
|
61
|
+
});
|
|
62
|
+
observer.disconnect();
|
|
63
|
+
body.append(...rows);
|
|
64
|
+
observer.observe(body, { childList: true });
|
|
65
|
+
table.dispatchEvent(new CustomEvent('table:sort', { bubbles: true, detail: { column: direction ? head : null, direction } }));
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const observer = new MutationObserver(rerank);
|
|
69
|
+
observer.observe(body, { childList: true });
|
|
70
|
+
rerank();
|
|
71
|
+
|
|
72
|
+
table.querySelectorAll('.table__head--sortable').forEach((head) => {
|
|
73
|
+
head.tabIndex = 0;
|
|
74
|
+
const next = () => {
|
|
75
|
+
const current = state.head === head ? state.direction : null;
|
|
76
|
+
sort(head, { null: 'ascending', ascending: 'descending', descending: null }[current]);
|
|
77
|
+
};
|
|
78
|
+
head.addEventListener('click', next);
|
|
79
|
+
head.addEventListener('keydown', (e) => {
|
|
80
|
+
if (e.key !== 'Enter' && e.key !== ' ') return;
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
next();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static value(cell) {
|
|
88
|
+
if (!cell) return '';
|
|
89
|
+
if (cell.dataset.sortValue !== undefined) return cell.dataset.sortValue;
|
|
90
|
+
const control = cell.querySelector('input, select, textarea');
|
|
91
|
+
if (control?.tagName === 'SELECT') return control.selectedOptions[0]?.text.trim() ?? '';
|
|
92
|
+
if (control) return control.value.trim();
|
|
93
|
+
return cell.textContent.trim();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static enhanceAll(selector = '[data-table-sort]', root = document) {
|
|
97
|
+
const instance = new TableSort();
|
|
98
|
+
root.querySelectorAll(selector).forEach(el => instance.mount(el));
|
|
99
|
+
return instance;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { ensureId, isDisabled } from '../util/dom.js';
|
|
2
|
+
|
|
3
|
+
// ============================================================================
|
|
4
|
+
// Tabs Component
|
|
5
|
+
// ============================================================================
|
|
6
|
+
|
|
7
|
+
export class Tabs {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.elements = new WeakMap();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
mount(element) {
|
|
13
|
+
if (this.elements.has(element)) return;
|
|
14
|
+
|
|
15
|
+
const tabsList = element.querySelector('[data-tabs-list]');
|
|
16
|
+
const tabsContainer = element.querySelector('[data-tabs-panels]');
|
|
17
|
+
|
|
18
|
+
if (!tabsList || !tabsContainer) return;
|
|
19
|
+
|
|
20
|
+
const tabs = [...tabsList.querySelectorAll('[data-tab]')];
|
|
21
|
+
const panels = [...tabsContainer.querySelectorAll('[data-tab-panel]')];
|
|
22
|
+
|
|
23
|
+
if (tabs.length === 0 || panels.length === 0) return;
|
|
24
|
+
|
|
25
|
+
const state = {
|
|
26
|
+
tabsList,
|
|
27
|
+
tabsContainer,
|
|
28
|
+
tabs,
|
|
29
|
+
panels,
|
|
30
|
+
activeTab: null,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Setup ARIA
|
|
34
|
+
tabsList.setAttribute('role', 'tablist');
|
|
35
|
+
tabs.forEach(tab => {
|
|
36
|
+
tab.setAttribute('role', 'tab');
|
|
37
|
+
tab.setAttribute('tabindex', tab.getAttribute('aria-selected') === 'true' ? '0' : '-1');
|
|
38
|
+
const panel = panels.find(p => p.id === tab.dataset.tab);
|
|
39
|
+
if (panel) {
|
|
40
|
+
tab.setAttribute('aria-controls', panel.id);
|
|
41
|
+
panel.setAttribute('aria-labelledby', ensureId(tab, 'tab'));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
panels.forEach(panel => {
|
|
45
|
+
panel.setAttribute('role', 'tabpanel');
|
|
46
|
+
panel.setAttribute('tabindex', '0');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// The animated variant slides one indicator under the selected tab
|
|
50
|
+
if (element.classList.contains('tabs--animated')) {
|
|
51
|
+
state.indicator = tabsList.querySelector('.tabs__indicator');
|
|
52
|
+
if (!state.indicator) {
|
|
53
|
+
state.indicator = document.createElement('span');
|
|
54
|
+
state.indicator.className = 'tabs__indicator';
|
|
55
|
+
state.indicator.setAttribute('aria-hidden', 'true');
|
|
56
|
+
tabsList.append(state.indicator);
|
|
57
|
+
}
|
|
58
|
+
new ResizeObserver(() => this._moveIndicator(state)).observe(tabsList);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Set up click handlers
|
|
62
|
+
tabs.forEach(tab => {
|
|
63
|
+
tab.addEventListener('click', (e) => this._handleTabClick(e, element, state));
|
|
64
|
+
tab.addEventListener('keydown', (e) => this._handleKeyDown(e, element, state));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Find and activate initial tab
|
|
68
|
+
const activeTab = tabs.find(t => t.getAttribute('aria-selected') === 'true') || tabs.find(t => !isDisabled(t));
|
|
69
|
+
if (activeTab) {
|
|
70
|
+
this._activateTab(element, activeTab.dataset.tab, state, false);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
element.setAttribute('data-tabs-enhanced', 'true');
|
|
74
|
+
this.elements.set(element, state);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_handleTabClick(event, element, state) {
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
const tab = event.currentTarget;
|
|
80
|
+
const panelId = tab.dataset.tab;
|
|
81
|
+
if (panelId && !isDisabled(tab)) {
|
|
82
|
+
this._activateTab(element, panelId, state, true);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_handleKeyDown(event, element, state) {
|
|
87
|
+
// Disabled tabs are skipped rather than focused
|
|
88
|
+
const enabled = state.tabs.filter(tab => !isDisabled(tab));
|
|
89
|
+
const currentIndex = enabled.indexOf(event.currentTarget);
|
|
90
|
+
let targetIndex = -1;
|
|
91
|
+
|
|
92
|
+
switch (event.key) {
|
|
93
|
+
case 'ArrowRight':
|
|
94
|
+
case 'ArrowDown':
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
targetIndex = (currentIndex + 1) % enabled.length;
|
|
97
|
+
break;
|
|
98
|
+
case 'ArrowLeft':
|
|
99
|
+
case 'ArrowUp':
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
targetIndex = (currentIndex - 1 + enabled.length) % enabled.length;
|
|
102
|
+
break;
|
|
103
|
+
case 'Home':
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
targetIndex = 0;
|
|
106
|
+
break;
|
|
107
|
+
case 'End':
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
targetIndex = enabled.length - 1;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (targetIndex >= 0 && enabled[targetIndex]) {
|
|
114
|
+
const targetTab = enabled[targetIndex];
|
|
115
|
+
targetTab.focus();
|
|
116
|
+
this._activateTab(element, targetTab.dataset.tab, state, true);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_activateTab(element, panelId, state, animate = true) {
|
|
121
|
+
state.tabs.forEach(tab => {
|
|
122
|
+
const isActive = tab.dataset.tab === panelId;
|
|
123
|
+
tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
|
|
124
|
+
tab.setAttribute('tabindex', isActive ? '0' : '-1');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
state.panels.forEach(panel => {
|
|
128
|
+
panel.hidden = panel.id !== panelId;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
state.activeTab = panelId;
|
|
132
|
+
this._moveIndicator(state);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_moveIndicator(state) {
|
|
136
|
+
if (!state.indicator) return;
|
|
137
|
+
const tab = state.tabs.find(t => t.dataset.tab === state.activeTab);
|
|
138
|
+
if (!tab) return;
|
|
139
|
+
state.tabsList.style.setProperty('--tabs-indicator-left', `${tab.offsetLeft}px`);
|
|
140
|
+
state.tabsList.style.setProperty('--tabs-indicator-width', `${tab.offsetWidth}px`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static enhanceAll(selector = '[data-tabs]', root = document) {
|
|
144
|
+
const instance = new Tabs();
|
|
145
|
+
root.querySelectorAll(selector).forEach(el => instance.mount(el));
|
|
146
|
+
return instance;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Toast Manager
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
export class ToastManager {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.container = null;
|
|
8
|
+
this.position = options.position || 'top-right';
|
|
9
|
+
this.defaultDuration = options.duration || 5000;
|
|
10
|
+
this._ensureContainer();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_ensureContainer() {
|
|
14
|
+
this.container = document.querySelector(`.toast__container--${this.position}`);
|
|
15
|
+
if (!this.container) {
|
|
16
|
+
this.container = document.createElement('div');
|
|
17
|
+
this.container.className = `toast__container toast__container--${this.position}`;
|
|
18
|
+
this.container.setAttribute('aria-label', 'Notifications');
|
|
19
|
+
document.body.appendChild(this.container);
|
|
20
|
+
}
|
|
21
|
+
// Announce toasts as they are added; error toasts interrupt via role="alert"
|
|
22
|
+
this.container.setAttribute('aria-live', 'polite');
|
|
23
|
+
this.container.setAttribute('aria-relevant', 'additions');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Show a toast. actions is an optional list of { label, icon, onClick } buttons; clicking one
|
|
28
|
+
* runs its handler and dismisses the toast (e.g. Undo after removing something).
|
|
29
|
+
*/
|
|
30
|
+
show({ message, title, type = 'info', duration = this.defaultDuration, progress = false, actions = [] }) {
|
|
31
|
+
const toast = document.createElement('div');
|
|
32
|
+
toast.className = `toast toast--${type}`;
|
|
33
|
+
if (type === 'error') toast.setAttribute('role', 'alert');
|
|
34
|
+
if (duration > 0) toast.style.setProperty('--toast-duration', `${duration}ms`);
|
|
35
|
+
|
|
36
|
+
const icons = {
|
|
37
|
+
info: '<path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0"/><path d="M12 9h.01"/><path d="M11 12h1v4h1"/>',
|
|
38
|
+
success: '<path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"/><path d="M9 12l2 2l4 -4"/>',
|
|
39
|
+
warning: '<path d="M12 9v4"/><path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z"/><path d="M12 16h.01"/>',
|
|
40
|
+
error: '<path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0"/><path d="M12 9v4"/><path d="M12 16h.01"/>',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
toast.innerHTML = `
|
|
44
|
+
<svg class="icon toast__icon" viewBox="0 0 24 24">${icons[type] || icons.info}</svg>
|
|
45
|
+
<div class="toast__content">
|
|
46
|
+
${title ? `<h4 class="toast__title">${title}</h4>` : ''}
|
|
47
|
+
<p class="toast__description">${message}</p>
|
|
48
|
+
${actions.length ? '<div class="toast__actions"></div>' : ''}
|
|
49
|
+
</div>
|
|
50
|
+
<button type="button" class="toast__close" aria-label="Dismiss notification">
|
|
51
|
+
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M18 6l-12 12"/><path d="M6 6l12 12"/></svg>
|
|
52
|
+
</button>
|
|
53
|
+
${progress && duration > 0 ? '<div class="toast__progress" aria-hidden="true"></div>' : ''}
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
const actionList = toast.querySelector('.toast__actions');
|
|
57
|
+
actions.forEach(({ label, icon = '', onClick }) => {
|
|
58
|
+
const button = document.createElement('button');
|
|
59
|
+
button.type = 'button';
|
|
60
|
+
button.className = 'btn btn--outline btn--sm';
|
|
61
|
+
button.innerHTML = `${icon}<span></span>`;
|
|
62
|
+
button.querySelector('span').textContent = label;
|
|
63
|
+
button.addEventListener('click', () => {
|
|
64
|
+
onClick?.();
|
|
65
|
+
this.dismiss(toast);
|
|
66
|
+
});
|
|
67
|
+
actionList.append(button);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Add close handler
|
|
71
|
+
const closeBtn = toast.querySelector('.toast__close');
|
|
72
|
+
closeBtn?.addEventListener('click', () => this.dismiss(toast));
|
|
73
|
+
|
|
74
|
+
this.container.appendChild(toast);
|
|
75
|
+
|
|
76
|
+
// Auto dismiss
|
|
77
|
+
if (duration > 0) {
|
|
78
|
+
setTimeout(() => this.dismiss(toast), duration);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return toast;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
dismiss(toast) {
|
|
85
|
+
if (!toast || !toast.parentNode) return;
|
|
86
|
+
// Plays toast-slide-in's offsets in reverse, so it leaves the way it came in
|
|
87
|
+
toast.style.animation = 'toast-slide-out 0.3s ease-in forwards';
|
|
88
|
+
setTimeout(() => toast.remove(), 300);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
info(message, options = {}) {
|
|
92
|
+
return this.show({ ...options, message, type: 'info' });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
success(message, options = {}) {
|
|
96
|
+
return this.show({ ...options, message, type: 'success' });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
warning(message, options = {}) {
|
|
100
|
+
return this.show({ ...options, message, type: 'warning' });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
error(message, options = {}) {
|
|
104
|
+
return this.show({ ...options, message, type: 'error' });
|
|
105
|
+
}
|
|
106
|
+
}
|