@schukai/monster 4.137.4 → 4.137.6

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/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.","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.137.4"}
1
+ {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.6"},"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.137.6"}
@@ -89,6 +89,7 @@ const layoutApplySymbol = Symbol("layoutApply");
89
89
  */
90
90
  const labelStateSymbol = Symbol("labelState");
91
91
  const layoutModeSymbol = Symbol("layoutMode");
92
+ const layoutMeasurementCacheSymbol = Symbol("layoutMeasurementCache");
92
93
  const lastNavClickTimeSymbol = Symbol("lastNavClickTime");
93
94
  const lastNavClickTargetSymbol = Symbol("lastNavClickTarget");
94
95
 
@@ -919,43 +920,38 @@ function applyPaginationLayout() {
919
920
  return;
920
921
  }
921
922
 
922
- list.classList.add("pagination-no-wrap");
923
- list.setAttribute("data-monster-adaptive-ready", "false");
924
-
925
923
  const prevLink = this.shadowRoot.querySelector(
926
924
  "a[data-monster-role=pagination-prev]",
927
925
  );
928
926
  const nextLink = this.shadowRoot.querySelector(
929
927
  "a[data-monster-role=pagination-next]",
930
928
  );
929
+ const signature = getPaginationLayoutSignature.call(this, list);
930
+ const cache = getPaginationLayoutCache.call(this);
931
931
 
932
- const widthFull = applyPaginationMode.call(
933
- this,
934
- list,
935
- prevLink,
936
- nextLink,
937
- "full",
938
- );
939
- const widthNoNumbers = applyPaginationMode.call(
940
- this,
941
- list,
942
- prevLink,
943
- nextLink,
944
- "no-numbers",
945
- );
946
- const widthCompactSummary = applyPaginationMode.call(
947
- this,
948
- list,
949
- prevLink,
950
- nextLink,
951
- "compact-summary",
952
- );
953
- const widthCompact = applyPaginationMode.call(
932
+ if (
933
+ cache.lastAppliedSignature === signature &&
934
+ cache.lastAvailableWidth === availableWidth &&
935
+ cache.lastAppliedMode === this[layoutModeSymbol] &&
936
+ list.getAttribute("data-monster-adaptive-ready") === "true"
937
+ ) {
938
+ return;
939
+ }
940
+
941
+ setClassEnabled(list, "pagination-no-wrap", true);
942
+ setAttributeValue(list, "data-monster-adaptive-ready", "false");
943
+
944
+ const {
945
+ widthFull,
946
+ widthNoNumbers,
947
+ widthCompactSummary,
948
+ widthCompact,
949
+ } = measurePaginationModeWidths.call(
954
950
  this,
955
951
  list,
956
952
  prevLink,
957
953
  nextLink,
958
- "compact",
954
+ signature,
959
955
  );
960
956
 
961
957
  const nextMode = choosePaginationMode.call(this, {
@@ -967,7 +963,10 @@ function applyPaginationLayout() {
967
963
  });
968
964
 
969
965
  applyPaginationMode.call(this, list, prevLink, nextLink, nextMode);
970
- list.setAttribute("data-monster-adaptive-ready", "true");
966
+ setAttributeValue(list, "data-monster-adaptive-ready", "true");
967
+ cache.lastAppliedSignature = signature;
968
+ cache.lastAvailableWidth = availableWidth;
969
+ cache.lastAppliedMode = nextMode;
971
970
  } finally {
972
971
  this[layoutApplySymbol] = false;
973
972
  }
@@ -998,7 +997,7 @@ function setNumberItemsVisible(list, visible) {
998
997
  for (const item of numberItems) {
999
998
  const container = item.parentElement;
1000
999
  if (!container) continue;
1001
- container.style.display = visible ? "" : "none";
1000
+ setStyleValue(container, "display", visible ? "" : "none");
1002
1001
  }
1003
1002
  }
1004
1003
 
@@ -1014,7 +1013,7 @@ function setSummaryVisible(list, visible) {
1014
1013
  if (!summaryItem) {
1015
1014
  return;
1016
1015
  }
1017
- summaryItem.style.display = visible ? "flex" : "none";
1016
+ setStyleValue(summaryItem, "display", visible ? "flex" : "none");
1018
1017
  }
1019
1018
 
1020
1019
  /**
@@ -1047,11 +1046,11 @@ function setCompactLabels(compact, prevLink, nextLink) {
1047
1046
  if (!state) return;
1048
1047
 
1049
1048
  if (compact) {
1050
- if (prevLink) prevLink.innerHTML = compactPrevIcon;
1051
- if (nextLink) nextLink.innerHTML = compactNextIcon;
1049
+ setInnerHTML(prevLink, compactPrevIcon);
1050
+ setInnerHTML(nextLink, compactNextIcon);
1052
1051
  } else {
1053
- if (prevLink) prevLink.innerHTML = state.previous;
1054
- if (nextLink) nextLink.innerHTML = state.next;
1052
+ setInnerHTML(prevLink, state.previous);
1053
+ setInnerHTML(nextLink, state.next);
1055
1054
  }
1056
1055
  }
1057
1056
 
@@ -1161,29 +1160,29 @@ function getEmbeddedAvailableWidth(parentNode) {
1161
1160
  function applyPaginationMode(list, prevLink, nextLink, mode) {
1162
1161
  switch (mode) {
1163
1162
  case "compact":
1164
- list.classList.add("pagination-numbers-hidden");
1165
- list.classList.add("pagination-compact");
1163
+ setClassEnabled(list, "pagination-numbers-hidden", true);
1164
+ setClassEnabled(list, "pagination-compact", true);
1166
1165
  setNumberItemsVisible(list, false);
1167
1166
  setSummaryVisible(list, true);
1168
1167
  setCompactLabels.call(this, true, prevLink, nextLink);
1169
1168
  break;
1170
1169
  case "compact-summary":
1171
- list.classList.add("pagination-numbers-hidden");
1172
- list.classList.add("pagination-compact");
1170
+ setClassEnabled(list, "pagination-numbers-hidden", true);
1171
+ setClassEnabled(list, "pagination-compact", true);
1173
1172
  setNumberItemsVisible(list, false);
1174
1173
  setSummaryVisible(list, true);
1175
1174
  setCompactLabels.call(this, true, prevLink, nextLink);
1176
1175
  break;
1177
1176
  case "no-numbers":
1178
- list.classList.add("pagination-numbers-hidden");
1179
- list.classList.remove("pagination-compact");
1177
+ setClassEnabled(list, "pagination-numbers-hidden", true);
1178
+ setClassEnabled(list, "pagination-compact", false);
1180
1179
  setNumberItemsVisible(list, false);
1181
1180
  setSummaryVisible(list, true);
1182
1181
  setCompactLabels.call(this, false, prevLink, nextLink);
1183
1182
  break;
1184
1183
  default:
1185
- list.classList.remove("pagination-numbers-hidden");
1186
- list.classList.remove("pagination-compact");
1184
+ setClassEnabled(list, "pagination-numbers-hidden", false);
1185
+ setClassEnabled(list, "pagination-compact", false);
1187
1186
  setNumberItemsVisible(list, true);
1188
1187
  setSummaryVisible(list, false);
1189
1188
  setCompactLabels.call(this, false, prevLink, nextLink);
@@ -1193,6 +1192,172 @@ function applyPaginationMode(list, prevLink, nextLink, mode) {
1193
1192
  return list.scrollWidth;
1194
1193
  }
1195
1194
 
1195
+ /**
1196
+ * @private
1197
+ * @param {HTMLElement} list
1198
+ * @param {HTMLElement|null} prevLink
1199
+ * @param {HTMLElement|null} nextLink
1200
+ * @param {string} signature
1201
+ * @return {object}
1202
+ */
1203
+ function measurePaginationModeWidths(list, prevLink, nextLink, signature) {
1204
+ const cache = getPaginationLayoutCache.call(this);
1205
+ if (cache.widthSignature === signature && cache.widths) {
1206
+ return cache.widths;
1207
+ }
1208
+
1209
+ const widths = {
1210
+ widthFull: applyPaginationMode.call(
1211
+ this,
1212
+ list,
1213
+ prevLink,
1214
+ nextLink,
1215
+ "full",
1216
+ ),
1217
+ widthNoNumbers: applyPaginationMode.call(
1218
+ this,
1219
+ list,
1220
+ prevLink,
1221
+ nextLink,
1222
+ "no-numbers",
1223
+ ),
1224
+ widthCompactSummary: applyPaginationMode.call(
1225
+ this,
1226
+ list,
1227
+ prevLink,
1228
+ nextLink,
1229
+ "compact-summary",
1230
+ ),
1231
+ widthCompact: applyPaginationMode.call(
1232
+ this,
1233
+ list,
1234
+ prevLink,
1235
+ nextLink,
1236
+ "compact",
1237
+ ),
1238
+ };
1239
+
1240
+ cache.widthSignature = signature;
1241
+ cache.widths = widths;
1242
+ return widths;
1243
+ }
1244
+
1245
+ /**
1246
+ * @private
1247
+ * @return {object}
1248
+ */
1249
+ function getPaginationLayoutCache() {
1250
+ if (!this[layoutMeasurementCacheSymbol]) {
1251
+ this[layoutMeasurementCacheSymbol] = {
1252
+ lastAppliedMode: null,
1253
+ lastAppliedSignature: null,
1254
+ lastAvailableWidth: null,
1255
+ widthSignature: null,
1256
+ widths: null,
1257
+ };
1258
+ }
1259
+
1260
+ return this[layoutMeasurementCacheSymbol];
1261
+ }
1262
+
1263
+ /**
1264
+ * @private
1265
+ * @param {HTMLElement} list
1266
+ * @return {string}
1267
+ */
1268
+ function getPaginationLayoutSignature(list) {
1269
+ const pagination = this.getOption("pagination", {});
1270
+ const itemSignature = Array.isArray(pagination.items)
1271
+ ? pagination.items
1272
+ .map((item) => {
1273
+ return [
1274
+ item?.class || "",
1275
+ item?.href || "",
1276
+ item?.label || "",
1277
+ item?.no ?? "",
1278
+ ].join(":");
1279
+ })
1280
+ .join("|")
1281
+ : "";
1282
+
1283
+ return [
1284
+ pagination.current ?? "",
1285
+ pagination.pages ?? "",
1286
+ pagination.summary ?? "",
1287
+ pagination.prevClass ?? "",
1288
+ pagination.prevHref ?? "",
1289
+ pagination.prevNo ?? "",
1290
+ pagination.nextClass ?? "",
1291
+ pagination.nextHref ?? "",
1292
+ pagination.nextNo ?? "",
1293
+ itemSignature,
1294
+ list.children.length,
1295
+ ].join("::");
1296
+ }
1297
+
1298
+ /**
1299
+ * @private
1300
+ * @param {HTMLElement|null} element
1301
+ * @param {string} className
1302
+ * @param {boolean} enabled
1303
+ * @return {void}
1304
+ */
1305
+ function setClassEnabled(element, className, enabled) {
1306
+ if (!(element instanceof HTMLElement)) {
1307
+ return;
1308
+ }
1309
+ if (element.classList.contains(className) !== enabled) {
1310
+ element.classList.toggle(className, enabled);
1311
+ }
1312
+ }
1313
+
1314
+ /**
1315
+ * @private
1316
+ * @param {HTMLElement|null} element
1317
+ * @param {string} name
1318
+ * @param {string} value
1319
+ * @return {void}
1320
+ */
1321
+ function setAttributeValue(element, name, value) {
1322
+ if (!(element instanceof HTMLElement)) {
1323
+ return;
1324
+ }
1325
+ if (element.getAttribute(name) !== value) {
1326
+ element.setAttribute(name, value);
1327
+ }
1328
+ }
1329
+
1330
+ /**
1331
+ * @private
1332
+ * @param {HTMLElement|null} element
1333
+ * @param {string} property
1334
+ * @param {string} value
1335
+ * @return {void}
1336
+ */
1337
+ function setStyleValue(element, property, value) {
1338
+ if (!(element instanceof HTMLElement)) {
1339
+ return;
1340
+ }
1341
+ if (element.style[property] !== value) {
1342
+ element.style[property] = value;
1343
+ }
1344
+ }
1345
+
1346
+ /**
1347
+ * @private
1348
+ * @param {HTMLElement|null} element
1349
+ * @param {string} value
1350
+ * @return {void}
1351
+ */
1352
+ function setInnerHTML(element, value) {
1353
+ if (!(element instanceof HTMLElement)) {
1354
+ return;
1355
+ }
1356
+ if (element.innerHTML !== value) {
1357
+ element.innerHTML = value;
1358
+ }
1359
+ }
1360
+
1196
1361
  /**
1197
1362
  * @private
1198
1363
  * @param {object} params
@@ -129,6 +129,14 @@ const switchElementSymbol = Symbol("switchElement");
129
129
  const layoutStateSymbol = Symbol("layoutState");
130
130
  const layoutFrameSymbol = Symbol("layoutFrame");
131
131
  const layoutTokenSymbol = Symbol("layoutToken");
132
+ const observedLayoutNodesSignatureSymbol = Symbol("observedLayoutNodesSignature");
133
+
134
+ /**
135
+ * @private
136
+ * @type {WeakMap<HTMLElement, number>}
137
+ */
138
+ const layoutNodeIds = new WeakMap();
139
+ let layoutNodeId = 0;
132
140
 
133
141
  /**
134
142
  * @private
@@ -209,6 +217,7 @@ class ButtonBar extends CustomElement {
209
217
  needsLayout: true,
210
218
  needsObserve: true,
211
219
  suppressSlotChange: false,
220
+ suppressMutation: false,
212
221
  };
213
222
 
214
223
  initControlReferences.call(this);
@@ -385,6 +394,10 @@ function initEventHandler() {
385
394
  const self = this;
386
395
 
387
396
  const mutationCallback = (mutationList) => {
397
+ if (self[layoutStateSymbol]?.suppressMutation) {
398
+ return;
399
+ }
400
+
388
401
  let needsRecalc = false;
389
402
  for (const mutation of mutationList) {
390
403
  if (mutation.type === "attributes") {
@@ -572,7 +585,6 @@ function runLayout() {
572
585
  * @return {Object}
573
586
  */
574
587
  function rearrangeButtons() {
575
- const state = this[layoutStateSymbol];
576
588
  let space = 0;
577
589
  try {
578
590
  space = this[dimensionsSymbol].getVia("data.space");
@@ -660,28 +672,22 @@ function rearrangeButtons() {
660
672
  const shouldShowSwitch =
661
673
  layout.buttonsToMoveToPopper.length > 0 && hasButtons;
662
674
 
663
- if (state) {
664
- state.suppressSlotChange = true;
665
- }
675
+ suppressLayoutFeedback.call(this);
666
676
 
667
677
  for (const button of layout.buttonsToMoveToPopper) {
668
- button.setAttribute("slot", "popper");
678
+ if (button.getAttribute("slot") !== "popper") {
679
+ button.setAttribute("slot", "popper");
680
+ }
669
681
  }
670
682
 
671
683
  for (const button of layout.visibleButtonsInMainSlot) {
672
- button.removeAttribute("slot");
673
- }
674
-
675
- if (state) {
676
- state.suppressSlotChange = false;
684
+ if (button.hasAttribute("slot")) {
685
+ button.removeAttribute("slot");
686
+ }
677
687
  }
678
688
 
679
- if (shouldShowSwitch) {
680
- this[switchElementSymbol].removeAttribute("hidden");
681
- this[switchElementSymbol].classList.remove("hidden");
682
- } else {
683
- this[switchElementSymbol].setAttribute("hidden", "");
684
- this[switchElementSymbol].classList.add("hidden");
689
+ setSwitchVisible.call(this, shouldShowSwitch);
690
+ if (!shouldShowSwitch) {
685
691
  hide.call(this);
686
692
  }
687
693
  }
@@ -831,34 +837,123 @@ function calculateButtonBarDimensions() {
831
837
  * @private
832
838
  */
833
839
  function updateResizeObserverObservation() {
840
+ const observedNodes = getLayoutObservedNodes.call(this);
841
+ const signature = getLayoutObservedNodesSignature(observedNodes);
842
+ if (this[observedLayoutNodesSignatureSymbol] === signature) {
843
+ return;
844
+ }
845
+ this[observedLayoutNodesSignatureSymbol] = signature;
846
+
834
847
  this[resizeObserverSymbol].disconnect();
835
848
  if (this[mutationObserverSymbol]) {
836
849
  this[mutationObserverSymbol].disconnect();
837
850
  }
838
851
 
839
- const slottedNodes = getSlottedElements.call(this);
840
- slottedNodes.forEach((node) => {
852
+ observedNodes.forEach((node) => {
841
853
  this[resizeObserverSymbol].observe(node);
842
- if (this[mutationObserverSymbol]) {
854
+ if (node !== this.parentElement && this[mutationObserverSymbol]) {
843
855
  this[mutationObserverSymbol].observe(node, {
844
856
  attributes: true,
845
857
  attributeFilter: ["style", "class", "hidden"],
846
858
  });
847
859
  }
848
860
  });
861
+ }
849
862
 
850
- requestAnimationFrame(() => {
851
- let parent = this.parentNode;
852
- while (!(parent instanceof HTMLElement) && parent !== null) {
853
- parent = parent.parentNode;
863
+ /**
864
+ * @private
865
+ * @return {HTMLElement[]}
866
+ */
867
+ function getLayoutObservedNodes() {
868
+ const observedNodes = [];
869
+ const slottedNodes = getSlottedElements.call(this);
870
+ slottedNodes.forEach((node) => {
871
+ if (node instanceof HTMLElement) {
872
+ observedNodes.push(node);
854
873
  }
874
+ });
855
875
 
856
- if (parent instanceof HTMLElement) {
857
- this[resizeObserverSymbol].observe(parent);
858
- }
876
+ let parent = this.parentNode;
877
+ while (!(parent instanceof HTMLElement) && parent !== null) {
878
+ parent = parent.parentNode;
879
+ }
880
+
881
+ if (parent instanceof HTMLElement) {
882
+ observedNodes.push(parent);
883
+ }
884
+
885
+ return observedNodes;
886
+ }
887
+
888
+ /**
889
+ * @private
890
+ * @param {HTMLElement[]} nodes
891
+ * @return {string}
892
+ */
893
+ function getLayoutObservedNodesSignature(nodes) {
894
+ return nodes.map(getLayoutNodeId).join("|");
895
+ }
896
+
897
+ /**
898
+ * @private
899
+ * @param {HTMLElement} node
900
+ * @return {number}
901
+ */
902
+ function getLayoutNodeId(node) {
903
+ let id = layoutNodeIds.get(node);
904
+ if (id === undefined) {
905
+ id = ++layoutNodeId;
906
+ layoutNodeIds.set(node, id);
907
+ }
908
+ return id;
909
+ }
910
+
911
+ /**
912
+ * @private
913
+ * @return {void}
914
+ */
915
+ function suppressLayoutFeedback() {
916
+ const state = this[layoutStateSymbol];
917
+ if (!state) {
918
+ return;
919
+ }
920
+
921
+ state.suppressSlotChange = true;
922
+ state.suppressMutation = true;
923
+ queueMicrotask(() => {
924
+ state.suppressSlotChange = false;
925
+ state.suppressMutation = false;
859
926
  });
860
927
  }
861
928
 
929
+ /**
930
+ * @private
931
+ * @param {boolean} visible
932
+ * @return {void}
933
+ */
934
+ function setSwitchVisible(visible) {
935
+ if (!(this[switchElementSymbol] instanceof HTMLElement)) {
936
+ return;
937
+ }
938
+
939
+ if (visible) {
940
+ if (this[switchElementSymbol].hasAttribute("hidden")) {
941
+ this[switchElementSymbol].removeAttribute("hidden");
942
+ }
943
+ if (this[switchElementSymbol].classList.contains("hidden")) {
944
+ this[switchElementSymbol].classList.remove("hidden");
945
+ }
946
+ return;
947
+ }
948
+
949
+ if (!this[switchElementSymbol].hasAttribute("hidden")) {
950
+ this[switchElementSymbol].setAttribute("hidden", "");
951
+ }
952
+ if (!this[switchElementSymbol].classList.contains("hidden")) {
953
+ this[switchElementSymbol].classList.add("hidden");
954
+ }
955
+ }
956
+
862
957
  /**
863
958
  * @private
864
959
  */
@@ -866,6 +961,7 @@ function disconnectResizeObserver() {
866
961
  if (this[resizeObserverSymbol] instanceof ResizeObserver) {
867
962
  this[resizeObserverSymbol].disconnect();
868
963
  }
964
+ this[observedLayoutNodesSignatureSymbol] = null;
869
965
  }
870
966
 
871
967
  /**
@@ -282,7 +282,7 @@ function getTemplate() {
282
282
  <div data-monster-replace="path:content"></div>
283
283
  </div>
284
284
  </div>
285
-
285
+
286
286
  </div>
287
287
  `;
288
288
  }