@schukai/monster 4.38.0 → 4.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,15 +17,15 @@ import { buildTree } from "../../data/buildtree.mjs";
17
17
  import { Datasource } from "../datatable/datasource.mjs";
18
18
  import { addAttributeToken } from "../../dom/attributes.mjs";
19
19
  import {
20
- ATTRIBUTE_ERRORMESSAGE,
21
- ATTRIBUTE_ROLE,
22
- ATTRIBUTE_UPDATER_INSERT_REFERENCE,
20
+ ATTRIBUTE_ERRORMESSAGE,
21
+ ATTRIBUTE_ROLE,
22
+ ATTRIBUTE_UPDATER_INSERT_REFERENCE,
23
23
  } from "../../dom/constants.mjs";
24
24
  import {
25
- assembleMethodSymbol,
26
- CustomElement,
27
- getSlottedElements,
28
- registerCustomElement,
25
+ assembleMethodSymbol,
26
+ CustomElement,
27
+ getSlottedElements,
28
+ registerCustomElement,
29
29
  } from "../../dom/customelement.mjs";
30
30
  import { findTargetElementFromEvent, fireEvent } from "../../dom/events.mjs";
31
31
  import { findElementWithSelectorUpwards } from "../../dom/util.mjs";
@@ -36,8 +36,8 @@ import { NodeRecursiveIterator } from "../../types/noderecursiveiterator.mjs";
36
36
  import { Observer } from "../../types/observer.mjs";
37
37
  import { validateInstance } from "../../types/validate.mjs";
38
38
  import {
39
- datasourceLinkedElementSymbol,
40
- handleDataSourceChanges,
39
+ datasourceLinkedElementSymbol,
40
+ handleDataSourceChanges,
41
41
  } from "../datatable/util.mjs";
42
42
  import { ATTRIBUTE_INTEND } from "./../constants.mjs";
43
43
  import { CommonStyleSheet } from "../stylesheet/common.mjs";
@@ -83,347 +83,355 @@ const firstRunDoneSymbol = Symbol("firstRunDone");
83
83
  * @fires entries-imported
84
84
  */
