@schukai/monster 4.38.2 → 4.38.4
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 +20 -0
- package/package.json +1 -1
- package/source/components/datatable/filter.mjs +1342 -1321
- package/source/components/datatable/save-button.mjs +331 -328
- package/source/components/form/select.mjs +2854 -2853
- package/source/components/form/toggle-switch.mjs +359 -359
- package/source/i18n/translations.mjs +215 -205
- 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 +21 -11
@@ -17,9 +17,9 @@ import { diff } from "../../data/diff.mjs";
|
|
17
17
|
import { addAttributeToken } from "../../dom/attributes.mjs";
|
18
18
|
import { ATTRIBUTE_ERRORMESSAGE } from "../../dom/constants.mjs";
|
19
19
|
import {
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
assembleMethodSymbol,
|
21
|
+
CustomElement,
|
22
|
+
registerCustomElement,
|
23
23
|
} from "../../dom/customelement.mjs";
|
24
24
|
import { findElementWithSelectorUpwards } from "../../dom/util.mjs";
|
25
25
|
import { isString, isArray } from "../../types/is.mjs";
|
@@ -35,8 +35,8 @@ import { SaveButtonStyleSheet } from "./stylesheet/save-button.mjs";
|
|
35
35
|
import "../form/state-button.mjs";
|
36
36
|
|
37
37
|
import {
|
38
|
-
|
39
|
-
|
38
|
+
handleDataSourceChanges,
|
39
|
+
datasourceLinkedElementSymbol,
|
40
40
|
} from "./util.mjs";
|
41
41
|
import { getLocaleOfDocument } from "../../dom/locale.mjs";
|
42
42
|
|
@@ -73,270 +73,270 @@ const badgeElementSymbol = Symbol("badgeElement");
|
|
73
73
|
* @summary This is a save button component that can be used to save changes to a datasource.
|
74
74
|
*/
|
75
75
|
class SaveButton extends CustomElement {
|
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
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
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
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
76
|
+
/**
|
77
|
+
* This method is called by the `instanceof` operator.
|
78
|
+
* @return {symbol}
|
79
|
+
*/
|
80
|
+
static get [instanceSymbol]() {
|
81
|
+
return Symbol.for(
|
82
|
+
"@schukai/monster/components/datatable/save-button@@instance",
|
83
|
+
);
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* To set the options via the HTML tag, the attribute `data-monster-options` must be used.
|
88
|
+
* @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
|
89
|
+
*
|
90
|
+
* The individual configuration values can be found in the table.
|
91
|
+
*
|
92
|
+
* @property {Object} templates Template definitions
|
93
|
+
* @property {string} templates.main Main template
|
94
|
+
* @property {object} datasource The datasource
|
95
|
+
* @property {string} datasource.selector The selector of the datasource
|
96
|
+
* @property {string} labels.button The button label
|
97
|
+
* @property {Object} classes The classes
|
98
|
+
* @property {string} classes.bar The bar class
|
99
|
+
* @property {string} classes.badge The badge class
|
100
|
+
* @property {Array} ignoreChanges The ignore changes (regex)
|
101
|
+
* @property {Array} data The data
|
102
|
+
* @property {boolean} disabled The disabled state
|
103
|
+
* @property {string} logLevel The log level (off, debug)
|
104
|
+
* @return {Object}
|
105
|
+
*/
|
106
|
+
get defaults() {
|
107
|
+
const obj = Object.assign({}, super.defaults, {
|
108
|
+
templates: {
|
109
|
+
main: getTemplate(),
|
110
|
+
},
|
111
|
+
|
112
|
+
labels: getTranslations(),
|
113
|
+
|
114
|
+
classes: {
|
115
|
+
bar: "monster-button-primary",
|
116
|
+
badge: "monster-badge-secondary hidden",
|
117
|
+
},
|
118
|
+
|
119
|
+
datasource: {
|
120
|
+
selector: null,
|
121
|
+
},
|
122
|
+
|
123
|
+
changes: "0",
|
124
|
+
|
125
|
+
ignoreChanges: [],
|
126
|
+
|
127
|
+
data: {},
|
128
|
+
|
129
|
+
disabled: false,
|
130
|
+
|
131
|
+
logLevel: "off",
|
132
|
+
});
|
133
|
+
|
134
|
+
updateOptionsFromArguments.call(this, obj);
|
135
|
+
return obj;
|
136
|
+
}
|
137
|
+
|
138
|
+
/**
|
139
|
+
*
|
140
|
+
* @return {string}
|
141
|
+
*/
|
142
|
+
static getTag() {
|
143
|
+
return "monster-datasource-save-button";
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* This method is responsible for assembling the component.
|
148
|
+
*
|
149
|
+
* It calls the parent's assemble method first, then initializes control references and event handlers.
|
150
|
+
* If the `datasource.selector` option is provided and is a string, it searches for the corresponding
|
151
|
+
* element in the DOM using that selector.
|
152
|
+
*
|
153
|
+
* If the selector matches exactly one element, it checks if the element is an instance of the `Datasource` class.
|
154
|
+
*
|
155
|
+
* If it is, the component's `datasourceLinkedElementSymbol` property is set to the element, and the component
|
156
|
+
* attaches an observer to the datasource's changes.
|
157
|
+
*
|
158
|
+
* The observer is a function that calls the `handleDataSourceChanges` method in the context of the component.
|
159
|
+
* Additionally, the component attaches an observer to itself, which also calls the `handleDataSourceChanges`
|
160
|
+
* method in the component's context.
|
161
|
+
*/
|
162
|
+
[assembleMethodSymbol]() {
|
163
|
+
super[assembleMethodSymbol]();
|
164
|
+
const self = this;
|
165
|
+
|
166
|
+
initControlReferences.call(this);
|
167
|
+
initEventHandler.call(this);
|
168
|
+
|
169
|
+
const selector = this.getOption("datasource.selector");
|
170
|
+
|
171
|
+
if (isString(selector)) {
|
172
|
+
const element = findElementWithSelectorUpwards(this, selector);
|
173
|
+
if (element === null) {
|
174
|
+
throw new Error("the selector must match exactly one element");
|
175
|
+
}
|
176
|
+
|
177
|
+
if (!(element instanceof Datasource)) {
|
178
|
+
throw new TypeError("the element must be a datasource");
|
179
|
+
}
|
180
|
+
|
181
|
+
if (element instanceof RestDatasource) {
|
182
|
+
element.addEventListener("monster-datasource-fetch", (event) => {
|
183
|
+
self[originValuesSymbol] = null;
|
184
|
+
});
|
185
|
+
}
|
186
|
+
|
187
|
+
this[datasourceLinkedElementSymbol] = element;
|
188
|
+
element.datasource.attachObserver(
|
189
|
+
new Observer(handleDataSourceChanges.bind(this)),
|
190
|
+
);
|
191
|
+
|
192
|
+
self[originValuesSymbol] = null;
|
193
|
+
|
194
|
+
element.datasource.attachObserver(
|
195
|
+
new Observer(function () {
|
196
|
+
if (!self[originValuesSymbol]) {
|
197
|
+
self[originValuesSymbol] = clone(
|
198
|
+
self[datasourceLinkedElementSymbol].data,
|
199
|
+
);
|
200
|
+
}
|
201
|
+
|
202
|
+
const currentValues = this.getRealSubject();
|
203
|
+
const ignoreChanges = self.getOption("ignoreChanges");
|
204
|
+
|
205
|
+
const result = diff(self[originValuesSymbol], currentValues);
|
206
|
+
if (self.getOption("logLevel") === "debug") {
|
207
|
+
console.groupCollapsed("SaveButton");
|
208
|
+
console.log(
|
209
|
+
"originValues",
|
210
|
+
JSON.parse(JSON.stringify(currentValues)),
|
211
|
+
);
|
212
|
+
console.log("result of diff", result);
|
213
|
+
console.log("ignoreChanges", ignoreChanges);
|
214
|
+
|
215
|
+
if (isArray(result) && result.length > 0) {
|
216
|
+
const formattedDiff = result.map((change) => ({
|
217
|
+
Operator: change?.operator,
|
218
|
+
Path: change?.path?.join("."),
|
219
|
+
"First Value": change?.first?.value,
|
220
|
+
"First Type": change?.first?.type,
|
221
|
+
"Second Value": change?.second?.value,
|
222
|
+
"Second Type": change?.second?.type,
|
223
|
+
}));
|
224
|
+
|
225
|
+
console.table(formattedDiff);
|
226
|
+
} else {
|
227
|
+
console.log("There are no changes to save");
|
228
|
+
}
|
229
|
+
console.groupEnd();
|
230
|
+
}
|
231
|
+
|
232
|
+
if (isArray(ignoreChanges) && ignoreChanges.length > 0) {
|
233
|
+
const itemsToRemove = [];
|
234
|
+
for (const item of result) {
|
235
|
+
for (const ignorePattern of ignoreChanges) {
|
236
|
+
const p = new RegExp(ignorePattern);
|
237
|
+
|
238
|
+
let matchPath = item.path;
|
239
|
+
if (isArray(item.path)) {
|
240
|
+
matchPath = item.path.join(".");
|
241
|
+
}
|
242
|
+
|
243
|
+
if (p.test(matchPath)) {
|
244
|
+
itemsToRemove.push(item);
|
245
|
+
break;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
for (const itemToRemove of itemsToRemove) {
|
251
|
+
const index = result.indexOf(itemToRemove);
|
252
|
+
if (index > -1) {
|
253
|
+
result.splice(index, 1);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
if (isArray(result) && result.length > 0) {
|
259
|
+
self[stateButtonElementSymbol].setState("changed");
|
260
|
+
self[stateButtonElementSymbol].setOption("disabled", false);
|
261
|
+
self.setOption("changes", result.length);
|
262
|
+
self.setOption(
|
263
|
+
"classes.badge",
|
264
|
+
new TokenList(self.getOption("classes.badge"))
|
265
|
+
.remove("hidden")
|
266
|
+
.toString(),
|
267
|
+
);
|
268
|
+
} else {
|
269
|
+
self[stateButtonElementSymbol].removeState();
|
270
|
+
self[stateButtonElementSymbol].setOption("disabled", true);
|
271
|
+
self.setOption("changes", 0);
|
272
|
+
self.setOption(
|
273
|
+
"classes.badge",
|
274
|
+
new TokenList(self.getOption("classes.badge"))
|
275
|
+
.add("hidden")
|
276
|
+
.toString(),
|
277
|
+
);
|
278
|
+
}
|
279
|
+
}),
|
280
|
+
);
|
281
|
+
}
|
282
|
+
|
283
|
+
this.attachObserver(
|
284
|
+
new Observer(() => {
|
285
|
+
handleDataSourceChanges.call(this);
|
286
|
+
}),
|
287
|
+
);
|
288
|
+
}
|
289
|
+
|
290
|
+
/**
|
291
|
+
*
|
292
|
+
* @return [CSSStyleSheet]
|
293
|
+
*/
|
294
|
+
static getCSSStyleSheet() {
|
295
|
+
return [SaveButtonStyleSheet, BadgeStyleSheet];
|
296
|
+
}
|
297
297
|
}
|
298
298
|
|
299
299
|
function getTranslations() {
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
300
|
+
const locale = getLocaleOfDocument();
|
301
|
+
switch (locale.language) {
|
302
|
+
case "de":
|
303
|
+
return {
|
304
|
+
button: "Speichern",
|
305
|
+
};
|
306
|
+
case "fr":
|
307
|
+
return {
|
308
|
+
button: "Enregistrer",
|
309
|
+
};
|
310
|
+
case "sp":
|
311
|
+
return {
|
312
|
+
button: "Guardar",
|
313
|
+
};
|
314
|
+
case "it":
|
315
|
+
return {
|
316
|
+
button: "Salva",
|
317
|
+
};
|
318
|
+
case "pl":
|
319
|
+
return {
|
320
|
+
button: "Zapisz",
|
321
|
+
};
|
322
|
+
case "no":
|
323
|
+
return {
|
324
|
+
button: "Lagre",
|
325
|
+
};
|
326
|
+
case "dk":
|
327
|
+
return {
|
328
|
+
button: "Gem",
|
329
|
+
};
|
330
|
+
case "sw":
|
331
|
+
return {
|
332
|
+
button: "Spara",
|
333
|
+
};
|
334
|
+
default:
|
335
|
+
case "en":
|
336
|
+
return {
|
337
|
+
button: "Save",
|
338
|
+
};
|
339
|
+
}
|
340
340
|
}
|
341
341
|
|
342
342
|
/**
|
@@ -350,68 +350,71 @@ function getTranslations() {
|
|
350
350
|
* @throws {Error} the selector must match exactly one element
|
351
351
|
*/
|
352
352
|
function initControlReferences() {
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
353
|
+
if (!this.shadowRoot) {
|
354
|
+
throw new Error("no shadow-root is defined");
|
355
|
+
}
|
356
|
+
|
357
|
+
this[stateButtonElementSymbol] = this.shadowRoot.querySelector(
|
358
|
+
"[data-monster-role=state-button]",
|
359
|
+
);
|
360
|
+
|
361
|
+
this[badgeElementSymbol] = this.shadowRoot.querySelector(
|
362
|
+
"[data-monster-role=badge]",
|
363
|
+
);
|
364
|
+
|
365
|
+
if (this[stateButtonElementSymbol]) {
|
366
|
+
queueMicrotask(() => {
|
367
|
+
const states = {
|
368
|
+
changed: new State(
|
369
|
+
"changed",
|
370
|
+
'<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">' +
|
371
|
+
'<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"/>' +
|
372
|
+
'<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"/>' +
|
373
|
+
"</svg>",
|
374
|
+
),
|
375
|
+
};
|
376
|
+
|
377
|
+
this[stateButtonElementSymbol].removeState();
|
378
|
+
this[stateButtonElementSymbol].setOption("disabled", "disabled");
|
379
|
+
this[stateButtonElementSymbol].setOption("states", states);
|
380
|
+
this[stateButtonElementSymbol].setOption(
|
381
|
+
"labels.button",
|
382
|
+
this.getOption("labels.button"),
|
383
|
+
);
|
384
|
+
});
|
385
|
+
}
|
386
|
+
|
387
|
+
return this;
|
388
388
|
}
|
389
389
|
|
390
390
|
/**
|
391
391
|
* @private
|
392
392
|
*/
|
393
393
|
function initEventHandler() {
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
394
|
+
queueMicrotask(() => {
|
395
|
+
this[stateButtonElementSymbol].setOption("actions.click", () => {
|
396
|
+
this[datasourceLinkedElementSymbol]
|
397
|
+
.write()
|
398
|
+
.then(() => {
|
399
|
+
this[originValuesSymbol] = null;
|
400
|
+
this[originValuesSymbol] = clone(
|
401
|
+
this[datasourceLinkedElementSymbol].data,
|
402
|
+
);
|
403
|
+
this[stateButtonElementSymbol].removeState();
|
404
|
+
this[stateButtonElementSymbol].setOption("disabled", true);
|
405
|
+
this.setOption("changes", 0);
|
406
|
+
this.setOption(
|
407
|
+
"classes.badge",
|
408
|
+
new TokenList(this.getOption("classes.badge"))
|
409
|
+
.add("hidden")
|
410
|
+
.toString(),
|
411
|
+
);
|
412
|
+
})
|
413
|
+
.catch((error) => {
|
414
|
+
addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, error.toString());
|
415
|
+
});
|
416
|
+
});
|
417
|
+
});
|
415
418
|
}
|
416
419
|
|
417
420
|
/**
|
@@ -419,10 +422,10 @@ function initEventHandler() {
|
|
419
422
|
* @deprecated 2024-12-31
|
420
423
|
*/
|
421
424
|
function updateOptionsFromArguments(options) {
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
425
|
+
const selector = this.getAttribute(ATTRIBUTE_DATASOURCE_SELECTOR);
|
426
|
+
if (selector) {
|
427
|
+
options.datasource.selector = selector;
|
428
|
+
}
|
426
429
|
}
|
427
430
|
|
428
431
|
/**
|
@@ -430,8 +433,8 @@ function updateOptionsFromArguments(options) {
|
|
430
433
|
* @return {string}
|
431
434
|
*/
|
432
435
|
function getTemplate() {
|
433
|
-
|
434
|
-
|
436
|
+
// language=HTML
|
437
|
+
return `
|
435
438
|
<div data-monster-role="control" part="control"
|
436
439
|
data-monster-attributes="disabled path:disabled | if:true">
|
437
440
|
<monster-state-button data-monster-role="state-button"></monster-state-button>
|