mensa 0.10.2 → 0.11.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 +4 -4
- data/.shipkit.yml +1 -1
- data/CHANGELOG.md +26 -0
- data/Gemfile.lock +1 -1
- data/README.md +129 -0
- data/app/assets/stylesheets/mensa/application.css +2 -0
- data/app/components/mensa/cells/component.css +47 -0
- data/app/components/mensa/cells/component_controller.js +406 -0
- data/app/components/mensa/table/component.css +22 -0
- data/app/components/mensa/table/component.html.erb +18 -5
- data/app/components/mensa/table/component.rb +6 -1
- data/app/components/mensa/table/component_controller.js +52 -0
- data/app/components/mensa/table_row/component.rb +7 -3
- data/app/components/mensa/tree/component.css +28 -0
- data/app/components/mensa/tree/component_controller.js +99 -0
- data/app/components/mensa/view/component.html.erb +3 -5
- data/app/components/mensa/view/component.rb +22 -0
- data/app/controllers/concerns/mensa/table_from_request.rb +37 -0
- data/app/controllers/mensa/tables/commands_controller.rb +30 -0
- data/app/controllers/mensa/tables/rows_controller.rb +28 -0
- data/app/controllers/mensa/tables_controller.rb +3 -22
- data/app/javascript/mensa/controllers/index.js +6 -0
- data/app/tables/mensa/base.rb +37 -0
- data/app/tables/mensa/cell.rb +51 -2
- data/app/tables/mensa/column.rb +55 -0
- data/app/tables/mensa/command.rb +57 -0
- data/app/tables/mensa/config/column_dsl.rb +3 -0
- data/app/tables/mensa/config/command_dsl.rb +19 -0
- data/app/tables/mensa/config/editable_dsl.rb +27 -0
- data/app/tables/mensa/config/table_dsl.rb +18 -0
- data/app/tables/mensa/grouping.rb +2 -1
- data/app/tables/mensa/row.rb +24 -0
- data/app/tables/mensa/scope.rb +9 -0
- data/app/tables/mensa/tree.rb +157 -0
- data/app/tables/mensa/tree_paging.rb +38 -0
- data/app/views/mensa/tables/rows/update.turbo_stream.erb +3 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/nl.yml +4 -0
- data/config/routes.rb +2 -0
- data/lib/mensa/configuration.rb +2 -0
- data/lib/mensa/version.rb +1 -1
- metadata +14 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 367ec0058ba38c67df1e2206fd2ed6d8e6434cd48a8d0100c573daf3f20cda8c
|
|
4
|
+
data.tar.gz: b60454e99d6b7605cdfaf20744179f4921eae817c651d832978a6612bf35168e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 313a032905770a42a651d4a8959f7ceda9d3d274fb8771deb963244dc8484f21347e4c73d46b9addbca4889195603e0c41f239136e6e403166c5ac20a920e585
|
|
7
|
+
data.tar.gz: 508ec1d41f8da805f55dd0fcca7dce7b12ef26d2d56d2d4671fbadad962b06adc974a08a5321bdb9ddd89087e84ae4067b233399b19bf3c3a1ad4b02afb486e9
|
data/.shipkit.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
---
|
|
2
|
-
previous_version: 0.10.
|
|
2
|
+
previous_version: 0.10.2
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.11.0 (2026-12-03)
|
|
4
|
+
|
|
5
|
+
- Trees: `tree parent: :parent_id, position: :position` shows a table's rows
|
|
6
|
+
as a tree, for hierarchies such as accounts and their sub-accounts,
|
|
7
|
+
categories or an org chart. Rows come depth first, siblings by their
|
|
8
|
+
position; the first column is indented with a toggle that folds a row's
|
|
9
|
+
children, remembered per table. A search or filter shows the rows that
|
|
10
|
+
match and their ancestors, these dimmed. A page is whole top-level
|
|
11
|
+
subtrees, up to `tree_page_size` rows (200). A tree isn't sorted by its
|
|
12
|
+
columns or grouped.
|
|
13
|
+
- Editing in cells: `editable` on a column and the table's `update` edit a
|
|
14
|
+
cell in place, as text, a number, a date, a time or a select, saved by
|
|
15
|
+
`PATCH /tables/:table_id/rows/:id`, after which the rows are rendered
|
|
16
|
+
again. What doesn't save is shown at the cell.
|
|
17
|
+
- Keyboard commands: `command :name do key "Tab"; process { … } end` does
|
|
18
|
+
something to the selected row (indenting it, moving it, adding one below),
|
|
19
|
+
by `POST /tables/:table_id/commands/:name/:record_id`, and selects the row
|
|
20
|
+
it gives, editing it with `edit true`.
|
|
21
|
+
- A table with editable cells or commands has a selected cell, moved with
|
|
22
|
+
the arrows, Home and End; Enter, F2 or typing edits it, Tab and Shift+Tab
|
|
23
|
+
go on to the next or previous editable cell.
|
|
24
|
+
- `table :name, pane: true` leaves room beside the rows, and the
|
|
25
|
+
`mensa:rows` event tells where each row is, to draw against them (a
|
|
26
|
+
timeline, bars).
|
|
27
|
+
- Rows have `data-mensa-id`.
|
|
28
|
+
|
|
3
29
|
## 0.10.1
|
|
4
30
|
|
|
5
31
|
- The export dialog names a table by its model's name in the plural
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -24,6 +24,9 @@ Features:
|
|
|
24
24
|
- [X] Exports can be scheduled to run recurring (daily/weekly/monthly/quarterly/bi-yearly/yearly)
|
|
25
25
|
You will have to bring your own mailer, see configuration for details.
|
|
26
26
|
- [X] Grouping rows by a column, with collapsible groups and count/sum/min/max per group
|
|
27
|
+
- [X] Trees of rows, folding and paged by whole branches
|
|
28
|
+
- [X] Editing in cells, and keyboard commands on the selected row
|
|
29
|
+
- [X] A pane beside the rows, to draw against them
|
|
27
30
|
|
|
28
31
|
## Usage
|
|
29
32
|
|
|
@@ -363,6 +366,132 @@ sorted by the group column first (in the direction it is sorted, ascending other
|
|
|
363
366
|
then by the chosen order. Counts and aggregates are calculated with SQL `GROUP BY` over
|
|
364
367
|
all filtered rows, so they cover the whole group, also when it continues on the next page.
|
|
365
368
|
|
|
369
|
+
### Trees
|
|
370
|
+
|
|
371
|
+
Rows that make a hierarchy, such as accounts and their sub-accounts,
|
|
372
|
+
categories, or an org chart, can be shown as a tree: give the table the column
|
|
373
|
+
that points to a row's parent, and optionally the one that orders siblings
|
|
374
|
+
(else they're ordered by primary key).
|
|
375
|
+
|
|
376
|
+
```ruby
|
|
377
|
+
class AccountsTable < ApplicationTable
|
|
378
|
+
tree parent: :parent_id, position: :position
|
|
379
|
+
|
|
380
|
+
column(:name)
|
|
381
|
+
column(:code)
|
|
382
|
+
end
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Rows come depth first, each right after its parent. The first column is
|
|
386
|
+
indented by the row's depth, with a toggle to fold a row's children away;
|
|
387
|
+
what's folded is remembered in the browser per table. A search or filter shows
|
|
388
|
+
the rows that match and their ancestors, so a match is never out of context;
|
|
389
|
+
the ancestors that don't match themselves are dimmed
|
|
390
|
+
(`mensa-table__row--context`). A tree is in its own order, so its columns
|
|
391
|
+
aren't sortable, and it isn't grouped.
|
|
392
|
+
|
|
393
|
+
A page holds whole top-level subtrees, as many as fit in `tree_page_size` rows
|
|
394
|
+
(200), so a branch is never split over pages. A top-level subtree larger than
|
|
395
|
+
that is a page of its own; it isn't split further by its children.
|
|
396
|
+
|
|
397
|
+
Each row is placed by one recursive query over the table's scope, which gives
|
|
398
|
+
it its depth (`row.depth`), whether it has children shown (`row.children?`) and
|
|
399
|
+
whether it's there only for a match below it (`row.context?`). A row whose
|
|
400
|
+
parent isn't in the scope is at the top.
|
|
401
|
+
|
|
402
|
+
### Editing in cells
|
|
403
|
+
|
|
404
|
+
A column's cells can be edited in place. Mark the column `editable`, and give
|
|
405
|
+
the table an `update` block that saves a value: it gets the record, the
|
|
406
|
+
column's name and the value, and returns true, or the errors (a string or an
|
|
407
|
+
array) to show at the cell.
|
|
408
|
+
|
|
409
|
+
```ruby
|
|
410
|
+
column(:name) do
|
|
411
|
+
editable true
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
column(:kind) do
|
|
415
|
+
editable do
|
|
416
|
+
as :select # :text, :number, :date, :time or :select
|
|
417
|
+
collection { Account.kinds } # a select's [[label, value], ...]
|
|
418
|
+
show ->(account) { !account.locked? } # which records' cells may be edited
|
|
419
|
+
value { |account| account.kind } # the input's value, when it isn't the one shown
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
update do |account, column, value|
|
|
424
|
+
account.update(column => value) || account.errors.full_messages
|
|
425
|
+
end
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
The input is chosen by the column's type when `as` isn't given. Mensa saves
|
|
429
|
+
with `PATCH /tables/:table_id/rows/:id` (`column`, `value` and the table's
|
|
430
|
+
state), finding the record through the table's scope and only for a cell that
|
|
431
|
+
may be edited, then renders the rows again, as a change may change other rows
|
|
432
|
+
too. The `update` block runs in a view context; authorize in it as you would in
|
|
433
|
+
a controller.
|
|
434
|
+
|
|
435
|
+
### Keyboard commands
|
|
436
|
+
|
|
437
|
+
A table with editable cells or commands has a selected cell. Clicking a cell
|
|
438
|
+
selects it (an editable one isn't followed as the row's link); the arrows,
|
|
439
|
+
Home and End move it; Enter, F2 or typing edits it; while editing, Enter saves,
|
|
440
|
+
Escape cancels, and Tab and Shift+Tab save and go on to the next or previous
|
|
441
|
+
editable cell.
|
|
442
|
+
|
|
443
|
+
A command does something to the selected row with a key, such as moving rows
|
|
444
|
+
in a hierarchy:
|
|
445
|
+
|
|
446
|
+
```ruby
|
|
447
|
+
command :indent do
|
|
448
|
+
key "Tab"
|
|
449
|
+
process { |account| account.indent! }
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
command :move_up do
|
|
453
|
+
key "Alt+Shift+ArrowUp"
|
|
454
|
+
show ->(account) { account.position.positive? }
|
|
455
|
+
process { |account| account.move_up! }
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
command :add_below do
|
|
459
|
+
key "Ctrl+Enter"
|
|
460
|
+
edit true # edit the selected row's first editable cell afterwards
|
|
461
|
+
process { |account| account.add_below! } # returns the row to select
|
|
462
|
+
end
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
A key is its modifiers (`Ctrl`, which is also Cmd on macOS, `Alt`, `Shift`)
|
|
466
|
+
and the key's name as the browser gives it (`Tab`, `Enter`, `Delete`,
|
|
467
|
+
`ArrowUp`, `a`). A command's key wins over Mensa's own keys, but not while a
|
|
468
|
+
cell is edited. Mensa runs it with `POST /tables/:table_id/commands/:name/:record_id`,
|
|
469
|
+
finding the record through the table's scope and only when `show` allows it,
|
|
470
|
+
then renders the rows again with the row `process` returns selected (the
|
|
471
|
+
record itself when it returns something else). `process` runs in a view
|
|
472
|
+
context.
|
|
473
|
+
|
|
474
|
+
### A pane beside the rows
|
|
475
|
+
|
|
476
|
+
`table :accounts, pane: true` leaves room to the right of the rows, for
|
|
477
|
+
something drawn against them, such as bars or a timeline. The rows keep their
|
|
478
|
+
width and the pane (`.mensa-table__pane`) takes the rest; both scroll with the
|
|
479
|
+
page. After the rows are rendered, folded or resized, the table's element sends
|
|
480
|
+
`mensa:rows`:
|
|
481
|
+
|
|
482
|
+
```js
|
|
483
|
+
document.addEventListener("mensa:rows", (event) => {
|
|
484
|
+
const { table, rows, headerHeight } = event.detail
|
|
485
|
+
// rows: [{ id, top, height, depth, hidden }], top in pixels from the top of
|
|
486
|
+
// the rows' table, header included; a hidden (folded) row has top null
|
|
487
|
+
const pane = event.target.querySelector(".mensa-table__pane")
|
|
488
|
+
})
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
Each row's `tr` has `data-mensa-id`, and in a tree `data-mensa-depth`.
|
|
492
|
+
|
|
493
|
+
The dummy app's chart of accounts (`/accounts`) shows all of these together.
|
|
494
|
+
|
|
366
495
|
### Fast
|
|
367
496
|
|
|
368
497
|
Mensa selects only the data it needs, based on the columns. Sometimes it needs additional columns to do it's work, but you don't want them displayed. This can be done by adding `internal true` to the column definition, or shorter: use `internal` instead. If your table scope joins an association, Mensa also auto-adds the foreign key column as internal when it is needed.
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
@import "../../../components/mensa/add_filter/component.css";
|
|
4
4
|
@import "../../../components/mensa/column_customizer/component.css";
|
|
5
5
|
@import "../../../components/mensa/cell/component.css";
|
|
6
|
+
@import "../../../components/mensa/cells/component.css";
|
|
6
7
|
@import "../../../components/mensa/control_bar/component.css";
|
|
7
8
|
@import "../../../components/mensa/empty_state/component.css";
|
|
8
9
|
@import "../../../components/mensa/filter_pill/component.css";
|
|
@@ -14,5 +15,6 @@
|
|
|
14
15
|
@import "../../../components/mensa/search/component.css";
|
|
15
16
|
@import "../../../components/mensa/table/component.css";
|
|
16
17
|
@import "../../../components/mensa/table_row/component.css";
|
|
18
|
+
@import "../../../components/mensa/tree/component.css";
|
|
17
19
|
@import "../../../components/mensa/view/component.css";
|
|
18
20
|
@import "../../../components/mensa/views/component.css";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/* A table with a selected cell (editable cells or commands) */
|
|
2
|
+
.mensa-table table.mensa-table--keyboard {
|
|
3
|
+
@apply outline-none;
|
|
4
|
+
|
|
5
|
+
td[data-mensa-column] {
|
|
6
|
+
@apply outline-none;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
td.mensa-table__cell--editable {
|
|
10
|
+
@apply cursor-text;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
td.mensa-table__cell--selected {
|
|
14
|
+
@apply ring-2 ring-inset ring-primary-500;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* The editor lies over the cell, whose content stays (hidden), so the
|
|
18
|
+
row keeps its height and the column its width */
|
|
19
|
+
td.mensa-table__cell--editing {
|
|
20
|
+
position: relative;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
td.mensa-table__cell--editing .mensa-table__editing-content {
|
|
24
|
+
visibility: hidden;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
td.mensa-table__cell--error {
|
|
28
|
+
@apply ring-2 ring-inset ring-red-500;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.mensa-table__editor {
|
|
33
|
+
@apply rounded-none border-0 bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 shadow-none ring-2 ring-inset ring-primary-500 focus:outline-none focus:ring-2;
|
|
34
|
+
z-index: 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* A date's, a time's and a select's controls need more room than a narrow
|
|
38
|
+
column has: they reach over the next cell instead of widening it */
|
|
39
|
+
.mensa-table input[type="date"].mensa-table__editor,
|
|
40
|
+
.mensa-table input[type="time"].mensa-table__editor,
|
|
41
|
+
.mensa-table select.mensa-table__editor {
|
|
42
|
+
min-width: 10rem;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.mensa-table select.mensa-table__editor {
|
|
46
|
+
@apply pr-8;
|
|
47
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import ApplicationController from "mensa/controllers/application_controller";
|
|
2
|
+
import { patch, post } from "@rails/request.js";
|
|
3
|
+
|
|
4
|
+
// A table's selected cell, moved with the arrows, edited in place, and the
|
|
5
|
+
// table's commands (keys that do something to the selected row).
|
|
6
|
+
//
|
|
7
|
+
// The table is rendered again after every change, so what's selected (the
|
|
8
|
+
// row's id and the column) is kept on the table's element, which stays, and
|
|
9
|
+
// picked up again when this controller connects.
|
|
10
|
+
export default class CellsController extends ApplicationController {
|
|
11
|
+
static values = {
|
|
12
|
+
rowsUrl: String,
|
|
13
|
+
commandsUrl: String,
|
|
14
|
+
commands: Array,
|
|
15
|
+
texts: Object,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
connect() {
|
|
19
|
+
super.connect();
|
|
20
|
+
// The row the server says to select (after a command), and to edit
|
|
21
|
+
const view = this.element.closest("[data-mensa-table-target='view']");
|
|
22
|
+
const host = this.host;
|
|
23
|
+
if (view && host && view.dataset.mensaSelectId) {
|
|
24
|
+
host.dataset.mensaSelectedId = view.dataset.mensaSelectId;
|
|
25
|
+
if (view.dataset.mensaEdit) host.dataset.mensaEdit = view.dataset.mensaEdit;
|
|
26
|
+
}
|
|
27
|
+
this.restore();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get host() {
|
|
31
|
+
return this.element.closest(".mensa-table");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// --- Selection ---
|
|
35
|
+
|
|
36
|
+
get rows() {
|
|
37
|
+
return Array.from(this.element.querySelectorAll("tbody tr[data-mensa-id]")).filter(
|
|
38
|
+
(row) => !row.classList.contains("hidden"),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
cellsOf(row) {
|
|
43
|
+
return Array.from(row.querySelectorAll("td[data-mensa-column]"));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get selectedCell() {
|
|
47
|
+
return this.element.querySelector("td.mensa-table__cell--selected");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get selectedRow() {
|
|
51
|
+
return this.selectedCell?.closest("tr") || null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
select(cell, { focus = true } = {}) {
|
|
55
|
+
if (!cell) return;
|
|
56
|
+
this.element.querySelectorAll(".mensa-table__cell--selected").forEach((el) => el.classList.remove("mensa-table__cell--selected"));
|
|
57
|
+
this.element.querySelectorAll(".mensa-table__row--selected").forEach((el) => el.classList.remove("mensa-table__row--selected"));
|
|
58
|
+
cell.classList.add("mensa-table__cell--selected");
|
|
59
|
+
const row = cell.closest("tr");
|
|
60
|
+
row.classList.add("mensa-table__row--selected");
|
|
61
|
+
|
|
62
|
+
const host = this.host;
|
|
63
|
+
if (host) {
|
|
64
|
+
host.dataset.mensaSelectedId = row.dataset.mensaId;
|
|
65
|
+
host.dataset.mensaSelectedColumn = cell.dataset.mensaColumn;
|
|
66
|
+
}
|
|
67
|
+
if (focus) {
|
|
68
|
+
cell.setAttribute("tabindex", "-1");
|
|
69
|
+
cell.focus({ preventScroll: true });
|
|
70
|
+
cell.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// After a render: the row and column kept, and editing when asked
|
|
75
|
+
restore() {
|
|
76
|
+
const host = this.host;
|
|
77
|
+
// Not while editing: that's where the user is
|
|
78
|
+
if (!host || !host.dataset.mensaSelectedId || this.editor) return;
|
|
79
|
+
|
|
80
|
+
const row = this.element.querySelector(`tbody tr[data-mensa-id="${CSS.escape(host.dataset.mensaSelectedId)}"]`);
|
|
81
|
+
if (!row) return;
|
|
82
|
+
|
|
83
|
+
const cells = this.cellsOf(row);
|
|
84
|
+
let cell = cells.find((each) => each.dataset.mensaColumn === host.dataset.mensaSelectedColumn) || cells[0];
|
|
85
|
+
const edit = host.dataset.mensaEdit;
|
|
86
|
+
delete host.dataset.mensaEdit;
|
|
87
|
+
if (edit === "first") {
|
|
88
|
+
cell = cells.find((each) => each.dataset.mensaEditable) || cell;
|
|
89
|
+
}
|
|
90
|
+
const focus = host.contains(document.activeElement) || document.activeElement === document.body || edit !== undefined;
|
|
91
|
+
this.select(cell, { focus });
|
|
92
|
+
if (edit && cell?.dataset.mensaEditable) this.beginEdit(cell);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// In the capture phase, before the row's link: an editable cell, or the
|
|
96
|
+
// field editing it, isn't the row's link
|
|
97
|
+
click(event) {
|
|
98
|
+
if (this.editor && this.editor.contains(event.target)) {
|
|
99
|
+
event.stopPropagation();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const cell = event.target.closest("td[data-mensa-column]");
|
|
103
|
+
if (!cell || !this.element.contains(cell)) return;
|
|
104
|
+
if (event.target.closest("a, button, input, select, textarea")) return;
|
|
105
|
+
|
|
106
|
+
this.select(cell);
|
|
107
|
+
if (cell.dataset.mensaEditable) event.stopPropagation();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
doubleClick(event) {
|
|
111
|
+
const cell = event.target.closest("td[data-mensa-editable]");
|
|
112
|
+
if (!cell || (this.editor && this.editor.contains(event.target))) return;
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
this.select(cell);
|
|
115
|
+
this.beginEdit(cell);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// --- Keys ---
|
|
119
|
+
|
|
120
|
+
keydown(event) {
|
|
121
|
+
if (this.editor) return this.editorKeydown(event);
|
|
122
|
+
// Keys in a field of a cell (a render block's) are the field's
|
|
123
|
+
if (event.target.closest("input, select, textarea, [contenteditable]")) return;
|
|
124
|
+
|
|
125
|
+
const cell = this.selectedCell;
|
|
126
|
+
if (!cell) {
|
|
127
|
+
if (["ArrowDown", "ArrowUp"].includes(event.key) && this.rows[0]) {
|
|
128
|
+
event.preventDefault();
|
|
129
|
+
this.select(this.cellsOf(this.rows[0])[0]);
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const command = this.commandsValue.find((each) => this.matches(event, each.key));
|
|
135
|
+
if (command) {
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
this.run(command.name, cell.closest("tr").dataset.mensaId);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
switch (event.key) {
|
|
142
|
+
case "ArrowUp":
|
|
143
|
+
event.preventDefault();
|
|
144
|
+
return this.moveRow(-1);
|
|
145
|
+
case "ArrowDown":
|
|
146
|
+
event.preventDefault();
|
|
147
|
+
return this.moveRow(1);
|
|
148
|
+
case "ArrowLeft":
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
return this.moveCell(-1);
|
|
151
|
+
case "ArrowRight":
|
|
152
|
+
event.preventDefault();
|
|
153
|
+
return this.moveCell(1);
|
|
154
|
+
case "Home":
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
return this.select(this.cellsOf(this.selectedRow)[0]);
|
|
157
|
+
case "End":
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
return this.select(this.cellsOf(this.selectedRow).at(-1));
|
|
160
|
+
case "Enter":
|
|
161
|
+
case "F2":
|
|
162
|
+
if (cell.dataset.mensaEditable) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
this.beginEdit(cell);
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
case "Escape":
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Typing starts editing, with what's typed
|
|
172
|
+
if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey && cell.dataset.mensaEditable) {
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
this.beginEdit(cell, event.key);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
moveRow(step) {
|
|
179
|
+
const rows = this.rows;
|
|
180
|
+
const index = rows.indexOf(this.selectedRow);
|
|
181
|
+
const target = rows[index + step];
|
|
182
|
+
if (!target) return;
|
|
183
|
+
const column = this.selectedCell.dataset.mensaColumn;
|
|
184
|
+
const cells = this.cellsOf(target);
|
|
185
|
+
this.select(cells.find((each) => each.dataset.mensaColumn === column) || cells[0]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
moveCell(step) {
|
|
189
|
+
const cells = this.cellsOf(this.selectedRow);
|
|
190
|
+
const target = cells[cells.indexOf(this.selectedCell) + step];
|
|
191
|
+
if (target) this.select(target);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// "Alt+Shift+ArrowUp": its modifiers exactly, and its key. Ctrl is also
|
|
195
|
+
// Cmd, for macOS
|
|
196
|
+
matches(event, key) {
|
|
197
|
+
if (!key) return false;
|
|
198
|
+
const parts = key.split("+");
|
|
199
|
+
const name = parts.pop();
|
|
200
|
+
const mods = new Set(parts.map((part) => part.toLowerCase()));
|
|
201
|
+
const ctrl = event.ctrlKey || event.metaKey;
|
|
202
|
+
if (mods.has("ctrl") !== ctrl) return false;
|
|
203
|
+
if (mods.has("alt") !== event.altKey) return false;
|
|
204
|
+
if (mods.has("shift") !== event.shiftKey) return false;
|
|
205
|
+
const pressed = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
|
206
|
+
return pressed === (name.length === 1 ? name.toLowerCase() : name);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// --- Editing ---
|
|
210
|
+
|
|
211
|
+
beginEdit(cell, typed = null) {
|
|
212
|
+
if (!cell?.dataset.mensaEditable || this.editor) return;
|
|
213
|
+
|
|
214
|
+
const as = cell.dataset.mensaEditAs || "text";
|
|
215
|
+
const value = cell.dataset.mensaEditValue || "";
|
|
216
|
+
let input;
|
|
217
|
+
if (as === "select") {
|
|
218
|
+
input = document.createElement("select");
|
|
219
|
+
let options = [];
|
|
220
|
+
try {
|
|
221
|
+
options = JSON.parse(cell.dataset.mensaEditCollection || "[]");
|
|
222
|
+
} catch (e) { }
|
|
223
|
+
options.forEach(([label, optionValue]) => {
|
|
224
|
+
const option = document.createElement("option");
|
|
225
|
+
option.value = optionValue;
|
|
226
|
+
option.textContent = label;
|
|
227
|
+
option.selected = optionValue === value;
|
|
228
|
+
input.appendChild(option);
|
|
229
|
+
});
|
|
230
|
+
} else {
|
|
231
|
+
input = document.createElement("input");
|
|
232
|
+
input.type = { number: "number", date: "date", time: "time" }[as] || "text";
|
|
233
|
+
if (as === "number") input.step = "any";
|
|
234
|
+
input.value = typed !== null && input.type === "text" ? typed : value;
|
|
235
|
+
}
|
|
236
|
+
input.className = "mensa-table__editor";
|
|
237
|
+
input.dataset.original = value;
|
|
238
|
+
|
|
239
|
+
this.editing = { cell, html: cell.innerHTML };
|
|
240
|
+
this.editor = input;
|
|
241
|
+
cell.classList.add("mensa-table__cell--editing");
|
|
242
|
+
cell.classList.remove("mensa-table__cell--error");
|
|
243
|
+
cell.removeAttribute("title");
|
|
244
|
+
this.placeEditor(cell, input);
|
|
245
|
+
input.addEventListener("blur", () => this.commit());
|
|
246
|
+
input.focus();
|
|
247
|
+
if (input.select && typed === null && input.type === "text") input.select();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// The editor lies over the cell, so the row keeps its height and the
|
|
251
|
+
// column its width: the cell's content stays, hidden, and the editor takes
|
|
252
|
+
// its place in the cell's font, its text where the content's was. A
|
|
253
|
+
// tree's indent and toggle stay visible before it.
|
|
254
|
+
placeEditor(cell, input) {
|
|
255
|
+
const style = getComputedStyle(cell);
|
|
256
|
+
const indent = cell.querySelector(".mensa-tree__indent");
|
|
257
|
+
const content = document.createElement("span");
|
|
258
|
+
content.className = "mensa-table__editing-content";
|
|
259
|
+
[...cell.childNodes].filter((node) => node !== indent).forEach((node) => content.appendChild(node));
|
|
260
|
+
cell.append(content, input);
|
|
261
|
+
|
|
262
|
+
const indented = indent ? Math.max(content.offsetLeft - 4, 0) : 0;
|
|
263
|
+
Object.assign(input.style, {
|
|
264
|
+
position: "absolute",
|
|
265
|
+
top: "0",
|
|
266
|
+
bottom: "0",
|
|
267
|
+
left: `${indented}px`,
|
|
268
|
+
right: "0",
|
|
269
|
+
width: "auto",
|
|
270
|
+
height: "100%",
|
|
271
|
+
margin: "0",
|
|
272
|
+
boxSizing: "border-box",
|
|
273
|
+
fontFamily: style.fontFamily,
|
|
274
|
+
fontSize: style.fontSize,
|
|
275
|
+
fontWeight: style.fontWeight,
|
|
276
|
+
lineHeight: style.lineHeight,
|
|
277
|
+
paddingTop: "0",
|
|
278
|
+
paddingBottom: "0",
|
|
279
|
+
paddingLeft: indent ? "4px" : style.paddingLeft,
|
|
280
|
+
paddingRight: input.tagName === "SELECT" ? "" : style.paddingRight
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
editorKeydown(event) {
|
|
285
|
+
event.stopPropagation();
|
|
286
|
+
switch (event.key) {
|
|
287
|
+
case "Enter":
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
return this.commit();
|
|
290
|
+
case "Escape":
|
|
291
|
+
event.preventDefault();
|
|
292
|
+
return this.cancel();
|
|
293
|
+
case "Tab": {
|
|
294
|
+
event.preventDefault();
|
|
295
|
+
const next = this.neighbourEditable(this.editing.cell, event.shiftKey ? -1 : 1);
|
|
296
|
+
return this.commit(next);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// The editable cell after (or before) cell, on to the next rows
|
|
302
|
+
neighbourEditable(cell, step) {
|
|
303
|
+
const all = this.rows.flatMap((row) => this.cellsOf(row)).filter((each) => each.dataset.mensaEditable);
|
|
304
|
+
return all[all.indexOf(cell) + step] || null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
cancel() {
|
|
308
|
+
if (!this.editing) return;
|
|
309
|
+
const { cell, html } = this.editing;
|
|
310
|
+
this.editing = null;
|
|
311
|
+
this.editor = null;
|
|
312
|
+
cell.innerHTML = html;
|
|
313
|
+
cell.classList.remove("mensa-table__cell--editing");
|
|
314
|
+
this.select(cell);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Saves what's edited when it changed, then selects next (and edits it)
|
|
318
|
+
async commit(next = null) {
|
|
319
|
+
if (!this.editing || this.committing) return;
|
|
320
|
+
const { cell } = this.editing;
|
|
321
|
+
const input = this.editor;
|
|
322
|
+
const value = input.value;
|
|
323
|
+
const host = this.host;
|
|
324
|
+
|
|
325
|
+
if (value === input.dataset.original) {
|
|
326
|
+
this.cancel();
|
|
327
|
+
if (next) {
|
|
328
|
+
this.select(next);
|
|
329
|
+
this.beginEdit(next);
|
|
330
|
+
}
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
this.committing = true;
|
|
335
|
+
const html = this.editing.html;
|
|
336
|
+
this.editing = null;
|
|
337
|
+
this.editor = null;
|
|
338
|
+
const row = cell.closest("tr");
|
|
339
|
+
if (host) {
|
|
340
|
+
const target = next || cell;
|
|
341
|
+
host.dataset.mensaSelectedId = target.closest("tr").dataset.mensaId;
|
|
342
|
+
host.dataset.mensaSelectedColumn = target.dataset.mensaColumn;
|
|
343
|
+
if (next) host.dataset.mensaEdit = "cell";
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
try {
|
|
347
|
+
const response = await patch(this.memberUrl(this.rowsUrlValue, row.dataset.mensaId), {
|
|
348
|
+
body: JSON.stringify({ column: cell.dataset.mensaColumn, value, ...this.tableState() }),
|
|
349
|
+
contentType: "application/json",
|
|
350
|
+
responseKind: "turbo-stream",
|
|
351
|
+
});
|
|
352
|
+
if (!response.ok) {
|
|
353
|
+
let errors = [];
|
|
354
|
+
try {
|
|
355
|
+
errors = (await response.json).errors || [];
|
|
356
|
+
} catch (e) { }
|
|
357
|
+
if (host) delete host.dataset.mensaEdit;
|
|
358
|
+
// Still editing, with what didn't save
|
|
359
|
+
this.editing = { cell, html };
|
|
360
|
+
this.editor = input;
|
|
361
|
+
this.showError(cell, errors.join(", ") || this.textsValue.not_saved);
|
|
362
|
+
}
|
|
363
|
+
} finally {
|
|
364
|
+
this.committing = false;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
showError(cell, message) {
|
|
369
|
+
cell.classList.add("mensa-table__cell--error");
|
|
370
|
+
cell.setAttribute("title", message);
|
|
371
|
+
if (this.editor) {
|
|
372
|
+
this.editor.setCustomValidity(message);
|
|
373
|
+
this.editor.reportValidity();
|
|
374
|
+
this.editor.addEventListener("input", () => this.editor?.setCustomValidity(""), { once: true });
|
|
375
|
+
this.editor.focus();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// --- Commands ---
|
|
380
|
+
|
|
381
|
+
async run(name, id) {
|
|
382
|
+
if (this.running) return;
|
|
383
|
+
this.running = true;
|
|
384
|
+
try {
|
|
385
|
+
const url = new URL(this.commandsUrlValue, window.location.origin);
|
|
386
|
+
url.pathname += `/${encodeURIComponent(name)}/${encodeURIComponent(id)}`;
|
|
387
|
+
const response = await post(url.toString(), {
|
|
388
|
+
body: JSON.stringify(this.tableState()),
|
|
389
|
+
contentType: "application/json",
|
|
390
|
+
responseKind: "turbo-stream",
|
|
391
|
+
});
|
|
392
|
+
// The rows are rendered again, with the row to select
|
|
393
|
+
await response;
|
|
394
|
+
} finally {
|
|
395
|
+
this.running = false;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// The table as the user sees it, to render it again the same way
|
|
400
|
+
tableState() {
|
|
401
|
+
const table = this.host?.["mensa-table"];
|
|
402
|
+
const state = table?.requestState ? table.requestState() : {};
|
|
403
|
+
const frame = this.element.closest("turbo-frame");
|
|
404
|
+
return { ...state, turbo_frame_id: frame?.id };
|
|
405
|
+
}
|
|
406
|
+
}
|