85
85
  class TreeMenu extends CustomElement {
86
- /**
87
- * This method is called by the `instanceof` operator.
88
- * @return {symbol}
89
- */
90
- static get [instanceSymbol]() {
91
- return Symbol.for("@schukai/monster/components/tree-menu@@instance");
92
- }
93
-
94
- /**
95
- */
96
- constructor() {
97
- super();
98
- this[preventChangeSymbol] = false;
99
- }
100
-
101
- /**
102
- * This method is called internal and should not be called directly.
103
- *
104
- * The defaults can be set either directly in the object or via an attribute in the HTML tag.
105
- * The value of the attribute `data-monster-options` in the HTML tag must be a JSON string.
106
- *
107
- * ```
108
- * <monster-treemenu data-monster-options="{}"></monster-treemenu>
109
- * ```
110
- *
111
- * Since 1.18.0 the JSON can be specified as a DataURI.
112
- *
113
- * ```
114
- * new Monster.Types.DataUrl(btoa(JSON.stringify({
115
- * shadowMode: 'open',
116
- * })),'application/json',true).toString()
117
- * ```
118
- * @property {Object} templates Template definitions
119
- * @property {string} templates.main Main template
120
- * @property {Datasource} datasource data source
121
- * @property {Object} mapping
122
- * @property {String} mapping.selector Path to select the appropriate entries
123
- * @property {String} mapping.labelTemplate template with the label placeholders in the form ${name}, where name is the key
124
- * @property {String} mapping.keyTemplate template with the key placeholders in the form ${name}, where name is the key
125
- * @property {String} mapping.rootReferences the root references
126
- * @property {String} mapping.idTemplate template with the id placeholders in the form ${name}, where name is the key
127
- * @property {String} mapping.parentKey the parent key
128
- * @property {String} mapping.selection the selection
129
- */
130
- get defaults() {
131
- return Object.assign({}, super.defaults, {
132
- classes: {
133
- control: "monster-theme-primary-1",
134
- label: "monster-theme-primary-1",
135
- },
136
-
137
- mapping: {
138
- rootReferences: ["0", undefined, null],
139
- idTemplate: "id",
140
- parentKey: "parent",
141
- selector: "*",
142
- labelTemplate: "",
143
- valueTemplate: "",
144
- iconTemplate: "",
145
- filter: undefined,
146
- },
147
-
148
- templates: {
149
- main: getTemplate(),
150
- },
151
-
152
- datasource: {
153
- selector: null,
154
- },
155
-
156
- actions: {
157
- open: null,
158
- close: null,
159
- select: (entry) => {
160
- throw new Error("no action defined for select");
161
- },
162
- },
163
-
164
- data: [],
165
- entries: [],
166
- });
167
- }
168
-
169
- /**
170
- * @return {void}
171
- */
172
- [assembleMethodSymbol]() {
173
- super[assembleMethodSymbol]();
174
-
175
- queueMicrotask(() => {
176
- initControlReferences.call(this);
177
- initEventHandler.call(this);
178
- initObserver.call(this);
179
- copyIconMap.call(this);
180
- });
181
- }
182
-
183
- /**
184
- * This method is called internal and should not be called directly.
185
- *
186
- * @return {CSSStyleSheet[]}
187
- */
188
- static getCSSStyleSheet() {
189
- return [CommonStyleSheet, TreeMenuStyleSheet];
190
- }
191
-
192
- /**
193
- * This method is called internal and should not be called directly.
194
- *
195
- * @return {string}
196
- */
197
- static getTag() {
198
- return "monster-tree-menu";
199
- }
200
-
201
- /**
202
- * @param {string} value
203
- * @param value
204
- */
205
- selectEntry(value) {
206
- this.shadowRoot
207
- .querySelectorAll("[data-monster-role=entry]")
208
- .forEach((entry) => {
209
- entry.classList.remove("selected");
210
- });
211
-
212
- value = String(value);
213
-
214
- const entries = this.getOption("entries");
215
- const index = entries.findIndex((entry) => entry.value === value);
216
-
217
- if (index === -1) {
218
- return;
219
- }
220
-
221
- const currentNode = this.shadowRoot.querySelector(
222
- "[data-monster-insert-reference=entries-" + index + "]",
223
- );
224
-
225
- if (!currentNode) {
226
- return;
227
- }
228
-
229
- currentNode.click();
230
-
231
- let intend = Number.parseInt(currentNode.getAttribute(ATTRIBUTE_INTEND));
232
-
233
- if (intend > 0) {
234
- const refSet = new Set();
235
- let ref = currentNode.previousElementSibling;
236
- while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
237
- const i = Number.parseInt(ref.getAttribute(ATTRIBUTE_INTEND));
238
-
239
- if (Number.isNaN(i)) {
240
- break;
241
- }
242
-
243
- if (i < intend) {
244
- if (ref.getAttribute("data-monster-state") !== "open") {
245
- ref.click();
246
- }
247
-
248
- if (i === 0) {
249
- break;
250
- }
251
- intend = i;
252
- }
253
- ref = ref.previousElementSibling;
254
- }
255
- }
256
- }
86
+ /**
87
+ * This method is called by the `instanceof` operator.
88
+ * @return {symbol}
89
+ */
90
+ static get [instanceSymbol]() {
91
+ return Symbol.for("@schukai/monster/components/tree-menu@@instance");
92
+ }
93
+
94
+ /**
95
+ */
96
+ constructor() {
97
+ super();
98
+ this[preventChangeSymbol] = false;
99
+ }
100
+
101
+ /**
102
+ * This method is called internal and should not be called directly.
103
+ *
104
+ * The defaults can be set either directly in the object or via an attribute in the HTML tag.
105
+ * The value of the attribute `data-monster-options` in the HTML tag must be a JSON string.
106
+ *
107
+ * ```
108
+ * <monster-treemenu data-monster-options="{}"></monster-treemenu>
109
+ * ```
110
+ *
111
+ * Since 1.18.0 the JSON can be specified as a DataURI.
112
+ *
113
+ * ```
114
+ * new Monster.Types.DataUrl(btoa(JSON.stringify({
115
+ * shadowMode: 'open',
116
+ * })),'application/json',true).toString()
117
+ * ```
118
+ * @property {Object} templates Template definitions
119
+ * @property {string} templates.main Main template
120
+ * @property {Datasource} datasource data source
121
+ * @property {Object} mapping
122
+ * @property {String} mapping.selector Path to select the appropriate entries
123
+ * @property {String} mapping.labelTemplate template with the label placeholders in the form ${name}, where name is the key
124
+ * @property {String} mapping.keyTemplate template with the key placeholders in the form ${name}, where name is the key
125
+ * @property {String} mapping.rootReferences the root references
126
+ * @property {String} mapping.idTemplate template with the id placeholders in the form ${name}, where name is the key
127
+ * @property {String} mapping.parentKey the parent key
128
+ * @property {String} mapping.selection the selection
129
+ * @property {Function} mapping.filter a filter function to filter the entries
130
+ * @property {Object} classes
131
+ * @property {String} classes.control the class for the control element
132
+ * @property {String} classes.label the class for the label element
133
+ * @property {Object} actions
134
+ * @property {Function} actions.open the action to open an entry (arguments, etnry, index, event)
135
+ * @property {Function} actions.close the action to close an entry (arguments, etnry, index, event)
136
+ * @property {Function} actions.select the action to select an entry (arguments, etnry, index, event)
137
+ */
138
+ get defaults() {
139
+ return Object.assign({}, super.defaults, {
140
+ classes: {
141
+ control: "monster-theme-primary-1",
142
+ label: "monster-theme-primary-1",
143
+ },
144
+
145
+ mapping: {
146
+ rootReferences: ["0", undefined, null],
147
+ idTemplate: "id",
148
+ parentKey: "parent",
149
+ selector: "*",
150
+ labelTemplate: "",
151
+ valueTemplate: "",
152
+ iconTemplate: "",
153
+ filter: undefined,
154
+ },
155
+
156
+ templates: {
157
+ main: getTemplate(),
158
+ },
159
+
160
+ datasource: {
161
+ selector: null,
162
+ },
163
+
164
+ actions: {
165
+ open: null,
166
+ close: null,
167
+ select: (entry) => {
168
+ console.warn("select action is not defined", entry);
169
+ },
170
+ },
171
+
172
+ data: [],
173
+ entries: [],
174
+ });
175
+ }
176
+
177
+ /**
178
+ * @return {void}
179
+ */
180
+ [assembleMethodSymbol]() {
181
+ super[assembleMethodSymbol]();
182
+
183
+ queueMicrotask(() => {
184
+ initControlReferences.call(this);
185
+ initEventHandler.call(this);
186
+ initObserver.call(this);
187
+ copyIconMap.call(this);
188
+ });
189
+ }
190
+
191
+ /**
192
+ * This method is called internal and should not be called directly.
193
+ *
194
+ * @return {CSSStyleSheet[]}
195
+ */
196
+ static getCSSStyleSheet() {
197
+ return [CommonStyleSheet, TreeMenuStyleSheet];
198
+ }
199
+
200
+ /**
201
+ * This method is called internal and should not be called directly.
202
+ *
203
+ * @return {string}
204
+ */
205
+ static getTag() {
206
+ return "monster-tree-menu";
207
+ }
208
+
209
+ /**
210
+ * @param {string} value
211
+ * @param value
212
+ */
213
+ selectEntry(value) {
214
+ this.shadowRoot
215
+ .querySelectorAll("[data-monster-role=entry]")
216
+ .forEach((entry) => {
217
+ entry.classList.remove("selected");
218
+ });
219
+
220
+ value = String(value);
221
+
222
+ const entries = this.getOption("entries");
223
+ const index = entries.findIndex((entry) => entry.value === value);
224
+
225
+ if (index === -1) {
226
+ return;
227
+ }
228
+
229
+ const currentNode = this.shadowRoot.querySelector(
230
+ "[data-monster-insert-reference=entries-" + index + "]",
231
+ );
232
+
233
+ if (!currentNode) {
234
+ return;
235
+ }
236
+
237
+ currentNode.click();
238
+
239
+ let intend = Number.parseInt(currentNode.getAttribute(ATTRIBUTE_INTEND));
240
+
241
+ if (intend > 0) {
242
+ const refSet = new Set();
243
+ let ref = currentNode.previousElementSibling;
244
+ while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
245
+ const i = Number.parseInt(ref.getAttribute(ATTRIBUTE_INTEND));
246
+
247
+ if (Number.isNaN(i)) {
248
+ break;
249
+ }
250
+
251
+ if (i < intend) {
252
+ if (ref.getAttribute("data-monster-state") !== "open") {
253
+ ref.click();
254
+ }
255
+
256
+ if (i === 0) {
257
+ break;
258
+ }
259
+ intend = i;
260
+ }
261
+ ref = ref.previousElementSibling;
262
+ }
263
+ }
264
+ }
257
265
  }
