@schukai/monster 3.73.3 → 3.73.5
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +15 -6
- package/package.json +1 -1
- package/source/components/datatable/dataset.mjs +23 -16
- package/source/components/datatable/filter.mjs +1 -0
- package/source/components/datatable/save-button.mjs +268 -261
- package/source/components/form/context-error.mjs +0 -1
- package/source/components/form/form.mjs +160 -314
- package/source/dom/customelement.mjs +9 -0
- package/source/dom/updater.mjs +10 -5
- package/source/types/version.mjs +1 -1
- package/test/cases/monster.mjs +1 -1
- package/test/web/test.html +2 -2
- package/test/web/tests.js +1989 -1799
@@ -12,33 +12,33 @@
|
|
12
12
|
* SPDX-License-Identifier: AGPL-3.0
|
13
13
|
*/
|
14
14
|
|
15
|
-
import {
|
16
|
-
import {
|
17
|
-
import {
|
18
|
-
import {
|
15
|
+
import {instanceSymbol, internalSymbol} from "../../constants.mjs";
|
16
|
+
import {diff} from "../../data/diff.mjs";
|
17
|
+
import {addAttributeToken} from "../../dom/attributes.mjs";
|
18
|
+
import {ATTRIBUTE_ERRORMESSAGE} from "../../dom/constants.mjs";
|
19
19
|
import {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
assembleMethodSymbol,
|
21
|
+
CustomElement,
|
22
|
+
attributeObserverSymbol,
|
23
|
+
registerCustomElement,
|
24
24
|
} from "../../dom/customelement.mjs";
|
25
|
-
import {
|
26
|
-
import {
|
27
|
-
import {
|
28
|
-
import {
|
29
|
-
import {
|
30
|
-
import {
|
31
|
-
import {
|
32
|
-
import {
|
33
|
-
import {
|
34
|
-
import {
|
25
|
+
import {findElementWithSelectorUpwards} from "../../dom/util.mjs";
|
26
|
+
import {isString, isArray} from "../../types/is.mjs";
|
27
|
+
import {Observer} from "../../types/observer.mjs";
|
28
|
+
import {TokenList} from "../../types/tokenlist.mjs";
|
29
|
+
import {clone} from "../../util/clone.mjs";
|
30
|
+
import {State} from "../form/types/state.mjs";
|
31
|
+
import {ATTRIBUTE_DATASOURCE_SELECTOR} from "./constants.mjs";
|
32
|
+
import {Datasource} from "./datasource.mjs";
|
33
|
+
import {BadgeStyleSheet} from "../stylesheet/badge.mjs";
|
34
|
+
import {SaveButtonStyleSheet} from "./stylesheet/save-button.mjs";
|
35
35
|
|
36
36
|
import {
|
37
|
-
|
38
|
-
|
37
|
+
handleDataSourceChanges,
|
38
|
+
datasourceLinkedElementSymbol,
|
39
39
|
} from "./util.mjs";
|
40
40
|
|
41
|
-
export {
|
41
|
+
export {SaveButton};
|
42
42
|
|
43
43
|
/**
|
44
44
|
* @private
|
@@ -46,6 +46,12 @@ export { SaveButton };
|
|
46
46
|
*/
|
47
47
|
const stateButtonElementSymbol = Symbol("stateButtonElement");
|
48
48
|
|
49
|
+
/**
|
50
|
+
* @private
|
51
|
+
* @type {symbol}
|
52
|
+
*/
|
53
|
+
const originValuesSymbol = Symbol("originValues");
|
54
|
+
|
49
55
|
/**
|
50
56
|
* @private
|
51
57
|
* @type {symbol}
|
@@ -53,185 +59,185 @@ const stateButtonElementSymbol = Symbol("stateButtonElement");
|
|
53
59
|
const badgeElementSymbol = Symbol("badgeElement");
|
54
60
|
|
55
61
|
class SaveButton extends CustomElement {
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
62
|
+
/**
|
63
|
+
* This method is called by the `instanceof` operator.
|
64
|
+
* @returns {symbol}
|
65
|
+
*/
|
66
|
+
static get [instanceSymbol]() {
|
67
|
+
return Symbol.for(
|
68
|
+
"@schukai/monster/components/datasource/save-button@@instance",
|
69
|
+
);
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
* To set the options via the html tag the attribute `data-monster-options` must be used.
|
74
|
+
* @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
|
75
|
+
*
|
76
|
+
* The individual configuration values can be found in the table.
|
77
|
+
*
|
78
|
+
* @property {Object} templates Template definitions
|
79
|
+
* @property {string} templates.main Main template
|
80
|
+
* @property {object} datasource The datasource
|
81
|
+
* @property {string} datasource.selector The selector of the datasource
|
82
|
+
* @property {string} labels.button The button label
|
83
|
+
* @property {Object} classes The classes
|
84
|
+
* @property {string} classes.bar The bar class
|
85
|
+
* @property {string} classes.badge The badge class
|
86
|
+
* @property {Array} ignoreChanges The ignore changes (regex)
|
87
|
+
* @property {Array} data The data
|
88
|
+
* @return {Object}
|
89
|
+
*/
|
90
|
+
get defaults() {
|
91
|
+
const obj = Object.assign({}, super.defaults, {
|
92
|
+
templates: {
|
93
|
+
main: getTemplate(),
|
94
|
+
},
|
95
|
+
|
96
|
+
labels: {
|
97
|
+
button: "save",
|
98
|
+
},
|
99
|
+
|
100
|
+
classes: {
|
101
|
+
bar: "monster-button-primary",
|
102
|
+
badge: "monster-badge-secondary hidden",
|
103
|
+
},
|
104
|
+
|
105
|
+
datasource: {
|
106
|
+
selector: null,
|
107
|
+
},
|
108
|
+
|
109
|
+
changes: "0",
|
110
|
+
|
111
|
+
ignoreChanges: [],
|
112
|
+
|
113
|
+
data: {},
|
114
|
+
|
115
|
+
disabled: false,
|
116
|
+
});
|
117
|
+
|
118
|
+
updateOptionsFromArguments.call(this, obj);
|
119
|
+
return obj;
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
*
|
124
|
+
* @return {string}
|
125
|
+
*/
|
126
|
+
static getTag() {
|
127
|
+
return "monster-datasource-save-button";
|
128
|
+
}
|
129
|
+
|
130
|
+
/**
|
131
|
+
* This method is responsible for assembling the component.
|
132
|
+
*
|
133
|
+
* It calls the parent's assemble method first, then initializes control references and event handlers.
|
134
|
+
* If the `datasource.selector` option is provided and is a string, it searches for the corresponding
|
135
|
+
* element in the DOM using that selector.
|
136
|
+
*
|
137
|
+
* If the selector matches exactly one element, it checks if the element is an instance of the `Datasource` class.
|
138
|
+
*
|
139
|
+
* If it is, the component's `datasourceLinkedElementSymbol` property is set to the element, and the component
|
140
|
+
* attaches an observer to the datasource's changes.
|
141
|
+
*
|
142
|
+
* The observer is a function that calls the `handleDataSourceChanges` method in the context of the component.
|
143
|
+
* Additionally, the component attaches an observer to itself, which also calls the `handleDataSourceChanges`
|
144
|
+
* method in the component's context.
|
145
|
+
*/
|
146
|
+
[assembleMethodSymbol]() {
|
147
|
+
super[assembleMethodSymbol]();
|
148
|
+
const self = this;
|
149
|
+
|
150
|
+
initControlReferences.call(this);
|
151
|
+
initEventHandler.call(this);
|
152
|
+
|
153
|
+
const selector = this.getOption("datasource.selector");
|
154
|
+
|
155
|
+
if (isString(selector)) {
|
156
|
+
const element = findElementWithSelectorUpwards(this, selector);
|
157
|
+
if (element === null) {
|
158
|
+
throw new Error("the selector must match exactly one element");
|
159
|
+
}
|
160
|
+
|
161
|
+
if (!(element instanceof Datasource)) {
|
162
|
+
throw new TypeError("the element must be a datasource");
|
163
|
+
}
|
164
|
+
|
165
|
+
this[datasourceLinkedElementSymbol] = element;
|
166
|
+
element.datasource.attachObserver(
|
167
|
+
new Observer(handleDataSourceChanges.bind(this)),
|
168
|
+
);
|
169
|
+
|
170
|
+
self[originValuesSymbol] = null;
|
171
|
+
|
172
|
+
element.datasource.attachObserver(
|
173
|
+
new Observer(function () {
|
174
|
+
if (!self[originValuesSymbol]) {
|
175
|
+
self[originValuesSymbol] = clone(self[datasourceLinkedElementSymbol].data);
|
176
|
+
}
|
177
|
+
|
178
|
+
const currentValues = this.getRealSubject();
|
179
|
+
const ignoreChanges = self.getOption("ignoreChanges");
|
180
|
+
|
181
|
+
const result = diff(self[originValuesSymbol], currentValues);
|
182
|
+
if (isArray(ignoreChanges) && ignoreChanges.length > 0) {
|
183
|
+
const itemsToRemove = [];
|
184
|
+
for (const item of result) {
|
185
|
+
for (const ignorePattern of ignoreChanges) {
|
186
|
+
const p = new RegExp(ignorePattern);
|
187
|
+
if (p.test(item.path)) {
|
188
|
+
itemsToRemove.push(item);
|
189
|
+
break;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
for (const itemToRemove of itemsToRemove) {
|
195
|
+
const index = result.indexOf(itemToRemove);
|
196
|
+
if (index > -1) {
|
197
|
+
result.splice(index, 1);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
if (isArray(result) && result.length > 0) {
|
203
|
+
self[stateButtonElementSymbol].setState("changed");
|
204
|
+
self[stateButtonElementSymbol].setOption("disabled", false);
|
205
|
+
self.setOption("changes", result.length);
|
206
|
+
self.setOption(
|
207
|
+
"classes.badge",
|
208
|
+
new TokenList(self.getOption("classes.badge"))
|
209
|
+
.remove("hidden")
|
210
|
+
.toString(),
|
211
|
+
);
|
212
|
+
} else {
|
213
|
+
self[stateButtonElementSymbol].removeState();
|
214
|
+
self[stateButtonElementSymbol].setOption("disabled", true);
|
215
|
+
self.setOption("changes", 0);
|
216
|
+
self.setOption(
|
217
|
+
"classes.badge",
|
218
|
+
new TokenList(self.getOption("classes.badge"))
|
219
|
+
.add("hidden")
|
220
|
+
.toString(),
|
221
|
+
);
|
222
|
+
}
|
223
|
+
}),
|
224
|
+
);
|
225
|
+
}
|
226
|
+
|
227
|
+
this.attachObserver(
|
228
|
+
new Observer(() => {
|
229
|
+
handleDataSourceChanges.call(this);
|
230
|
+
}),
|
231
|
+
);
|
232
|
+
}
|
233
|
+
|
234
|
+
/**
|
235
|
+
*
|
236
|
+
* @return [CSSStyleSheet]
|
237
|
+
*/
|
238
|
+
static getCSSStyleSheet() {
|
239
|
+
return [SaveButtonStyleSheet, BadgeStyleSheet];
|
240
|
+
}
|
235
241
|
}
|
236
242
|
|
237
243
|
/**
|
@@ -239,77 +245,78 @@ class SaveButton extends CustomElement {
|
|
239
245
|
* @return {Monster.Components.Datatable.Form}
|
240
246
|
*/
|
241
247
|
function initControlReferences() {
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
248
|
+
if (!this.shadowRoot) {
|
249
|
+
throw new Error("no shadow-root is defined");
|
250
|
+
}
|
251
|
+
|
252
|
+
this[stateButtonElementSymbol] = this.shadowRoot.querySelector(
|
253
|
+
"[data-monster-role=state-button]",
|
254
|
+
);
|
255
|
+
|
256
|
+
this[badgeElementSymbol] = this.shadowRoot.querySelector(
|
257
|
+
"[data-monster-role=badge]",
|
258
|
+
);
|
259
|
+
|
260
|
+
if (this[stateButtonElementSymbol]) {
|
261
|
+
setTimeout(() => {
|
262
|
+
const states = {
|
263
|
+
changed: new State(
|
264
|
+
"changed",
|
265
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cloud-arrow-up" viewBox="0 0 16 16">\n' +
|
266
|
+
' <path fill-rule="evenodd" d="M7.646 5.146a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 6.707V10.5a.5.5 0 0 1-1 0V6.707L6.354 7.854a.5.5 0 1 1-.708-.708z"/>\n' +
|
267
|
+
' <path d="M4.406 3.342A5.53 5.53 0 0 1 8 2c2.69 0 4.923 2 5.166 4.579C14.758 6.804 16 8.137 16 9.773 16 11.569 14.502 13 12.687 13H3.781C1.708 13 0 11.366 0 9.318c0-1.763 1.266-3.223 2.942-3.593.143-.863.698-1.723 1.464-2.383m.653.757c-.757.653-1.153 1.44-1.153 2.056v.448l-.445.049C2.064 6.805 1 7.952 1 9.318 1 10.785 2.23 12 3.781 12h8.906C13.98 12 15 10.988 15 9.773c0-1.216-1.02-2.228-2.313-2.228h-.5v-.5C12.188 4.825 10.328 3 8 3a4.53 4.53 0 0 0-2.941 1.1z"/>\n' +
|
268
|
+
"</svg>",
|
269
|
+
),
|
270
|
+
};
|
271
|
+
|
272
|
+
this[stateButtonElementSymbol].removeState();
|
273
|
+
this[stateButtonElementSymbol].setOption("disabled", "disabled");
|
274
|
+
this[stateButtonElementSymbol].setOption("states", states);
|
275
|
+
this[stateButtonElementSymbol].setOption(
|
276
|
+
"labels.button",
|
277
|
+
this.getOption("labels.button"),
|
278
|
+
);
|
279
|
+
}, 1);
|
280
|
+
}
|
281
|
+
|
282
|
+
return this;
|
277
283
|
}
|
278
284
|
|
279
285
|
/**
|
280
286
|
* @private
|
281
287
|
*/
|
282
288
|
function initEventHandler() {
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
289
|
+
setTimeout(() => {
|
290
|
+
this[stateButtonElementSymbol].setOption("actions.click", () => {
|
291
|
+
this[datasourceLinkedElementSymbol]
|
292
|
+
.write()
|
293
|
+
.then(() => {
|
294
|
+
this[originValuesSymbol] = null;
|
295
|
+
this[stateButtonElementSymbol].removeState();
|
296
|
+
this[stateButtonElementSymbol].setOption("disabled", true);
|
297
|
+
this.setOption("changes", 0);
|
298
|
+
this.setOption(
|
299
|
+
"classes.badge",
|
300
|
+
new TokenList(this.getOption("classes.badge"))
|
301
|
+
.add("hidden")
|
302
|
+
.toString(),
|
303
|
+
);
|
304
|
+
})
|
305
|
+
.catch((error) => {
|
306
|
+
addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, error.toString());
|
307
|
+
});
|
308
|
+
});
|
309
|
+
}, 1);
|
303
310
|
}
|
304
311
|
|
305
312
|
/**
|
306
313
|
* @param {Object} options
|
307
314
|
*/
|
308
315
|
function updateOptionsFromArguments(options) {
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
316
|
+
const selector = this.getAttribute(ATTRIBUTE_DATASOURCE_SELECTOR);
|
317
|
+
if (selector) {
|
318
|
+
options.datasource.selector = selector;
|
319
|
+
}
|
313
320
|
}
|
314
321
|
|
315
322
|
/**
|
@@ -317,8 +324,8 @@ function updateOptionsFromArguments(options) {
|
|
317
324
|
* @return {string}
|
318
325
|
*/
|
319
326
|
function getTemplate() {
|
320
|
-
|
321
|
-
|
327
|
+
// language=HTML
|
328
|
+
return `
|
322
329
|
<div data-monster-role="control" part="control"
|
323
330
|
data-monster-attributes="disabled path:disabled | if:true">
|
324
331
|
<monster-state-button data-monster-role="state-button">save</monster-state-button>
|