@schukai/monster 4.122.1 → 4.123.0

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 CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
 
4
4
 
5
+ ## [4.123.0] - 2026-02-17
6
+
7
+ ### Add Features
8
+
9
+ - Optimize tree menu entry visibility handling
10
+ - Enhance tree menu entry state management for improved performance and consistency
11
+
12
+
13
+
14
+ ## [4.122.2] - 2026-02-16
15
+
16
+ ### Bug Fixes
17
+
18
+ - Improve pagination logic and add corresponding tests
19
+
20
+
21
+
5
22
  ## [4.122.1] - 2026-02-13
6
23
 
7
24
  ### Bug Fixes
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","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"},"type":"module","version":"4.122.1"}
1
+ {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","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"},"type":"module","version":"4.123.0"}
@@ -691,8 +691,16 @@ function handleDataSourceChanges() {
691
691
  * @return {object}
692
692
  */
693
693
  function buildPagination(current, max) {
694
- current = parseInt(current, 10);
695
- max = parseInt(max, 10);
694
+ const parsedCurrent = Number.parseInt(current, 10);
695
+ const parsedMax = Number.parseInt(max, 10);
696
+
697
+ max = Number.isFinite(parsedMax) && parsedMax >= 1 ? parsedMax : 1;
698
+ current = Number.isFinite(parsedCurrent) ? parsedCurrent : 1;
699
+ if (current < 1) {
700
+ current = 1;
701
+ } else if (current > max) {
702
+ current = max;
703
+ }
696
704
 
697
705
  let prev = current === 1 ? null : current - 1;
698
706
  let next = current === max ? null : current + 1;
@@ -1229,7 +1237,7 @@ function getTemplate() {
1229
1237
  data-monster-attributes="
1230
1238
  class path:pagination.prevClass | prefix: previous,
1231
1239
  data-page-no path:pagination.prevNo,
1232
- href path:pagination.prevHref | prefix: #"
1240
+ href path:pagination.prevHref"
1233
1241
  data-monster-replace="path:labels.previous">Previous</a></li>
1234
1242
  <li part="summary" data-monster-role="pagination-summary">
1235
1243
  <span data-monster-replace="path:pagination.summary"></span>
@@ -1239,7 +1247,7 @@ function getTemplate() {
1239
1247
  part="next-link"
1240
1248
  data-monster-attributes="class path:pagination.nextClass | prefix: next,
1241
1249
  data-page-no path:pagination.nextNo,
1242
- href path:pagination.nextHref | prefix: #"
1250
+ href path:pagination.nextHref"
1243
1251
  data-monster-replace="path:labels.next">Next</a></li>
1244
1252
  </ul>
1245
1253
  </nav>
@@ -124,6 +124,9 @@ class HtmlTreeMenu extends CustomElement {
124
124
  onlazyloaded: null,
125
125
  onlazyerror: null,
126
126
  },
127
+ updater: {
128
+ batchUpdates: true,
129
+ },
127
130
  entries: [],
128
131
  });
129
132
  }
@@ -687,50 +690,46 @@ function applyEntryState(index, state, event) {
687
690
  event,
688
691
  });
689
692
 
690
- this.setOption("entries." + index + ".state", state);
691
- const newVisibility = state === "open" ? "visible" : "hidden";
693
+ const entries = this.getOption("entries", []);
694
+ const nextEntries = [...entries];
695
+ let changed = false;
692
696
 
693
- const entryNode = this.shadowRoot.querySelector(
694
- "[data-monster-insert-reference=entries-" + index + "]",
695
- );
697
+ nextEntries[index] = Object.assign({}, entry, {
698
+ state,
699
+ });
700
+ changed = true;
701
+ const newVisibility = state === "open" ? "visible" : "hidden";
702
+ const targetIntend = entry.intend;
703
+ const end = getSubtreeEndIndex(entries, index);
704
+ const childIntend = targetIntend + 1;
696
705
 
697
- if (entryNode?.hasAttribute(ATTRIBUTE_INTEND)) {
698
- const intend = entryNode.getAttribute(ATTRIBUTE_INTEND);
699
- let ref = entryNode.nextElementSibling;
700
- const childIntend = parseInt(intend) + 1;
706
+ for (let i = index + 1; i < end; i += 1) {
707
+ const childEntry = entries[i];
701
708
 
702
- const cmp = (a, b) => {
703
- if (state === "open") {
704
- return a === b;
709
+ if (state === "open") {
710
+ if (childEntry.intend !== childIntend) {
711
+ continue;
705
712
  }
706
-
707
- return a >= b;
708
- };
709
-
710
- while (ref?.hasAttribute(ATTRIBUTE_INTEND)) {
711
- const refIntend = ref.getAttribute(ATTRIBUTE_INTEND);
712
-
713
- if (!cmp(Number.parseInt(refIntend), childIntend)) {
714
- if (refIntend === intend) {
715
- break;
716
- }
717
- ref = ref.nextElementSibling;
713
+ if (childEntry.visibility === newVisibility) {
718
714
  continue;
719
715
  }
720
-
721
- const refIndex = ref
722
- .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
723
- .split("-")
724
- .pop();
725
-
726
- this.setOption("entries." + refIndex + ".visibility", newVisibility);
727
-
728
- if (state === "close") {
729
- this.setOption("entries." + refIndex + ".state", "close");
716
+ } else {
717
+ if (childEntry.visibility === newVisibility) {
718
+ continue;
730
719
  }
720
+ }
731
721
 
732
- ref = ref.nextElementSibling;
722
+ const updates = { visibility: newVisibility };
723
+ if (state === "close" && childEntry["has-children"]) {
724
+ updates.state = "close";
733
725
  }
726
+
727
+ nextEntries[i] = Object.assign({}, childEntry, updates);
728
+ changed = true;
729
+ }
730
+
731
+ if (changed === true) {
732
+ this.setOption("entries", nextEntries);
734
733
  }
735
734
  }
736
735
 
@@ -751,16 +750,29 @@ function setEntryVisibility(value, visibility) {
751
750
  return;
752
751
  }
753
752
 
754
- this.setOption("entries." + index + ".visibility", visibility);
755
- const intend = target.intend;
756
- for (let i = index + 1; i < entries.length; i += 1) {
757
- if (entries[i].intend <= intend) {
758
- break;
759
- }
753
+ const nextEntries = [...entries];
754
+ let changed = false;
755
+ if (target.visibility !== visibility) {
756
+ nextEntries[index] = Object.assign({}, target, {
757
+ visibility,
758
+ });
759
+ changed = true;
760
+ }
761
+ const end = getSubtreeEndIndex(entries, index);
762
+ for (let i = index + 1; i < end; i += 1) {
760
763
  if (visibility === "hidden") {
761
- this.setOption("entries." + i + ".visibility", "hidden");
764
+ if (entries[i].visibility !== "hidden") {
765
+ nextEntries[i] = Object.assign({}, entries[i], {
766
+ visibility: "hidden",
767
+ });
768
+ changed = true;
769
+ }
762
770
  }
763
771
  }
772
+
773
+ if (changed === true) {
774
+ this.setOption("entries", nextEntries);
775
+ }
764
776
  }
765
777
 
766
778
  /**
@@ -804,6 +816,9 @@ function collapseEntry(value) {
804
816
  if (!entry || entry["has-children"] === false) {
805
817
  return;
806
818
  }
819
+ if (entry.state === "close") {
820
+ return;
821
+ }
807
822
 
808
823
  const doAction = getAction.call(this, ["oncollapse", "close"]);
809
824
  if (isFunction(doAction)) {
@@ -814,15 +829,38 @@ function collapseEntry(value) {
814
829
  index,
815
830
  });
816
831
 
817
- this.setOption("entries." + index + ".state", "close");
818
-
819
832
  const entries = this.getOption("entries", []);
833
+ const nextEntries = [...entries];
834
+ let changed = false;
835
+ nextEntries[index] = Object.assign({}, entries[index], {
836
+ state: "close",
837
+ });
838
+ changed = true;
820
839
  const end = getSubtreeEndIndex(entries, index);
821
840
  for (let i = index + 1; i < end; i += 1) {
822
- this.setOption("entries." + i + ".visibility", "hidden");
823
- if (entries[i]["has-children"]) {
824
- this.setOption("entries." + i + ".state", "close");
841
+ const childEntry = entries[i];
842
+ const hasChildren = childEntry["has-children"] === true;
843
+ if (
844
+ childEntry.visibility === "hidden" &&
845
+ (!hasChildren || childEntry.state === "close")
846
+ ) {
847
+ continue;
848
+ }
849
+
850
+ const updates = {
851
+ visibility: "hidden",
852
+ };
853
+ if (hasChildren && childEntry.state !== "close") {
854
+ updates.state = "close";
825
855
  }
856
+
857
+ const updatedChild = Object.assign({}, childEntry, updates);
858
+ nextEntries[i] = updatedChild;
859
+ changed = true;
860
+ }
861
+
862
+ if (changed === true) {
863
+ this.setOption("entries", nextEntries);
826
864
  }
827
865
  }
828
866
 
@@ -990,16 +1028,37 @@ function expandEntryByIndex(index) {
990
1028
  index,
991
1029
  });
992
1030
 
993
- this.setOption("entries." + index + ".state", "open");
994
- this.setOption("entries." + index + ".visibility", "visible");
995
-
996
1031
  const entries = this.getOption("entries", []);
1032
+ const nextEntries = [...entries];
1033
+ let changed = false;
1034
+ nextEntries[index] = Object.assign({}, entry, {
1035
+ state: "open",
1036
+ visibility: "visible",
1037
+ });
1038
+ changed = true;
997
1039
  const end = getSubtreeEndIndex(entries, index);
998
1040
  for (let i = index + 1; i < end; i += 1) {
999
- this.setOption("entries." + i + ".visibility", "visible");
1000
- if (entries[i]["has-children"]) {
1001
- this.setOption("entries." + i + ".state", "open");
1041
+ const childEntry = entries[i];
1042
+ const changedVisibility = childEntry.visibility !== "visible";
1043
+ const changedState =
1044
+ childEntry["has-children"] && childEntry.state !== "open";
1045
+
1046
+ if (!changedVisibility && !changedState) {
1047
+ continue;
1002
1048
  }
1049
+
1050
+ const updatedChild = Object.assign({}, childEntry, {
1051
+ visibility: "visible",
1052
+ });
1053
+ if (childEntry["has-children"]) {
1054
+ updatedChild.state = "open";
1055
+ }
1056
+ nextEntries[i] = updatedChild;
1057
+ changed = true;
1058
+ }
1059
+
1060
+ if (changed === true) {
1061
+ this.setOption("entries", nextEntries);
1003
1062
  }
1004
1063
  }
1005
1064
 
@@ -173,6 +173,10 @@ class TreeMenu extends CustomElement {
173
173
  },
174
174
  },
175
175
 
176
+ updater: {
177
+ batchUpdates: true,
178
+ },
179
+
176
180
  data: [],
177
181
  entries: [],
178
182
  });
@@ -387,7 +391,14 @@ function initEventHandler() {
387
391
  doAction.call(this, this.getOption("entries." + index), index);
388
392
  }
389
393
 
390
- this.setOption("entries." + index + ".state", newState);
394
+ const entries = this.getOption("entries", []);
395
+ const nextEntries = [...entries];
396
+ let changed = false;
397
+
398
+ nextEntries[index] = Object.assign({}, currentEntry, {
399
+ state: newState,
400
+ });
401
+ changed = true;
391
402
  const newVisibility = newState === "open" ? "visible" : "hidden";
392
403
 
393
404
  if (container.hasAttribute(ATTRIBUTE_INTEND)) {
@@ -418,16 +429,25 @@ function initEventHandler() {
418
429
  .getAttribute(ATTRIBUTE_UPDATER_INSERT_REFERENCE)
419
430
  .split("-")
420
431
  .pop();
421
-
422
- this.setOption("entries." + refIndex + ".visibility", newVisibility);
423
-
424
- if (newState === "close") {
425
- this.setOption("entries." + refIndex + ".state", "close");
432
+ const childIndex = Number.parseInt(refIndex);
433
+ const childEntry = entries[childIndex];
434
+ if (!childEntry) {
435
+ ref = ref.nextElementSibling;
436
+ continue;
426
437
  }
427
438
 
439
+ nextEntries[childIndex] = Object.assign({}, childEntry, {
440
+ visibility: newVisibility,
441
+ ...(newState === "close" ? { state: "close" } : {}),
442
+ });
443
+ changed = true;
428
444
  ref = ref.nextElementSibling;
429
445
  }
430
446
  }
447
+
448
+ if (changed === true) {
449
+ this.setOption("entries", nextEntries);
450
+ }
431
451
  };
432
452
 
433
453
  const types = this.getOption("toggleEventType", ["click"]);
@@ -0,0 +1,99 @@
1
+
2
+ import * as chai from 'chai';
3
+ import {chaiDom} from "../../../util/chai-dom.mjs";
4
+ import {initJSDOM} from "../../../util/jsdom.mjs";
5
+
6
+ let expect = chai.expect;
7
+ chai.use(chaiDom);
8
+
9
+ let Pagination;
10
+
11
+ describe('Pagination', function () {
12
+
13
+ before(function (done) {
14
+ initJSDOM().then(() => {
15
+ import("../../../../source/components/datatable/pagination.mjs").then((m) => {
16
+ Pagination = m['Pagination'];
17
+ done();
18
+ }).catch(e => done(e));
19
+ });
20
+ });
21
+
22
+ afterEach(() => {
23
+ let mocks = document.getElementById('mocks');
24
+ mocks.innerHTML = "";
25
+ });
26
+
27
+ it('normalizes NaN and avoids NaN hrefs', function (done) {
28
+ let mocks = document.getElementById('mocks');
29
+ const control = document.createElement('monster-pagination');
30
+ mocks.appendChild(control);
31
+
32
+ setTimeout(() => {
33
+ try {
34
+ control.setPaginationState({currentPage: Number.NaN, totalPages: Number.NaN});
35
+
36
+ const pagination = control.getOption('pagination');
37
+
38
+ expect(pagination.current).to.equal(1);
39
+ expect(pagination.prevHref).to.equal('#');
40
+ expect(pagination.nextHref).to.equal('#');
41
+
42
+ const hrefs = [pagination.prevHref, pagination.nextHref, ...pagination.items.map(i => i.href)];
43
+ hrefs.forEach((href) => {
44
+ expect(href).to.not.contain('NaN');
45
+ expect(href.startsWith('##')).to.equal(false);
46
+ });
47
+ } catch (e) {
48
+ return done(e);
49
+ }
50
+
51
+ done();
52
+ }, 0);
53
+ });
54
+
55
+ it('clamps current page and does not double-prefix hash', function (done) {
56
+ let mocks = document.getElementById('mocks');
57
+ const control = document.createElement('monster-pagination');
58
+ mocks.appendChild(control);
59
+
60
+ setTimeout(() => {
61
+ try {
62
+ control.setPaginationState({currentPage: 10, totalPages: 3});
63
+
64
+ const pagination = control.getOption('pagination');
65
+ expect(pagination.current).to.equal(3);
66
+ expect(pagination.nextHref).to.equal('#');
67
+ expect(pagination.prevHref).to.equal('#page-2');
68
+
69
+ setTimeout(() => {
70
+ try {
71
+ const prevLink = control.shadowRoot.querySelector(
72
+ 'a[data-monster-role=pagination-prev]'
73
+ );
74
+ const nextLink = control.shadowRoot.querySelector(
75
+ 'a[data-monster-role=pagination-next]'
76
+ );
77
+
78
+ expect(prevLink.getAttribute('href')).to.equal('#page-2');
79
+ expect(nextLink.getAttribute('href')).to.equal('#');
80
+ } catch (e) {
81
+ return done(e);
82
+ }
83
+
84
+ done();
85
+ }, 0);
86
+ } catch (e) {
87
+ return done(e);
88
+ }
89
+ }, 0);
90
+ });
91
+
92
+ describe('document.createElement', function () {
93
+ it('should instance of Pagination', function () {
94
+ expect(document.createElement('monster-pagination')).is.instanceof(Pagination);
95
+ });
96
+ });
97
+
98
+ });
99
+
@@ -22,6 +22,7 @@ import "../cases/components/host/overlay.mjs";
22
22
  import "../cases/components/host/util.mjs";
23
23
  import "../cases/components/host/details.mjs";
24
24
  import "../cases/components/datatable/writeback-sanitizer.mjs";
25
+ import "../cases/components/datatable/pagination.mjs";
25
26
  import "../cases/text/formatter.mjs";
26
27
  import "../cases/text/generate-range-comparison-expression.mjs";
27
28
  import "../cases/text/util.mjs";