258
266
 
259
267
  /**
260
268
  * @private
261
269
  */
262
270
  function copyIconMap() {
263
- const nodes = getSlottedElements.call(this, "svg", null);
264
- if (nodes.size > 0) {
265
- for (const node of nodes) {
266
- this.shadowRoot.appendChild(node);
267
- }
268
- }
271
+ const nodes = getSlottedElements.call(this, "svg", null);
272
+ if (nodes.size > 0) {
273
+ for (const node of nodes) {
274
+ this.shadowRoot.appendChild(node);
275
+ }
276
+ }
269
277
  }
270
278
 
271
279
  /**
272
280
  * @private
273
281
  */
274
282
  function initEventHandler() {
275
- const selector = this.getOption("datasource.selector");
276
-
277
- if (isString(selector)) {
278
- const element = findElementWithSelectorUpwards(this, selector);
279
- if (element === null) {
280
- throw new Error("the selector must match exactly one element");
281
- }
282
-
283
- if (!(element instanceof HTMLElement)) {
284
- throw new TypeError("the element must be an HTMLElement");
285
- }
286
-
287
- customElements.whenDefined(element.tagName.toLocaleLowerCase()).then(() => {
288
- if (!(element instanceof Datasource)) {
289
- throw new TypeError("the element must be a datasource");
290
- }
291
-
292
- this[datasourceLinkedElementSymbol] = element;
293
-
294
- handleDataSourceChanges.call(this);
295
- element.datasource.attachObserver(
296
- new Observer(handleDataSourceChanges.bind(this)),
297
- );
298
-
299
- this.attachObserver(
300
- new Observer(() => {
301
- if (this[preventChangeSymbol] === true) {
302
- return;
303
- }
304
- this[preventChangeSymbol] = true;
305
- queueMicrotask(() => {
306
- importEntries.call(this);
307
- });
308
- }),
309
- );
310
- });
311
- }
312
-
313
- this[openEntryEventHandlerSymbol] = (event) => {
314
- const container = findTargetElementFromEvent(
315
- event,
316
- ATTRIBUTE_ROLE,
317
- "entry",
318
- );
319
-
320
- if (!(container instanceof HTMLElement)) {
321
- return;
322
- }
323
-
324
- const index = container
325
- .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
326
- .split("-")
327
- .pop();
328
-
329
- const currentEntry = this.getOption("entries." + index);
330
-
331
- if (currentEntry["has-children"] === false) {
332
- const doAction = this.getOption("actions.select");
333
-
334
- this.shadowRoot
335
- .querySelectorAll("[data-monster-role=entry].selected")
336
- .forEach((entry) => {
337
- entry.classList.remove("selected");
338
- });
339
-
340
- let intend = currentEntry.intend;
341
- if (intend > 0) {
342
- let ref = container.previousElementSibling;
343
- while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
344
- const i = Number.parseInt(ref.getAttribute(ATTRIBUTE_INTEND));
345
-
346
- if (Number.isNaN(i)) {
347
- break;
348
- }
349
-
350
- if (i < intend) {
351
- ref.classList.add("selected");
352
- if (i === 0) {
353
- break;
354
- }
355
- intend = i;
356
- }
357
- ref = ref.previousElementSibling;
358
- }
359
- }
360
-
361
- container.classList.add("selected");
362
-
363
- if (isFunction(doAction)) {
364
- doAction.call(this, currentEntry, index);
365
- }
366
- return;
367
- }
368
-
369
- const currentState = this.getOption("entries." + index + ".state");
370
-
371
- const newState = currentState === "close" ? "open" : "close";
372
-
373
- const doAction = this.getOption("actions." + newState);
374
- if (isFunction(doAction)) {
375
- doAction.call(this, this.getOption("entries." + index), index);
376
- }
377
-
378
- this.setOption("entries." + index + ".state", newState);
379
- const newVisibility = newState === "open" ? "visible" : "hidden";
380
-
381
- if (container.hasAttribute(ATTRIBUTE_INTEND)) {
382
- const intend = container.getAttribute(ATTRIBUTE_INTEND);
383
- let ref = container.nextElementSibling;
384
- const childIntend = parseInt(intend) + 1;
385
-
386
- const cmp = (a, b) => {
387
- if (newState === "open") {
388
- return a === b;
389
- }
390
-
391
- return a >= b;
392
- };
393
-
394
- while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
395
- const refIntend = ref.getAttribute(ATTRIBUTE_INTEND);
396
-
397
- if (!cmp(Number.parseInt(refIntend), childIntend)) {
398
- if (refIntend === intend) {
399
- break;
400
- }
401
- ref = ref.nextElementSibling;
402
- continue;
403
- }
404
-
405
- const refIndex = ref
406
- .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
407
- .split("-")
408
- .pop();
409
-
410
- this.setOption("entries." + refIndex + ".visibility", newVisibility);
411
-
412
- if (newState === "close") {
413
- this.setOption("entries." + refIndex + ".state", "close");
414
- }
415
-
416
- ref = ref.nextElementSibling;
417
- }
418
- }
419
- };
420
-
421
- const types = this.getOption("toggleEventType", ["click"]);
422
- for (const [, type] of Object.entries(types)) {
423
- this.shadowRoot.addEventListener(type, this[openEntryEventHandlerSymbol]);
424
- }
425
-
426
- return this;
283
+ const selector = this.getOption("datasource.selector");
284
+
285
+ if (isString(selector)) {
286
+ const element = findElementWithSelectorUpwards(this, selector);
287
+ if (element === null) {
288
+ throw new Error("the selector must match exactly one element");
289
+ }
290
+
291
+ if (!(element instanceof HTMLElement)) {
292
+ throw new TypeError("the element must be an HTMLElement");
293
+ }
294
+
295
+ customElements.whenDefined(element.tagName.toLocaleLowerCase()).then(() => {
296
+ if (!(element instanceof Datasource)) {
297
+ throw new TypeError("the element must be a datasource");
298
+ }
299
+
300
+ this[datasourceLinkedElementSymbol] = element;
301
+
302
+ handleDataSourceChanges.call(this);
303
+ element.datasource.attachObserver(
304
+ new Observer(handleDataSourceChanges.bind(this)),
305
+ );
306
+
307
+ this.attachObserver(
308
+ new Observer(() => {
309
+ if (this[preventChangeSymbol] === true) {
310
+ return;
311
+ }
312
+ this[preventChangeSymbol] = true;
313
+ queueMicrotask(() => {
314
+ importEntries.call(this);
315
+ });
316
+ }),
317
+ );
318
+ });
319
+ }
320
+
321
+ this[openEntryEventHandlerSymbol] = (event) => {
322
+ const container = findTargetElementFromEvent(
323
+ event,
324
+ ATTRIBUTE_ROLE,
325
+ "entry",
326
+ );
327
+
328
+ if (!(container instanceof HTMLElement)) {
329
+ return;
330
+ }
331
+
332
+ const index = container
333
+ .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
334
+ .split("-")
335
+ .pop();
336
+
337
+ const currentEntry = this.getOption("entries." + index);
338
+
339
+ if (currentEntry["has-children"] === false) {
340
+ const doAction = this.getOption("actions.select");
341
+
342
+ this.shadowRoot
343
+ .querySelectorAll("[data-monster-role=entry].selected")
344
+ .forEach((entry) => {
345
+ entry.classList.remove("selected");
346
+ });
347
+
348
+ let intend = currentEntry.intend;
349
+ if (intend > 0) {
350
+ let ref = container.previousElementSibling;
351
+ while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
352
+ const i = Number.parseInt(ref.getAttribute(ATTRIBUTE_INTEND));
353
+
354
+ if (Number.isNaN(i)) {
355
+ break;
356
+ }
357
+
358
+ if (i < intend) {
359
+ ref.classList.add("selected");
360
+ if (i === 0) {
361
+ break;
362
+ }
363
+ intend = i;
364
+ }
365
+ ref = ref.previousElementSibling;
366
+ }
367
+ }
368
+
369
+ container.classList.add("selected");
370
+
371
+ if (isFunction(doAction)) {
372
+ doAction.call(this, currentEntry, index, event);
373
+ }
374
+ return;
375
+ }
376
+
377
+ const currentState = this.getOption("entries." + index + ".state");
378
+
379
+ const newState = currentState === "close" ? "open" : "close";
380
+
381
+ const doAction = this.getOption("actions." + newState);
382
+ if (isFunction(doAction)) {
383
+ doAction.call(this, this.getOption("entries." + index), index);
384
+ }
385
+
386
+ this.setOption("entries." + index + ".state", newState);
387
+ const newVisibility = newState === "open" ? "visible" : "hidden";
388
+
389
+ if (container.hasAttribute(ATTRIBUTE_INTEND)) {
390
+ const intend = container.getAttribute(ATTRIBUTE_INTEND);
391
+ let ref = container.nextElementSibling;
392
+ const childIntend = parseInt(intend) + 1;
393
+
394
+ const cmp = (a, b) => {
395
+ if (newState === "open") {
396
+ return a === b;
397
+ }
398
+
399
+ return a >= b;
400
+ };
401
+
402
+ while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
403
+ const refIntend = ref.getAttribute(ATTRIBUTE_INTEND);
404
+
405
+ if (!cmp(Number.parseInt(refIntend), childIntend)) {
406
+ if (refIntend === intend) {
407
+ break;
408
+ }
409
+ ref = ref.nextElementSibling;
410
+ continue;
411
+ }
412
+
413
+ const refIndex = ref
414
+ .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
415
+ .split("-")
416
+ .pop();
417
+
418
+ this.setOption("entries." + refIndex + ".visibility", newVisibility);
419
+
420
+ if (newState === "close") {
421
+ this.setOption("entries." + refIndex + ".state", "close");
422
+ }
423
+
424
+ ref = ref.nextElementSibling;
425
+ }
426
+ }
427
+ };
428
+
429
+ const types = this.getOption("toggleEventType", ["click"]);
430
+ for (const [, type] of Object.entries(types)) {
431
+ this.shadowRoot.addEventListener(type, this[openEntryEventHandlerSymbol]);
432
+ }
433
+
434
+ return this;
427
435
  }
