@schukai/monster 5.2.6 → 5.2.7
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 +8 -0
- package/package.json +1 -1
- package/source/components/tree-menu/entry-events.mjs +26 -0
- package/source/components/tree-menu/html-tree-menu.mjs +31 -38
- package/source/components/tree-menu/tree-menu.mjs +15 -17
- package/source/types/version.mjs +1 -1
- package/test/cases/components/tree-menu/select-entry.mjs +150 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.6"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","exports":{".":"./source/monster.mjs","./CHANGELOG.md":"./CHANGELOG.md","./LICENSE":"./LICENSE","./README.md":"./README.md","./documentation/migrations/monster-5-options.md":"./documentation/migrations/monster-5-options.md","./package.json":"./package.json","./source/*":"./source/*","./test/*":"./test/*"},"files":["source","test/cases","test/util","documentation/migrations/monster-5-options.md","CHANGELOG.md"],"homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"sideEffects":["./source/monster.mjs","./source/components/**","./source/i18n/**","./test/**"],"type":"module","version":"5.2.
|
|
1
|
+
{"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.6"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","exports":{".":"./source/monster.mjs","./CHANGELOG.md":"./CHANGELOG.md","./LICENSE":"./LICENSE","./README.md":"./README.md","./documentation/migrations/monster-5-options.md":"./documentation/migrations/monster-5-options.md","./package.json":"./package.json","./source/*":"./source/*","./test/*":"./test/*"},"files":["source","test/cases","test/util","documentation/migrations/monster-5-options.md","CHANGELOG.md"],"homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"sideEffects":["./source/monster.mjs","./source/components/**","./source/i18n/**","./test/**"],"type":"module","version":"5.2.7"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
|
|
3
|
+
* Node module: @schukai/monster
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
|
|
6
|
+
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
|
|
7
|
+
*
|
|
8
|
+
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
|
|
9
|
+
* Acquiring a commercial license allows you to use this software without complying with AGPLv3 terms.
|
|
10
|
+
* For more information about purchasing a commercial license, please contact Volker Schukai.
|
|
11
|
+
*
|
|
12
|
+
* SPDX-License-Identifier: AGPL-3.0
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Bind the configured entry interactions and return the event to dispatch
|
|
17
|
+
* for programmatic activation.
|
|
18
|
+
* @private
|
|
19
|
+
*/
|
|
20
|
+
export function bindEntryEvents(menu, handler) {
|
|
21
|
+
const types = Object.values(menu.getOption("toggleEventType", ["click"]));
|
|
22
|
+
for (const type of types) {
|
|
23
|
+
menu.shadowRoot.addEventListener(type, handler);
|
|
24
|
+
}
|
|
25
|
+
return types.includes("click") ? "click" : types[0];
|
|
26
|
+
}
|
|
@@ -24,12 +24,16 @@ import {
|
|
|
24
24
|
CustomElement,
|
|
25
25
|
registerCustomElement,
|
|
26
26
|
} from "../../dom/customelement.mjs";
|
|
27
|
-
import {
|
|
28
|
-
|
|
27
|
+
import {
|
|
28
|
+
findTargetElementFromEvent,
|
|
29
|
+
fireCustomEvent,
|
|
30
|
+
fireEvent,
|
|
31
|
+
} from "../../dom/events.mjs";
|
|
29
32
|
import { isFunction, isString } from "../../types/is.mjs";
|
|
30
33
|
import { ID } from "../../types/id.mjs";
|
|
31
34
|
import { CommonStyleSheet } from "../stylesheet/common.mjs";
|
|
32
35
|
import { TreeMenuStyleSheet } from "./stylesheet/tree-menu.mjs";
|
|
36
|
+
import { bindEntryEvents } from "./entry-events.mjs";
|
|
33
37
|
import { ATTRIBUTE_INTEND } from "./../constants.mjs";
|
|
34
38
|
|
|
35
39
|
export { HtmlTreeMenu };
|
|
@@ -44,7 +48,7 @@ const entryIndexSymbol = Symbol("entryIndex");
|
|
|
44
48
|
* @private
|
|
45
49
|
* @type {symbol}
|
|
46
50
|
*/
|
|
47
|
-
const
|
|
51
|
+
const entryEventTypeSymbol = Symbol("entryEventType");
|
|
48
52
|
|
|
49
53
|
/**
|
|
50
54
|
* HtmlTreeMenu
|
|
@@ -162,33 +166,20 @@ class HtmlTreeMenu extends CustomElement {
|
|
|
162
166
|
* @return {void}
|
|
163
167
|
*/
|
|
164
168
|
selectEntry(value) {
|
|
165
|
-
this.
|
|
166
|
-
|
|
167
|
-
.forEach((entry) => {
|
|
168
|
-
entry.classList.remove("selected");
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
value = String(value);
|
|
172
|
-
const index = findEntryIndex.call(this, value);
|
|
173
|
-
if (index === -1) {
|
|
169
|
+
const match = this.findEntry(value);
|
|
170
|
+
if (!match?.node) {
|
|
174
171
|
return;
|
|
175
172
|
}
|
|
176
173
|
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const allowParentSelect = this.getOption("features.selectParents") === true;
|
|
186
|
-
if (currentEntry?.["has-children"] === true && allowParentSelect) {
|
|
187
|
-
applySelection.call(this, currentEntry, Number(index), currentNode);
|
|
188
|
-
return;
|
|
174
|
+
const { entry, index, node } = match;
|
|
175
|
+
if (
|
|
176
|
+
entry["has-children"] === true &&
|
|
177
|
+
this.getOption("features.selectParents") === true
|
|
178
|
+
) {
|
|
179
|
+
applySelection.call(this, entry, index, node);
|
|
180
|
+
} else if (this[entryEventTypeSymbol]) {
|
|
181
|
+
fireEvent(node, this[entryEventTypeSymbol]);
|
|
189
182
|
}
|
|
190
|
-
|
|
191
|
-
currentNode.click();
|
|
192
183
|
}
|
|
193
184
|
|
|
194
185
|
/**
|
|
@@ -317,7 +308,7 @@ class HtmlTreeMenu extends CustomElement {
|
|
|
317
308
|
* @private
|
|
318
309
|
*/
|
|
319
310
|
function initEventHandler() {
|
|
320
|
-
this[
|
|
311
|
+
this[entryEventTypeSymbol] = bindEntryEvents(this, (event) => {
|
|
321
312
|
const container = findTargetElementFromEvent(
|
|
322
313
|
event,
|
|
323
314
|
ATTRIBUTE_ROLE,
|
|
@@ -399,12 +390,7 @@ function initEventHandler() {
|
|
|
399
390
|
if (allowParentSelect) {
|
|
400
391
|
applySelection.call(this, currentEntry, Number(index), container, event);
|
|
401
392
|
}
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
const types = this.getOption("toggleEventType", ["click"]);
|
|
405
|
-
for (const [, type] of Object.entries(types)) {
|
|
406
|
-
this.shadowRoot.addEventListener(type, this[openEntryEventHandlerSymbol]);
|
|
407
|
-
}
|
|
393
|
+
});
|
|
408
394
|
}
|
|
409
395
|
|
|
410
396
|
/**
|
|
@@ -569,10 +555,15 @@ function getEntryValue(li) {
|
|
|
569
555
|
* @return {number}
|
|
570
556
|
*/
|
|
571
557
|
function findEntryIndex(value) {
|
|
572
|
-
if (this[entryIndexSymbol].has(value)) {
|
|
573
|
-
return this[entryIndexSymbol].get(value);
|
|
574
|
-
}
|
|
575
558
|
const entries = this.getOption("entries", []);
|
|
559
|
+
const index = this[entryIndexSymbol]?.get(value);
|
|
560
|
+
if (
|
|
561
|
+
index !== undefined &&
|
|
562
|
+
entries[index] &&
|
|
563
|
+
String(entries[index].value) === value
|
|
564
|
+
) {
|
|
565
|
+
return index;
|
|
566
|
+
}
|
|
576
567
|
return entries.findIndex((entry) => String(entry.value) === value);
|
|
577
568
|
}
|
|
578
569
|
|
|
@@ -593,8 +584,10 @@ function rebuildEntryIndex() {
|
|
|
593
584
|
* @return {HTMLElement|null}
|
|
594
585
|
*/
|
|
595
586
|
function getEntryNode(index) {
|
|
596
|
-
return
|
|
597
|
-
|
|
587
|
+
return (
|
|
588
|
+
this.shadowRoot?.querySelector(
|
|
589
|
+
`[data-monster-insert-reference=entries-${index}]`,
|
|
590
|
+
) ?? null
|
|
598
591
|
);
|
|
599
592
|
}
|
|
600
593
|
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
import { ATTRIBUTE_INTEND } from "./../constants.mjs";
|
|
43
43
|
import { CommonStyleSheet } from "../stylesheet/common.mjs";
|
|
44
44
|
import { TreeMenuStyleSheet } from "./stylesheet/tree-menu.mjs";
|
|
45
|
+
import { bindEntryEvents } from "./entry-events.mjs";
|
|
45
46
|
|
|
46
47
|
export { TreeMenu };
|
|
47
48
|
|
|
@@ -67,7 +68,7 @@ const controlElementSymbol = Symbol("controlElement");
|
|
|
67
68
|
* @private
|
|
68
69
|
* @type {symbol}
|
|
69
70
|
*/
|
|
70
|
-
const
|
|
71
|
+
const entryEventTypeSymbol = Symbol("entryEventType");
|
|
71
72
|
|
|
72
73
|
/**
|
|
73
74
|
* @private
|
|
@@ -218,11 +219,10 @@ class TreeMenu extends CustomElement {
|
|
|
218
219
|
* @param {string} value
|
|
219
220
|
*/
|
|
220
221
|
selectEntry(value) {
|
|
221
|
-
this
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
});
|
|
222
|
+
const eventType = this[entryEventTypeSymbol];
|
|
223
|
+
if (!eventType) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
226
|
|
|
227
227
|
value = String(value);
|
|
228
228
|
|
|
@@ -241,12 +241,11 @@ class TreeMenu extends CustomElement {
|
|
|
241
241
|
return;
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
-
currentNode
|
|
244
|
+
fireEvent(currentNode, eventType);
|
|
245
245
|
|
|
246
246
|
let intend = Number.parseInt(currentNode.getAttribute(ATTRIBUTE_INTEND));
|
|
247
247
|
|
|
248
248
|
if (intend > 0) {
|
|
249
|
-
const refSet = new Set();
|
|
250
249
|
let ref = currentNode.previousElementSibling;
|
|
251
250
|
while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
|
|
252
251
|
const i = Number.parseInt(ref.getAttribute(ATTRIBUTE_INTEND));
|
|
@@ -256,8 +255,12 @@ class TreeMenu extends CustomElement {
|
|
|
256
255
|
}
|
|
257
256
|
|
|
258
257
|
if (i < intend) {
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
const refIndex = ref
|
|
259
|
+
.getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
|
|
260
|
+
.split("-")
|
|
261
|
+
.pop();
|
|
262
|
+
if (this.getOption(`entries.${refIndex}.state`) !== "open") {
|
|
263
|
+
fireEvent(ref, eventType);
|
|
261
264
|
}
|
|
262
265
|
|
|
263
266
|
if (i === 0) {
|
|
@@ -325,7 +328,7 @@ function initEventHandler() {
|
|
|
325
328
|
});
|
|
326
329
|
}
|
|
327
330
|
|
|
328
|
-
this[
|
|
331
|
+
this[entryEventTypeSymbol] = bindEntryEvents(this, (event) => {
|
|
329
332
|
const container = findTargetElementFromEvent(
|
|
330
333
|
event,
|
|
331
334
|
ATTRIBUTE_ROLE,
|
|
@@ -447,12 +450,7 @@ function initEventHandler() {
|
|
|
447
450
|
if (changed === true) {
|
|
448
451
|
this.setOption("entries", nextEntries);
|
|
449
452
|
}
|
|
450
|
-
};
|
|
451
|
-
|
|
452
|
-
const types = this.getOption("toggleEventType", ["click"]);
|
|
453
|
-
for (const [, type] of Object.entries(types)) {
|
|
454
|
-
this.shadowRoot.addEventListener(type, this[openEntryEventHandlerSymbol]);
|
|
455
|
-
}
|
|
453
|
+
});
|
|
456
454
|
|
|
457
455
|
return this;
|
|
458
456
|
}
|
package/source/types/version.mjs
CHANGED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { initJSDOM } from "../../../util/jsdom.mjs";
|
|
3
|
+
|
|
4
|
+
describe("Tree menu programmatic selection (#613)", () => {
|
|
5
|
+
let menu;
|
|
6
|
+
let selected;
|
|
7
|
+
const frame = () => new Promise((resolve) => requestAnimationFrame(resolve));
|
|
8
|
+
const button = (value) =>
|
|
9
|
+
menu.shadowRoot.querySelector(
|
|
10
|
+
`[data-monster-role=button][value="${value}"]`,
|
|
11
|
+
);
|
|
12
|
+
const marked = () =>
|
|
13
|
+
Array.from(
|
|
14
|
+
menu.shadowRoot.querySelectorAll(".selected [data-monster-role=button]"),
|
|
15
|
+
(node) => node.getAttribute("value"),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
before(async function () {
|
|
19
|
+
this.timeout(10000);
|
|
20
|
+
await initJSDOM();
|
|
21
|
+
await import("../../../../source/components/tree-menu/html-tree-menu.mjs");
|
|
22
|
+
await import("../../../../source/components/tree-menu/tree-menu.mjs");
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => menu.remove());
|
|
25
|
+
|
|
26
|
+
async function mount(tag, options = {}) {
|
|
27
|
+
menu = document.createElement(tag);
|
|
28
|
+
selected = [];
|
|
29
|
+
if (tag === "monster-html-tree-menu") {
|
|
30
|
+
menu.innerHTML = `<ul>
|
|
31
|
+
<li id="root">Root<ul hidden><li id="one">One</li><li id="two">Two</li></ul></li>
|
|
32
|
+
<li id="link"><a href="#destination">Link</a></li>
|
|
33
|
+
</ul>`;
|
|
34
|
+
} else {
|
|
35
|
+
menu.setOption(
|
|
36
|
+
"entries",
|
|
37
|
+
["root", "one", "two"].map((value, index) => ({
|
|
38
|
+
value,
|
|
39
|
+
label: value,
|
|
40
|
+
intend: index === 0 ? 0 : 1,
|
|
41
|
+
state: "close",
|
|
42
|
+
visibility: index === 0 ? "visible" : "hidden",
|
|
43
|
+
"has-children": index === 0,
|
|
44
|
+
})),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
menu.setOptions(options);
|
|
48
|
+
menu.setOption("actions.select", (entry) => selected.push(entry.value));
|
|
49
|
+
document.getElementById("mocks").append(menu);
|
|
50
|
+
await frame();
|
|
51
|
+
expect(button("two")).to.exist;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const tag of ["monster-html-tree-menu", "monster-tree-menu"]) {
|
|
55
|
+
describe(tag, () => {
|
|
56
|
+
it("ignores selection before the menu is mounted", () => {
|
|
57
|
+
menu = document.createElement(tag);
|
|
58
|
+
menu.selectEntry("one");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("selects a leaf once and marks its ancestor", async () => {
|
|
62
|
+
await mount(tag);
|
|
63
|
+
menu.selectEntry("one");
|
|
64
|
+
await frame();
|
|
65
|
+
expect(selected).to.deep.equal(["one"]);
|
|
66
|
+
expect(marked()).to.deep.equal(["root", "one"]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("keeps the current selection when the target is missing", async () => {
|
|
70
|
+
await mount(tag);
|
|
71
|
+
button("one").click();
|
|
72
|
+
menu.selectEntry("absent");
|
|
73
|
+
expect(marked()).to.deep.equal(["root", "one"]);
|
|
74
|
+
expect(selected).to.deep.equal(["one"]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("honors a non-click activation configuration", async () => {
|
|
78
|
+
await mount(tag, { toggleEventType: ["dblclick"] });
|
|
79
|
+
button("one").click();
|
|
80
|
+
expect(selected).to.be.empty;
|
|
81
|
+
menu.selectEntry("two");
|
|
82
|
+
expect(selected).to.deep.equal(["two"]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe("HTML menu", () => {
|
|
88
|
+
it("selects a selectable parent without expanding it", async () => {
|
|
89
|
+
await mount("monster-html-tree-menu", {
|
|
90
|
+
features: { selectParents: true },
|
|
91
|
+
});
|
|
92
|
+
const events = [];
|
|
93
|
+
menu.addEventListener("monster-html-tree-menu-select", ({ detail }) => {
|
|
94
|
+
events.push([detail.entry.value, detail.index]);
|
|
95
|
+
});
|
|
96
|
+
menu.selectEntry("root");
|
|
97
|
+
expect(events).to.deep.equal([["root", 0]]);
|
|
98
|
+
expect(selected).to.deep.equal(["root"]);
|
|
99
|
+
expect(marked()).to.deep.equal(["root"]);
|
|
100
|
+
expect(menu.findEntry("root").entry.state).to.equal("close");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("toggles a parent when parent selection is disabled", async () => {
|
|
104
|
+
await mount("monster-html-tree-menu");
|
|
105
|
+
menu.selectEntry("root");
|
|
106
|
+
expect(menu.findEntry("root").entry.state).to.equal("open");
|
|
107
|
+
menu.selectEntry("root");
|
|
108
|
+
expect(menu.findEntry("root").entry.state).to.equal("close");
|
|
109
|
+
expect(selected).to.be.empty;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("keeps the selection when a link's navigation is canceled", async () => {
|
|
113
|
+
await mount("monster-html-tree-menu");
|
|
114
|
+
button("one").click();
|
|
115
|
+
const navigated = [];
|
|
116
|
+
menu.addEventListener("monster-html-tree-menu-navigate", (event) => {
|
|
117
|
+
navigated.push(event.detail.entry.value);
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
});
|
|
120
|
+
menu.selectEntry("link");
|
|
121
|
+
expect(navigated).to.deep.equal(["link"]);
|
|
122
|
+
expect(selected).to.deep.equal(["one"]);
|
|
123
|
+
expect(marked()).to.deep.equal(["root", "one"]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("resolves the current entry after reordering the entries option", async () => {
|
|
127
|
+
await mount("monster-html-tree-menu");
|
|
128
|
+
const [root, one, two, link] = menu.getOption("entries");
|
|
129
|
+
menu.setOption("entries", [root, two, one, link]);
|
|
130
|
+
await frame();
|
|
131
|
+
expect(menu.findEntry("one").entry.value).to.equal("one");
|
|
132
|
+
menu.selectEntry("one");
|
|
133
|
+
expect(selected).to.deep.equal(["one"]);
|
|
134
|
+
expect(marked()).to.deep.equal(["root", "one"]);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("keeps TreeMenu ancestors open on consecutive selections before the next render", async () => {
|
|
139
|
+
await mount("monster-tree-menu");
|
|
140
|
+
menu.selectEntry("one");
|
|
141
|
+
menu.selectEntry("two");
|
|
142
|
+
await frame();
|
|
143
|
+
expect(selected).to.deep.equal(["one", "two"]);
|
|
144
|
+
expect(marked()).to.deep.equal(["root", "two"]);
|
|
145
|
+
expect(menu.getOption("entries.0.state")).to.equal("open");
|
|
146
|
+
expect(
|
|
147
|
+
button("two").parentElement.getAttribute("data-monster-visibility"),
|
|
148
|
+
).to.equal("visible");
|
|
149
|
+
});
|
|
150
|
+
});
|