wraplet 0.8.0 → 0.10.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/dist/AbstractWraplet.d.ts +13 -14
- package/dist/Core.d.ts +41 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/Wraplet.d.ts +3 -1
- package/dist/types/WrapletChildDefinition.d.ts +2 -1
- package/dist/types/global.d.ts +1 -1
- package/dist/utils.d.ts +2 -0
- package/package.json +18 -18
|
@@ -2,20 +2,19 @@ import { WrapletChildrenMap } from "./types/WrapletChildrenMap";
|
|
|
2
2
|
import { WrapletChildren } from "./types/WrapletChildren";
|
|
3
3
|
import { Wraplet } from "./types/Wraplet";
|
|
4
4
|
import { DeepWriteable } from "./types/Utils";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import Core from "./Core";
|
|
6
|
+
export type CommonMethods = {
|
|
7
|
+
destroy: {};
|
|
8
|
+
};
|
|
9
|
+
export declare abstract class AbstractWraplet<M extends WrapletChildrenMap = {}, N extends Node = Node, CM extends CommonMethods = CommonMethods> implements Wraplet<N> {
|
|
7
10
|
isWraplet: true;
|
|
8
|
-
|
|
9
|
-
* This is the log of all element accessors, available for easier debugging.
|
|
10
|
-
*/
|
|
11
|
-
private __debugElementAccessors;
|
|
12
|
-
protected children: WrapletChildren<T>;
|
|
11
|
+
protected core: Core<M, N, CM>;
|
|
13
12
|
protected static debug: boolean;
|
|
14
|
-
constructor(
|
|
15
|
-
|
|
16
|
-
protected
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
protected static createWraplets<T extends AbstractWraplet<any> = never>(
|
|
13
|
+
constructor(node: N, mapAlterCallback?: ((map: DeepWriteable<M>) => void) | null);
|
|
14
|
+
protected get node(): N;
|
|
15
|
+
protected get children(): WrapletChildren<M>;
|
|
16
|
+
accessNode(callback: (node: N) => void): void;
|
|
17
|
+
destroy(): void;
|
|
18
|
+
protected abstract defineChildrenMap(): M;
|
|
19
|
+
protected static createWraplets<N extends Node, T extends AbstractWraplet<any, N> = never>(node: ParentNode, selector: string, additional_args?: unknown[]): T[];
|
|
21
20
|
}
|
package/dist/Core.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { WrapletChildren } from "./types/WrapletChildren";
|
|
2
|
+
import { Wraplet } from "./types/Wraplet";
|
|
3
|
+
import { WrapletChildrenMap } from "./types/WrapletChildrenMap";
|
|
4
|
+
import { CommonMethods } from "./AbstractWraplet";
|
|
5
|
+
export default class Core<M extends WrapletChildrenMap = {}, N extends Node = Node, CM extends CommonMethods = CommonMethods> {
|
|
6
|
+
node: N;
|
|
7
|
+
private map;
|
|
8
|
+
private wraplet;
|
|
9
|
+
/**
|
|
10
|
+
* This is the log of all node accessors, available for easier debugging.
|
|
11
|
+
*/
|
|
12
|
+
private __debugNodeAccessors;
|
|
13
|
+
private isDestroyed;
|
|
14
|
+
private listeners;
|
|
15
|
+
children: WrapletChildren<M>;
|
|
16
|
+
constructor(node: N, map: M, wraplet: Wraplet<N>);
|
|
17
|
+
instantiateChildren(node: N): WrapletChildren<M>;
|
|
18
|
+
accessNode(callback: (node: N) => void): void;
|
|
19
|
+
createWraplet(wrapletClass: new (...args: any[]) => Wraplet<N>, childElement: Node, args?: unknown[]): Wraplet<N>;
|
|
20
|
+
/**
|
|
21
|
+
* This method allows executing the specified method on the wraplet and all its children.
|
|
22
|
+
* The original wraplet and all children need to have this method implemented.
|
|
23
|
+
*/
|
|
24
|
+
executeOnChildren(children: WrapletChildren<M>, method: keyof CM & string, payload?: CM[keyof CM]): void;
|
|
25
|
+
getChildrenMap(): M;
|
|
26
|
+
/**
|
|
27
|
+
* This method removes from nodes references to this wraplet and its children recuresively.
|
|
28
|
+
*
|
|
29
|
+
* @protected
|
|
30
|
+
*/
|
|
31
|
+
destroy(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Remove the wraplet from the list of wraplets.
|
|
34
|
+
*/
|
|
35
|
+
removeWrapletFromNode(wraplet: Wraplet<N>, node: N): void;
|
|
36
|
+
private addDefaultsToChildDefinition;
|
|
37
|
+
private wrapletHasMethodGuard;
|
|
38
|
+
private isParentNode;
|
|
39
|
+
private childTypeGuard;
|
|
40
|
+
addEventListener(node: Node, eventName: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
|
|
41
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.wraplet=t():e.wraplet=t()}(self,(
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.wraplet=t():e.wraplet=t()}(self,()=>(()=>{"use strict";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{AbstractWraplet:()=>i,isWraplet:()=>n});class r extends Error{}class o extends Error{}function n(e){return"object"==typeof e&&null!==e&&"isWraplet"in e&&!0===e.isWraplet}class s{node;map;wraplet;__debugNodeAccessors=[];isDestroyed=!1;listeners=[];children;constructor(e,t,r){this.node=e,this.map=t,this.wraplet=r,this.children=this.instantiateChildren(e),this.node.wraplets||(this.node.wraplets=[]),this.node.wraplets.push(r)}instantiateChildren(e){const t={};if(!this.isParentNode(e)){if(Object.keys(this.map).length>0)throw new o("If the node provided cannot have children, the children map should be empty.");return t}for(const n in this.map){const s=this.map[n],i=s.selector,a=s.Class,l=s.args||[],c=s.multiple,d=s.required;if(!i){if(d)throw new o(`${this.constructor.name}: Child "${n}" cannot at the same be required and have no selector.`);t[n]=c?[]:null;continue}const h=e.querySelectorAll(i);if(0===h.length){if(d)throw new r(`${this.constructor.name}: Couldn't find a node for the wraplet "${n}". Selector used: "${i}".`);this.constructor.debug&&console.log(`${this.constructor.name}: Optional child '${n}' has not been found. Selector used: "${i}"`),t[n]=c?[]:null;continue}const u=[];for(const e of h)if(u.push(this.createWraplet(a,e,l)),!c)break;const p=c?u:u[0];if(c&&!p&&this.constructor.debug&&console.log(`${this.constructor.name}: no items for the multiple child '${n}' have been found. Selector used: "${i}"`),!this.childTypeGuard(p,n)){if(void 0===p)throw new Error(`${this.constructor.name}: Couldn't intantionate the "${n}" child. Selector used: "${i}".`);throw new Error(`${this.constructor.name}: Child value doesn't match the map. Value: ${p}. Expected: ${this.map[n].Class.name}`)}t[n]=p}return t}accessNode(e){this.__debugNodeAccessors.push(e),e(this.node)}createWraplet(e,t,r=[]){return new e(...[t,...r])}executeOnChildren(e,t,r){for(const o of Object.entries(e)){const e=o[0],s=o[1];if(this.getChildrenMap()[e].destructable)if(Array.isArray(s))for(const o of s){if(!n(o))throw new Error("Internal logic error. Item is not a wraplet.");if(!this.wrapletHasMethodGuard(o,t))throw new Error(`Internal logic error. Action "${String(t)}" is not defined for the child "${e}".`);r?o[t](r):o[t]()}else if(n(s)){if(!this.wrapletHasMethodGuard(s,t))throw new Error(`Internal logic error. Action "${String(t)}" is not defined for the child "${e}".`);r?s[t](r):s[t]()}}}getChildrenMap(){const e={},t=this.map;for(const r in t)e[r]=this.addDefaultsToChildDefinition(t[r]);return e}destroy(){if(this.isDestroyed)throw new Error("Wraplet is already destroyed.");for(const e of this.listeners){const t=e.node,r=e.eventName,o=e.callback,n=e.options;t.removeEventListener(r,o,n)}this.removeWrapletFromNode(this.wraplet,this.node),this.executeOnChildren(this.children,"destroy"),this.isDestroyed=!0}removeWrapletFromNode(e,t){const r=t.wraplets?.findIndex(t=>t===e);void 0!==r&&r>-1&&t.wraplets?.splice(r,1)}addDefaultsToChildDefinition(e){return{args:[],destructable:!0,...e}}wrapletHasMethodGuard(e,t){return"function"==typeof e[t]}isParentNode(e){return"function"==typeof e.querySelectorAll}childTypeGuard(e,t){const r=this.getChildrenMap(),o=r[t].Class,n=r[t].required;return r[t].multiple?!!Array.isArray(e)&&(!n||e.every(e=>e instanceof o)):n?e instanceof o:e instanceof o||null===e}addEventListener(e,t,r,o){this.listeners.push({node:e,eventName:t,callback:r,options:o}),e.addEventListener(t,r,o)}}class i{isWraplet=!0;core;static debug=!1;constructor(e,t=null){if(!e)throw new Error("Node is required to create a wraplet.");const r=this.defineChildrenMap();t&&t(r),this.core=new s(e,r,this)}get node(){return this.core.node}get children(){return this.core.children}accessNode(e){this.core.accessNode(e)}destroy(){this.core.destroy()}static createWraplets(e,t,r=[]){if(this instanceof i)throw new Error("You cannot instantiate an abstract class.");const o=[],n=e.querySelectorAll(t);for(const e of n)o.push(new this(e,...r));return o}}return t})());
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAiB,QAAID,IAErBD,EAAc,QAAIC,GACnB,CATD,CASGK,MAAM,I,mBCRT,IAAIC,EAAsB,CCA1BA,EAAwB,CAACL,EAASM,KACjC,IAAI,IAAIC,KAAOD,EACXD,EAAoBG,EAAEF,EAAYC,KAASF,EAAoBG,EAAER,EAASO,IAC5EE,OAAOC,eAAeV,EAASO,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,IAE1E,ECNDF,EAAwB,CAACQ,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFT,EAAyBL,IACH,o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/bootstrap","webpack://wraplet/webpack/runtime/define property getters","webpack://wraplet/webpack/runtime/hasOwnProperty shorthand","webpack://wraplet/webpack/runtime/make namespace object","webpack://wraplet/./src/errors.ts","webpack://wraplet/./src/AbstractWraplet.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"wraplet\"] = factory();\n\telse\n\t\troot[\"wraplet\"] = factory();\n})(self, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export class MissingRequiredChildError extends Error {\n}\nexport class MapError extends Error {\n}\n","import { MapError, MissingRequiredChildError } from \"./errors\";\nexport class AbstractWraplet {\n constructor(element, mapAlterCallback = null) {\n this.element = element;\n this.isWraplet = true;\n /**\n * This is the log of all element accessors, available for easier debugging.\n */\n this.__debugElementAccessors = [];\n if (!element) {\n throw new Error(\"Element is required to create a wraplet.\");\n }\n const map = this.defineChildrenMap();\n if (mapAlterCallback) {\n mapAlterCallback(map);\n }\n this.children = this.instantiateChildren(map);\n if (!this.element.wraplets) {\n this.element.wraplets = [];\n }\n this.element.wraplets.push(this);\n }\n accessElement(callback) {\n this.__debugElementAccessors.push(callback);\n callback(this.element);\n }\n instantiateChildren(map) {\n const children = {};\n for (const id in map) {\n const item = map[id];\n const selector = item.selector;\n const wrapletClass = item.Class;\n const args = item.args || [];\n const multiple = item.multiple;\n const isRequired = item.required;\n if (!selector) {\n if (isRequired) {\n throw new MapError(`${this.constructor.name}: Child \"${id}\" cannot at the same be required and have no selector.`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childElements = this.element.querySelectorAll(selector);\n if (childElements.length === 0) {\n if (isRequired) {\n throw new MissingRequiredChildError(`${this.constructor.name}: Couldn't find an element for the wraplet \"${id}\". Selector used: \"${selector}\".`);\n }\n if (this.constructor.debug) {\n console.log(`${this.constructor.name}: Optional child '${id}' has not been found. Selector used: \"${selector}\"`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childWraplet = [];\n for (const childElement of childElements) {\n childWraplet.push(this.createWraplet(wrapletClass, childElement, args));\n if (!multiple) {\n break;\n }\n }\n const value = multiple\n ? childWraplet\n : childWraplet[0];\n if (multiple && !value && this.constructor.debug) {\n console.log(`${this.constructor.name}: no items for the multiple child '${id}' have been found. Selector used: \"${selector}\"`);\n }\n if (!this.childTypeGuard(value, id)) {\n if (typeof value === \"undefined\") {\n throw new Error(`${this.constructor.name}: Couldn't intantionate the \"${id}\" child. Selector used: \"${selector}\".`);\n }\n throw new Error(`${this.constructor.name}: Child value doesn't match the map. Value: ${value}. Expected: ${map[id][\"Class\"].name}`);\n }\n children[id] = value;\n }\n // Now we should have all properties set, so let's assert the final form.\n return children;\n }\n createWraplet(wrapletClass, childElement, args = []) {\n return new wrapletClass(...[...[childElement], ...args]);\n }\n childTypeGuard(variable, id) {\n const map = this.defineChildrenMap();\n const Class = map[id].Class;\n const isRequired = map[id].required;\n const isMultiple = map[id].multiple;\n if (isMultiple) {\n const isArray = Array.isArray(variable);\n if (!isArray) {\n return false;\n }\n if (isRequired) {\n return variable.every((value) => value instanceof Class);\n }\n return true;\n }\n if (isRequired) {\n return variable instanceof Class;\n }\n return variable instanceof Class || variable === null;\n }\n // We can afford \"any\" here because this method is only for the external usage, and external\n // callers don't need to know what map is the current wraplet using, as it's its internal\n // matter.\n static createWraplets(document, additional_args = [], selector) {\n if (this instanceof AbstractWraplet) {\n throw new Error(\"You cannot instantiate an abstract class.\");\n }\n const result = [];\n const foundElements = document.querySelectorAll(selector);\n for (const element of foundElements) {\n result.push(new this(element, ...additional_args));\n }\n return result;\n }\n}\nAbstractWraplet.debug = false;\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","MissingRequiredChildError","Error","MapError","AbstractWraplet","constructor","element","mapAlterCallback","this","isWraplet","__debugElementAccessors","map","defineChildrenMap","children","instantiateChildren","wraplets","push","accessElement","callback","id","item","selector","wrapletClass","Class","args","multiple","isRequired","required","name","childElements","querySelectorAll","length","debug","console","log","childWraplet","childElement","createWraplet","childTypeGuard","variable","Array","isArray","every","createWraplets","document","additional_args","result","foundElements"],"sourceRoot":""}
|
|
1
|
+
{"version":3,"file":"index.js","mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAiB,QAAID,IAErBD,EAAc,QAAIC,GACnB,CATD,CASGK,KAAM,I,mBCRT,IAAIC,EAAsB,CCA1BA,EAAwB,CAACL,EAASM,KACjC,IAAI,IAAIC,KAAOD,EACXD,EAAoBG,EAAEF,EAAYC,KAASF,EAAoBG,EAAER,EAASO,IAC5EE,OAAOC,eAAeV,EAASO,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,MCJ3EF,EAAwB,CAACQ,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFT,EAAyBL,IACH,oBAAXkB,QAA0BA,OAAOC,aAC1CV,OAAOC,eAAeV,EAASkB,OAAOC,YA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AEPuB,MAAMC,QAAQe,MAI1BtC,GACOsC,EAASC,MAAOnE,GAAUA,aAAiByB,IAItDG,EACOsC,aAAoBzC,EAExByC,aAAoBzC,GAAsB,OAAbyC,CACxC,CACA,gBAAAE,CAAiB9D,EAAMoD,EAAWjB,EAAUkB,GACxC7C,KAAKH,UAAUM,KAAK,CAAEX,OAAMoD,YAAWjB,WAAUkB,YACjDrD,EAAK8D,iBAAiBV,EAAWjB,EAAUkB,EAC/C,EClNG,MAAMU,EACTjE,WAAY,EACZkE,KACAC,cAAe,EACf,WAAA1D,CAAYP,EAAMkE,EAAmB,MACjC,IAAKlE,EACD,MAAM,IAAIJ,MAAM,yCAEpB,MAAMK,EAAMO,KAAK2D,oBACbD,GACAA,EAAiBjE,GAErBO,KAAKwD,KAAO,IAAIjE,EAAKC,EAAMC,EAAKO,KACpC,CACA,QAAIR,GACA,OAAOQ,KAAKwD,KAAKhE,IACrB,CACA,YAAIM,GACA,OAAOE,KAAKwD,KAAK1D,QACrB,CACA,UAAA4B,CAAWC,GACP3B,KAAKwD,KAAK9B,WAAWC,EACzB,CACA,OAAAe,GACI1C,KAAKwD,KAAKd,SACd,CAIA,qBAAOkB,CAAepE,EAAMiB,EAAUoD,EAAkB,IACpD,GAAI7D,gBAAgBuD,EAChB,MAAM,IAAInE,MAAM,6CAEpB,MAAM0E,EAAS,GACTC,EAAgBvE,EAAK0B,iBAAiBT,GAC5C,IAAK,MAAMuD,KAAWD,EAClBD,EAAO3D,KAAK,IAAIH,KAAKgE,KAAYH,IAErC,OAAOC,CACX,E","sources":["webpack://wraplet/webpack/universalModuleDefinition","webpack://wraplet/webpack/bootstrap","webpack://wraplet/webpack/runtime/define property getters","webpack://wraplet/webpack/runtime/hasOwnProperty shorthand","webpack://wraplet/webpack/runtime/make namespace object","webpack://wraplet/./src/errors.ts","webpack://wraplet/./src/utils.ts","webpack://wraplet/./src/Core.ts","webpack://wraplet/./src/AbstractWraplet.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"wraplet\"] = factory();\n\telse\n\t\troot[\"wraplet\"] = factory();\n})(self, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export class MissingRequiredChildError extends Error {\n}\nexport class MapError extends Error {\n}\n","export function isWraplet(obj) {\n return (typeof obj === \"object\" &&\n obj !== null &&\n \"isWraplet\" in obj &&\n obj.isWraplet === true);\n}\n","import { MapError, MissingRequiredChildError } from \"./errors\";\nimport { isWraplet } from \"./utils\";\nexport default class Core {\n node;\n map;\n wraplet;\n /**\n * This is the log of all node accessors, available for easier debugging.\n */\n __debugNodeAccessors = [];\n isDestroyed = false;\n listeners = [];\n children;\n constructor(node, map, wraplet) {\n this.node = node;\n this.map = map;\n this.wraplet = wraplet;\n this.children = this.instantiateChildren(node);\n if (!this.node.wraplets) {\n this.node.wraplets = [];\n }\n this.node.wraplets.push(wraplet);\n }\n instantiateChildren(node) {\n const children = {};\n // We check if are dealing with the ParentNode object.\n if (!this.isParentNode(node)) {\n if (Object.keys(this.map).length > 0) {\n throw new MapError(\"If the node provided cannot have children, the children map should be empty.\");\n }\n return children;\n }\n for (const id in this.map) {\n const item = this.map[id];\n const selector = item.selector;\n const wrapletClass = item.Class;\n const args = item.args || [];\n const multiple = item.multiple;\n const isRequired = item.required;\n if (!selector) {\n if (isRequired) {\n throw new MapError(`${this.constructor.name}: Child \"${id}\" cannot at the same be required and have no selector.`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childElements = node.querySelectorAll(selector);\n if (childElements.length === 0) {\n if (isRequired) {\n throw new MissingRequiredChildError(`${this.constructor.name}: Couldn't find a node for the wraplet \"${id}\". Selector used: \"${selector}\".`);\n }\n if (this.constructor.debug) {\n console.log(`${this.constructor.name}: Optional child '${id}' has not been found. Selector used: \"${selector}\"`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childWraplet = [];\n for (const childElement of childElements) {\n childWraplet.push(this.createWraplet(wrapletClass, childElement, args));\n if (!multiple) {\n break;\n }\n }\n const value = multiple\n ? childWraplet\n : childWraplet[0];\n if (multiple && !value && this.constructor.debug) {\n console.log(`${this.constructor.name}: no items for the multiple child '${id}' have been found. Selector used: \"${selector}\"`);\n }\n if (!this.childTypeGuard(value, id)) {\n if (typeof value === \"undefined\") {\n throw new Error(`${this.constructor.name}: Couldn't intantionate the \"${id}\" child. Selector used: \"${selector}\".`);\n }\n throw new Error(`${this.constructor.name}: Child value doesn't match the map. Value: ${value}. Expected: ${this.map[id][\"Class\"].name}`);\n }\n children[id] = value;\n }\n // Now we should have all properties set, so let's assert the final form.\n return children;\n }\n accessNode(callback) {\n this.__debugNodeAccessors.push(callback);\n callback(this.node);\n }\n createWraplet(wrapletClass, childElement, args = []) {\n return new wrapletClass(...[...[childElement], ...args]);\n }\n /**\n * This method allows executing the specified method on the wraplet and all its children.\n * The original wraplet and all children need to have this method implemented.\n */\n executeOnChildren(children, method, payload) {\n for (const childEntries of Object.entries(children)) {\n const name = childEntries[0];\n const child = childEntries[1];\n const map = this.getChildrenMap();\n if (!map[name].destructable) {\n continue;\n }\n if (Array.isArray(child)) {\n for (const item of child) {\n if (!isWraplet(item)) {\n throw new Error(\"Internal logic error. Item is not a wraplet.\");\n }\n if (!this.wrapletHasMethodGuard(item, method)) {\n throw new Error(`Internal logic error. Action \"${String(method)}\" is not defined for the child \"${name}\".`);\n }\n if (payload) {\n item[method](payload);\n }\n else {\n item[method]();\n }\n }\n }\n else if (isWraplet(child)) {\n if (!this.wrapletHasMethodGuard(child, method)) {\n throw new Error(`Internal logic error. Action \"${String(method)}\" is not defined for the child \"${name}\".`);\n }\n if (payload) {\n child[method](payload);\n }\n else {\n child[method]();\n }\n }\n }\n }\n getChildrenMap() {\n const map = {};\n const definedMap = this.map;\n for (const id in definedMap) {\n map[id] = this.addDefaultsToChildDefinition(definedMap[id]);\n }\n return map;\n }\n /**\n * This method removes from nodes references to this wraplet and its children recuresively.\n *\n * @protected\n */\n destroy() {\n if (this.isDestroyed) {\n throw new Error(\"Wraplet is already destroyed.\");\n }\n // Remove listeners.\n for (const listener of this.listeners) {\n const node = listener.node;\n const eventName = listener.eventName;\n const callback = listener.callback;\n const options = listener.options;\n node.removeEventListener(eventName, callback, options);\n }\n this.removeWrapletFromNode(this.wraplet, this.node);\n this.executeOnChildren(this.children, \"destroy\");\n this.isDestroyed = true;\n }\n /**\n * Remove the wraplet from the list of wraplets.\n */\n removeWrapletFromNode(wraplet, node) {\n const index = node.wraplets?.findIndex((value) => {\n return value === wraplet;\n });\n if (index !== undefined && index > -1) {\n node.wraplets?.splice(index, 1);\n }\n }\n addDefaultsToChildDefinition(definition) {\n return {\n ...{\n args: [],\n destructable: true,\n },\n ...definition,\n };\n }\n wrapletHasMethodGuard(wraplet, method) {\n return typeof wraplet[method] === \"function\";\n }\n isParentNode(node) {\n return typeof node.querySelectorAll === \"function\";\n }\n childTypeGuard(variable, id) {\n const map = this.getChildrenMap();\n const Class = map[id].Class;\n const isRequired = map[id].required;\n const isMultiple = map[id].multiple;\n if (isMultiple) {\n const isArray = Array.isArray(variable);\n if (!isArray) {\n return false;\n }\n if (isRequired) {\n return variable.every((value) => value instanceof Class);\n }\n return true;\n }\n if (isRequired) {\n return variable instanceof Class;\n }\n return variable instanceof Class || variable === null;\n }\n addEventListener(node, eventName, callback, options) {\n this.listeners.push({ node, eventName, callback, options });\n node.addEventListener(eventName, callback, options);\n }\n}\n","import Core from \"./Core\";\nexport class AbstractWraplet {\n isWraplet = true;\n core;\n static debug = false;\n constructor(node, mapAlterCallback = null) {\n if (!node) {\n throw new Error(\"Node is required to create a wraplet.\");\n }\n const map = this.defineChildrenMap();\n if (mapAlterCallback) {\n mapAlterCallback(map);\n }\n this.core = new Core(node, map, this);\n }\n get node() {\n return this.core.node;\n }\n get children() {\n return this.core.children;\n }\n accessNode(callback) {\n this.core.accessNode(callback);\n }\n destroy() {\n this.core.destroy();\n }\n // We can afford \"any\" here because this method is only for the external usage, and external\n // callers don't need to know what map is the current wraplet using, as it's its internal\n // matter.\n static createWraplets(node, selector, additional_args = []) {\n if (this instanceof AbstractWraplet) {\n throw new Error(\"You cannot instantiate an abstract class.\");\n }\n const result = [];\n const foundElements = node.querySelectorAll(selector);\n for (const element of foundElements) {\n result.push(new this(element, ...additional_args));\n }\n return result;\n }\n}\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","MissingRequiredChildError","Error","MapError","isWraplet","Core","node","map","wraplet","__debugNodeAccessors","isDestroyed","listeners","children","constructor","this","instantiateChildren","wraplets","push","isParentNode","keys","length","id","item","selector","wrapletClass","Class","args","multiple","isRequired","required","name","childElements","querySelectorAll","debug","console","log","childWraplet","childElement","createWraplet","childTypeGuard","accessNode","callback","executeOnChildren","method","payload","childEntries","entries","child","getChildrenMap","destructable","Array","isArray","wrapletHasMethodGuard","String","definedMap","addDefaultsToChildDefinition","destroy","listener","eventName","options","removeEventListener","removeWrapletFromNode","index","findIndex","undefined","splice","variable","every","addEventListener","AbstractWraplet","core","static","mapAlterCallback","defineChildrenMap","createWraplets","additional_args","result","foundElements","element"],"sourceRoot":""}
|
package/dist/types/Wraplet.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbstractWraplet } from "../AbstractWraplet";
|
|
2
|
-
export type WrapletChildDefinition<T extends AbstractWraplet = AbstractWraplet
|
|
2
|
+
export type WrapletChildDefinition<T extends AbstractWraplet<any, any, any> = AbstractWraplet<any, any, any>> = {
|
|
3
3
|
selector?: string;
|
|
4
4
|
Class: {
|
|
5
5
|
new (...args: any[]): T;
|
|
@@ -7,4 +7,5 @@ export type WrapletChildDefinition<T extends AbstractWraplet = AbstractWraplet>
|
|
|
7
7
|
required: boolean;
|
|
8
8
|
multiple: boolean;
|
|
9
9
|
args?: unknown[];
|
|
10
|
+
destructable?: boolean;
|
|
10
11
|
};
|
package/dist/types/global.d.ts
CHANGED
package/dist/utils.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wraplet",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,22 +22,22 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"typescript": "^5.
|
|
26
|
-
"ts-loader": "^9.5.
|
|
27
|
-
"@eslint/js": "^9.
|
|
28
|
-
"eslint": "^9.
|
|
29
|
-
"globals": "^
|
|
30
|
-
"typescript-eslint": "^8.
|
|
31
|
-
"webpack": "^5.
|
|
32
|
-
"webpack-cli": "^
|
|
33
|
-
"prettier": "^3.
|
|
34
|
-
"eslint-config-prettier": "^
|
|
35
|
-
"eslint-plugin-prettier": "^5.
|
|
36
|
-
"prettier-eslint": "^16.
|
|
37
|
-
"jest": "^
|
|
38
|
-
"jest-cli": "^
|
|
39
|
-
"ts-jest": "^29.
|
|
40
|
-
"@types/jest": "^
|
|
41
|
-
"jest-environment-jsdom": "^
|
|
25
|
+
"typescript": "^5.8.3",
|
|
26
|
+
"ts-loader": "^9.5.2",
|
|
27
|
+
"@eslint/js": "^9.29.0",
|
|
28
|
+
"eslint": "^9.29.0",
|
|
29
|
+
"globals": "^16.2.0",
|
|
30
|
+
"typescript-eslint": "^8.35.0",
|
|
31
|
+
"webpack": "^5.99.9",
|
|
32
|
+
"webpack-cli": "^6.0.1",
|
|
33
|
+
"prettier": "^3.6.1",
|
|
34
|
+
"eslint-config-prettier": "^10.1.5",
|
|
35
|
+
"eslint-plugin-prettier": "^5.5.1",
|
|
36
|
+
"prettier-eslint": "^16.4.2",
|
|
37
|
+
"jest": "^30.0.3",
|
|
38
|
+
"jest-cli": "^30.0.3",
|
|
39
|
+
"ts-jest": "^29.4.0",
|
|
40
|
+
"@types/jest": "^30.0.0",
|
|
41
|
+
"jest-environment-jsdom": "^30.0.2"
|
|
42
42
|
}
|
|
43
43
|
}
|