428
436
 
429
437
  /**
@@ -441,81 +449,81 @@ function initObserver() {}
441
449
  * @private
442
450
  */
443
451
  function importEntries() {
444
- const data = this.getOption("data");
445
- this[internalNodesSymbol] = new Map();
446
-
447
- const mappingOptions = this.getOption("mapping", {});
448
-
449
- const filter = mappingOptions?.["filter"];
450
- const rootReferences = mappingOptions?.["rootReferences"];
451
-
452
- const id = this.getOption("mapping.idTemplate");
453
- const parentKey = this.getOption("mapping.parentKey");
454
-
455
- const selector = mappingOptions?.["selector"];
456
-
457
- let filteredData;
458
- if (this[firstRunDoneSymbol] !== true) {
459
- filteredData = data?.filter(
460
- (entry) =>
461
- !entry[parentKey] ||
462
- entry[parentKey] === null ||
463
- entry[parentKey] === undefined ||
464
- entry[parentKey] === 0,
465
- );
466
- setTimeout(() => {
467
- this[firstRunDoneSymbol] = true;
468
- importEntries.call(this);
469
- }, 0);
470
- } else {
471
- filteredData = data;
472
- }
473
-
474
- let nodes;
475
- try {
476
- nodes = buildTree(filteredData, selector, id, parentKey, {
477
- filter,
478
- rootReferences,
479
- });
480
- } catch (error) {
481
- addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, String(error));
482
- return this;
483
- }
484
-
485
- const options = [];
486
- for (const node of nodes) {
487
- const iterator = new NodeRecursiveIterator(node);
488
- for (const n of iterator) {
489
- const formattedValues = formatKeyLabel.call(this, n);
490
-
491
- const label = formattedValues?.label;
492
- const value = formattedValues?.value;
493
- const icon = formattedValues?.icon;
494
- const intend = n.level;
495
-
496
- const visibility = intend > 0 ? "hidden" : "visible";
497
- const state = "close";
498
-
499
- this[internalNodesSymbol].set(value, n);
500
-
501
- options.push({
502
- value,
503
- label,
504
- icon,
505
- intend,
506
- state,
507
- visibility,
508
- ["has-children"]: n.hasChildNodes(),
509
- });
510
- }
511
- }
512
-
513
- queueMicrotask(() => {
514
- this.setOption("entries", options);
515
- fireEvent(this, "entries-imported");
516
- });
517
-
518
- return this;
452
+ const data = this.getOption("data");
453
+ this[internalNodesSymbol] = new Map();
454
+
455
+ const mappingOptions = this.getOption("mapping", {});
456
+
457
+ const filter = mappingOptions?.["filter"];
458
+ const rootReferences = mappingOptions?.["rootReferences"];
459
+
460
+ const id = this.getOption("mapping.idTemplate");
461
+ const parentKey = this.getOption("mapping.parentKey");
462
+
463
+ const selector = mappingOptions?.["selector"];
464
+
465
+ let filteredData;
466
+ if (this[firstRunDoneSymbol] !== true) {
467
+ filteredData = data?.filter(
468
+ (entry) =>
469
+ !entry[parentKey] ||
470
+ entry[parentKey] === null ||
471
+ entry[parentKey] === undefined ||
472
+ entry[parentKey] === 0,
473
+ );
474
+ setTimeout(() => {
475
+ this[firstRunDoneSymbol] = true;
476
+ importEntries.call(this);
477
+ }, 0);
478
+ } else {
479
+ filteredData = data;
480
+ }
481
+
482
+ let nodes;
483
+ try {
484
+ nodes = buildTree(filteredData, selector, id, parentKey, {
485
+ filter,
486
+ rootReferences,
487
+ });
488
+ } catch (error) {
489
+ addAttributeToken(this, ATTRIBUTE_ERRORMESSAGE, String(error));
490
+ return this;
491
+ }
492
+
493
+ const options = [];
494
+ for (const node of nodes) {
495
+ const iterator = new NodeRecursiveIterator(node);
496
+ for (const n of iterator) {
497
+ const formattedValues = formatKeyLabel.call(this, n);
498
+
499
+ const label = formattedValues?.label;
500
+ const value = formattedValues?.value;
501
+ const icon = formattedValues?.icon;
502
+ const intend = n.level;
503
+
504
+ const visibility = intend > 0 ? "hidden" : "visible";
505
+ const state = "close";
506
+
507
+ this[internalNodesSymbol].set(value, n);
508
+
509
+ options.push({
510
+ value,
511
+ label,
512
+ icon,
513
+ intend,
514
+ state,
515
+ visibility,
516
+ ["has-children"]: n.hasChildNodes(),
517
+ });
518
+ }
519
+ }
520
+
521
+ queueMicrotask(() => {
522
+ this.setOption("entries", options);
523
+ fireEvent(this, "entries-imported");
524
+ });
525
+
526
+ return this;
519
527
  }
