@schukai/monster 3.22.1 → 3.25.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schukai/monster",
3
- "version": "3.22.1",
3
+ "version": "3.25.0",
4
4
  "description": "Monster is a simple library for creating fast, robust and lightweight websites.",
5
5
  "keywords": [
6
6
  "framework",
@@ -16,7 +16,6 @@ export {
16
16
  ATTRIBUTE_UPDATER_SELECT_THIS,
17
17
  ATTRIBUTE_UPDATER_REPLACE,
18
18
  ATTRIBUTE_UPDATER_INSERT,
19
- ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID,
20
19
  ATTRIBUTE_UPDATER_INSERT_REFERENCE,
21
20
  ATTRIBUTE_UPDATER_REMOVE,
22
21
  ATTRIBUTE_UPDATER_BIND,
@@ -143,14 +142,6 @@ const ATTRIBUTE_UPDATER_REPLACE = `${ATTRIBUTE_PREFIX}replace`;
143
142
  */
144
143
  const ATTRIBUTE_UPDATER_INSERT = `${ATTRIBUTE_PREFIX}insert`;
145
144
 
146
- /**
147
- * @memberOf Monster.DOM
148
- * @type {string}
149
- * @license AGPLv3
150
- * @since 3.21.0
151
- */
152
- const ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID = `${ATTRIBUTE_UPDATER_INSERT}-template-id`;
153
-
154
145
  /**
155
146
  * @memberOf Monster.DOM
156
147
  * @type {string}
@@ -5,18 +5,19 @@
5
5
  * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
6
6
  */
7
7
 
8
- import { internalSymbol } from "../constants.mjs";
9
- import { extend } from "../data/extend.mjs";
10
- import { Pathfinder } from "../data/pathfinder.mjs";
8
+ import {internalSymbol} from "../constants.mjs";
9
+ import {extend} from "../data/extend.mjs";
10
+ import {Pathfinder} from "../data/pathfinder.mjs";
11
+ import {Formatter} from "../text/formatter.mjs";
11
12
 
12
- import { parseDataURL } from "../types/dataurl.mjs";
13
- import { getGlobalObject } from "../types/global.mjs";
13
+ import {parseDataURL} from "../types/dataurl.mjs";
14
+ import {getGlobalObject} from "../types/global.mjs";
14
15
  import {isArray, isFunction, isIterable, isObject, isString} from "../types/is.mjs";
15
- import { Observer } from "../types/observer.mjs";
16
- import { ProxyObserver } from "../types/proxyobserver.mjs";
17
- import { validateFunction, validateInstance, validateObject, validateString } from "../types/validate.mjs";
18
- import { clone } from "../util/clone.mjs";
19
- import { addAttributeToken, getLinkedObjects, hasObjectLink } from "./attributes.mjs";
16
+ import {Observer} from "../types/observer.mjs";
17
+ import {ProxyObserver} from "../types/proxyobserver.mjs";
18
+ import {validateFunction, validateInstance, validateObject, validateString} from "../types/validate.mjs";
19
+ import {clone} from "../util/clone.mjs";
20
+ import {addAttributeToken, getLinkedObjects, hasObjectLink} from "./attributes.mjs";
20
21
  import {
21
22
  ATTRIBUTE_DISABLED,
22
23
  ATTRIBUTE_ERRORMESSAGE,
@@ -24,9 +25,9 @@ import {
24
25
  ATTRIBUTE_OPTIONS_SELECTOR,
25
26
  customElementUpdaterLinkSymbol,
26
27
  } from "./constants.mjs";
27
- import { findDocumentTemplate, Template } from "./template.mjs";
28
- import { addObjectWithUpdaterToElement } from "./updater.mjs";
29
- import { instanceSymbol } from "../constants.mjs";
28
+ import {findDocumentTemplate, Template} from "./template.mjs";
29
+ import {addObjectWithUpdaterToElement} from "./updater.mjs";
30
+ import {instanceSymbol} from "../constants.mjs";
30
31
  import {getDocumentTranslations, Translations} from "../i18n/translations.mjs";
31
32
 
32
33
  export {
@@ -267,6 +268,7 @@ class CustomElement extends HTMLElement {
267
268
  * @property {Boolean} delegatesFocus=true A boolean that, when set to true, specifies behavior that mitigates custom element issues around focusability. When a non-focusable part of the shadow DOM is clicked, the first focusable part is given focus, and the shadow host is given any available :focus styling.
268
269
  * @property {Object} templates Templates
269
270
  * @property {string} templates.main=undefined Main template
271
+ * @property {Object} templateMapping Template mapping
270
272
  *
271
273
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
272
274
  * @since 1.8.0
@@ -279,6 +281,7 @@ class CustomElement extends HTMLElement {
279
281
  templates: {
280
282
  main: undefined,
281
283
  },
284
+ templateMapping: {},
282
285
  };
283
286
  }
284
287
 
@@ -288,7 +291,7 @@ class CustomElement extends HTMLElement {
288
291
  * The labels are defined in the options object.
289
292
  * The key of the label is used to retrieve the translation from the document.
290
293
  * If the translation is different from the label, the label is updated.
291
- *
294
+ *
292
295
  * Before you can use this method, you must have loaded the translations.
293
296
  *
294
297
  * @returns {Monster.DOM.CustomElement}
@@ -301,10 +304,10 @@ class CustomElement extends HTMLElement {
301
304
  }
302
305
 
303
306
  const labels = this.getOption("labels");
304
- if(!isIterable(labels)){
307
+ if (!isIterable(labels)) {
305
308
  return this;
306
309
  }
307
-
310
+
308
311
  for (const key in labels) {
309
312
  const text = translations.getText(key, labels[key]);
310
313
  if (text !== labels[key]) {
@@ -313,8 +316,8 @@ class CustomElement extends HTMLElement {
313
316
 
314
317
  }
315
318
  return this;
316
- }
317
-
319
+ }
320
+
318
321
  /**
319
322
  * There is no check on the name by this class. the developer is responsible for assigning an appropriate tag.
320
323
  * if the name is not valid, registerCustomElement() will issue an error
@@ -327,7 +330,7 @@ class CustomElement extends HTMLElement {
327
330
  static getTag() {
328
331
  throw new Error("the method getTag must be overwritten by the derived class.");
329
332
  }
330
-
333
+
331
334
 
332
335
  /**
333
336
  * At this point a `CSSStyleSheet` object can be returned. If the environment does not
@@ -354,7 +357,7 @@ class CustomElement extends HTMLElement {
354
357
  static getCSSStyleSheet() {
355
358
  return undefined;
356
359
  }
357
-
360
+
358
361
  /**
359
362
  * attach a new observer
360
363
  *
@@ -398,7 +401,8 @@ class CustomElement extends HTMLElement {
398
401
 
399
402
  try {
400
403
  value = new Pathfinder(this[internalSymbol].getRealSubject()["options"]).getVia(path);
401
- } catch (e) {}
404
+ } catch (e) {
405
+ }
402
406
 
403
407
  if (value === undefined) return defaultValue;
404
408
  return value;
@@ -468,7 +472,8 @@ class CustomElement extends HTMLElement {
468
472
  try {
469
473
  initShadowRoot.call(self);
470
474
  elements = self.shadowRoot.childNodes;
471
- } catch (e) {}
475
+ } catch (e) {
476
+ }
472
477
 
473
478
  try {
474
479
  initCSSStylesheet.call(this);
@@ -519,7 +524,8 @@ class CustomElement extends HTMLElement {
519
524
  * @return {void}
520
525
  * @since 1.7.0
521
526
  */
522
- disconnectedCallback() {}
527
+ disconnectedCallback() {
528
+ }
523
529
 
524
530
  /**
525
531
  * The custom element has been moved into a new document (e.g. someone called document.adoptNode(el)).
@@ -527,7 +533,8 @@ class CustomElement extends HTMLElement {
527
533
  * @return {void}
528
534
  * @since 1.7.0
529
535
  */
530
- adoptedCallback() {}
536
+ adoptedCallback() {
537
+ }
531
538
 
532
539
  /**
533
540
  * Called when an observed attribute has been added, removed, updated, or replaced. Also called for initial
@@ -816,7 +823,8 @@ function parseOptionsJSON(data) {
816
823
  try {
817
824
  let dataUrl = parseDataURL(data);
818
825
  data = dataUrl.content;
819
- } catch (e) {}
826
+ } catch (e) {
827
+ }
820
828
 
821
829
  try {
822
830
  obj = JSON.parse(data);
@@ -838,6 +846,11 @@ function initHtmlContent() {
838
846
  } catch (e) {
839
847
  let html = this.getOption("templates.main", "");
840
848
  if (isString(html) && html.length > 0) {
849
+
850
+ const mapping = this.getOption("templateMapping", {});
851
+ if (isObject(mapping)) {
852
+ html = new Formatter(mapping).format(html);
853
+ }
841
854
  this.innerHTML = html;
842
855
  }
843
856
  }
@@ -934,6 +947,11 @@ function initShadowRoot() {
934
947
  return this;
935
948
  }
936
949
 
950
+ const mapping = this.getOption("templateMapping", {});
951
+ if (isObject(mapping)) {
952
+ html = new Formatter(mapping).format(html);
953
+ }
954
+
937
955
  this.shadowRoot.innerHTML = html;
938
956
  return this;
939
957
  }
@@ -17,8 +17,7 @@ import {
17
17
  ATTRIBUTE_UPDATER_INSERT_REFERENCE,
18
18
  ATTRIBUTE_UPDATER_REMOVE,
19
19
  ATTRIBUTE_UPDATER_REPLACE,
20
- ATTRIBUTE_UPDATER_SELECT_THIS,
21
- ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID
20
+ ATTRIBUTE_UPDATER_SELECT_THIS
22
21
  } from "../dom/constants.mjs";
23
22
 
24
23
  import {Base} from "../types/base.mjs";
@@ -514,50 +513,6 @@ function insertElement(change) {
514
513
  }
515
514
  }
516
515
 
517
- /**
518
- * @private
519
- * @param container
520
- * @param key
521
- * @param ref
522
- * @param path
523
- * @returns {any}
524
- */
525
- function internalTemplateLookUp(container, key, ref, path) {
526
-
527
- let templateID = key;
528
- let template;
529
-
530
- if (container.hasAttribute(ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID)) {
531
- templateID = container.getAttribute(ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID);
532
- template = findDocumentTemplate(templateID, container);
533
- if (template instanceof HTMLTemplateElement) {
534
- return template;
535
- }
536
- }
537
-
538
- if (container.closest(`[${ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID}]`)) {
539
- templateID = container.closest(`[${ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID}]`).getAttribute(ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID);
540
- template = findDocumentTemplate(templateID, container);
541
- if (template instanceof HTMLTemplateElement) {
542
- return template;
543
- }
544
- }
545
-
546
- const rootNode = container.getRootNode();
547
- if (rootNode instanceof ShadowRoot) {
548
- const host = rootNode?.host;
549
- if (host && host.hasAttribute(ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID)) {
550
- templateID = host.getAttribute(ATTRIBUTE_UPDATER_INSERT_TEMPLATE_ID);
551
- template = findDocumentTemplate(templateID, container);
552
- if (template instanceof HTMLTemplateElement) {
553
- return template;
554
- }
555
- }
556
- }
557
-
558
- return findDocumentTemplate(templateID, container);
559
- }
560
-
561
516
  /**
562
517
  *
563
518
  * @private
@@ -570,8 +525,7 @@ function internalTemplateLookUp(container, key, ref, path) {
570
525
  * @throws {Error} no template was found with the specified key.
571
526
  */
572
527
  function appendNewDocumentFragment(container, key, ref, path) {
573
-
574
- let template = internalTemplateLookUp(container, key, ref, path);
528
+ let template = findDocumentTemplate(key, container);
575
529
 
576
530
  let nodes = template.createDocumentFragment();
577
531
  for (const [, node] of Object.entries(nodes.childNodes)) {
@@ -142,7 +142,7 @@ function getMonsterVersion() {
142
142
  }
143
143
 
144
144
  /** don't touch, replaced by make with package.json version */
145
- monsterVersion = new Version("3.22.1");
145
+ monsterVersion = new Version("3.25.0");
146
146
 
147
147
  return monsterVersion;
148
148
  }
@@ -7,7 +7,7 @@ describe('Monster', function () {
7
7
  let monsterVersion
8
8
 
9
9
  /** don´t touch, replaced by make with package.json version */
10
- monsterVersion = new Version("3.22.1")
10
+ monsterVersion = new Version("3.25.0")
11
11
 
12
12
  let m = getMonsterVersion();
13
13