native-document 1.0.28 → 1.0.30
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/native-document.dev.js +144 -2
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.min.js +1 -1
- package/index.d.ts +4 -0
- package/index.js +1 -0
- package/package.json +2 -3
- package/readme.md +2 -2
- package/src/data/Observable.js +0 -2
- package/src/data/ObservableItem.js +13 -6
- package/src/data/observable-helpers/array.js +3 -0
- package/src/data/observable-helpers/computed.js +3 -0
- package/src/utils/args-types.js +10 -1
- package/src/utils/plugins-manager.js +66 -5
- package/src/utils/property-accumulator.js +43 -0
- package/src/wrappers/ElementCreator.js +7 -0
- package/src/wrappers/NDElement.js +2 -0
- package/types/args-types.d.ts +58 -0
- package/types/plugins-manager.d.ts +65 -0
- package/types/polyfill.d.ts +10 -0
- package/types/property-accumulator.d.ts +33 -0
- package/types/validator.ts +55 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
var NativeDocument=function(t){"use strict";let n={};n={log(){},warn(){},error(){},disable(){}};var r=n;const o=function(){let e=0;const t=new Map;return{register(n){const r=++e;return t.set(r,new WeakRef(n)),r},unregister(e){t.delete(e)},getObservableById:e=>t.get(e)?.deref(),cleanup(){for(const[e,n]of t){const e=n.deref();e&&e.cleanup()}t.clear()},cleanObservables(e){if(t.size<e)return;let n=0;for(const[e,r]of t)r.deref()||(t.delete(e),n++);n>0&&r.log("Memory Auto Clean",`🧹 Cleaned ${n} orphaned observables`)}}}();class s extends Error{constructor(e,t={}){super(e),this.name="NativeDocumentError",this.context=t,this.timestamp=(new Date).toISOString()}}function i(e,t){this.observable=e,this.checker=t,this.unSubscriptions=[]}function a(e){this.$previousValue=e,this.$currentValue=e,this.$isCleanedUp=!1,this.$listeners=null,this.$watchers=null,this.$memoryId=null}i.prototype.__$isObservableChecker=!0,i.prototype.subscribe=function(e){const t=this.observable.subscribe(t=>{e&&e(this.checker(t))});return this.unSubscriptions.push(t),t},i.prototype.check=function(e){return this.observable.check(()=>e(this.val()))},i.prototype.val=function(){return this.checker&&this.checker(this.observable.val())},i.prototype.set=function(e){return this.observable.set(e)},i.prototype.trigger=function(){return this.observable.trigger()},i.prototype.cleanup=function(){return this.observable.cleanup()},Object.defineProperty(a.prototype,"$value",{get(){return this.$currentValue},set(e){this.set(e)},configurable:!0}),a.prototype.__$isObservable=!0;const u=function(){};a.prototype.triggerListeners=function(e){const t=this.$listeners,n=this.$previousValue,r=this.$currentValue;if(e=e||{},t?.length)for(let o=0,s=t.length;o<s;o++)t[o](r,n,e)},a.prototype.triggerWatchers=function(){if(!this.$watchers)return;const e=this.$watchers,t=this.$previousValue,n=this.$currentValue;e.has(n)&&e.get(n).forEach(e=>{e.set?e.set(!0):e(!0)}),e.has(t)&&e.get(t).forEach(e=>{e.set?e.set(!1):e(!1)})},a.prototype.triggerAll=function(e){this.triggerListeners(e),this.triggerWatchers()},a.prototype.assocTrigger=function(){this.$watchers?.size&&this.$listeners?.length?this.trigger=this.triggerAll:this.$listeners?.length?this.trigger=this.triggerListeners:this.$watchers?.size?this.trigger=this.triggerWatchers:this.trigger=u},a.prototype.trigger=u,a.prototype.set=function(e){const t="function"==typeof e?e(this.$currentValue):e;this.$currentValue!==t&&(this.$previousValue=this.$currentValue,this.$currentValue=t,this.trigger())},a.prototype.val=function(){return this.$currentValue},a.prototype.disconnectAll=function(){if(this.$listeners?.splice(0),this.$previousValue=null,this.$currentValue=null,this.$watchers)for(const[e,t]of this.$watchers)t.splice(0);this.$watchers?.clear(),this.$listeners=null,this.$watchers=null,this.trigger=u},a.prototype.cleanup=function(){o.unregister(this.$memoryId),this.disconnectAll(),this.$isCleanedUp=!0,delete this.$value},a.prototype.subscribe=function(e){if(this.$listeners=this.$listeners??[],this.$isCleanedUp)return r.warn("Observable subscription","⚠️ Attempted to subscribe to a cleaned up observable."),()=>{};if("function"!=typeof e)throw new s("Callback must be a function");return this.$listeners.push(e),this.assocTrigger(),()=>{this.unsubscribe(e),this.assocTrigger()}},a.prototype.on=function(e,t){this.$watchers=this.$watchers??new Map;let n=this.$watchers.get(e);return n||(n=[],this.$watchers.set(e,n)),n.push(t),this.assocTrigger(),()=>{const r=n.indexOf(t);n?.splice(r,1),0===n.size&&(this.$watchers?.delete(e),n=null),this.assocTrigger()}},a.prototype.unsubscribe=function(e){const t=this.$listeners.indexOf(e);t>-1&&this.$listeners.splice(t,1),this.assocTrigger()},a.prototype.check=function(e){return new i(this,e)},a.prototype.get=a.prototype.check,a.prototype.when=function(e){return{$target:e,$observer:this}},a.prototype.toString=function(){return this.$memoryId||o.register(this),"{{#ObItem::("+this.$memoryId+")}}"};const l=(e,t,n)=>{if(f.isFunction(n))return n(e,t);if(f.isObservable(e)){const r=e.val();return r&&n?r[n]:t}return f.isObject(e)?e[n]??t:e},c=function(e,t){return e.replace(new RegExp(`^[${t}]+|[${t}]+$`,"g"),"")},h={mounted:new WeakMap,mountedSupposedSize:0,unmounted:new WeakMap,unmountedSupposedSize:0,observer:null,checkMutation:function(e){for(const t of e){if(h.mountedSupposedSize>0)for(const e of t.addedNodes){const t=h.mounted.get(e);t&&(t.inDom=!0,t.mounted&&t.mounted(e))}if(h.unmountedSupposedSize>0)for(const e of t.removedNodes){const t=h.unmounted.get(e);t&&(t.inDom=!1,t.unmounted&&!0===t.unmounted(e)&&(t.disconnect(),e.nd?.remove()))}}},watch:function(e,t=!1){let n={inDom:t,mounted:null,unmounted:null,disconnect:()=>{h.mounted.delete(e),h.unmounted.delete(e),h.mountedSupposedSize--,h.unmountedSupposedSize--,n=null}};return{disconnect:n.disconnect,mounted:t=>{n.mounted=t,h.mounted.set(e,n),h.mountedSupposedSize++},unmounted:t=>{n.unmounted=t,h.unmounted.set(e,n),h.unmountedSupposedSize++}}}};h.observer=new MutationObserver(h.checkMutation),h.observer.observe(document.body,{childList:!0,subtree:!0});const d=["Click","DblClick","MouseDown","MouseEnter","MouseLeave","MouseMove","MouseOut","MouseOver","MouseUp","Wheel","KeyDown","KeyPress","KeyUp","Blur","Change","Focus","Input","Invalid","Reset","Search","Select","Submit","Drag","DragEnd","DragEnter","DragLeave","DragOver","DragStart","Drop","AfterPrint","BeforePrint","BeforeUnload","Error","HashChange","Load","Offline","Online","PageHide","PageShow","Resize","Scroll","Unload","Abort","CanPlay","CanPlayThrough","DurationChange","Emptied","Ended","LoadedData","LoadedMetadata","LoadStart","Pause","Play","Playing","Progress","RateChange","Seeked","Seeking","Stalled","Suspend","TimeUpdate","VolumeChange","Waiting","TouchCancel","TouchEnd","TouchMove","TouchStart","AnimationEnd","AnimationIteration","AnimationStart","TransitionEnd","Copy","Cut","Paste","FocusIn","FocusOut","ContextMenu"];function p(e){this.$element=e,this.$observer=null}p.prototype.__$isNDElement=!0;for(const e of d){const t=e.toLowerCase();p.prototype["on"+e]=function(e){return this.$element.addEventListener(t,e),this},p.prototype["onPrevent"+e]=function(e){return this.$element.addEventListener(t,function(t){t.preventDefault(),e&&e(t)}),this},p.prototype["onStop"+e]=function(e){return this.$element.addEventListener(t,function(t){t.stopPropagation(),e&&e(t)}),this},p.prototype["onPreventStop"+e]=function(e){return this.$element.addEventListener(t,function(t){t.stopPropagation(),t.preventDefault(),e&&e(t)}),this}}p.prototype.ref=function(e,t){return e[t]=this.$element,this},p.prototype.unmountChildren=function(){let e=this.$element;for(let t=0,n=e.children.length;t<n;t++){let n=e.children[t];n.$ndProx||n.nd?.remove(),n=null}return e=null,this},p.prototype.remove=function(){let e=this.$element;return e.nd.unmountChildren(),e.$ndProx=null,delete e.nd?.on?.prevent,delete e.nd?.on,delete e.nd,e=null,this},p.prototype.lifecycle=function(e){return this.$observer=this.$observer||h.watch(this.$element),e.mounted&&this.$observer.mounted(e.mounted),e.unmounted&&this.$observer.unmounted(e.unmounted),this},p.prototype.mounted=function(e){return this.lifecycle({mounted:e})},p.prototype.unmounted=function(e){return this.lifecycle({unmounted:e})},p.prototype.htmlElement=function(){return this.$element},p.prototype.node=p.prototype.htmlElement;const f={isObservable:e=>e instanceof a||e instanceof i||e?.__$isObservable,isProxy:e=>e?.__isProxy__,isObservableChecker:e=>e instanceof i||e?.__$isObservableChecker,isArray:e=>Array.isArray(e),isString:e=>"string"==typeof e,isNumber:e=>"number"==typeof e,isBoolean:e=>"boolean"==typeof e,isFunction:e=>"function"==typeof e,isAsyncFunction:e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,isObject:e=>"object"==typeof e,isJson:e=>"object"==typeof e&&null!==e&&"Object"===e.constructor.name&&!Array.isArray(e),isElement:e=>e instanceof HTMLElement||e instanceof DocumentFragment||e instanceof Text,isFragment:e=>e instanceof DocumentFragment,isStringOrObservable(e){return this.isString(e)||this.isObservable(e)},isValidChild(e){return null===e||this.isElement(e)||this.isObservable(e)||this.isNDElement(e)||["string","number","boolean"].includes(typeof e)},isNDElement:e=>e instanceof p||e?.constructor?.__$isNDElement,isValidChildren(e){Array.isArray(e)||(e=[e]);return 0===e.filter(e=>!this.isValidChild(e)).length},validateChildren(e){Array.isArray(e)||(e=[e]);const t=e.filter(e=>!this.isValidChild(e));if(t.length>0)throw new s(`Invalid children detected: ${t.map(e=>typeof e).join(", ")}`);return e},containsObservables:e=>!!e&&(f.isObject(e)&&Object.values(e).some(e=>f.isObservable(e))),containsObservableReference:e=>!(!e||"string"!=typeof e)&&/\{\{#ObItem::\([0-9]+\)\}\}/.test(e),validateAttributes(e){if(!e||"object"!=typeof e)return e;const t=[],n=Object.keys(e).filter(e=>t.includes(e));return n.length>0&&r.warn("Validator",`Reserved attributes found: ${n.join(", ")}`),e},validateEventCallback(e){if("function"!=typeof e)throw new s("Event callback must be a function")}};function m(e){const t=document.createDocumentFragment(),n=document.createComment("Anchor Start : "+e),o=document.createComment("/ Anchor End "+e);t.appendChild(n),t.appendChild(o),t.nativeInsertBefore=t.insertBefore,t.nativeAppendChild=t.appendChild;const s=function(e,n,r){e!==t?e.insertBefore(S.getChild(n),r):e.nativeInsertBefore(S.getChild(n),r)};return t.appendElement=function(e,n=null){o.parentNode!==t?o.parentNode?.insertBefore(e,n||o):o.parentNode.nativeInsertBefore(e,n||o)},t.appendChild=function(e,n=null){const i=o.parentNode;if(i){if(n=n??o,f.isArray(e)){const r=document.createDocumentFragment();for(let t=0,n=e.length;t<n;t++)r.appendChild(S.getChild(e[t]));return s(i,r,n),t}s(i,e,n)}else r.error("Anchor","Anchor : parent not found",e)},t.removeChildren=function(){const e=o.parentNode;if(e===t)return;if(e.firstChild===n&&e.lastChild===o)return void e.replaceChildren(n,o);let r,s=n.nextSibling;const i=document.createDocumentFragment();for(;s&&s!==o;)r=s.nextSibling,i.append(s),s=r;i.replaceChildren()},t.remove=function(){if(o.parentNode===t)return;let e,r=n.nextSibling;for(;r!==o;)e=r.nextSibling,t.nativeAppendChild(r),r=e},t.removeWithAnchors=function(){t.removeChildren(),n.remove(),o.remove()},t.replaceContent=function(e){const r=o.parentNode;r&&(r.firstChild!==n||r.lastChild!==o?(t.removeChildren(),r.insertBefore(e,o)):r.replaceChildren(n,e,o))},t.insertBefore=function(e,n=null){t.appendChild(e,n)},t.clear=function(){t.remove()},t.endElement=function(){return o},t.startElement=function(){return n},t}const b=["checked","selected","disabled","readonly","required","autofocus","multiple","autocomplete","hidden","contenteditable","spellcheck","translate","draggable","async","defer","autoplay","controls","loop","muted","download","reversed","open","default","formnovalidate","novalidate","scoped","itemscope","allowfullscreen","allowpaymentrequest","playsinline"];function g(e){return new a(e)}function v(e,t){for(let n in t){const r=t[n];f.isObservable(r)?(e.classList.toggle(n,r.val()),r.subscribe(t=>e.classList.toggle(n,t))):r.$observer?(e.classList.toggle(n,r.$observer.val()),r.$observer.on(r.$target,function(t){e.classList.toggle(n,t)})):e.classList.toggle(n,r)}}function y(e,t){for(let n in t){const r=t[n];f.isObservable(r)?(e.style[n]=r.val(),r.subscribe(t=>{e.style[n]=t})):e.style[n]=r}}function w(e,t,n){const r=f.isObservable(n)?n.val():n;f.isBoolean(r)?e[t]=r:e[t]=r===e.value,f.isObservable(n)&&(["checked"].includes(t)&&e.addEventListener("input",()=>{f.isBoolean(r)?n.set(e[t]):n.set(e.value)}),n.subscribe(n=>{f.isBoolean(n)?e[t]=n:e[t]=n===e.value}))}function $(e,t,n){const r=n=>{"value"!==t?e.setAttribute(t,n):e.value=n};r(n.val()),n.subscribe(r),"value"===t&&e.addEventListener("input",()=>n.set(e.value))}g.getById=function(e){const t=o.getObservableById(parseInt(e));if(!t)throw new s("Observable.getById : No observable found with id "+e);return t},g.cleanup=function(e){e.cleanup()},g.autoCleanup=function(e=!1,t={}){if(!e)return;const{interval:n=6e4,threshold:r=100}=t;window.addEventListener("beforeunload",()=>{o.cleanup()}),setInterval(()=>o.cleanObservables(r),n)};const C=new Map;let O=null;const S={createTextNode:()=>(O||(O=document.createTextNode("")),O.cloneNode()),createObservableNode(e,t){const n=S.createTextNode();return t.subscribe(e=>n.nodeValue=String(e)),n.nodeValue=t.val(),e&&e.appendChild(n),n},createStaticTextNode(e,t){let n=S.createTextNode();return n.nodeValue=String(t),e&&e.appendChild(n),n},createElement(e){if(e){if(C.has(e))return C.get(e).cloneNode();const t=document.createElement(e);return C.set(e,t),t.cloneNode()}return new m("Fragment")},processChildren(e,t){if(null===e)return;const n=Array.isArray(e)?e:[e];for(let e=0,r=n.length;e<r;e++){let r=this.getChild(n[e]);null!==r&&t.appendChild(r)}},getChild(e){if(null===e)return null;if(f.isString(e)&&(e=e.resolveObservableTemplate?e.resolveObservableTemplate():e,f.isString(e)))return S.createStaticTextNode(null,e);if(f.isElement(e))return e;if(f.isObservable(e))return S.createObservableNode(null,e);if(f.isNDElement(e))return e.$element??e.$build?.()??null;if(f.isArray(e)){const t=document.createDocumentFragment();for(let n=0,r=e.length;n<r;n++)t.appendChild(this.getChild(e[n]));return t}return f.isFunction(e)?this.getChild(e()):S.createStaticTextNode(null,e)},processAttributes(e,t){f.isFragment(e)||t&&function(e,t){if(f.validateAttributes(t),!f.isObject(t))throw new s("Attributes must be an object");for(let n in t){const r=n.toLowerCase();let o=t[r];if(f.isString(o)){if(o=o.resolveObservableTemplate?o.resolveObservableTemplate():o,f.isString(o)){e.setAttribute(r,o);continue}const t=o.filter(e=>f.isObservable(e));o=g.computed(()=>o.map(e=>f.isObservable(e)?e.val():e).join(" ")||" ",t)}"class"===r&&f.isJson(o)?v(e,o):"style"===r&&f.isJson(o)?y(e,o):b.includes(r)?w(e,r,o):f.isObservable(o)?$(e,r,o):e.setAttribute(r,o)}}(e,t)},setup:(e,t,n)=>e};function E(e,t){const n=e.toLowerCase();return function(e,o=null){try{if(!f.isJson(e)){const t=o;o=e,e=t}const r=S.createElement(n),s="function"==typeof t?t(r):r;return S.processAttributes(s,e),S.processChildren(o,s),S.setup(s,e,t)}catch(e){r.error("ElementCreation",`Error creating ${n}`,e)}}}Object.defineProperty(HTMLElement.prototype,"nd",{configurable:!0,get(){return this.$nd||(this.$nd=new p(this),this.$nd.nd=this.$nd),this.$nd}});class A extends Error{constructor(e,t){super(`${e}\n\n${t.join("\n")}\n\n`)}}const k={string:e=>({name:e,type:"string",validate:e=>f.isString(e)}),number:e=>({name:e,type:"number",validate:e=>f.isNumber(e)}),boolean:e=>({name:e,type:"boolean",validate:e=>f.isBoolean(e)}),observable:e=>({name:e,type:"observable",validate:e=>f.isObservable(e)}),element:e=>({name:e,type:"element",validate:e=>f.isElement(e)}),function:e=>({name:e,type:"function",validate:e=>f.isFunction(e)}),object:e=>({name:e,type:"object",validate:e=>f.isObject(e)}),objectNotNull:e=>({name:e,type:"object",validate:e=>f.isObject(e)&&null!==e}),children:e=>({name:e,type:"children",validate:e=>f.validateChildren(e)}),attributes:e=>({name:e,type:"attributes",validate:e=>f.validateAttributes(e)}),optional:e=>({...e,optional:!0}),oneOf:(e,...t)=>({name:e,type:"oneOf",types:t,validate:e=>t.some(t=>t.validate(e))})},I=(e,t,n="Function")=>{if(!f.isArray(t))throw new s("withValidation : argSchema must be an array");return function(...r){return((e,t,n="Function")=>{if(!t)return;const r=[],o=t.filter(e=>!e.optional).length;if(e.length<o&&r.push(`${n}: Expected at least ${o} arguments, got ${e.length}`),t.forEach((t,o)=>{const s=o+1,i=e[o];if(void 0!==i){if(!t.validate(i)){const e=i?.constructor?.name||typeof i;r.push(`${n}: Invalid argument '${t.name}' at position ${s}, expected ${t.type}, got ${e}`)}}else t.optional||r.push(`${n}: Missing required argument '${t.name}' at position ${s}`)}),r.length>0)throw new A("Argument validation failed",r)})(r,t,e.name||n),e.apply(this,r)}};Function.prototype.args=function(...e){return I(this,e)},Function.prototype.errorBoundary=function(e){return(...t)=>{try{return this.apply(this,t)}catch(t){return e(t)}}},String.prototype.use=function(e){const t=this;return g.computed(()=>t.replace(/\$\{(.*?)}/g,(t,n)=>{const r=e[n];return f.isObservable(r)?r.val():r}),Object.values(e))},String.prototype.resolveObservableTemplate=function(){return f.containsObservableReference(this)?this.split(/(\{\{#ObItem::\([0-9]+\)\}\})/g).filter(Boolean).map(e=>{if(!f.containsObservableReference(e))return e;const[t,n]=e.match(/\{\{#ObItem::\(([0-9]+)\)\}\}/);return g.getById(n)}):this.valueOf()};const x=["push","pop","shift","unshift","reverse","sort","splice"];g.array=function(e){if(!Array.isArray(e))throw new s("Observable.array : target must be an array");const t=g(e);x.forEach(e=>{t[e]=function(...n){const r=t.val()[e](...n);return t.trigger({action:e,args:n,result:r}),r}}),t.clear=function(){return t.val().length=0,t.trigger({action:"clear"}),!0},t.merge=function(e){t.set([...t.val(),...e])},t.populateAndRender=function(e,n){t.trigger({action:"populate",args:[t.val(),e,n]})},t.remove=function(e){const n=t.val().splice(e,1);return 0===n.length?[]:(t.trigger({action:"remove",args:[e],result:n[0]}),n)},t.swap=function(e,n){const r=t.val(),o=r.length;if(o<e||o<n)return!1;if(n<e){const t=e;e=n,n=t}const s=r[e],i=r[n];return r[e]=i,r[n]=s,t.trigger({action:"swap",args:[e,n],result:[s,i]}),!0},t.length=function(){return t.val().length};return["map","filter","reduce","some","every","find","findIndex","concat"].forEach(e=>{t[e]=function(...n){return t.val()[e](...n)}}),t},g.batch=function(e){const t=g(0),n=function(){if(f.isAsyncFunction(e))return e(...arguments).then(()=>{t.trigger()}).catch(e=>{throw e});e(...arguments),t.trigger()};return n.$observer=t,n},g.init=function(e){const t={};for(const n in e){const r=e[n];f.isJson(r)?t[n]=g.init(r):f.isArray(r)?t[n]=g.array(r):t[n]=g(r)}const n=function(){};return new Proxy(t,{get:(e,r)=>"__isProxy__"===r||("$value"===r?function(){const e={};for(const n in t){const r=t[n];f.isObservable(r)?e[n]=r.val():f.isProxy(r)?e[n]=r.$value:e[n]=r}return e}():"$clone"===r?n:void 0!==e[r]?e[r]:void 0),set(e,t,n){void 0!==e[t]&&e[t].set(n)}})},g.value=function(e){if(f.isObservable(e))return e.val();if(f.isProxy(e))return e.$value;if(f.isArray(e)){const t=[];for(let n=0,r=e.length;n<r;n++){const r=e[n];t.push(g.value(r))}return t}return e},g.update=function(e,t){for(const n in t){const r=e[n],o=t[n];if(f.isObservable(r)){if(f.isArray(o)){g.update(r,o);continue}r.set(o)}else f.isProxy(r)?g.update(r,o):e[n]=o}},g.object=g.init,g.json=g.init,g.computed=function(e,t=[]){const n=new a(e()),r=()=>n.set(e());if(f.isFunction(t)){if(!f.isObservable(t.$observer))throw new s("Observable.computed : dependencies must be valid batch function");return t.$observer.subscribe(r),n}return t.forEach(e=>e.subscribe(r)),n};const F=function(){const e=new Map;return{use(t){const{observer:n,subscribers:r}=e.get(t),o=g(n.val()),s=n.subscribe(e=>o.set(e)),i=o.subscribe(e=>n.set(e));return o.destroy=()=>{s(),i(),o.cleanup()},r.add(o),o},follow(e){return this.use(e)},create(t,n){const r=g(n);return e.set(t,{observer:r,subscribers:new Set}),r},get(t){const n=e.get(t);return n?n.observer:null},getWithSubscribers:t=>e.get(t),delete(t){const n=e.get(t);n&&(n.observer.cleanup(),n.subscribers.forEach(e=>e.destroy()),n.observer.clear())}}}();const D=function(e,t,n=null){if(!f.isObservable(e))return r.warn("ShowIf","ShowIf : condition must be an Observable / "+n,e);const o=new m("Show if : "+(n||""));let s=null;const i=()=>s||(s=S.getChild(t),s);return e.val()&&o.appendChild(i()),e.subscribe(e=>{e?o.appendChild(i()):o.remove()}),o},T=function(e,t,n=!0){if(!f.isObservable(e))throw new s("Toggle : condition must be an Observable");const r=new m,o=new Map,i=function(e){if(n&&o.has(e))return o.get(e);let r=t[e];return r?(r=S.getChild(r),n&&o.set(e,r),r):null},a=e.val(),u=i(a);return u&&r.appendChild(u),e.subscribe(e=>{const t=i(e);r.remove(),t&&r.appendChild(t)}),r},R=function(e,t,n){if(!f.isObservable(e))throw new s("Toggle : condition must be an Observable");return T(e,{true:t,false:n})},L=E("div"),N=E("span"),P=E("label"),M=E("p"),q=M,B=E("strong"),j=E("h1"),_=E("h2"),V=E("h3"),H=E("h4"),z=E("h5"),W=E("h6"),U=E("br"),J=E("a"),K=E("pre"),Q=E("code"),G=E("blockquote"),X=E("hr"),Y=E("em"),Z=E("small"),ee=E("mark"),te=E("del"),ne=E("ins"),re=E("sub"),oe=E("sup"),se=E("abbr"),ie=E("cite"),ae=E("q"),ue=E("dl"),le=E("dt"),ce=E("dd"),he=E("form",function(e){return e.submit=function(t){return"function"==typeof t?(e.onSubmit(e=>{e.preventDefault(),t(e)}),e):(this.setAttribute("action",t),e)},e.multipartFormData=function(){return this.setAttribute("enctype","multipart/form-data"),e},e.post=function(t){return this.setAttribute("method","post"),this.setAttribute("action",t),e},e.get=function(e){this.setAttribute("method","get"),this.setAttribute("action",e)},e}),de=E("input"),pe=E("textarea"),fe=pe,me=E("select"),be=E("fieldset"),ge=E("option"),ve=E("legend"),ye=E("datalist"),we=E("output"),$e=E("progress"),Ce=E("meter"),Oe=E("button"),Se=E("main"),Ee=E("section"),Ae=E("article"),ke=E("aside"),Ie=E("nav"),xe=E("figure"),Fe=E("figcaption"),De=E("header"),Te=E("footer"),Re=E("img"),Le=function(e,t){return Re({src:e,...t})},Ne=E("details"),Pe=E("summary"),Me=E("dialog"),qe=E("menu"),Be=E("ol"),je=E("ul"),_e=E("li"),Ve=_e,He=Be,ze=je,We=E("audio"),Ue=E("video"),Je=E("source"),Ke=E("track"),Qe=E("canvas"),Ge=E("svg"),Xe=E("time"),Ye=E("data"),Ze=E("address"),et=E("kbd"),tt=E("samp"),nt=E("var"),rt=E("wbr"),ot=E("caption"),st=E("table"),it=E("thead"),at=E("tfoot"),ut=E("tbody"),lt=E("tr"),ct=lt,ht=E("th"),dt=ht,pt=ht,ft=E("td"),mt=ft,bt=E("");var gt=Object.freeze({__proto__:null,Abbr:se,Address:Ze,Anchor:m,Article:Ae,Aside:ke,AsyncImg:function(e,t,n,r){const o=Le(t||e,n),i=new Image;return i.onload=()=>{f.isFunction(r)&&r(null,o),o.src=e},i.onerror=()=>{f.isFunction(r)&&r(new s("Image not found"))},f.isObservable(e)&&e.subscribe(e=>{i.src=e}),i.src=e,o},Audio:We,BaseImage:Re,Blockquote:G,Br:U,Button:Oe,Canvas:Qe,Caption:ot,Checkbox:e=>de({type:"checkbox",...e}),Cite:ie,Code:Q,ColorInput:e=>de({type:"color",...e}),Data:Ye,Datalist:ye,DateInput:e=>de({type:"date",...e}),DateTimeInput:e=>de({type:"datetime-local",...e}),Dd:ce,Del:te,Details:Ne,Dialog:Me,Div:L,Dl:ue,Dt:le,Em:Y,EmailInput:e=>de({type:"email",...e}),FieldSet:be,FigCaption:Fe,Figure:xe,FileInput:e=>de({type:"file",...e}),Footer:Te,ForEach:function(e,t,n){const o=new m("ForEach"),s=o.endElement();o.startElement();let i=new Map,a=null;const u=new Set,c=e=>{for(const[t,n]of i.entries()){if(u.has(t))continue;const r=n.child?.deref();e&&r&&e.removeChild(r),n.indexObserver?.cleanup(),n.child=null,n.indexObserver=null,i.delete(n.keyId),a&&a.delete(n.keyId)}},h=(e,o)=>{const s=l(e,o,n);if(i.has(s)){const e=i.get(s);if(e.indexObserver?.set(o),e.isNew=!1,e.child?.deref())return s;i.delete(s)}try{const n=t.length>=2?g(o):null;let r=S.getChild(t(e,n));i.set(s,{keyId:s,isNew:!0,child:new WeakRef(r),indexObserver:n})}catch(e){throw r.error("ForEach",`Error creating element for key ${s}`,e),e}return s},d=()=>{const t=s.parentNode;if(!t)return;const n=f.isObservable(e)?e.val():e;if(u.clear(),Array.isArray(n))for(let e=0,t=n.length;e<t;e++){const t=h(n[e],e);u.add(t)}else for(const e in n){const t=h(n[e],e);u.add(t)}if(0===u.size)return o.removeChildren(),c(),void a?.clear();c(t),a&&0!==a.size?(()=>{let e=document.createDocumentFragment();const t=Array.from(u);Array.from(a);for(const n in t){const r=t[n],o=i.get(r);if(!o)continue;const s=o.child.deref();s&&e.appendChild(s)}o.replaceContent(e)})():(e=>{const t=document.createDocumentFragment();for(const e of u){const n=i.get(e);if(!n)continue;const r=n.child?.deref();r&&t.appendChild(r)}e.insertBefore(t,s)})(t),a?.clear(),a=new Set([...u])};return d(),f.isObservable(e)&&e.subscribe(d),o},ForEachArray:function(e,t,n,o={}){const s=new m("ForEach Array"),i=s.endElement(),a=s.startElement();let u=new Map,c=0;const h=t.length>=2,d=new WeakMap,p=()=>{s.removeChildren(),w(),c=0},b=(e,t)=>d.has(e)?d.get(e):l(e,t,n),v=e=>C(b(e)),y=(e,t=!0)=>{if(!e)return;const n=e.child;e.indexObserver?.deref()?.cleanup(),e.child=null,e.indexObserver=null,t&&(n?.remove(),u.delete(e.keyId))},w=()=>{for(const[e,t]of u.entries())y(t,!1);u.clear()},$=(e,n)=>{const o=b(e,n);if(u.has(o)){const e=u.get(o);e.indexObserver?.deref()?.set(n);const t=e.child;if(t)return t;u.delete(o)}try{const r=h?g(n):null;let s=S.getChild(t(e,r));return u.set(o,{keyId:o,child:s,indexObserver:r?new WeakRef(r):null}),d.set(e,o),s}catch(e){throw r.error("ForEach",`Error creating element for key ${o}`,e),e}},C=function(e){const t=u.get(e);if(!t)return null;const n=t.child;return n||(y(t,!1),null)},O=function(e,t){const n=u.get(e);if(!n)return null;const r=n.child;if(!r)return null;t?t.appendChild(r):r.remove()},E={toFragment(e,t=0){const n=document.createDocumentFragment();for(let t=0,r=e.length;t<r;t++)n.append($(e[t],c)),c++;return n},add(e,t=0){const n=E.toFragment(e);setTimeout(()=>{s.appendElement(n)},t)},replace(e){p(),E.add(e)},reOrder(e){let t=null;const n=document.createDocumentFragment();for(const r of e)t=v(r),t&&n.appendChild(t);t=null,s.appendElement(n,i)},removeOne(e,t){let n=v(e);n&&((e,t=!0)=>{y(u.get(e),t)})(b(e,t),!0),n=null},clear:p,merge(e){E.add(e,0)},push(e){let t=0;o.pushDelay&&(t=o.pushDelay(e)??0),E.add(e,t)},populate([e,t,n]){const r=document.createDocumentFragment();for(let o=0;o<t;o++){const t=n(o);e.push(t),r.append($(t,o)),c++}s.appendChild(r),r.replaceChildren()},unshift(e){s.insertBefore(E.toFragment(e),a.nextSibling)},splice(e,t){const[n,r,...o]=e;let a=null;const u=document.createDocumentFragment();if(t.length>0){let e=b(t[0],n);if(1===t.length)O(e,u);else if(t.length>1){const r=C(e);a=r?.previousSibling;for(let e=0;e<t.length;e++){const r=b(t[e],n+e);O(r,u)}}}else a=i;u.replaceChildren(),o&&o.length&&a&&s.insertBefore(E.toFragment(o),a.nextSibling)},reverse(e,t){E.reOrder(t)},sort(e,t){E.reOrder(t)},remove(e,t){E.removeOne(t)},pop(e,t){E.removeOne(t)},shift(e,t){E.removeOne(t)},swap(e,t){const n=i.parentNode;let r=v(t[0]),o=v(t[1]);if(!r||!o)return;const s=o.nextSibling;n.insertBefore(o,r),n.insertBefore(r,s),r=null,o=null}},A=(e,t,n)=>{if("clear"!==n.action&&e.length){if(n?.action)E[n.action]&&E[n.action](n.args,n.result);else{if(0===c)return void E.add(e);E.replace(e)}((e,t=0)=>{if(!h)return;let n=t;for(let r=t,o=e?.length;r<o;r++){const t=u.get(b(e[r],r));t&&(t.indexObserver?.deref()?.set(n),n++)}})(e,0)}else{if(0===c)return;p()}};return A(e.val(),0,{action:null}),f.isObservable(e)&&e.subscribe(A),s},Form:he,Fragment:bt,H1:j,H2:_,H3:V,H4:H,H5:z,H6:W,Header:De,HiddenInput:e=>de({type:"hidden",...e}),HideIf:function(e,t,n){const r=g(!e.val());return e.subscribe(e=>r.set(!e)),D(r,t,n)},HideIfNot:function(e,t,n){return D(e,t,n)},Hr:X,Img:Le,Input:de,Ins:ne,Kbd:et,Label:P,LazyImg:function(e,t){return Le(e,{...t,loading:"lazy"})},Legend:ve,Li:Ve,Link:J,ListItem:_e,Main:Se,Mark:ee,Match:T,Menu:qe,Meter:Ce,MonthInput:e=>de({type:"month",...e}),NativeDocumentFragment:m,Nav:Ie,NumberInput:e=>de({type:"number",...e}),Ol:He,Option:ge,OrderedList:Be,Output:we,P:M,Paragraph:q,PasswordInput:e=>de({type:"password",...e}),Pre:K,Progress:$e,Quote:ae,Radio:e=>de({type:"radio",...e}),RangeInput:e=>de({type:"range",...e}),ReadonlyInput:e=>de({readonly:!0,...e}),Samp:tt,SearchInput:e=>de({type:"search",...e}),Section:Ee,Select:me,ShowIf:D,SimpleButton:(e,t)=>Oe(e,{type:"button",...t}),Small:Z,Source:Je,Span:N,Strong:B,Sub:re,SubmitButton:(e,t)=>Oe(e,{type:"submit",...t}),Summary:Pe,Sup:oe,Svg:Ge,Switch:R,TBody:ut,TBodyCell:mt,TFoot:at,TFootCell:pt,THead:it,THeadCell:dt,TRow:ct,Table:st,Td:ft,TelInput:e=>de({type:"tel",...e}),TextArea:pe,TextInput:fe,Th:ht,Time:Xe,TimeInput:e=>de({type:"time",...e}),Tr:lt,Track:Ke,Ul:ze,UnorderedList:je,UrlInput:e=>de({type:"url",...e}),Var:nt,Video:Ue,Wbr:rt,WeekInput:e=>de({type:"week",...e}),When:function(e){if(!f.isObservable(e))throw new s("When : condition must be an Observable");let t=null,n=null;return{show(e){return t=e,this},otherwise:r=>(n=r,R(e,t,n))}}});const vt={};function yt(e,t,n={}){e="/"+c(e,"/");let r=null,o=n.name||null;const s=n.middlewares||[],i=n.shouldRebuild||!1,a=n.with||{},u={},l=[],h=e=>{if(!e)return null;const[t,n]=e.split(":");let r=a[t];return!r&&n&&(r=vt[n]),r||(r="[^/]+"),r=r.replace("(","(?:"),{name:t,pattern:`(${r})`}},d=()=>{if(r)return r;const t=e.replace(/\{(.*?)}/gi,(e,t)=>{const n=h(t);return n&&n.pattern?(u[n.name]=n.pattern,l.push(n.name),n.pattern):e});return r=new RegExp("^"+t+"$"),r};this.name=()=>o,this.component=()=>t,this.middlewares=()=>s,this.shouldRebuild=()=>i,this.path=()=>e,this.match=function(e){e="/"+c(e,"/");if(!d().exec(e))return!1;const t={};return d().exec(e).forEach((e,n)=>{if(n<1)return;const r=l[n-1];t[r]=e}),t},this.url=function(t){const n=e.replace(/\{(.*?)}/gi,(e,n)=>{const r=h(n);if(t.params&&t.params[r.name])return t.params[r.name];throw new Error(`Missing parameter '${r.name}'`)}),r="object"==typeof t.query?new URLSearchParams(t.query).toString():null;return(t.basePath?t.basePath:"")+(r?`${n}?${r}`:n)}}class wt extends Error{constructor(e,t){super(e),this.context=t}}const $t=(e,t)=>{const n=[];return e.forEach(e=>{n.push(c(e.suffix,"/"))}),n.push(c(t,"/")),n.join("/")},Ct=(e,t)=>{const n=[];return e.forEach(e=>{e.options.middlewares&&n.push(...e.options.middlewares)}),t&&n.push(...t),n},Ot=(e,t)=>{const n=[];return e.forEach(e=>{e.options?.name&&n.push(e.options.name)}),t&&n.push(t),n.join(".")};function St(){const e=[];let t=0;const n=n=>{const o=t+n;if(!e[o])return;t=o;const{route:s,params:i,query:a,path:u}=e[o];r(u)},r=e=>{window.location.replace(`${window.location.pathname}${window.location.search}#${e}`)},o=()=>window.location.hash.slice(1);this.push=function(n){const{route:s,params:i,query:a,path:u}=this.resolve(n);u!==o()&&(e.splice(t+1),e.push({route:s,params:i,query:a,path:u}),t++,r(u))},this.replace=function(n){const{route:r,params:s,query:i,path:a}=this.resolve(n);a!==o()&&(e[t]={route:r,params:s,query:i,path:a})},this.forward=function(){return t<e.length-1&&n(1)},this.back=function(){return t>0&&n(-1)},this.init=function(n){window.addEventListener("hashchange",()=>{const{route:e,params:t,query:n,path:r}=this.resolve(o());this.handleRouteChange(e,t,n,r)});const{route:r,params:s,query:i,path:a}=this.resolve(n||o());e.push({route:r,params:s,query:i,path:a}),t=0,this.handleRouteChange(r,s,i,a)}}function Et(){this.push=function(e){try{const{route:t,path:n,params:r,query:o}=this.resolve(e);if(window.history.state&&window.history.state.path===n)return;window.history.pushState({name:t.name(),params:r,path:n},t.name()||n,n),this.handleRouteChange(t,r,o,n)}catch(e){r.error("HistoryRouter","Error in pushState",e)}},this.replace=function(e){const{route:t,path:n,params:o}=this.resolve(e);try{window.history.replaceState({name:t.name(),params:o,path:n},t.name()||n,n),this.handleRouteChange(t,o,{},n)}catch(e){r.error("HistoryRouter","Error in replaceState",e)}},this.forward=function(){window.history.forward()},this.back=function(){window.history.back()},this.init=function(e){window.addEventListener("popstate",e=>{try{if(!e.state||!e.state.path)return;const t=e.state.path,{route:n,params:r,query:o,path:s}=this.resolve(t);if(!n)return;this.handleRouteChange(n,r,o,s)}catch(e){r.error("HistoryRouter","Error in popstate event",e)}});const{route:t,params:n,query:o,path:s}=this.resolve(e||window.location.pathname+window.location.search);this.handleRouteChange(t,n,o,s)}}function At(){const e=[];let t=0;const n=n=>{const r=t+n;if(!e[r])return;t=r;const{route:o,params:s,query:i,path:a}=e[r];this.handleRouteChange(o,s,i,a)};this.push=function(n){const{route:r,params:o,query:s,path:i}=this.resolve(n);e[t]&&e[t].path===i||(e.splice(t+1),e.push({route:r,params:o,query:s,path:i}),t++,this.handleRouteChange(r,o,s,i))},this.replace=function(n){const{route:r,params:o,query:s,path:i}=this.resolve(n);e[t]={route:r,params:o,query:s,path:i},this.handleRouteChange(r,o,s,i)},this.forward=function(){return t<e.length-1&&n(1)},this.back=function(){return t>0&&n(-1)},this.init=function(n){const r=n||window.location.pathname+window.location.search,{route:o,params:s,query:i,path:a}=this.resolve(r);e.push({route:o,params:s,query:i,path:a}),t=0,this.handleRouteChange(o,s,i,a)}}const kt="default";function It(e={}){const t=[],n={},o=[],s=[],i={route:null,params:null,query:null,path:null,hash:null};if("hash"===e.mode)St.apply(this,[]);else if("history"===e.mode)Et.apply(this,[]);else{if("memory"!==e.mode)throw new wt("Invalid router mode "+e.mode);At.apply(this,[])}const a=function(e,t){for(const n of s)try{n(e),t&&t(e)}catch(e){r.warn("Route Listener","Error in listener:",e)}};this.routes=()=>[...t],this.currentState=()=>({...i}),this.add=function(e,r,s){const i=new yt($t(o,e),r,{...s,middlewares:Ct(o,s?.middlewares||[]),name:s?.name?Ot(o,s.name):null});return t.push(i),i.name()&&(n[i.name()]=i),this},this.group=function(e,t,n){if(!f.isFunction(n))throw new wt("Callback must be a function");return o.push({suffix:e,options:t}),n(),o.pop(),this},this.generateUrl=function(e,t={},r={}){const o=n[e];if(!o)throw new wt(`Route not found for name: ${e}`);return o.url({params:t,query:r})},this.resolve=function(e){if(f.isJson(e)){const t=n[e.name];if(!t)throw new wt(`Route not found for name: ${e.name}`);return{route:t,params:e.params,query:e.query,path:t.url({...e})}}const[r,o]=e.split("?"),s="/"+c(r,"/");let i,a=null;for(const e of t)if(i=e.match(s),i){a=e;break}if(!a)throw new wt(`Route not found for url: ${r}`);const u={};if(o){const e=new URLSearchParams(o).entries();for(const[t,n]of e)u[t]=n}return{route:a,params:i,query:u,path:e}},this.subscribe=function(e){if(!f.isFunction(e))throw new wt("Listener must be a function");return s.push(e),()=>{s.splice(s.indexOf(e),1)}},this.handleRouteChange=function(e,t,n,r){i.route=e,i.params=t,i.query=n,i.path=r;const o=[...e.middlewares(),a];let s=0;const u={...i},l=e=>{if(s++,!(s>=o.length))return o[s](e||u,l)};return o[s](u,l)}}function xt(e,t){const{to:n,href:r,...o}=e,s=n||r;if(f.isString(s)){const e=It.get();return J({...o,href:s},t).nd.onPreventClick(()=>{e.push(s)})}const i=s.router||kt,a=It.get(i);if(!a)throw new wt('Router not found "'+i+'" for link "'+s.name+'"');const u=a.generateUrl(s.name,s.params,s.query);return J({...o,href:u},t).nd.onPreventClick(()=>{a.push(u)})}It.routers={},It.create=function(t,n){if(!f.isFunction(n))throw r.error("Router","Callback must be a function",e),new wt("Callback must be a function");const o=new It(t);return It.routers[t.name||kt]=o,n(o),o.init(t.entry),o.mount=function(e){if(f.isString(e)){const t=document.querySelector(e);if(!t)throw new wt(`Container not found for selector: ${e}`);e=t}else if(!f.isElement(e))throw new wt("Container must be a string or an Element");return function(e,t){const n=new Map,r=function(e){t.innerHTML="",t.appendChild(e)},o=function(e){if(!e.route)return;const{route:t,params:o,query:s,path:i}=e;if(n.has(i)){const e=n.get(i);return void r(e)}const a=t.component()({params:o,query:s});n.set(i,a),r(a)};return e.subscribe(o),o(e.currentState()),t}(o,e)},o},It.get=function(e){const t=It.routers[e||kt];if(!t)throw new wt(`Router not found for name: ${e}`);return t},It.push=function(e,t=null){return It.get(t).push(e)},It.replace=function(e,t=null){return It.get(t).replace(e)},It.forward=function(e=null){return It.get(e).forward()},It.back=function(e=null){return It.get(e).back()},xt.blank=function(e,t){return J({...e,target:"_blank"},t)};var Ft=Object.freeze({__proto__:null,Link:xt,RouteParamPatterns:vt,Router:It});return t.ArgTypes=k,t.ElementCreator=S,t.HtmlElementWrapper=E,t.NDElement=p,t.Observable=g,t.Store=F,t.elements=gt,t.router=Ft,t.withValidation=I,t}({});
|
|
1
|
+
var NativeDocument=function(t){"use strict";let n={};n={log(){},warn(){},error(){},disable(){}};var r=n;const o=function(){let e=0;const t=new Map;return{register(n){const r=++e;return t.set(r,new WeakRef(n)),r},unregister(e){t.delete(e)},getObservableById:e=>t.get(e)?.deref(),cleanup(){for(const[e,n]of t){const e=n.deref();e&&e.cleanup()}t.clear()},cleanObservables(e){if(t.size<e)return;let n=0;for(const[e,r]of t)r.deref()||(t.delete(e),n++);n>0&&r.log("Memory Auto Clean",`🧹 Cleaned ${n} orphaned observables`)}}}();class s extends Error{constructor(e,t={}){super(e),this.name="NativeDocumentError",this.context=t,this.timestamp=(new Date).toISOString()}}function i(e,t){this.observable=e,this.checker=t,this.unSubscriptions=[]}i.prototype.__$isObservableChecker=!0,i.prototype.subscribe=function(e){const t=this.observable.subscribe(t=>{e&&e(this.checker(t))});return this.unSubscriptions.push(t),t},i.prototype.check=function(e){return this.observable.check(()=>e(this.val()))},i.prototype.val=function(){return this.checker&&this.checker(this.observable.val())},i.prototype.set=function(e){return this.observable.set(e)},i.prototype.trigger=function(){return this.observable.trigger()},i.prototype.cleanup=function(){return this.observable.cleanup()};const a=function(){const e=new Map,t=new Map;return{list:()=>t,add(n,r){if(!r||"object"!=typeof r)throw new Error(`Plugin ${n} must be an object`);if(!e.has(n)){r.$name=n,e.set(n,r),"function"==typeof r?.init&&r.init();for(const e in r)/^on[A-Z]/.test(e)&&(t.has(e)||t.set(e,new Set),t.get(e).add(r))}},remove(n){if(!e.has(n))return;const r=e.get(n);"function"==typeof r.cleanup&&r.cleanup();for(const[e,n]of t.entries())n.has(r)&&n.delete(r),0===n.size&&t.delete(e);e.delete(n)},emit(e,...n){const o="on"+e;if(!t.has(o))return;const s=t.get(o);for(const e of s){const t=e[o];if("function"==typeof t)try{t.call(e,...n)}catch(t){r.error("Plugin Manager",`Error in plugin ${e.$name} for event ${o}`,t)}}}}}();function u(e){this.$previousValue=e,this.$currentValue=e,this.$isCleanedUp=!1,this.$listeners=null,this.$watchers=null,this.$memoryId=null,a.emit("CreateObservable",this)}Object.defineProperty(u.prototype,"$value",{get(){return this.$currentValue},set(e){this.set(e)},configurable:!0}),u.prototype.__$isObservable=!0;const l=function(){};u.prototype.triggerListeners=function(e){const t=this.$listeners,n=this.$previousValue,r=this.$currentValue;if(e=e||{},t?.length)for(let o=0,s=t.length;o<s;o++)t[o](r,n,e)},u.prototype.triggerWatchers=function(){if(!this.$watchers)return;const e=this.$watchers,t=this.$previousValue,n=this.$currentValue;e.has(n)&&e.get(n).forEach(e=>{e.set?e.set(!0):e(!0)}),e.has(t)&&e.get(t).forEach(e=>{e.set?e.set(!1):e(!1)})},u.prototype.triggerAll=function(e){this.triggerListeners(e),this.triggerWatchers()},u.prototype.assocTrigger=function(){this.$watchers?.size&&this.$listeners?.length?this.trigger=this.triggerAll:this.$listeners?.length?this.trigger=this.triggerListeners:this.$watchers?.size?this.trigger=this.triggerWatchers:this.trigger=l},u.prototype.trigger=l,u.prototype.set=function(e){const t="function"==typeof e?e(this.$currentValue):e;this.$currentValue!==t&&(this.$previousValue=this.$currentValue,this.$currentValue=t,a.emit("ObservableBeforeChange",this),this.trigger(),a.emit("ObservableAfterChange",this))},u.prototype.val=function(){return this.$currentValue},u.prototype.disconnectAll=function(){if(this.$listeners?.splice(0),this.$previousValue=null,this.$currentValue=null,this.$watchers)for(const[e,t]of this.$watchers)t.splice(0);this.$watchers?.clear(),this.$listeners=null,this.$watchers=null,this.trigger=l},u.prototype.cleanup=function(){o.unregister(this.$memoryId),this.disconnectAll(),this.$isCleanedUp=!0,delete this.$value},u.prototype.subscribe=function(e,t=null){if(this.$listeners=this.$listeners??[],this.$isCleanedUp)return r.warn("Observable subscription","⚠️ Attempted to subscribe to a cleaned up observable."),()=>{};if("function"!=typeof e)throw new s("Callback must be a function");return this.$listeners.push(e),this.assocTrigger(),a.emit("ObservableSubscribe",this,t),()=>{this.unsubscribe(e),this.assocTrigger(),a.emit("ObservableUnsubscribe",this)}},u.prototype.on=function(e,t){this.$watchers=this.$watchers??new Map;let n=this.$watchers.get(e);return n||(n=[],this.$watchers.set(e,n)),n.push(t),this.assocTrigger(),()=>{const r=n.indexOf(t);n?.splice(r,1),0===n.size&&(this.$watchers?.delete(e),n=null),this.assocTrigger()}},u.prototype.unsubscribe=function(e){const t=this.$listeners.indexOf(e);t>-1&&this.$listeners.splice(t,1),this.assocTrigger()},u.prototype.check=function(e){return new i(this,e)},u.prototype.get=u.prototype.check,u.prototype.when=function(e){return{$target:e,$observer:this}},u.prototype.toString=function(){return this.$memoryId||o.register(this),"{{#ObItem::("+this.$memoryId+")}}"};const c=(e,t,n)=>{if(m.isFunction(n))return n(e,t);if(m.isObservable(e)){const r=e.val();return r&&n?r[n]:t}return m.isObject(e)?e[n]??t:e},h=function(e,t){return e.replace(new RegExp(`^[${t}]+|[${t}]+$`,"g"),"")},d={mounted:new WeakMap,mountedSupposedSize:0,unmounted:new WeakMap,unmountedSupposedSize:0,observer:null,checkMutation:function(e){for(const t of e){if(d.mountedSupposedSize>0)for(const e of t.addedNodes){const t=d.mounted.get(e);t&&(t.inDom=!0,t.mounted&&t.mounted(e))}if(d.unmountedSupposedSize>0)for(const e of t.removedNodes){const t=d.unmounted.get(e);t&&(t.inDom=!1,t.unmounted&&!0===t.unmounted(e)&&(t.disconnect(),e.nd?.remove()))}}},watch:function(e,t=!1){let n={inDom:t,mounted:null,unmounted:null,disconnect:()=>{d.mounted.delete(e),d.unmounted.delete(e),d.mountedSupposedSize--,d.unmountedSupposedSize--,n=null}};return{disconnect:n.disconnect,mounted:t=>{n.mounted=t,d.mounted.set(e,n),d.mountedSupposedSize++},unmounted:t=>{n.unmounted=t,d.unmounted.set(e,n),d.unmountedSupposedSize++}}}};d.observer=new MutationObserver(d.checkMutation),d.observer.observe(document.body,{childList:!0,subtree:!0});const p=["Click","DblClick","MouseDown","MouseEnter","MouseLeave","MouseMove","MouseOut","MouseOver","MouseUp","Wheel","KeyDown","KeyPress","KeyUp","Blur","Change","Focus","Input","Invalid","Reset","Search","Select","Submit","Drag","DragEnd","DragEnter","DragLeave","DragOver","DragStart","Drop","AfterPrint","BeforePrint","BeforeUnload","Error","HashChange","Load","Offline","Online","PageHide","PageShow","Resize","Scroll","Unload","Abort","CanPlay","CanPlayThrough","DurationChange","Emptied","Ended","LoadedData","LoadedMetadata","LoadStart","Pause","Play","Playing","Progress","RateChange","Seeked","Seeking","Stalled","Suspend","TimeUpdate","VolumeChange","Waiting","TouchCancel","TouchEnd","TouchMove","TouchStart","AnimationEnd","AnimationIteration","AnimationStart","TransitionEnd","Copy","Cut","Paste","FocusIn","FocusOut","ContextMenu"];function f(e){this.$element=e,this.$observer=null,a.emit("NDElementCreated",e,this)}f.prototype.__$isNDElement=!0;for(const e of p){const t=e.toLowerCase();f.prototype["on"+e]=function(e){return this.$element.addEventListener(t,e),this},f.prototype["onPrevent"+e]=function(e){return this.$element.addEventListener(t,function(t){t.preventDefault(),e&&e(t)}),this},f.prototype["onStop"+e]=function(e){return this.$element.addEventListener(t,function(t){t.stopPropagation(),e&&e(t)}),this},f.prototype["onPreventStop"+e]=function(e){return this.$element.addEventListener(t,function(t){t.stopPropagation(),t.preventDefault(),e&&e(t)}),this}}f.prototype.ref=function(e,t){return e[t]=this.$element,this},f.prototype.unmountChildren=function(){let e=this.$element;for(let t=0,n=e.children.length;t<n;t++){let n=e.children[t];n.$ndProx||n.nd?.remove(),n=null}return e=null,this},f.prototype.remove=function(){let e=this.$element;return e.nd.unmountChildren(),e.$ndProx=null,delete e.nd?.on?.prevent,delete e.nd?.on,delete e.nd,e=null,this},f.prototype.lifecycle=function(e){return this.$observer=this.$observer||d.watch(this.$element),e.mounted&&this.$observer.mounted(e.mounted),e.unmounted&&this.$observer.unmounted(e.unmounted),this},f.prototype.mounted=function(e){return this.lifecycle({mounted:e})},f.prototype.unmounted=function(e){return this.lifecycle({unmounted:e})},f.prototype.htmlElement=function(){return this.$element},f.prototype.node=f.prototype.htmlElement;const m={isObservable:e=>e instanceof u||e instanceof i||e?.__$isObservable,isProxy:e=>e?.__isProxy__,isObservableChecker:e=>e instanceof i||e?.__$isObservableChecker,isArray:e=>Array.isArray(e),isString:e=>"string"==typeof e,isNumber:e=>"number"==typeof e,isBoolean:e=>"boolean"==typeof e,isFunction:e=>"function"==typeof e,isAsyncFunction:e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,isObject:e=>"object"==typeof e,isJson:e=>"object"==typeof e&&null!==e&&"Object"===e.constructor.name&&!Array.isArray(e),isElement:e=>e instanceof HTMLElement||e instanceof DocumentFragment||e instanceof Text,isFragment:e=>e instanceof DocumentFragment,isStringOrObservable(e){return this.isString(e)||this.isObservable(e)},isValidChild(e){return null===e||this.isElement(e)||this.isObservable(e)||this.isNDElement(e)||["string","number","boolean"].includes(typeof e)},isNDElement:e=>e instanceof f||e?.constructor?.__$isNDElement,isValidChildren(e){Array.isArray(e)||(e=[e]);return 0===e.filter(e=>!this.isValidChild(e)).length},validateChildren(e){Array.isArray(e)||(e=[e]);const t=e.filter(e=>!this.isValidChild(e));if(t.length>0)throw new s(`Invalid children detected: ${t.map(e=>typeof e).join(", ")}`);return e},containsObservables:e=>!!e&&(m.isObject(e)&&Object.values(e).some(e=>m.isObservable(e))),containsObservableReference:e=>!(!e||"string"!=typeof e)&&/\{\{#ObItem::\([0-9]+\)\}\}/.test(e),validateAttributes(e){if(!e||"object"!=typeof e)return e;const t=[],n=Object.keys(e).filter(e=>t.includes(e));return n.length>0&&r.warn("Validator",`Reserved attributes found: ${n.join(", ")}`),e},validateEventCallback(e){if("function"!=typeof e)throw new s("Event callback must be a function")}};function b(e){const t=document.createDocumentFragment(),n=document.createComment("Anchor Start : "+e),o=document.createComment("/ Anchor End "+e);t.appendChild(n),t.appendChild(o),t.nativeInsertBefore=t.insertBefore,t.nativeAppendChild=t.appendChild;const s=function(e,n,r){e!==t?e.insertBefore(E.getChild(n),r):e.nativeInsertBefore(E.getChild(n),r)};return t.appendElement=function(e,n=null){o.parentNode!==t?o.parentNode?.insertBefore(e,n||o):o.parentNode.nativeInsertBefore(e,n||o)},t.appendChild=function(e,n=null){const i=o.parentNode;if(i){if(n=n??o,m.isArray(e)){const r=document.createDocumentFragment();for(let t=0,n=e.length;t<n;t++)r.appendChild(E.getChild(e[t]));return s(i,r,n),t}s(i,e,n)}else r.error("Anchor","Anchor : parent not found",e)},t.removeChildren=function(){const e=o.parentNode;if(e===t)return;if(e.firstChild===n&&e.lastChild===o)return void e.replaceChildren(n,o);let r,s=n.nextSibling;const i=document.createDocumentFragment();for(;s&&s!==o;)r=s.nextSibling,i.append(s),s=r;i.replaceChildren()},t.remove=function(){if(o.parentNode===t)return;let e,r=n.nextSibling;for(;r!==o;)e=r.nextSibling,t.nativeAppendChild(r),r=e},t.removeWithAnchors=function(){t.removeChildren(),n.remove(),o.remove()},t.replaceContent=function(e){const r=o.parentNode;r&&(r.firstChild!==n||r.lastChild!==o?(t.removeChildren(),r.insertBefore(e,o)):r.replaceChildren(n,e,o))},t.insertBefore=function(e,n=null){t.appendChild(e,n)},t.clear=function(){t.remove()},t.endElement=function(){return o},t.startElement=function(){return n},t}const g=["checked","selected","disabled","readonly","required","autofocus","multiple","autocomplete","hidden","contenteditable","spellcheck","translate","draggable","async","defer","autoplay","controls","loop","muted","download","reversed","open","default","formnovalidate","novalidate","scoped","itemscope","allowfullscreen","allowpaymentrequest","playsinline"];function v(e){return new u(e)}function y(e,t){for(let n in t){const r=t[n];m.isObservable(r)?(e.classList.toggle(n,r.val()),r.subscribe(t=>e.classList.toggle(n,t))):r.$observer?(e.classList.toggle(n,r.$observer.val()===r.$target),r.$observer.on(r.$target,function(t){e.classList.toggle(n,t)})):e.classList.toggle(n,r)}}function w(e,t){for(let n in t){const r=t[n];m.isObservable(r)?(e.style[n]=r.val(),r.subscribe(t=>{e.style[n]=t})):e.style[n]=r}}function $(e,t,n){const r=m.isObservable(n)?n.val():n;m.isBoolean(r)?e[t]=r:e[t]=r===e.value,m.isObservable(n)&&(["checked"].includes(t)&&e.addEventListener("input",()=>{m.isBoolean(r)?n.set(e[t]):n.set(e.value)}),n.subscribe(n=>{m.isBoolean(n)?e[t]=n:e[t]=n===e.value}))}function C(e,t,n){const r=n=>{"value"!==t?e.setAttribute(t,n):e.value=n};r(n.val()),n.subscribe(r),"value"===t&&e.addEventListener("input",()=>n.set(e.value))}v.getById=function(e){const t=o.getObservableById(parseInt(e));if(!t)throw new s("Observable.getById : No observable found with id "+e);return t},v.cleanup=function(e){e.cleanup()},v.autoCleanup=function(e=!1,t={}){if(!e)return;const{interval:n=6e4,threshold:r=100}=t;window.addEventListener("beforeunload",()=>{o.cleanup()}),setInterval(()=>o.cleanObservables(r),n)};const O=new Map;let S=null;const E={createTextNode:()=>(S||(S=document.createTextNode("")),S.cloneNode()),createObservableNode(e,t){const n=E.createTextNode();return t.subscribe(e=>n.nodeValue=String(e)),n.nodeValue=t.val(),e&&e.appendChild(n),n},createStaticTextNode(e,t){let n=E.createTextNode();return n.nodeValue=String(t),e&&e.appendChild(n),n},createElement(e){if(e){if(O.has(e))return O.get(e).cloneNode();const t=document.createElement(e);return O.set(e,t),t.cloneNode()}return new b("Fragment")},processChildren(e,t){if(null===e)return;const n=Array.isArray(e)?e:[e];a.emit("BeforeProcessChildren",t);for(let e=0,r=n.length;e<r;e++){let r=this.getChild(n[e]);null!==r&&t.appendChild(r)}a.emit("AfterProcessChildren",t)},getChild(e){if(null===e)return null;if(m.isString(e)&&(e=e.resolveObservableTemplate?e.resolveObservableTemplate():e,m.isString(e)))return E.createStaticTextNode(null,e);if(m.isElement(e))return e;if(m.isObservable(e))return E.createObservableNode(null,e);if(m.isNDElement(e))return e.$element??e.$build?.()??null;if(m.isArray(e)){const t=document.createDocumentFragment();for(let n=0,r=e.length;n<r;n++)t.appendChild(this.getChild(e[n]));return t}return m.isFunction(e)?(a.emit("BeforeProcessComponent",e),this.getChild(e())):E.createStaticTextNode(null,e)},processAttributes(e,t){m.isFragment(e)||t&&function(e,t){if(m.validateAttributes(t),!m.isObject(t))throw new s("Attributes must be an object");for(let n in t){const r=n.toLowerCase();let o=t[r];if(m.isString(o)){if(o=o.resolveObservableTemplate?o.resolveObservableTemplate():o,m.isString(o)){e.setAttribute(r,o);continue}const t=o.filter(e=>m.isObservable(e));o=v.computed(()=>o.map(e=>m.isObservable(e)?e.val():e).join(" ")||" ",t)}"class"===r&&m.isJson(o)?y(e,o):"style"===r&&m.isJson(o)?w(e,o):g.includes(r)?$(e,r,o):m.isObservable(o)?C(e,r,o):e.setAttribute(r,o)}}(e,t)},setup:(e,t,n)=>(a.emit("Setup",e,t,n),e)};function A(e,t){const n=e.toLowerCase();return function(e,o=null){try{if(!m.isJson(e)){const t=o;o=e,e=t}const r=E.createElement(n),s="function"==typeof t?t(r):r;return E.processAttributes(s,e),E.processChildren(o,s),E.setup(s,e,t)}catch(e){r.error("ElementCreation",`Error creating ${n}`,e)}}}Object.defineProperty(HTMLElement.prototype,"nd",{configurable:!0,get(){return this.$nd||(this.$nd=new f(this),this.$nd.nd=this.$nd),this.$nd}});class k extends Error{constructor(e,t){super(`${e}\n\n${t.join("\n")}\n\n`)}}const I={string:e=>({name:e,type:"string",validate:e=>m.isString(e)}),number:e=>({name:e,type:"number",validate:e=>m.isNumber(e)}),boolean:e=>({name:e,type:"boolean",validate:e=>m.isBoolean(e)}),observable:e=>({name:e,type:"observable",validate:e=>m.isObservable(e)}),element:e=>({name:e,type:"element",validate:e=>m.isElement(e)}),function:e=>({name:e,type:"function",validate:e=>m.isFunction(e)}),object:e=>({name:e,type:"object",validate:e=>m.isObject(e)}),objectNotNull:e=>({name:e,type:"object",validate:e=>m.isObject(e)&&null!==e}),children:e=>({name:e,type:"children",validate:e=>m.validateChildren(e)}),attributes:e=>({name:e,type:"attributes",validate:e=>m.validateAttributes(e)}),optional:e=>({...e,optional:!0}),oneOf:(e,...t)=>({name:e,type:"oneOf",types:t,validate:e=>t.some(t=>t.validate(e))})},x=(e,t,n="Function")=>{if(!m.isArray(t))throw new s("withValidation : argSchema must be an array");return function(...r){return((e,t,n="Function")=>{if(!t)return;const r=[],o=t.filter(e=>!e.optional).length;if(e.length<o&&r.push(`${n}: Expected at least ${o} arguments, got ${e.length}`),t.forEach((t,o)=>{const s=o+1,i=e[o];if(void 0!==i){if(!t.validate(i)){const e=i?.constructor?.name||typeof i;r.push(`${n}: Invalid argument '${t.name}' at position ${s}, expected ${t.type}, got ${e}`)}}else t.optional||r.push(`${n}: Missing required argument '${t.name}' at position ${s}`)}),r.length>0)throw new k("Argument validation failed",r)})(r,t,e.name||n),e.apply(this,r)}};Function.prototype.args=function(...e){return x(this,e)},Function.prototype.errorBoundary=function(e){return(...t)=>{try{return this.apply(this,t)}catch(t){return e(t)}}},String.prototype.use=function(e){const t=this;return v.computed(()=>t.replace(/\$\{(.*?)}/g,(t,n)=>{const r=e[n];return m.isObservable(r)?r.val():r}),Object.values(e))},String.prototype.resolveObservableTemplate=function(){return m.containsObservableReference(this)?this.split(/(\{\{#ObItem::\([0-9]+\)\}\})/g).filter(Boolean).map(e=>{if(!m.containsObservableReference(e))return e;const[t,n]=e.match(/\{\{#ObItem::\(([0-9]+)\)\}\}/);return v.getById(n)}):this.valueOf()};const F=["push","pop","shift","unshift","reverse","sort","splice"];v.array=function(e){if(!Array.isArray(e))throw new s("Observable.array : target must be an array");const t=v(e);a.emit("CreateObservableArray",t),F.forEach(e=>{t[e]=function(...n){const r=t.val()[e](...n);return t.trigger({action:e,args:n,result:r}),r}}),t.clear=function(){return t.val().length=0,t.trigger({action:"clear"}),!0},t.merge=function(e){t.set([...t.val(),...e])},t.populateAndRender=function(e,n){t.trigger({action:"populate",args:[t.val(),e,n]})},t.remove=function(e){const n=t.val().splice(e,1);return 0===n.length?[]:(t.trigger({action:"remove",args:[e],result:n[0]}),n)},t.swap=function(e,n){const r=t.val(),o=r.length;if(o<e||o<n)return!1;if(n<e){const t=e;e=n,n=t}const s=r[e],i=r[n];return r[e]=i,r[n]=s,t.trigger({action:"swap",args:[e,n],result:[s,i]}),!0},t.length=function(){return t.val().length};return["map","filter","reduce","some","every","find","findIndex","concat"].forEach(e=>{t[e]=function(...n){return t.val()[e](...n)}}),t},v.batch=function(e){const t=v(0),n=function(){if(m.isAsyncFunction(e))return e(...arguments).then(()=>{t.trigger()}).catch(e=>{throw e});e(...arguments),t.trigger()};return n.$observer=t,n},v.init=function(e){const t={};for(const n in e){const r=e[n];m.isJson(r)?t[n]=v.init(r):m.isArray(r)?t[n]=v.array(r):t[n]=v(r)}const n=function(){};return new Proxy(t,{get:(e,r)=>"__isProxy__"===r||("$value"===r?function(){const e={};for(const n in t){const r=t[n];m.isObservable(r)?e[n]=r.val():m.isProxy(r)?e[n]=r.$value:e[n]=r}return e}():"$clone"===r?n:void 0!==e[r]?e[r]:void 0),set(e,t,n){void 0!==e[t]&&e[t].set(n)}})},v.value=function(e){if(m.isObservable(e))return e.val();if(m.isProxy(e))return e.$value;if(m.isArray(e)){const t=[];for(let n=0,r=e.length;n<r;n++){const r=e[n];t.push(v.value(r))}return t}return e},v.update=function(e,t){for(const n in t){const r=e[n],o=t[n];if(m.isObservable(r)){if(m.isArray(o)){v.update(r,o);continue}r.set(o)}else m.isProxy(r)?v.update(r,o):e[n]=o}},v.object=v.init,v.json=v.init,v.computed=function(e,t=[]){const n=new u(e()),r=()=>n.set(e());if(a.emit("CreateObservableComputed",n,t),m.isFunction(t)){if(!m.isObservable(t.$observer))throw new s("Observable.computed : dependencies must be valid batch function");return t.$observer.subscribe(r),n}return t.forEach(e=>e.subscribe(r)),n};const D=function(){const e=new Map;return{use(t){const{observer:n,subscribers:r}=e.get(t),o=v(n.val()),s=n.subscribe(e=>o.set(e)),i=o.subscribe(e=>n.set(e));return o.destroy=()=>{s(),i(),o.cleanup()},r.add(o),o},follow(e){return this.use(e)},create(t,n){const r=v(n);return e.set(t,{observer:r,subscribers:new Set}),r},get(t){const n=e.get(t);return n?n.observer:null},getWithSubscribers:t=>e.get(t),delete(t){const n=e.get(t);n&&(n.observer.cleanup(),n.subscribers.forEach(e=>e.destroy()),n.observer.clear())}}}();const T=function(e,t,n=null){if(!m.isObservable(e))return r.warn("ShowIf","ShowIf : condition must be an Observable / "+n,e);const o=new b("Show if : "+(n||""));let s=null;const i=()=>s||(s=E.getChild(t),s);return e.val()&&o.appendChild(i()),e.subscribe(e=>{e?o.appendChild(i()):o.remove()}),o},P=function(e,t,n=!0){if(!m.isObservable(e))throw new s("Toggle : condition must be an Observable");const r=new b,o=new Map,i=function(e){if(n&&o.has(e))return o.get(e);let r=t[e];return r?(r=E.getChild(r),n&&o.set(e,r),r):null},a=e.val(),u=i(a);return u&&r.appendChild(u),e.subscribe(e=>{const t=i(e);r.remove(),t&&r.appendChild(t)}),r},R=function(e,t,n){if(!m.isObservable(e))throw new s("Toggle : condition must be an Observable");return P(e,{true:t,false:n})},L=A("div"),N=A("span"),B=A("label"),M=A("p"),q=M,j=A("strong"),_=A("h1"),V=A("h2"),H=A("h3"),z=A("h4"),W=A("h5"),U=A("h6"),J=A("br"),K=A("a"),Q=A("pre"),Z=A("code"),G=A("blockquote"),X=A("hr"),Y=A("em"),ee=A("small"),te=A("mark"),ne=A("del"),re=A("ins"),oe=A("sub"),se=A("sup"),ie=A("abbr"),ae=A("cite"),ue=A("q"),le=A("dl"),ce=A("dt"),he=A("dd"),de=A("form",function(e){return e.submit=function(t){return"function"==typeof t?(e.onSubmit(e=>{e.preventDefault(),t(e)}),e):(this.setAttribute("action",t),e)},e.multipartFormData=function(){return this.setAttribute("enctype","multipart/form-data"),e},e.post=function(t){return this.setAttribute("method","post"),this.setAttribute("action",t),e},e.get=function(e){this.setAttribute("method","get"),this.setAttribute("action",e)},e}),pe=A("input"),fe=A("textarea"),me=fe,be=A("select"),ge=A("fieldset"),ve=A("option"),ye=A("legend"),we=A("datalist"),$e=A("output"),Ce=A("progress"),Oe=A("meter"),Se=A("button"),Ee=A("main"),Ae=A("section"),ke=A("article"),Ie=A("aside"),xe=A("nav"),Fe=A("figure"),De=A("figcaption"),Te=A("header"),Pe=A("footer"),Re=A("img"),Le=function(e,t){return Re({src:e,...t})},Ne=A("details"),Be=A("summary"),Me=A("dialog"),qe=A("menu"),je=A("ol"),_e=A("ul"),Ve=A("li"),He=Ve,ze=je,We=_e,Ue=A("audio"),Je=A("video"),Ke=A("source"),Qe=A("track"),Ze=A("canvas"),Ge=A("svg"),Xe=A("time"),Ye=A("data"),et=A("address"),tt=A("kbd"),nt=A("samp"),rt=A("var"),ot=A("wbr"),st=A("caption"),it=A("table"),at=A("thead"),ut=A("tfoot"),lt=A("tbody"),ct=A("tr"),ht=ct,dt=A("th"),pt=dt,ft=dt,mt=A("td"),bt=mt,gt=A("");var vt=Object.freeze({__proto__:null,Abbr:ie,Address:et,Anchor:b,Article:ke,Aside:Ie,AsyncImg:function(e,t,n,r){const o=Le(t||e,n),i=new Image;return i.onload=()=>{m.isFunction(r)&&r(null,o),o.src=e},i.onerror=()=>{m.isFunction(r)&&r(new s("Image not found"))},m.isObservable(e)&&e.subscribe(e=>{i.src=e}),i.src=e,o},Audio:Ue,BaseImage:Re,Blockquote:G,Br:J,Button:Se,Canvas:Ze,Caption:st,Checkbox:e=>pe({type:"checkbox",...e}),Cite:ae,Code:Z,ColorInput:e=>pe({type:"color",...e}),Data:Ye,Datalist:we,DateInput:e=>pe({type:"date",...e}),DateTimeInput:e=>pe({type:"datetime-local",...e}),Dd:he,Del:ne,Details:Ne,Dialog:Me,Div:L,Dl:le,Dt:ce,Em:Y,EmailInput:e=>pe({type:"email",...e}),FieldSet:ge,FigCaption:De,Figure:Fe,FileInput:e=>pe({type:"file",...e}),Footer:Pe,ForEach:function(e,t,n){const o=new b("ForEach"),s=o.endElement();o.startElement();let i=new Map,a=null;const u=new Set,l=e=>{for(const[t,n]of i.entries()){if(u.has(t))continue;const r=n.child?.deref();e&&r&&e.removeChild(r),n.indexObserver?.cleanup(),n.child=null,n.indexObserver=null,i.delete(n.keyId),a&&a.delete(n.keyId)}},h=(e,o)=>{const s=c(e,o,n);if(i.has(s)){const e=i.get(s);if(e.indexObserver?.set(o),e.isNew=!1,e.child?.deref())return s;i.delete(s)}try{const n=t.length>=2?v(o):null;let r=E.getChild(t(e,n));i.set(s,{keyId:s,isNew:!0,child:new WeakRef(r),indexObserver:n})}catch(e){throw r.error("ForEach",`Error creating element for key ${s}`,e),e}return s},d=()=>{const t=s.parentNode;if(!t)return;const n=m.isObservable(e)?e.val():e;if(u.clear(),Array.isArray(n))for(let e=0,t=n.length;e<t;e++){const t=h(n[e],e);u.add(t)}else for(const e in n){const t=h(n[e],e);u.add(t)}if(0===u.size)return o.removeChildren(),l(),void a?.clear();l(t),a&&0!==a.size?(()=>{let e=document.createDocumentFragment();const t=Array.from(u);Array.from(a);for(const n in t){const r=t[n],o=i.get(r);if(!o)continue;const s=o.child.deref();s&&e.appendChild(s)}o.replaceContent(e)})():(e=>{const t=document.createDocumentFragment();for(const e of u){const n=i.get(e);if(!n)continue;const r=n.child?.deref();r&&t.appendChild(r)}e.insertBefore(t,s)})(t),a?.clear(),a=new Set([...u])};return d(),m.isObservable(e)&&e.subscribe(d),o},ForEachArray:function(e,t,n,o={}){const s=new b("ForEach Array"),i=s.endElement(),a=s.startElement();let u=new Map,l=0;const h=t.length>=2,d=new WeakMap,p=()=>{s.removeChildren(),w(),l=0},f=(e,t)=>d.has(e)?d.get(e):c(e,t,n),g=e=>C(f(e)),y=(e,t=!0)=>{if(!e)return;const n=e.child;e.indexObserver?.deref()?.cleanup(),e.child=null,e.indexObserver=null,t&&(n?.remove(),u.delete(e.keyId))},w=()=>{for(const[e,t]of u.entries())y(t,!1);u.clear()},$=(e,n)=>{const o=f(e,n);if(u.has(o)){const e=u.get(o);e.indexObserver?.deref()?.set(n);const t=e.child;if(t)return t;u.delete(o)}try{const r=h?v(n):null;let s=E.getChild(t(e,r));return u.set(o,{keyId:o,child:s,indexObserver:r?new WeakRef(r):null}),d.set(e,o),s}catch(e){throw r.error("ForEach",`Error creating element for key ${o}`,e),e}},C=function(e){const t=u.get(e);if(!t)return null;const n=t.child;return n||(y(t,!1),null)},O=function(e,t){const n=u.get(e);if(!n)return null;const r=n.child;if(!r)return null;t?t.appendChild(r):r.remove()},S={toFragment(e,t=0){const n=document.createDocumentFragment();for(let t=0,r=e.length;t<r;t++)n.append($(e[t],l)),l++;return n},add(e,t=0){const n=S.toFragment(e);setTimeout(()=>{s.appendElement(n)},t)},replace(e){p(),S.add(e)},reOrder(e){let t=null;const n=document.createDocumentFragment();for(const r of e)t=g(r),t&&n.appendChild(t);t=null,s.appendElement(n,i)},removeOne(e,t){let n=g(e);n&&((e,t=!0)=>{y(u.get(e),t)})(f(e,t),!0),n=null},clear:p,merge(e){S.add(e,0)},push(e){let t=0;o.pushDelay&&(t=o.pushDelay(e)??0),S.add(e,t)},populate([e,t,n]){const r=document.createDocumentFragment();for(let o=0;o<t;o++){const t=n(o);e.push(t),r.append($(t,o)),l++}s.appendChild(r),r.replaceChildren()},unshift(e){s.insertBefore(S.toFragment(e),a.nextSibling)},splice(e,t){const[n,r,...o]=e;let a=null;const u=document.createDocumentFragment();if(t.length>0){let e=f(t[0],n);if(1===t.length)O(e,u);else if(t.length>1){const r=C(e);a=r?.previousSibling;for(let e=0;e<t.length;e++){const r=f(t[e],n+e);O(r,u)}}}else a=i;u.replaceChildren(),o&&o.length&&a&&s.insertBefore(S.toFragment(o),a.nextSibling)},reverse(e,t){S.reOrder(t)},sort(e,t){S.reOrder(t)},remove(e,t){S.removeOne(t)},pop(e,t){S.removeOne(t)},shift(e,t){S.removeOne(t)},swap(e,t){const n=i.parentNode;let r=g(t[0]),o=g(t[1]);if(!r||!o)return;const s=o.nextSibling;n.insertBefore(o,r),n.insertBefore(r,s),r=null,o=null}},A=(e,t,n)=>{if("clear"!==n.action&&e.length){if(n?.action)S[n.action]&&S[n.action](n.args,n.result);else{if(0===l)return void S.add(e);S.replace(e)}((e,t=0)=>{if(!h)return;let n=t;for(let r=t,o=e?.length;r<o;r++){const t=u.get(f(e[r],r));t&&(t.indexObserver?.deref()?.set(n),n++)}})(e,0)}else{if(0===l)return;p()}};return A(e.val(),0,{action:null}),m.isObservable(e)&&e.subscribe(A),s},Form:de,Fragment:gt,H1:_,H2:V,H3:H,H4:z,H5:W,H6:U,Header:Te,HiddenInput:e=>pe({type:"hidden",...e}),HideIf:function(e,t,n){const r=v(!e.val());return e.subscribe(e=>r.set(!e)),T(r,t,n)},HideIfNot:function(e,t,n){return T(e,t,n)},Hr:X,Img:Le,Input:pe,Ins:re,Kbd:tt,Label:B,LazyImg:function(e,t){return Le(e,{...t,loading:"lazy"})},Legend:ye,Li:He,Link:K,ListItem:Ve,Main:Ee,Mark:te,Match:P,Menu:qe,Meter:Oe,MonthInput:e=>pe({type:"month",...e}),NativeDocumentFragment:b,Nav:xe,NumberInput:e=>pe({type:"number",...e}),Ol:ze,Option:ve,OrderedList:je,Output:$e,P:M,Paragraph:q,PasswordInput:e=>pe({type:"password",...e}),Pre:Q,Progress:Ce,Quote:ue,Radio:e=>pe({type:"radio",...e}),RangeInput:e=>pe({type:"range",...e}),ReadonlyInput:e=>pe({readonly:!0,...e}),Samp:nt,SearchInput:e=>pe({type:"search",...e}),Section:Ae,Select:be,ShowIf:T,SimpleButton:(e,t)=>Se(e,{type:"button",...t}),Small:ee,Source:Ke,Span:N,Strong:j,Sub:oe,SubmitButton:(e,t)=>Se(e,{type:"submit",...t}),Summary:Be,Sup:se,Svg:Ge,Switch:R,TBody:lt,TBodyCell:bt,TFoot:ut,TFootCell:ft,THead:at,THeadCell:pt,TRow:ht,Table:it,Td:mt,TelInput:e=>pe({type:"tel",...e}),TextArea:fe,TextInput:me,Th:dt,Time:Xe,TimeInput:e=>pe({type:"time",...e}),Tr:ct,Track:Qe,Ul:We,UnorderedList:_e,UrlInput:e=>pe({type:"url",...e}),Var:rt,Video:Je,Wbr:ot,WeekInput:e=>pe({type:"week",...e}),When:function(e){if(!m.isObservable(e))throw new s("When : condition must be an Observable");let t=null,n=null;return{show(e){return t=e,this},otherwise:r=>(n=r,R(e,t,n))}}});const yt={};function wt(e,t,n={}){e="/"+h(e,"/");let r=null,o=n.name||null;const s=n.middlewares||[],i=n.shouldRebuild||!1,a=n.with||{},u={},l=[],c=e=>{if(!e)return null;const[t,n]=e.split(":");let r=a[t];return!r&&n&&(r=yt[n]),r||(r="[^/]+"),r=r.replace("(","(?:"),{name:t,pattern:`(${r})`}},d=()=>{if(r)return r;const t=e.replace(/\{(.*?)}/gi,(e,t)=>{const n=c(t);return n&&n.pattern?(u[n.name]=n.pattern,l.push(n.name),n.pattern):e});return r=new RegExp("^"+t+"$"),r};this.name=()=>o,this.component=()=>t,this.middlewares=()=>s,this.shouldRebuild=()=>i,this.path=()=>e,this.match=function(e){e="/"+h(e,"/");if(!d().exec(e))return!1;const t={};return d().exec(e).forEach((e,n)=>{if(n<1)return;const r=l[n-1];t[r]=e}),t},this.url=function(t){const n=e.replace(/\{(.*?)}/gi,(e,n)=>{const r=c(n);if(t.params&&t.params[r.name])return t.params[r.name];throw new Error(`Missing parameter '${r.name}'`)}),r="object"==typeof t.query?new URLSearchParams(t.query).toString():null;return(t.basePath?t.basePath:"")+(r?`${n}?${r}`:n)}}class $t extends Error{constructor(e,t){super(e),this.context=t}}const Ct=(e,t)=>{const n=[];return e.forEach(e=>{n.push(h(e.suffix,"/"))}),n.push(h(t,"/")),n.join("/")},Ot=(e,t)=>{const n=[];return e.forEach(e=>{e.options.middlewares&&n.push(...e.options.middlewares)}),t&&n.push(...t),n},St=(e,t)=>{const n=[];return e.forEach(e=>{e.options?.name&&n.push(e.options.name)}),t&&n.push(t),n.join(".")};function Et(){const e=[];let t=0;const n=n=>{const o=t+n;if(!e[o])return;t=o;const{route:s,params:i,query:a,path:u}=e[o];r(u)},r=e=>{window.location.replace(`${window.location.pathname}${window.location.search}#${e}`)},o=()=>window.location.hash.slice(1);this.push=function(n){const{route:s,params:i,query:a,path:u}=this.resolve(n);u!==o()&&(e.splice(t+1),e.push({route:s,params:i,query:a,path:u}),t++,r(u))},this.replace=function(n){const{route:r,params:s,query:i,path:a}=this.resolve(n);a!==o()&&(e[t]={route:r,params:s,query:i,path:a})},this.forward=function(){return t<e.length-1&&n(1)},this.back=function(){return t>0&&n(-1)},this.init=function(n){window.addEventListener("hashchange",()=>{const{route:e,params:t,query:n,path:r}=this.resolve(o());this.handleRouteChange(e,t,n,r)});const{route:r,params:s,query:i,path:a}=this.resolve(n||o());e.push({route:r,params:s,query:i,path:a}),t=0,this.handleRouteChange(r,s,i,a)}}function At(){this.push=function(e){try{const{route:t,path:n,params:r,query:o}=this.resolve(e);if(window.history.state&&window.history.state.path===n)return;window.history.pushState({name:t.name(),params:r,path:n},t.name()||n,n),this.handleRouteChange(t,r,o,n)}catch(e){r.error("HistoryRouter","Error in pushState",e)}},this.replace=function(e){const{route:t,path:n,params:o}=this.resolve(e);try{window.history.replaceState({name:t.name(),params:o,path:n},t.name()||n,n),this.handleRouteChange(t,o,{},n)}catch(e){r.error("HistoryRouter","Error in replaceState",e)}},this.forward=function(){window.history.forward()},this.back=function(){window.history.back()},this.init=function(e){window.addEventListener("popstate",e=>{try{if(!e.state||!e.state.path)return;const t=e.state.path,{route:n,params:r,query:o,path:s}=this.resolve(t);if(!n)return;this.handleRouteChange(n,r,o,s)}catch(e){r.error("HistoryRouter","Error in popstate event",e)}});const{route:t,params:n,query:o,path:s}=this.resolve(e||window.location.pathname+window.location.search);this.handleRouteChange(t,n,o,s)}}function kt(){const e=[];let t=0;const n=n=>{const r=t+n;if(!e[r])return;t=r;const{route:o,params:s,query:i,path:a}=e[r];this.handleRouteChange(o,s,i,a)};this.push=function(n){const{route:r,params:o,query:s,path:i}=this.resolve(n);e[t]&&e[t].path===i||(e.splice(t+1),e.push({route:r,params:o,query:s,path:i}),t++,this.handleRouteChange(r,o,s,i))},this.replace=function(n){const{route:r,params:o,query:s,path:i}=this.resolve(n);e[t]={route:r,params:o,query:s,path:i},this.handleRouteChange(r,o,s,i)},this.forward=function(){return t<e.length-1&&n(1)},this.back=function(){return t>0&&n(-1)},this.init=function(n){const r=n||window.location.pathname+window.location.search,{route:o,params:s,query:i,path:a}=this.resolve(r);e.push({route:o,params:s,query:i,path:a}),t=0,this.handleRouteChange(o,s,i,a)}}const It="default";function xt(e={}){const t=[],n={},o=[],s=[],i={route:null,params:null,query:null,path:null,hash:null};if("hash"===e.mode)Et.apply(this,[]);else if("history"===e.mode)At.apply(this,[]);else{if("memory"!==e.mode)throw new $t("Invalid router mode "+e.mode);kt.apply(this,[])}const a=function(e,t){for(const n of s)try{n(e),t&&t(e)}catch(e){r.warn("Route Listener","Error in listener:",e)}};this.routes=()=>[...t],this.currentState=()=>({...i}),this.add=function(e,r,s){const i=new wt(Ct(o,e),r,{...s,middlewares:Ot(o,s?.middlewares||[]),name:s?.name?St(o,s.name):null});return t.push(i),i.name()&&(n[i.name()]=i),this},this.group=function(e,t,n){if(!m.isFunction(n))throw new $t("Callback must be a function");return o.push({suffix:e,options:t}),n(),o.pop(),this},this.generateUrl=function(e,t={},r={}){const o=n[e];if(!o)throw new $t(`Route not found for name: ${e}`);return o.url({params:t,query:r})},this.resolve=function(e){if(m.isJson(e)){const t=n[e.name];if(!t)throw new $t(`Route not found for name: ${e.name}`);return{route:t,params:e.params,query:e.query,path:t.url({...e})}}const[r,o]=e.split("?"),s="/"+h(r,"/");let i,a=null;for(const e of t)if(i=e.match(s),i){a=e;break}if(!a)throw new $t(`Route not found for url: ${r}`);const u={};if(o){const e=new URLSearchParams(o).entries();for(const[t,n]of e)u[t]=n}return{route:a,params:i,query:u,path:e}},this.subscribe=function(e){if(!m.isFunction(e))throw new $t("Listener must be a function");return s.push(e),()=>{s.splice(s.indexOf(e),1)}},this.handleRouteChange=function(e,t,n,r){i.route=e,i.params=t,i.query=n,i.path=r;const o=[...e.middlewares(),a];let s=0;const u={...i},l=e=>{if(s++,!(s>=o.length))return o[s](e||u,l)};return o[s](u,l)}}function Ft(e,t){const{to:n,href:r,...o}=e,s=n||r;if(m.isString(s)){const e=xt.get();return K({...o,href:s},t).nd.onPreventClick(()=>{e.push(s)})}const i=s.router||It,a=xt.get(i);if(!a)throw new $t('Router not found "'+i+'" for link "'+s.name+'"');const u=a.generateUrl(s.name,s.params,s.query);return K({...o,href:u},t).nd.onPreventClick(()=>{a.push(u)})}xt.routers={},xt.create=function(t,n){if(!m.isFunction(n))throw r.error("Router","Callback must be a function",e),new $t("Callback must be a function");const o=new xt(t);return xt.routers[t.name||It]=o,n(o),o.init(t.entry),o.mount=function(e){if(m.isString(e)){const t=document.querySelector(e);if(!t)throw new $t(`Container not found for selector: ${e}`);e=t}else if(!m.isElement(e))throw new $t("Container must be a string or an Element");return function(e,t){const n=new Map,r=function(e){t.innerHTML="",t.appendChild(e)},o=function(e){if(!e.route)return;const{route:t,params:o,query:s,path:i}=e;if(n.has(i)){const e=n.get(i);return void r(e)}const a=t.component()({params:o,query:s});n.set(i,a),r(a)};return e.subscribe(o),o(e.currentState()),t}(o,e)},o},xt.get=function(e){const t=xt.routers[e||It];if(!t)throw new $t(`Router not found for name: ${e}`);return t},xt.push=function(e,t=null){return xt.get(t).push(e)},xt.replace=function(e,t=null){return xt.get(t).replace(e)},xt.forward=function(e=null){return xt.get(e).forward()},xt.back=function(e=null){return xt.get(e).back()},Ft.blank=function(e,t){return K({...e,target:"_blank"},t)};var Dt=Object.freeze({__proto__:null,Link:Ft,RouteParamPatterns:yt,Router:xt});return t.ArgTypes=I,t.ElementCreator=E,t.HtmlElementWrapper=A,t.NDElement=f,t.Observable=v,t.Store=D,t.classPropertyAccumulator=function(e=[]){let t=m.isString(e)?e.split(" ").filter(Boolean):e;const n=m.isArray(t);return{add(e,r=!0){n?t.push(e):t[e]=r},value:()=>n?t.join(" "):{...t}}},t.cssPropertyAccumulator=function(e={}){let t=m.isString(e)?e.split(";").filter(Boolean):e;const n=m.isArray(t);return{add(e,r){n?t.push(e+" : "+r):t[e]=r},value:()=>n?t.join(";").concat(";"):{...t}}},t.elements=vt,t.normalizeComponentArgs=function(e,t){if(!m.isJson(t)){const n=e;e=t,t=n}return{props:e,children:t}},t.router=Dt,t.withValidation=x,t}({});
|
package/index.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ export * from './types/images';
|
|
|
6
6
|
export * from './types/control-flow';
|
|
7
7
|
export * from './types/router';
|
|
8
8
|
export * from './types/store';
|
|
9
|
+
export * from './types/plugins-manager';
|
|
10
|
+
export * from './types/property-accumulator';
|
|
11
|
+
export * from './types/validator';
|
|
12
|
+
export * from './types/args-types';
|
|
9
13
|
|
|
10
14
|
// Main static exports
|
|
11
15
|
import { ObservableStatic } from './types/observable';
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { NDElement } from './src/wrappers/NDElement'
|
|
|
4
4
|
|
|
5
5
|
import './src/utils/prototypes.js';
|
|
6
6
|
|
|
7
|
+
export * from './src/utils/property-accumulator';
|
|
7
8
|
export * from './src/utils/plugins-manager';
|
|
8
9
|
export * from './src/utils/args-types';
|
|
9
10
|
export * from './src/utils/validator'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "native-document",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@rollup/plugin-replace": "^6.0.2",
|
|
16
16
|
"@rollup/plugin-terser": "^0.4.4",
|
|
17
|
-
"eslint": "^9.33.0"
|
|
18
|
-
"rollup-plugin-gzip": "^4.1.1"
|
|
17
|
+
"eslint": "^9.33.0"
|
|
19
18
|
}
|
|
20
19
|
}
|
package/readme.md
CHANGED
|
@@ -13,7 +13,7 @@ NativeDocument combines the familiarity of vanilla JavaScript with the power of
|
|
|
13
13
|
|
|
14
14
|
### **Instant Start**
|
|
15
15
|
```html
|
|
16
|
-
<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document/dist/native-document.min.js"></script>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
### **Familiar API**
|
|
@@ -48,7 +48,7 @@ document.body.appendChild(App);
|
|
|
48
48
|
|
|
49
49
|
### Option 1: CDN (Instant Start)
|
|
50
50
|
```html
|
|
51
|
-
<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document
|
|
51
|
+
<script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document/dist/native-document.min.js"></script>
|
|
52
52
|
<script>
|
|
53
53
|
const { Div } = NativeDocument.elements
|
|
54
54
|
const { Observable } = NativeDocument
|
package/src/data/Observable.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import ObservableItem from './ObservableItem';
|
|
2
|
-
import Validator from "../utils/validator";
|
|
3
2
|
import MemoryManager from "./MemoryManager";
|
|
4
3
|
import NativeDocumentError from "../errors/NativeDocumentError";
|
|
5
|
-
import {debounce} from "../utils/helpers.js";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
*
|
|
@@ -2,7 +2,7 @@ import DebugManager from "../utils/debug-manager";
|
|
|
2
2
|
import MemoryManager from "./MemoryManager";
|
|
3
3
|
import NativeDocumentError from "../errors/NativeDocumentError";
|
|
4
4
|
import ObservableChecker from "./ObservableChecker";
|
|
5
|
-
import
|
|
5
|
+
import PluginsManager from "../utils/plugins-manager";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
*
|
|
@@ -18,6 +18,7 @@ export default function ObservableItem(value) {
|
|
|
18
18
|
this.$watchers = null;
|
|
19
19
|
|
|
20
20
|
this.$memoryId = null;
|
|
21
|
+
PluginsManager.emit('CreateObservable', this);
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
Object.defineProperty(ObservableItem.prototype, '$value', {
|
|
@@ -86,7 +87,7 @@ ObservableItem.prototype.assocTrigger = function() {
|
|
|
86
87
|
return;
|
|
87
88
|
}
|
|
88
89
|
this.trigger = noneTrigger;
|
|
89
|
-
}
|
|
90
|
+
};
|
|
90
91
|
ObservableItem.prototype.trigger = noneTrigger;
|
|
91
92
|
|
|
92
93
|
/**
|
|
@@ -99,7 +100,9 @@ ObservableItem.prototype.set = function(data) {
|
|
|
99
100
|
}
|
|
100
101
|
this.$previousValue = this.$currentValue;
|
|
101
102
|
this.$currentValue = newValue;
|
|
103
|
+
PluginsManager.emit('ObservableBeforeChange', this);
|
|
102
104
|
this.trigger();
|
|
105
|
+
PluginsManager.emit('ObservableAfterChange', this);
|
|
103
106
|
};
|
|
104
107
|
|
|
105
108
|
ObservableItem.prototype.val = function() {
|
|
@@ -119,20 +122,22 @@ ObservableItem.prototype.disconnectAll = function() {
|
|
|
119
122
|
this.$listeners = null;
|
|
120
123
|
this.$watchers = null;
|
|
121
124
|
this.trigger = noneTrigger;
|
|
122
|
-
}
|
|
125
|
+
};
|
|
126
|
+
|
|
123
127
|
ObservableItem.prototype.cleanup = function() {
|
|
124
128
|
MemoryManager.unregister(this.$memoryId);
|
|
125
129
|
this.disconnectAll();
|
|
126
130
|
this.$isCleanedUp = true;
|
|
127
131
|
delete this.$value;
|
|
128
|
-
}
|
|
132
|
+
};
|
|
129
133
|
|
|
130
134
|
/**
|
|
131
135
|
*
|
|
132
136
|
* @param {Function} callback
|
|
137
|
+
* @param {any} target
|
|
133
138
|
* @returns {(function(): void)}
|
|
134
139
|
*/
|
|
135
|
-
ObservableItem.prototype.subscribe = function(callback) {
|
|
140
|
+
ObservableItem.prototype.subscribe = function(callback, target = null) {
|
|
136
141
|
this.$listeners = this.$listeners ?? [];
|
|
137
142
|
if (this.$isCleanedUp) {
|
|
138
143
|
DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
|
|
@@ -144,9 +149,11 @@ ObservableItem.prototype.subscribe = function(callback) {
|
|
|
144
149
|
|
|
145
150
|
this.$listeners.push(callback);
|
|
146
151
|
this.assocTrigger();
|
|
152
|
+
PluginsManager.emit('ObservableSubscribe', this, target);
|
|
147
153
|
return () => {
|
|
148
154
|
this.unsubscribe(callback);
|
|
149
155
|
this.assocTrigger();
|
|
156
|
+
PluginsManager.emit('ObservableUnsubscribe', this);
|
|
150
157
|
};
|
|
151
158
|
};
|
|
152
159
|
|
|
@@ -203,4 +210,4 @@ ObservableItem.prototype.toString = function() {
|
|
|
203
210
|
MemoryManager.register(this);
|
|
204
211
|
}
|
|
205
212
|
return '{{#ObItem::(' +this.$memoryId+ ')}}';
|
|
206
|
-
}
|
|
213
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import NativeDocumentError from "../../errors/NativeDocumentError";
|
|
2
2
|
import {Observable} from "../Observable";
|
|
3
|
+
import PluginsManager from "../../utils/plugins-manager";
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
const methods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice'];
|
|
@@ -15,6 +16,8 @@ Observable.array = function(target) {
|
|
|
15
16
|
}
|
|
16
17
|
const observer = Observable(target);
|
|
17
18
|
|
|
19
|
+
PluginsManager.emit('CreateObservableArray', observer);
|
|
20
|
+
|
|
18
21
|
methods.forEach((method) => {
|
|
19
22
|
observer[method] = function(...values) {
|
|
20
23
|
const result = observer.val()[method](...values);
|
|
@@ -2,6 +2,7 @@ import ObservableItem from "../ObservableItem";
|
|
|
2
2
|
import Validator from "../../utils/validator";
|
|
3
3
|
import NativeDocumentError from "../../errors/NativeDocumentError";
|
|
4
4
|
import {Observable} from "../Observable";
|
|
5
|
+
import PluginsManager from "../../utils/plugins-manager";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
*
|
|
@@ -14,6 +15,8 @@ Observable.computed = function(callback, dependencies = []) {
|
|
|
14
15
|
const observable = new ObservableItem(initialValue);
|
|
15
16
|
const updatedValue = () => observable.set(callback());
|
|
16
17
|
|
|
18
|
+
PluginsManager.emit('CreateObservableComputed', observable, dependencies)
|
|
19
|
+
|
|
17
20
|
if(Validator.isFunction(dependencies)) {
|
|
18
21
|
if(!Validator.isObservable(dependencies.$observer)) {
|
|
19
22
|
throw new NativeDocumentError('Observable.computed : dependencies must be valid batch function');
|
package/src/utils/args-types.js
CHANGED
|
@@ -97,4 +97,13 @@ export const withValidation = (fn, argSchema, fnName = 'Function') => {
|
|
|
97
97
|
validateArgs(args, argSchema, fn.name || fnName);
|
|
98
98
|
return fn.apply(this, args);
|
|
99
99
|
};
|
|
100
|
-
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const normalizeComponentArgs = function(props, children) {
|
|
103
|
+
if(!Validator.isJson(children)) {
|
|
104
|
+
const temp = props;
|
|
105
|
+
props = children;
|
|
106
|
+
children = temp;
|
|
107
|
+
}
|
|
108
|
+
return { props, children };
|
|
109
|
+
}
|
|
@@ -1,12 +1,73 @@
|
|
|
1
|
+
import DebugManager from "./debug-manager";
|
|
1
2
|
|
|
2
|
-
const
|
|
3
|
+
const PluginsManager = (function() {
|
|
3
4
|
|
|
4
|
-
const $plugins =
|
|
5
|
+
const $plugins = new Map();
|
|
6
|
+
const $pluginByEvents = new Map();
|
|
5
7
|
|
|
6
8
|
return {
|
|
7
|
-
list
|
|
8
|
-
|
|
9
|
+
list() {
|
|
10
|
+
return $pluginByEvents;
|
|
11
|
+
},
|
|
12
|
+
add(name, plugin){
|
|
13
|
+
if (!plugin || typeof plugin !== 'object') {
|
|
14
|
+
throw new Error(`Plugin ${name} must be an object`);
|
|
15
|
+
}
|
|
16
|
+
if($plugins.has(name)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
plugin.$name = name;
|
|
21
|
+
$plugins.set(name ,plugin);
|
|
22
|
+
if(typeof plugin?.init === 'function') {
|
|
23
|
+
plugin.init();
|
|
24
|
+
}
|
|
25
|
+
for(const methodName in plugin) {
|
|
26
|
+
if(/^on[A-Z]/.test(methodName)) {
|
|
27
|
+
if(!$pluginByEvents.has(methodName)) {
|
|
28
|
+
$pluginByEvents.set(methodName, new Set());
|
|
29
|
+
}
|
|
30
|
+
$pluginByEvents.get(methodName).add(plugin);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
remove(pluginName){
|
|
35
|
+
if(!$plugins.has(pluginName)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const plugin = $plugins.get(pluginName);
|
|
39
|
+
if(typeof plugin.cleanup === 'function') {
|
|
40
|
+
plugin.cleanup();
|
|
41
|
+
}
|
|
42
|
+
for(const [name, sets] of $pluginByEvents.entries() ) {
|
|
43
|
+
if(sets.has(plugin)) {
|
|
44
|
+
sets.delete(plugin);
|
|
45
|
+
}
|
|
46
|
+
if(sets.size === 0) {
|
|
47
|
+
$pluginByEvents.delete(name);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
$plugins.delete(pluginName);
|
|
51
|
+
},
|
|
52
|
+
emit(event, ...data) {
|
|
53
|
+
const eventMethodName = 'on'+event
|
|
54
|
+
if(!$pluginByEvents.has(eventMethodName)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const plugins = $pluginByEvents.get(eventMethodName);
|
|
58
|
+
|
|
59
|
+
for(const plugin of plugins) {
|
|
60
|
+
const callback = plugin[eventMethodName];
|
|
61
|
+
if(typeof callback === 'function') {
|
|
62
|
+
try{
|
|
63
|
+
callback.call(plugin, ...data);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
DebugManager.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventMethodName}`, error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
9
70
|
};
|
|
10
71
|
}());
|
|
11
72
|
|
|
12
|
-
export default
|
|
73
|
+
export default PluginsManager;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import Validator from "./validator";
|
|
2
|
+
|
|
3
|
+
export const cssPropertyAccumulator = function(initialValue = {}) {
|
|
4
|
+
let data = Validator.isString(initialValue) ? initialValue.split(';').filter(Boolean) : initialValue;
|
|
5
|
+
const isArray = Validator.isArray(data);
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
add(key, value) {
|
|
9
|
+
if(isArray) {
|
|
10
|
+
data.push(key+' : '+value);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
data[key] = value;
|
|
14
|
+
},
|
|
15
|
+
value() {
|
|
16
|
+
if(isArray) {
|
|
17
|
+
return data.join(';').concat(';');
|
|
18
|
+
}
|
|
19
|
+
return { ...data };
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const classPropertyAccumulator = function(initialValue = []) {
|
|
25
|
+
let data = Validator.isString(initialValue) ? initialValue.split(" ").filter(Boolean) : initialValue;
|
|
26
|
+
const isArray = Validator.isArray(data);
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
add(key, value = true) {
|
|
30
|
+
if(isArray) {
|
|
31
|
+
data.push(key);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
data[key] = value;
|
|
35
|
+
},
|
|
36
|
+
value() {
|
|
37
|
+
if(isArray) {
|
|
38
|
+
return data.join(' ');
|
|
39
|
+
}
|
|
40
|
+
return { ...data };
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Anchor from "../elements/anchor";
|
|
2
2
|
import Validator from "../utils/validator";
|
|
3
3
|
import AttributesWrapper from "./AttributesWrapper";
|
|
4
|
+
import PluginsManager from "../utils/plugins-manager";
|
|
4
5
|
|
|
5
6
|
const $nodeCache = new Map();
|
|
6
7
|
let $textNodeCache = null;
|
|
@@ -63,11 +64,15 @@ export const ElementCreator = {
|
|
|
63
64
|
if(children === null) return;
|
|
64
65
|
const childrenArray = Array.isArray(children) ? children : [children];
|
|
65
66
|
|
|
67
|
+
PluginsManager.emit('BeforeProcessChildren', parent);
|
|
68
|
+
|
|
66
69
|
for(let i = 0, length = childrenArray.length; i < length; i++) {
|
|
67
70
|
let child = this.getChild(childrenArray[i]);
|
|
68
71
|
if (child === null) continue;
|
|
69
72
|
parent.appendChild(child);
|
|
70
73
|
}
|
|
74
|
+
|
|
75
|
+
PluginsManager.emit('AfterProcessChildren', parent);
|
|
71
76
|
},
|
|
72
77
|
getChild(child) {
|
|
73
78
|
if(child === null) {
|
|
@@ -96,6 +101,7 @@ export const ElementCreator = {
|
|
|
96
101
|
return fragment;
|
|
97
102
|
}
|
|
98
103
|
if(Validator.isFunction(child)) {
|
|
104
|
+
PluginsManager.emit('BeforeProcessComponent', child);
|
|
99
105
|
return this.getChild(child());
|
|
100
106
|
}
|
|
101
107
|
return ElementCreator.createStaticTextNode(null, child);
|
|
@@ -119,6 +125,7 @@ export const ElementCreator = {
|
|
|
119
125
|
* @returns {HTMLElement|DocumentFragment}
|
|
120
126
|
*/
|
|
121
127
|
setup(element, attributes, customWrapper) {
|
|
128
|
+
PluginsManager.emit('Setup', element, attributes, customWrapper);
|
|
122
129
|
return element;
|
|
123
130
|
}
|
|
124
131
|
};
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import DocumentObserver from "./DocumentObserver";
|
|
2
2
|
import { EVENTS } from "../utils/events";
|
|
3
|
+
import PluginsManager from "../utils/plugins-manager";
|
|
3
4
|
|
|
4
5
|
export function NDElement(element) {
|
|
5
6
|
this.$element = element;
|
|
6
7
|
this.$observer = null;
|
|
8
|
+
PluginsManager.emit('NDElementCreated', element, this);
|
|
7
9
|
}
|
|
8
10
|
NDElement.prototype.__$isNDElement = true;
|
|
9
11
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {ObservableItem} from "./observable";
|
|
2
|
+
import {ValidChildren} from "./validator";
|
|
3
|
+
|
|
4
|
+
type ValidateFunction<T = any> = (value: any) => value is T;
|
|
5
|
+
|
|
6
|
+
interface ArgType<T = any> {
|
|
7
|
+
name: string;
|
|
8
|
+
type: string;
|
|
9
|
+
validate: ValidateFunction<T>;
|
|
10
|
+
optional?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface OneOfArgType extends Omit<ArgType, 'validate'> {
|
|
14
|
+
type: 'oneOf';
|
|
15
|
+
types: ArgType[];
|
|
16
|
+
validate: (value: any) => boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type AnyArgType = ArgType | OneOfArgType;
|
|
20
|
+
|
|
21
|
+
type Element = HTMLElement | DocumentFragment | Text;
|
|
22
|
+
|
|
23
|
+
export interface OptionalArgType<T = any> extends ArgType<T> {
|
|
24
|
+
optional: true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export declare const ArgTypes: {
|
|
28
|
+
string: (name: string) => ArgType<string>;
|
|
29
|
+
number: (name: string) => ArgType<number>;
|
|
30
|
+
boolean: (name: string) => ArgType<boolean>;
|
|
31
|
+
observable: (name: string) => ArgType<ObservableItem>;
|
|
32
|
+
element: (name: string) => ArgType<Element>;
|
|
33
|
+
function: (name: string) => ArgType<Function>;
|
|
34
|
+
object: (name: string) => ArgType<object>;
|
|
35
|
+
objectNotNull: (name: string) => ArgType<NonNullable<object>>;
|
|
36
|
+
children: (name: string) => ArgType<ValidChildren>;
|
|
37
|
+
attributes: (name: string) => ArgType<any>;
|
|
38
|
+
|
|
39
|
+
optional: <T extends AnyArgType>(argType: T) => T & { optional: true };
|
|
40
|
+
|
|
41
|
+
oneOf: (name: string, ...argTypes: ArgType[]) => OneOfArgType;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type ArgSchema = AnyArgType[];
|
|
45
|
+
|
|
46
|
+
export declare function withValidation<T extends (...args: any[]) => any>(
|
|
47
|
+
fn: T,
|
|
48
|
+
argSchema: ArgSchema,
|
|
49
|
+
fnName?: string
|
|
50
|
+
): T;
|
|
51
|
+
|
|
52
|
+
export declare function normalizeComponentArgs(
|
|
53
|
+
props: any,
|
|
54
|
+
children: any
|
|
55
|
+
): {
|
|
56
|
+
props: any;
|
|
57
|
+
children: any;
|
|
58
|
+
};
|