520
528
 
521
529
  /**
@@ -525,27 +533,27 @@ function importEntries() {
525
533
  * @private
526
534
  */
527
535
  function formatKeyLabel(node) {
528
- validateInstance(node, Node);
536
+ validateInstance(node, Node);
529
537
 
530
- const label = new Formatter(node.value).format(
531
- this.getOption("mapping.labelTemplate"),
532
- );
538
+ const label = new Formatter(node.value).format(
539
+ this.getOption("mapping.labelTemplate"),
540
+ );
533
541
 
534
- const value = new Formatter(node.value).format(
535
- this.getOption("mapping.valueTemplate"),
536
- );
542
+ const value = new Formatter(node.value).format(
543
+ this.getOption("mapping.valueTemplate"),
544
+ );
537
545
 
538
- const iconID = new Formatter(node.value).format(
539
- this.getOption("mapping.iconTemplate"),
540
- );
546
+ const iconID = new Formatter(node.value).format(
547
+ this.getOption("mapping.iconTemplate"),
548
+ );
541
549
 
542
- const icon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><use xlink:href="#${iconID}"></use></svg>`;
550
+ const icon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><use xlink:href="#${iconID}"></use></svg>`;
543
551
 
544
- return {
545
- value,
546
- label,
547
- icon,
548
- };
552
+ return {
553
+ value,
554
+ label,
555
+ icon,
556
+ };
549
557
  }
550
558
 
551
559
  /**
@@ -553,15 +561,15 @@ function formatKeyLabel(node) {
553
561
  * @return {TreeMenu}
554
562
  */
555
563
  function initControlReferences() {
556
- if (!this.shadowRoot) {
557
- throw new Error("no shadow-root is defined");
558
- }
564
+ if (!this.shadowRoot) {
565
+ throw new Error("no shadow-root is defined");
566
+ }
559
567
 
560
- this[controlElementSymbol] = this.shadowRoot.querySelector(
561
- "[data-monster-role=control]",
562
- );
568
+ this[controlElementSymbol] = this.shadowRoot.querySelector(
569
+ "[data-monster-role=control]",
570
+ );
563
571
 
564
- return this;
572
+ return this;
565
573
  }
566
574
 
567
575
  /**
@@ -569,8 +577,8 @@ function initControlReferences() {
569
577
  * @return {string}
570
578
  */
571
579
  function getTemplate() {
572
- // language=HTML
573
- return `
580
+ // language=HTML
581
+ return `
574
582
  <slot></slot>
575
583
 
576
584
  <template id="entries">