@sealcode/sealgen 0.19.17 → 0.19.18
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/@types/controllers/sealgen-table.stimulus.d.ts +9 -0
- package/@types/forms/controls/table.d.ts +2 -0
- package/lib/controllers/sealgen-table.stimulus.js +98 -0
- package/lib/forms/controls/table.js +49 -43
- package/lib/mount.js +8 -10
- package/package.json +1 -1
- package/src/controllers/sealgen-table.stimulus.ts +115 -0
- package/src/forms/controls/table.ts +94 -81
- package/src/mount.ts +8 -12
- package/lib/controllers/table-add-button.stimulus.js +0 -35
- package/src/controllers/table-add-button.stimulus.ts +0 -35
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Controller } from "stimulus";
|
|
2
|
+
export default class TableController extends Controller {
|
|
3
|
+
static targets: string[];
|
|
4
|
+
tableTarget: HTMLTableElement;
|
|
5
|
+
templateTarget: HTMLTemplateElement;
|
|
6
|
+
getTbody(): HTMLTableSectionElement;
|
|
7
|
+
addRow(): void;
|
|
8
|
+
swapRows(e: MouseEvent): void;
|
|
9
|
+
}
|
|
@@ -10,6 +10,7 @@ export type TableControlOptions<F extends Record<string, FormField>> = {
|
|
|
10
10
|
[field_name in keyof F]: FormControl;
|
|
11
11
|
};
|
|
12
12
|
allow_removing?: boolean;
|
|
13
|
+
allow_reordering?: boolean;
|
|
13
14
|
label_add?: string;
|
|
14
15
|
label: string;
|
|
15
16
|
label_remove?: string;
|
|
@@ -36,4 +37,5 @@ export declare class Table<F extends Record<string, FormField>> extends FormCont
|
|
|
36
37
|
setLabel(value: string): this;
|
|
37
38
|
setRemoveLabel(value: string): this;
|
|
38
39
|
setAddLabel(value: string): this;
|
|
40
|
+
setAllowReordering(value: boolean): this;
|
|
39
41
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Controller } from "stimulus";
|
|
2
|
+
class TableController extends Controller {
|
|
3
|
+
static {
|
|
4
|
+
this.targets = ["table", "template"];
|
|
5
|
+
}
|
|
6
|
+
getTbody() {
|
|
7
|
+
const tbody = this.element.closest("turbo-frame")?.querySelector("tbody");
|
|
8
|
+
if (!tbody) {
|
|
9
|
+
throw new Error("tbody not found");
|
|
10
|
+
}
|
|
11
|
+
return tbody;
|
|
12
|
+
}
|
|
13
|
+
getRowIndex(tr) {
|
|
14
|
+
let result = 0;
|
|
15
|
+
for (const child of Array.from(this.getTbody().children)) {
|
|
16
|
+
if (child == tr) {
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
result++;
|
|
20
|
+
}
|
|
21
|
+
return -1;
|
|
22
|
+
}
|
|
23
|
+
addRow() {
|
|
24
|
+
const tbody = this.getTbody();
|
|
25
|
+
const orig_template = this.templateTarget.innerHTML;
|
|
26
|
+
const placeholder = this.element.getAttribute("data-table-placeholder");
|
|
27
|
+
if (!placeholder) {
|
|
28
|
+
throw new Error("Could not find placeholder attribute");
|
|
29
|
+
}
|
|
30
|
+
this.templateTarget.innerHTML = orig_template.replaceAll(
|
|
31
|
+
placeholder,
|
|
32
|
+
String(tbody.querySelectorAll("tr").length)
|
|
33
|
+
);
|
|
34
|
+
tbody.appendChild(this.templateTarget.cloneNode(true).content);
|
|
35
|
+
this.templateTarget.innerHTML = orig_template;
|
|
36
|
+
this.reindex();
|
|
37
|
+
}
|
|
38
|
+
swapRows(index_a, index_b) {
|
|
39
|
+
const tbody = this.getTbody();
|
|
40
|
+
const [a, b] = [index_a, index_b].sort().reverse();
|
|
41
|
+
tbody.insertBefore(tbody.children[a], tbody.children[b]);
|
|
42
|
+
this.reindex();
|
|
43
|
+
}
|
|
44
|
+
moveUp(e) {
|
|
45
|
+
const target = e.target;
|
|
46
|
+
const tr = target.closest("tr");
|
|
47
|
+
if (!tr) {
|
|
48
|
+
console.error("Not within a tr");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const index_a = this.getRowIndex(tr);
|
|
52
|
+
if (index_a < 0) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const index_b = index_a - 1;
|
|
56
|
+
this.swapRows(index_a, index_b);
|
|
57
|
+
}
|
|
58
|
+
moveDown(e) {
|
|
59
|
+
const target = e.target;
|
|
60
|
+
const tr = target.closest("tr");
|
|
61
|
+
if (!tr) {
|
|
62
|
+
console.error("Not within a tr");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const index_a = this.getRowIndex(tr);
|
|
66
|
+
if (index_a < 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const index_b = index_a + 1;
|
|
70
|
+
this.swapRows(index_a, index_b);
|
|
71
|
+
}
|
|
72
|
+
reindex() {
|
|
73
|
+
let result = 0;
|
|
74
|
+
for (const tr of Array.from(this.getTbody().children)) {
|
|
75
|
+
tr.querySelectorAll("input").forEach((input) => {
|
|
76
|
+
input.setAttribute(
|
|
77
|
+
"name",
|
|
78
|
+
(input.getAttribute("name") || "").replace(
|
|
79
|
+
/[(\d+)](?!.*\d)/,
|
|
80
|
+
// last number in a regex
|
|
81
|
+
result.toString()
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
result++;
|
|
86
|
+
}
|
|
87
|
+
this.element.querySelectorAll(
|
|
88
|
+
`[data-action="sealgen-table#moveUp"], [data-action="sealgen-table#moveDown"]`
|
|
89
|
+
).forEach((e) => e.disabled = false);
|
|
90
|
+
this.element.querySelectorAll(
|
|
91
|
+
'tr:first-child [data-action="sealgen-table#moveUp"], tr:last-child [data-action="sealgen-table#moveDown"]'
|
|
92
|
+
).forEach((e) => e.disabled = true);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export {
|
|
96
|
+
TableController as default
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=sealgen-table.stimulus.js.map
|
|
@@ -49,19 +49,17 @@ class Table extends FormControl {
|
|
|
49
49
|
${this.options.allow_removing ? (
|
|
50
50
|
/* HTML */
|
|
51
51
|
`<td>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
formnovalidate
|
|
64
|
-
formaction="${this.getActionURL(
|
|
52
|
+
<button onclick="this.closest('tr').remove()">
|
|
53
|
+
remove
|
|
54
|
+
</button>
|
|
55
|
+
<noscript>
|
|
56
|
+
<input
|
|
57
|
+
type="submit"
|
|
58
|
+
data-turbo-frame="${this.getFrameID()}"
|
|
59
|
+
value="${this.options.label_remove || "remove"}"
|
|
60
|
+
form="${fctx.form_id}"
|
|
61
|
+
formnovalidate
|
|
62
|
+
formaction="${this.getActionURL(
|
|
65
63
|
this.table_field.name,
|
|
66
64
|
{
|
|
67
65
|
remove: {
|
|
@@ -69,9 +67,19 @@ class Table extends FormControl {
|
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
)}"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
/>
|
|
71
|
+
</noscript>
|
|
72
|
+
</td>`
|
|
73
|
+
) : ""}${this.options.allow_reordering ? (
|
|
74
|
+
/* HTML */
|
|
75
|
+
`<td>
|
|
76
|
+
<button data-action="sealgen-table#moveUp">
|
|
77
|
+
\u2191
|
|
78
|
+
</button>
|
|
79
|
+
<button data-action="sealgen-table#moveDown">
|
|
80
|
+
\u2193
|
|
81
|
+
</button>
|
|
82
|
+
</td>`
|
|
75
83
|
) : ""}
|
|
76
84
|
</tr>`
|
|
77
85
|
);
|
|
@@ -89,13 +97,17 @@ class Table extends FormControl {
|
|
|
89
97
|
].join(" ")}"
|
|
90
98
|
>
|
|
91
99
|
<label>${this.options.label || this.table_field.label}</label>
|
|
92
|
-
<div
|
|
93
|
-
|
|
100
|
+
<div
|
|
101
|
+
class="table__wrapper"
|
|
102
|
+
data-controller="sealgen-table"
|
|
103
|
+
data-table-placeholder="${TABLE_COLUMN_FIELD_INDEX_PLACEHOLDER}"
|
|
104
|
+
>
|
|
105
|
+
<table data-sealgen-table-target="table">
|
|
94
106
|
<tbody>
|
|
95
107
|
${rows?.map((row, index) => make_row(row, index))}
|
|
96
108
|
</tbody>
|
|
97
109
|
</table>
|
|
98
|
-
<template>
|
|
110
|
+
<template data-sealgen-table-target="template">
|
|
99
111
|
${make_row(null, TABLE_COLUMN_FIELD_INDEX_PLACEHOLDER)}
|
|
100
112
|
</template>
|
|
101
113
|
|
|
@@ -104,19 +116,17 @@ class Table extends FormControl {
|
|
|
104
116
|
this.options.allow_adding ? (
|
|
105
117
|
/* HTML */
|
|
106
118
|
`<button
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
formnovalidate
|
|
119
|
-
formaction="${this.getActionURL(
|
|
119
|
+
data-action="click->sealgen-table#addRow"
|
|
120
|
+
>
|
|
121
|
+
${this.options.label_add || "add"}
|
|
122
|
+
</button>
|
|
123
|
+
<noscript>
|
|
124
|
+
<input
|
|
125
|
+
type="submit"
|
|
126
|
+
value="add"
|
|
127
|
+
form="${fctx.form_id}"
|
|
128
|
+
formnovalidate
|
|
129
|
+
formaction="${this.getActionURL(
|
|
120
130
|
this.table_field.name,
|
|
121
131
|
{
|
|
122
132
|
insert: {
|
|
@@ -125,8 +135,8 @@ class Table extends FormControl {
|
|
|
125
135
|
}
|
|
126
136
|
}
|
|
127
137
|
)}"
|
|
128
|
-
|
|
129
|
-
|
|
138
|
+
/>
|
|
139
|
+
</noscript>`
|
|
130
140
|
) : ""}
|
|
131
141
|
</div>
|
|
132
142
|
</turbo-frame>`;
|
|
@@ -144,7 +154,7 @@ class Table extends FormControl {
|
|
|
144
154
|
router.post("/", async (ctx, next) => {
|
|
145
155
|
const action = ctx.$body.action;
|
|
146
156
|
const field_name = ctx.$body.field_name;
|
|
147
|
-
if (!is(action, predicates.object) || !is(field_name, predicates.string)) {
|
|
157
|
+
if (!is(action, predicates.object) || !is(field_name, predicates.string) || field_name !== this.table_field.name) {
|
|
148
158
|
await next();
|
|
149
159
|
return;
|
|
150
160
|
}
|
|
@@ -154,10 +164,6 @@ class Table extends FormControl {
|
|
|
154
164
|
},
|
|
155
165
|
action
|
|
156
166
|
)) {
|
|
157
|
-
if (field_name !== this.table_field.name) {
|
|
158
|
-
await next();
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
167
|
if (!ctx.$body[this.table_field.name]) {
|
|
162
168
|
ctx.$body[this.table_field.name] = {};
|
|
163
169
|
}
|
|
@@ -169,10 +175,6 @@ class Table extends FormControl {
|
|
|
169
175
|
},
|
|
170
176
|
action
|
|
171
177
|
)) {
|
|
172
|
-
if (field_name !== this.table_field.name) {
|
|
173
|
-
await next();
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
178
|
if (!ctx.$body[this.table_field.name]) {
|
|
177
179
|
ctx.$body[this.table_field.name] = {};
|
|
178
180
|
}
|
|
@@ -221,6 +223,10 @@ class Table extends FormControl {
|
|
|
221
223
|
this.options.label_add = value;
|
|
222
224
|
return this;
|
|
223
225
|
}
|
|
226
|
+
setAllowReordering(value) {
|
|
227
|
+
this.options.allow_reordering = value;
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
224
230
|
}
|
|
225
231
|
export {
|
|
226
232
|
TABLE_COLUMN_FIELD_INDEX_PLACEHOLDER,
|
package/lib/mount.js
CHANGED
|
@@ -10,19 +10,17 @@ async function handleHtmlPromise(ctx, next) {
|
|
|
10
10
|
}
|
|
11
11
|
function mount(router, url, mountable, use_dummy_app = false, file_manager = use_dummy_app ? new FileManager("/tmp", "/uploaded_files") : void 0) {
|
|
12
12
|
const raw_url = typeof url === "string" ? url : url.rawURL;
|
|
13
|
-
const imageRouter = new KoaResponsiveImageRouter({
|
|
14
|
-
staticPath: "/tmp",
|
|
15
|
-
thumbnailSize: 10,
|
|
16
|
-
cacheManagerResolutionThreshold: 10,
|
|
17
|
-
imageStoragePath: "/tmp",
|
|
18
|
-
smartCropStoragePath: "/tmp"
|
|
19
|
-
});
|
|
20
|
-
const fileManager = new FileManager("/tmp", "/uploaded_files");
|
|
21
13
|
const args = use_dummy_app ? [
|
|
22
14
|
async (ctx, next) => {
|
|
23
15
|
ctx.$app = {
|
|
24
|
-
fileManager,
|
|
25
|
-
imageRouter
|
|
16
|
+
fileManager: new FileManager("/tmp", "/uploaded_files"),
|
|
17
|
+
imageRouter: new KoaResponsiveImageRouter({
|
|
18
|
+
staticPath: "/tmp",
|
|
19
|
+
thumbnailSize: 10,
|
|
20
|
+
cacheManagerResolutionThreshold: 10,
|
|
21
|
+
imageStoragePath: "/tmp",
|
|
22
|
+
smartCropStoragePath: "/tmp"
|
|
23
|
+
}),
|
|
26
24
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
27
25
|
Logger: new Proxy(
|
|
28
26
|
{},
|
package/package.json
CHANGED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { Controller } from "stimulus";
|
|
3
|
+
|
|
4
|
+
export default class TableController extends Controller {
|
|
5
|
+
static targets = ["table", "template"];
|
|
6
|
+
declare tableTarget: HTMLTableElement;
|
|
7
|
+
declare templateTarget: HTMLTemplateElement;
|
|
8
|
+
|
|
9
|
+
getTbody() {
|
|
10
|
+
const tbody = this.element
|
|
11
|
+
.closest("turbo-frame")
|
|
12
|
+
?.querySelector("tbody");
|
|
13
|
+
if (!tbody) {
|
|
14
|
+
throw new Error("tbody not found");
|
|
15
|
+
}
|
|
16
|
+
return tbody;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getRowIndex(tr: HTMLTableRowElement) {
|
|
20
|
+
let result = 0;
|
|
21
|
+
for (const child of Array.from(this.getTbody().children)) {
|
|
22
|
+
if (child == tr) {
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
result++;
|
|
26
|
+
}
|
|
27
|
+
return -1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
addRow() {
|
|
31
|
+
const tbody = this.getTbody();
|
|
32
|
+
const orig_template = this.templateTarget.innerHTML;
|
|
33
|
+
const placeholder = this.element.getAttribute("data-table-placeholder");
|
|
34
|
+
if (!placeholder) {
|
|
35
|
+
throw new Error("Could not find placeholder attribute");
|
|
36
|
+
}
|
|
37
|
+
this.templateTarget.innerHTML = orig_template.replaceAll(
|
|
38
|
+
placeholder,
|
|
39
|
+
String(tbody.querySelectorAll("tr").length)
|
|
40
|
+
);
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
42
|
+
tbody.appendChild((this.templateTarget.cloneNode(true) as any).content);
|
|
43
|
+
this.templateTarget.innerHTML = orig_template;
|
|
44
|
+
this.reindex();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
swapRows(index_a: number, index_b: number) {
|
|
48
|
+
const tbody = this.getTbody();
|
|
49
|
+
const [a, b] = [index_a, index_b].sort().reverse(); // we want them in descending order
|
|
50
|
+
tbody.insertBefore(tbody.children[a], tbody.children[b]);
|
|
51
|
+
this.reindex();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
moveUp(e: MouseEvent) {
|
|
55
|
+
const target = e.target as HTMLButtonElement;
|
|
56
|
+
const tr = target.closest("tr");
|
|
57
|
+
if (!tr) {
|
|
58
|
+
console.error("Not within a tr");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const index_a = this.getRowIndex(tr);
|
|
62
|
+
if (index_a < 0) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const index_b = index_a - 1;
|
|
66
|
+
this.swapRows(index_a, index_b);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
moveDown(e: MouseEvent) {
|
|
70
|
+
const target = e.target as HTMLButtonElement;
|
|
71
|
+
const tr = target.closest("tr");
|
|
72
|
+
if (!tr) {
|
|
73
|
+
console.error("Not within a tr");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const index_a = this.getRowIndex(tr);
|
|
77
|
+
if (index_a < 0) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const index_b = index_a + 1;
|
|
81
|
+
this.swapRows(index_a, index_b);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
reindex() {
|
|
85
|
+
let result = 0;
|
|
86
|
+
|
|
87
|
+
// assign proper number to each input within a tr by changing input names
|
|
88
|
+
for (const tr of Array.from(this.getTbody().children)) {
|
|
89
|
+
tr.querySelectorAll("input").forEach((input) => {
|
|
90
|
+
input.setAttribute(
|
|
91
|
+
"name",
|
|
92
|
+
(input.getAttribute("name") || "").replace(
|
|
93
|
+
/[(\d+)](?!.*\d)/, // last number in a regex
|
|
94
|
+
result.toString()
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
result++;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// disable up/down buttons for first/last items
|
|
102
|
+
// first reset all states to enabled
|
|
103
|
+
this.element
|
|
104
|
+
.querySelectorAll(
|
|
105
|
+
`[data-action="sealgen-table#moveUp"], [data-action="sealgen-table#moveDown"]`
|
|
106
|
+
)
|
|
107
|
+
.forEach((e: HTMLButtonElement) => (e.disabled = false));
|
|
108
|
+
// then disable the ones at the top and bottom
|
|
109
|
+
this.element
|
|
110
|
+
.querySelectorAll(
|
|
111
|
+
'tr:first-child [data-action="sealgen-table#moveUp"], tr:last-child [data-action="sealgen-table#moveDown"]'
|
|
112
|
+
)
|
|
113
|
+
.forEach((e: HTMLButtonElement) => (e.disabled = true));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -21,6 +21,7 @@ export type TableControlOptions<F extends Record<string, FormField>> = {
|
|
|
21
21
|
[field_name in keyof F]: FormControl;
|
|
22
22
|
};
|
|
23
23
|
allow_removing?: boolean;
|
|
24
|
+
allow_reordering?: boolean;
|
|
24
25
|
label_add?: string;
|
|
25
26
|
label: string;
|
|
26
27
|
label_remove?: string;
|
|
@@ -62,8 +63,8 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
62
63
|
| null,
|
|
63
64
|
row_index: number | string
|
|
64
65
|
) => {
|
|
65
|
-
return typeof row_index == "number"
|
|
66
|
-
|
|
66
|
+
return typeof row_index == "number" ?
|
|
67
|
+
{
|
|
67
68
|
data: {
|
|
68
69
|
raw_values:
|
|
69
70
|
(Object.fromEntries(
|
|
@@ -78,7 +79,7 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
78
79
|
field_messages: {},
|
|
79
80
|
},
|
|
80
81
|
}
|
|
81
|
-
:
|
|
82
|
+
: {};
|
|
82
83
|
};
|
|
83
84
|
|
|
84
85
|
const make_row = async (
|
|
@@ -89,7 +90,7 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
89
90
|
>;
|
|
90
91
|
}
|
|
91
92
|
| null,
|
|
92
|
-
row_index: number | string
|
|
93
|
+
row_index: number | string // when string, it's a placeholder, it will not parse as a number
|
|
93
94
|
) => {
|
|
94
95
|
return /* HTML */ tempstream`<tr>
|
|
95
96
|
${Promise.all(
|
|
@@ -108,33 +109,42 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
108
109
|
)
|
|
109
110
|
)}
|
|
110
111
|
${
|
|
111
|
-
this.options.allow_removing
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
112
|
+
this.options.allow_removing ?
|
|
113
|
+
/* HTML */ `<td>
|
|
114
|
+
<button onclick="this.closest('tr').remove()">
|
|
115
|
+
remove
|
|
116
|
+
</button>
|
|
117
|
+
<noscript>
|
|
118
|
+
<input
|
|
119
|
+
type="submit"
|
|
120
|
+
data-turbo-frame="${this.getFrameID()}"
|
|
121
|
+
value="${this.options.label_remove ||
|
|
122
|
+
"remove"}"
|
|
123
|
+
form="${fctx.form_id}"
|
|
124
|
+
formnovalidate
|
|
125
|
+
formaction="${this.getActionURL(
|
|
126
|
+
this.table_field.name,
|
|
127
|
+
{
|
|
128
|
+
remove: {
|
|
129
|
+
index: row_index,
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
)}"
|
|
133
|
+
/>
|
|
134
|
+
</noscript>
|
|
135
|
+
</td>`
|
|
136
|
+
: ""
|
|
137
|
+
}${
|
|
138
|
+
this.options.allow_reordering ?
|
|
139
|
+
/* HTML */ `<td>
|
|
140
|
+
<button data-action="sealgen-table#moveUp">
|
|
141
|
+
↑
|
|
142
|
+
</button>
|
|
143
|
+
<button data-action="sealgen-table#moveDown">
|
|
144
|
+
↓
|
|
145
|
+
</button>
|
|
146
|
+
</td>`
|
|
147
|
+
: ""
|
|
138
148
|
}
|
|
139
149
|
</tr>`;
|
|
140
150
|
};
|
|
@@ -145,60 +155,62 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
145
155
|
"form-input__wrapper",
|
|
146
156
|
"form-input__wrapper--type--table",
|
|
147
157
|
"form-input__wrapper--options-count--" + rows.length.toString(),
|
|
148
|
-
...(rows.length >= 5
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
...(rows.length >= 10
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
...(rows.length >= 15
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
...(rows.length >= 20
|
|
158
|
-
|
|
159
|
-
|
|
158
|
+
...(rows.length >= 5 ?
|
|
159
|
+
["form-input__wrapper--options-count--5-or-more"]
|
|
160
|
+
: []),
|
|
161
|
+
...(rows.length >= 10 ?
|
|
162
|
+
["form-input__wrapper--options-count--10-or-more"]
|
|
163
|
+
: []),
|
|
164
|
+
...(rows.length >= 15 ?
|
|
165
|
+
["form-input__wrapper--options-count--15-or-more"]
|
|
166
|
+
: []),
|
|
167
|
+
...(rows.length >= 20 ?
|
|
168
|
+
["form-input__wrapper--options-count--20-or-more"]
|
|
169
|
+
: []),
|
|
160
170
|
].join(" ")}"
|
|
161
171
|
>
|
|
162
172
|
<label>${this.options.label || this.table_field.label}</label>
|
|
163
|
-
<div
|
|
164
|
-
|
|
173
|
+
<div
|
|
174
|
+
class="table__wrapper"
|
|
175
|
+
data-controller="sealgen-table"
|
|
176
|
+
data-table-placeholder="${TABLE_COLUMN_FIELD_INDEX_PLACEHOLDER}"
|
|
177
|
+
>
|
|
178
|
+
<table data-sealgen-table-target="table">
|
|
165
179
|
<tbody>
|
|
166
180
|
${rows?.map((row, index) => make_row(row, index))}
|
|
167
181
|
</tbody>
|
|
168
182
|
</table>
|
|
169
|
-
<template>
|
|
183
|
+
<template data-sealgen-table-target="template">
|
|
170
184
|
${make_row(null, TABLE_COLUMN_FIELD_INDEX_PLACEHOLDER)}
|
|
171
185
|
</template>
|
|
172
186
|
|
|
173
187
|
${
|
|
174
188
|
/* because of https://github.com/ljharb/qs/issues/252 we have to
|
|
175
189
|
put the indexes in the field names */
|
|
176
|
-
this.options.allow_adding
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
</noscript>`
|
|
201
|
-
: ""
|
|
190
|
+
this.options.allow_adding ?
|
|
191
|
+
/* HTML */ `<button
|
|
192
|
+
data-action="click->sealgen-table#addRow"
|
|
193
|
+
>
|
|
194
|
+
${this.options.label_add || "add"}
|
|
195
|
+
</button>
|
|
196
|
+
<noscript>
|
|
197
|
+
<input
|
|
198
|
+
type="submit"
|
|
199
|
+
value="add"
|
|
200
|
+
form="${fctx.form_id}"
|
|
201
|
+
formnovalidate
|
|
202
|
+
formaction="${this.getActionURL(
|
|
203
|
+
this.table_field.name,
|
|
204
|
+
{
|
|
205
|
+
insert: {
|
|
206
|
+
index: rows.length,
|
|
207
|
+
value: {},
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
)}"
|
|
211
|
+
/>
|
|
212
|
+
</noscript>`
|
|
213
|
+
: ""
|
|
202
214
|
}
|
|
203
215
|
</div>
|
|
204
216
|
</turbo-frame>`;
|
|
@@ -221,11 +233,13 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
221
233
|
const field_name = ctx.$body.field_name;
|
|
222
234
|
if (
|
|
223
235
|
!is(action, predicates.object) ||
|
|
224
|
-
!is(field_name, predicates.string)
|
|
236
|
+
!is(field_name, predicates.string) ||
|
|
237
|
+
field_name !== this.table_field.name
|
|
225
238
|
) {
|
|
226
239
|
await next();
|
|
227
240
|
return;
|
|
228
241
|
}
|
|
242
|
+
|
|
229
243
|
if (
|
|
230
244
|
this.options.allow_adding &&
|
|
231
245
|
hasShape(
|
|
@@ -235,16 +249,13 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
235
249
|
action
|
|
236
250
|
)
|
|
237
251
|
) {
|
|
238
|
-
if (field_name !== this.table_field.name) {
|
|
239
|
-
await next();
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
252
|
if (!ctx.$body[this.table_field.name]) {
|
|
243
253
|
ctx.$body[this.table_field.name] = {};
|
|
244
254
|
}
|
|
245
255
|
(ctx.$body[this.table_field.name] as any)[action.insert.index] =
|
|
246
256
|
await this.options.make_new_row(ctx);
|
|
247
257
|
}
|
|
258
|
+
|
|
248
259
|
if (
|
|
249
260
|
hasShape(
|
|
250
261
|
{
|
|
@@ -253,10 +264,6 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
253
264
|
action
|
|
254
265
|
)
|
|
255
266
|
) {
|
|
256
|
-
if (field_name !== this.table_field.name) {
|
|
257
|
-
await next();
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
267
|
if (!ctx.$body[this.table_field.name]) {
|
|
261
268
|
ctx.$body[this.table_field.name] = {};
|
|
262
269
|
}
|
|
@@ -270,6 +277,7 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
270
277
|
)
|
|
271
278
|
);
|
|
272
279
|
}
|
|
280
|
+
|
|
273
281
|
ctx.override_reaction = {
|
|
274
282
|
action: "stay",
|
|
275
283
|
content: await form.render(
|
|
@@ -310,4 +318,9 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
|
|
|
310
318
|
this.options.label_add = value;
|
|
311
319
|
return this;
|
|
312
320
|
}
|
|
321
|
+
|
|
322
|
+
setAllowReordering(value: boolean) {
|
|
323
|
+
this.options.allow_reordering = value;
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
313
326
|
}
|
package/src/mount.ts
CHANGED
|
@@ -25,16 +25,6 @@ export function mount(
|
|
|
25
25
|
): void {
|
|
26
26
|
const raw_url = typeof url === "string" ? url : url.rawURL;
|
|
27
27
|
|
|
28
|
-
const imageRouter = new KoaResponsiveImageRouter({
|
|
29
|
-
staticPath: "/tmp",
|
|
30
|
-
thumbnailSize: 10,
|
|
31
|
-
cacheManagerResolutionThreshold: 10,
|
|
32
|
-
imageStoragePath: "/tmp",
|
|
33
|
-
smartCropStoragePath: "/tmp",
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const fileManager = new FileManager("/tmp", "/uploaded_files");
|
|
37
|
-
|
|
38
28
|
const args =
|
|
39
29
|
use_dummy_app ?
|
|
40
30
|
[
|
|
@@ -44,8 +34,14 @@ export function mount(
|
|
|
44
34
|
// regard
|
|
45
35
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
46
36
|
ctx.$app = {
|
|
47
|
-
fileManager,
|
|
48
|
-
imageRouter
|
|
37
|
+
fileManager: new FileManager("/tmp", "/uploaded_files"),
|
|
38
|
+
imageRouter: new KoaResponsiveImageRouter({
|
|
39
|
+
staticPath: "/tmp",
|
|
40
|
+
thumbnailSize: 10,
|
|
41
|
+
cacheManagerResolutionThreshold: 10,
|
|
42
|
+
imageStoragePath: "/tmp",
|
|
43
|
+
smartCropStoragePath: "/tmp",
|
|
44
|
+
}),
|
|
49
45
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
50
46
|
Logger: new Proxy(
|
|
51
47
|
{},
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Controller } from "stimulus";
|
|
2
|
-
class table_add_button_stimulus_default extends Controller {
|
|
3
|
-
constructor() {
|
|
4
|
-
super(...arguments);
|
|
5
|
-
this.defaultRowsNumber = 20;
|
|
6
|
-
}
|
|
7
|
-
static {
|
|
8
|
-
this.targets = ["textarea"];
|
|
9
|
-
}
|
|
10
|
-
addRow() {
|
|
11
|
-
const template = this.element.closest("turbo-frame")?.querySelector("template");
|
|
12
|
-
if (!template) {
|
|
13
|
-
throw new Error("Couldn't find template");
|
|
14
|
-
}
|
|
15
|
-
const tbody = this.element.closest("turbo-frame")?.querySelector("tbody");
|
|
16
|
-
if (!tbody) {
|
|
17
|
-
throw new Error("tbody not found");
|
|
18
|
-
}
|
|
19
|
-
const orig_template = template.innerHTML;
|
|
20
|
-
const placeholder = this.element.getAttribute("data-table-placeholder");
|
|
21
|
-
if (!placeholder) {
|
|
22
|
-
throw new Error("Could find placeholder attribute");
|
|
23
|
-
}
|
|
24
|
-
template.innerHTML = orig_template.replaceAll(
|
|
25
|
-
placeholder,
|
|
26
|
-
String(tbody.querySelectorAll("tr").length)
|
|
27
|
-
);
|
|
28
|
-
tbody.appendChild(template.cloneNode(true).content);
|
|
29
|
-
template.innerHTML = orig_template;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export {
|
|
33
|
-
table_add_button_stimulus_default as default
|
|
34
|
-
};
|
|
35
|
-
//# sourceMappingURL=table-add-button.stimulus.js.map
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/// <reference lib="dom" />
|
|
2
|
-
import { Controller } from "stimulus";
|
|
3
|
-
|
|
4
|
-
export default class extends Controller {
|
|
5
|
-
static targets = ["textarea"];
|
|
6
|
-
defaultRowsNumber = 20;
|
|
7
|
-
|
|
8
|
-
addRow() {
|
|
9
|
-
const template = this.element
|
|
10
|
-
.closest("turbo-frame")
|
|
11
|
-
?.querySelector("template");
|
|
12
|
-
if (!template) {
|
|
13
|
-
throw new Error("Couldn't find template");
|
|
14
|
-
}
|
|
15
|
-
const tbody = this.element
|
|
16
|
-
.closest("turbo-frame")
|
|
17
|
-
?.querySelector("tbody");
|
|
18
|
-
if (!tbody) {
|
|
19
|
-
throw new Error("tbody not found");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const orig_template = template.innerHTML;
|
|
23
|
-
const placeholder = this.element.getAttribute("data-table-placeholder");
|
|
24
|
-
if (!placeholder) {
|
|
25
|
-
throw new Error("Could find placeholder attribute");
|
|
26
|
-
}
|
|
27
|
-
template.innerHTML = orig_template.replaceAll(
|
|
28
|
-
placeholder,
|
|
29
|
-
String(tbody.querySelectorAll("tr").length)
|
|
30
|
-
);
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
32
|
-
tbody.appendChild((template.cloneNode(true) as any).content);
|
|
33
|
-
template.innerHTML = orig_template;
|
|
34
|
-
}
|
|
35
|
-
}
|