laghu 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/README.md +211 -29
- package/SECURITY.md +45 -0
- package/dist/laghu.d.ts +32 -8
- package/dist/laghu.min.js +1 -1
- package/dist/router.min.js +1 -0
- package/package.json +5 -3
- package/src/laghu.js +577 -87
- package/src/router.js +123 -0
package/dist/laghu.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";var e="undefined"!=typeof window&&!1!==window.__LAGHU_DEV__,t=new Map,n=new Map,r=new WeakMap,a=null,o=new Set,i=!1,c=function(e){return e};function u(){i=!1;var t=Array.from(o);o.clear();for(var n=0;n<t.length;n++)try{t[n]()}catch(t){e&&"undefined"!=typeof console&&console.error("[laghu]",t)}}function l(e){var t=!1,n=function(){if(!t){a=n;try{e()}finally{a=null}}};return n(),function(){t=!0}}function f(e,t){var n=r.get(e);if(n){var a=n.get(t);a&&a.forEach(function(e){!function(e){o.add(e),i||(i=!0,Promise.resolve().then(u))}(e)})}}function d(e){return null!==e&&"object"==typeof e}function s(e,n){if(n){var r=String(n).split("."),a=r[0].trim();if("$store"===a){var o=r[1],i=t.get(o);if(!i)return;for(var c=i,u=2;u<r.length;u++){if(!d(c))return;c=c[r[u]]}return c}if(a in Object(e)){for(var l=e[a],f=1;f<r.length;f++){if(!d(l)&&"function"!=typeof l)return;l=l[r[f]]}return l}}}function v(e,n,r){var a=String(n).split(".");if("$store"!==a[0].trim()){for(var o=e,i=0;i<a.length-1;i++){var c=a[i];d(o[c])||(o[c]={}),o=o[c]}o[a[a.length-1]]=r}else{var u=t.get(a[1]);if(!u)return;for(var l=u,f=2;f<a.length-1;f++)d(l[a[f]])||(l[a[f]]={}),l=l[a[f]];l[a[a.length-1]]=r}}function h(e){return d(e)?new Proxy(e,{get:function(e,t,n){"string"==typeof t&&function(e,t){if(a){var n=r.get(e);n||(n=new Map,r.set(e,n));var o=n.get(t);o||(o=new Set,n.set(t,o)),o.add(a)}}(e,t);var o=Reflect.get(e,t,n);return d(o)?h(o):o},set:function(e,t,n){var r=e[t],a=Reflect.set(e,t,n);return r!==n&&"string"==typeof t&&f(e,t),a},deleteProperty:function(e,t){var n=Reflect.deleteProperty(e,t);return"string"==typeof t&&f(e,t),n}}):e}function p(e,t){for(var n=[],r=0,a=null,o="",i=0;i<e.length;i++){var c=e[i];a?(o+=c,c===a&&"\\"!==e[i-1]&&(a=null)):'"'!==c&&"'"!==c?("("===c&&r++,")"===c&&r--,0!==r||e.slice(i,i+t.length)!==t?o+=c:(n.push(o),o="",i+=t.length-1)):(a=c,o+=c)}return n.push(o),n}function m(t,n){var r=String(n).trim();if(r){var a=p(r,"||");if(a.length>1){for(var o,i=0;i<a.length;i++)if(o=m(t,a[i]))return o;return o}var c=p(r,"&&");if(c.length>1){for(var u,l=0;l<c.length;l++)if(!(u=m(t,c[l])))return u;return u}var f=r.match(/^(.*?)(===|!==|>=|<=|==|!=|>|<)(.*)$/);if(f){var d=m(t,f[1]),v=m(t,f[3]),h=f[2];return">"===h?d>v:"<"===h?d<v:">="===h?d>=v:"<="===h?d<=v:"=="===h?d==v:"!="===h?d!=v:"==="===h?d===v:d!==v}if("!"===r[0])return!m(t,r.slice(1));if('"'===r[0]&&'"'===r[r.length-1]||"'"===r[0]&&"'"===r[r.length-1])return r.slice(1,-1);if(/^-?\d+(\.\d+)?$/.test(r))return Number(r);if("true"===r)return!0;if("false"===r)return!1;if("null"===r)return null;if("undefined"!==r){var g=r.match(/^([\w.$]+)\(\)$/);if(g){var b=s(t,g[1]);return"function"==typeof b?b.call(t):void(e&&"undefined"!=typeof console&&console.warn('[laghu] unknown method "'+r+'"'))}return s(t,r)}}}function g(t,n,r){var a=String(r).trim();if(a){var o=a.match(/^([\w.$]+)\+\+$/);if(o)v(t,o[1],Number(m(t,o[1])||0)+1);else{var i=a.match(/^([\w.$]+)--$/);if(i)v(t,i[1],Number(m(t,i[1])||0)-1);else{var c=a.match(/^([\w.$]+)\s*=\s*(.+)$/);if(!c||/^(==|===|!=|!==|>=|<=)/.test(a.slice(c[1].length))){var u=a.replace(/\(\)$/,""),l=n[u]||s(t,u);if("function"!=typeof l){var f=m(t,a);"function"==typeof f?f.call(t):void 0===f&&(e&&"undefined"!=typeof console&&console.warn('[laghu] unknown action "'+a+'"'),"undefined"!=typeof CustomEvent&&"undefined"!=typeof document&&document.dispatchEvent&&document.dispatchEvent(new CustomEvent("laghu:unknown-action",{detail:{name:a}})))}else l.call(t)}else v(t,c[1],m(t,c[2]))}}}}function b(e,t){var n=e._bcu;n||(n=[],e._bcu=n),n.push(t)}function y(e){e._bcu&&(e._bcu.forEach(function(e){try{e()}catch(e){}}),e._bcu=[]);for(var t=e.querySelectorAll?e.querySelectorAll("*"):[],n=0;n<t.length;n++){var r=t[n];if(r._bcu&&(r._bcu.forEach(function(e){try{e()}catch(e){}}),r._bcu=[]),r._bReq){try{r._bReq.abort()}catch(e){}r._bReq=null}}var a=e;if(a._bReq){try{a._bReq.abort()}catch(e){}a._bReq=null}}function E(t){if(!t)return{};try{return JSON.parse(t)}catch(n){return e&&"undefined"!=typeof console&&console.warn("[laghu] data-scope must be JSON. Got: "+t),{}}}function A(e,t){if(null==e)return t;if("boolean"==typeof t)return"false"!==e&&"0"!==e;if("number"==typeof t){var n=Number(e);return isNaN(n)?t:n}return"true"===e||"false"!==e&&(""!==e&&!isNaN(Number(e))&&/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e)}function S(e){var t=String(e).trim().match(/^(\d+(?:\.\d+)?)(ms|s)?$/);if(!t)return 0;var n=parseFloat(t[1]);return"s"===t[2]?1e3*n:n}function w(e){var t=e.tagName;return"FORM"===t?"submit":"INPUT"===t||"SELECT"===t||"TEXTAREA"===t?"change":"click"}function C(e,t){var n,r={on:"",delay:0,throttle:0,once:!1,changed:!1,queue:"last",from:"",every:0,load:!1,revealed:!1},a=String(e||"").trim();if(!a)return r.on=w(t),r;(n=a.match(/every\s+(\d+(?:\.\d+)?(?:ms|s)?)/))&&(r.every=S(n[1]),a=a.replace(n[0]," ")),(n=a.match(/delay:(\S+)/))&&(r.delay=S(n[1]),a=a.replace(n[0]," ")),(n=a.match(/throttle:(\S+)/))&&(r.throttle=S(n[1]),a=a.replace(n[0]," ")),(n=a.match(/queue:(first|last|all|none)/))&&(r.queue=n[1],a=a.replace(n[0]," ")),(n=a.match(/from:(\S+)/))&&(r.from=n[1],a=a.replace(n[0]," ")),/\bonce\b/.test(a)&&(r.once=!0,a=a.replace(/\bonce\b/," ")),/\bchanged\b/.test(a)&&(r.changed=!0,a=a.replace(/\bchanged\b/," ")),/\bload\b/.test(a)&&(r.load=!0,a=a.replace(/\bload\b/," ")),/\brevealed\b/.test(a)&&(r.revealed=!0,a=a.replace(/\brevealed\b/," "));var o=a.trim().split(/\s+/).filter(Boolean)[0]||"";return r.on=o||w(t),r}function N(e,t,n){var r=C(t,e),a={timer:0,lastFire:0,lastVal:null,done:!1},o=function(t){if(!a.done){var o=e;if(r.changed&&("value"in o||"checked"in o)){var c=!("type"in o)||"checkbox"!==o.type&&"radio"!==o.type?String(o.value):String(o.checked)+":"+String(o.value);if(c===a.lastVal)return;a.lastVal=c}var u=Date.now();if(!(r.throttle&&u-a.lastFire<r.throttle)){var l=function(){a.lastFire=Date.now(),n(t),r.once&&(a.done=!0,i())};r.delay?(clearTimeout(a.timer),a.timer=setTimeout(l,r.delay)):l()}}},i=function(){},c=e;if("document"===r.from&&"undefined"!=typeof document)c=document;else if("body"===r.from&&"undefined"!=typeof document)c=document.body;else if(r.from&&"undefined"!=typeof document){var u=document,l=function(e){var t=e.target;t&&t.closest&&t.closest(r.from)&&o(e)};return u.addEventListener(r.on,l),b(e,i=function(){u.removeEventListener(r.on,l),clearTimeout(a.timer)}),i}if(r.every){var f=setInterval(function(){n(new CustomEvent("laghu:poll"))},r.every);return b(e,i=function(){clearInterval(f),clearTimeout(a.timer)}),i}if(r.load){if("undefined"!=typeof document&&"loading"===document.readyState){var d=function(){o(new CustomEvent("laghu:load"))};document.addEventListener("DOMContentLoaded",d,{once:!0}),i=function(){document.removeEventListener("DOMContentLoaded",d)}}else{var s=function(){o(new CustomEvent("laghu:load"))};"undefined"!=typeof queueMicrotask?queueMicrotask(s):setTimeout(s,0),i=function(){}}return b(e,i),i}if(r.revealed){if("undefined"!=typeof IntersectionObserver){var v=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(o(new CustomEvent("laghu:revealed")),r.once&&v.disconnect())})});v.observe(e),i=function(){v.disconnect(),clearTimeout(a.timer)}}else o(new CustomEvent("laghu:revealed")),i=function(){};return b(e,i),i}return c.addEventListener(r.on,o),b(e,i=function(){c.removeEventListener(r.on,o),clearTimeout(a.timer)}),i}function q(t,r,a){a=a||{};var o=r;t instanceof Element&&t.hasAttribute&&t.hasAttribute("data-scope")&&!t._bScope&&(t._bScope=o),t.querySelectorAll("[data-bind],[data-text]").forEach(function(e){var t=e,n=t.getAttribute("data-bind")||t.getAttribute("data-text")||"",r=l(function(){t.textContent=String(m(o,n)??"")});b(t,r)}),t.querySelectorAll("[data-html]").forEach(function(e){var t=e,n=t.getAttribute("data-html")||"",r=l(function(){t.innerHTML=c(String(m(o,n)??""))});b(t,r)}),Array.prototype.forEach.call(t.querySelectorAll("*"),function(e){var t=e;Array.prototype.forEach.call(t.attributes||[],function(e){var n=e.name;if(0===n.indexOf("data-bind:")){var r=n.slice(10),a=e.value,i=l(function(){var e=m(o,a);!1===e||null==e?t.removeAttribute(r):t.setAttribute(r,String(e))});b(t,i)}else if(0===n.indexOf("data-class:")){var c=n.slice(11),u=e.value,f=l(function(){t.classList.toggle(c,!!m(o,u))});b(t,f)}else if(0===n.indexOf("data-style:")){var d=n.slice(11),s=e.value,v=l(function(){var e=m(o,s);t.style[d]=!1===e||null==e?"":String(e)});b(t,v)}})}),t.querySelectorAll("[data-model]").forEach(function(e){var t=e,n=t.getAttribute("data-model")||"",r="checkbox"===t.type,a="radio"===t.type,i="SELECT"===t.tagName&&t.multiple,c=l(function(){var e=m(o,n);if(document.activeElement!==t||r||a||"checkbox"===t.type||t.value!==String(e??""))if(r)Array.isArray(e)?t.checked=e.indexOf(t.value)>=0:t.checked=!!e;else if(a)t.checked=String(e??"")===String(t.value);else if(i){var c=Array.isArray(e)?e.map(String):[];Array.prototype.forEach.call(t.options||[],function(e){e.selected=c.indexOf(String(e.value))>=0})}else"SELECT"!==t.tagName&&void 0===t.value||(t.value=null==e?"":String(e))});b(t,c);var u=r||a||"SELECT"===t.tagName?"change":"input",f=function(){if(r){var e=m(o,n);if(Array.isArray(e)){var a=e.slice(),c=a.indexOf(t.value);t.checked&&c<0&&a.push(t.value),!t.checked&&c>=0&&a.splice(c,1),v(o,n,a)}else v(o,n,t.checked)}else if(i){var u=[];Array.prototype.forEach.call(t.selectedOptions||[],function(e){u.push(e.value)}),v(o,n,u)}else"number"===t.type||"number"===t.getAttribute("data-type")?v(o,n,""===t.value?null:Number(t.value)):v(o,n,t.value)};t.addEventListener(u,f),b(t,function(){t.removeEventListener(u,f)})}),t.querySelectorAll("[data-show]").forEach(function(e){var t=e,n=t.getAttribute("data-show")||"",r=l(function(){t.hidden=!m(o,n)});b(t,r)}),Array.prototype.forEach.call(t.querySelectorAll("[data-if]"),function(e){var t=e;if("TEMPLATE"!==t.tagName){var n=t.getAttribute("data-if")||"",r=l(function(){t.hidden=!m(o,n)});b(t,r)}}),Array.prototype.forEach.call(t.querySelectorAll("template[data-if]"),function(e){var t=e,n=t.getAttribute("data-if")||"",r=document.createComment("b-if");t.parentNode&&t.parentNode.insertBefore(r,t);var i=null,c=l(function(){var e=!!m(o,n);if(e&&!i)i=t.content.cloneNode(!0),r.parentNode&&r.parentNode.insertBefore(i,r.nextSibling),r.parentNode&&r.parentNode instanceof Element&&q(r.parentNode,o,a);else if(!e&&i){for(var c=r.nextSibling;c&&c!==t;){var u=c.nextSibling;if(c instanceof Element&&y(c),c.parentNode&&c.parentNode.removeChild(c),i&&c===i.lastChild)break;c=u}i=null}});b(t,c)}),Array.prototype.forEach.call(t.querySelectorAll("template[data-for]"),function(t){var n=t,r=n.getAttribute("data-for")||"",i=r.match(/^\s*(\w+)(?:\s*,\s*(\w+))?\s+in\s+(.+)\s*$/);if(i){var c=i[1],u=i[2],f=i[3],d=n.getAttribute("data-key")||"",s=document.createComment("b-for");n.parentNode&&n.parentNode.insertBefore(s,n);var v=new Map,h=l(function(){var t=m(o,f)||[];Array.isArray(t)||(e&&"undefined"!=typeof console&&console.warn("[laghu] data-for not array: "+f),t=[]);var r=s.parentNode;if(r){for(var i=new Set,l=[],h=0;h<t.length;h++){var p=t[h],g=Object.assign(Object.create(o),{});g[c]=p;var b=d?m(g,d):h;null==b&&(b=h),i.add(b),l.push(b);var E=v.get(b);if(E)E.scope[c]=p,u&&(E.scope[u]=h);else{var A=Object.create(o);A[c]=p,u&&(A[u]=h);var S=n.content.cloneNode(!0),w=document.createElement("span");w.style.display="contents",d&&w.setAttribute("data-bkey",String(b)),w.appendChild(S),r.insertBefore(w,s),q(w,A,a),v.set(b,{nodes:[w],scope:A})}}for(var C=0;C<l.length;C++){var N=v.get(l[C]);if(N){var L=N.nodes[0],T=s;L.nextSibling,r.insertBefore(L,T)}}v.forEach(function(e,t){i.has(t)||(e.nodes.forEach(function(e){e instanceof Element&&y(e),e.parentNode&&e.parentNode.removeChild(e)}),v.delete(t))})}});b(n,h)}else e&&"undefined"!=typeof console&&console.warn('[laghu] bad data-for "'+r+'"')}),t.querySelectorAll("[data-click]").forEach(function(e){var t=e,n=t.getAttribute("data-click")||"",r=t.getAttribute("data-trigger")||"click";N(t,r,function(){g(o,a||{},n)})}),Array.prototype.forEach.call(t.querySelectorAll("*"),function(e){var t=e;Array.prototype.forEach.call(t.attributes||[],function(e){if(0===e.name.indexOf("data-on:")){var n=e.name.slice(8).split("."),r=n[0],i=n.slice(1),c=e.value,u=t.getAttribute("data-trigger")||r;N(t,u,function(n){i.indexOf("prevent")>=0&&n.preventDefault(),i.indexOf("stop")>=0&&n.stopPropagation();var r=Object.create(o);if(r.$event=n,g(r,a||{},c),i.indexOf("once")>=0)try{t.removeAttribute(e.name)}catch(e){}})}})}),t.querySelectorAll("[data-init]").forEach(function(e){var t=e.getAttribute("data-init")||"";try{g(o,a||{},t)}catch(e){}}),n.forEach(function(n,r){t.querySelectorAll("[data-"+r+"]").forEach(function(t){var a=t;try{var i=n({el:a,get:function(){return m(o,a.getAttribute("data-"+r)||"")},effect:l,scope:o});"function"==typeof i&&b(a,i)}catch(t){e&&"undefined"!=typeof console&&console.error("[laghu] directive "+r,t)}})}),Array.prototype.forEach.call(t.querySelectorAll("*"),function(t){var n=t,r="",a="";n.hasAttribute("data-get")?(r="GET",a=n.getAttribute("data-get")||""):n.hasAttribute("data-post")?(r="POST",a=n.getAttribute("data-post")||""):n.hasAttribute("data-put")?(r="PUT",a=n.getAttribute("data-put")||""):n.hasAttribute("data-patch")?(r="PATCH",a=n.getAttribute("data-patch")||""):n.hasAttribute("data-delete")&&(r="DELETE",a=n.getAttribute("data-delete")||""),(a||n.hasAttribute("data-get")||n.hasAttribute("data-post")||n.hasAttribute("data-put")||n.hasAttribute("data-patch")||n.hasAttribute("data-delete"))&&(n.hasAttribute("data-method")&&(r=String(n.getAttribute("data-method")||r).toUpperCase()),r||(r="GET"),function(t,n,r,a){var o=t.getAttribute("data-target")||"",i=t.getAttribute("data-swap")||"inner",u=t.getAttribute("data-trigger")||w(t),l=function(){!function(t,n,r,a,o,i){var u=C(t.getAttribute("data-trigger")||w(t),t),l=t;if(l._bInflight&&("first"===u.queue||"none"===u.queue))return;if("last"===u.queue&&l._bReq)try{l._bReq.abort()}catch(e){}var f="undefined"!=typeof AbortController?new AbortController:null;f&&(l._bReq=f);l._bInflight=!0;var d=function(e){var n=t.getAttribute("data-indicator"),r=n?document.querySelector(n):null;r&&r instanceof HTMLElement&&(r.hidden=!e),t instanceof HTMLElement&&(e?t.setAttribute("data-request",""):t.removeAttribute("data-request"),t.hasAttribute("data-disable")&&t.toggleAttribute("disabled",e))};d(!0);var s=new CustomEvent("laghu:before-request",{cancelable:!0,detail:{url:a,method:r}});if(!t.dispatchEvent(s))return l._bInflight=!1,void d(!1);var h={method:r,headers:{Accept:"application/json, text/html"},signal:f?f.signal:void 0},p=t.getAttribute("data-vals"),m=null;if(p){var g="form"===p.trim()&&t instanceof HTMLFormElement?T(t):L(p)||{},b=t instanceof HTMLFormElement?t:t.closest?t.closest("form"):null;if(b&&"form"!==p.trim()){var y=T(b);for(var E in y)g[E]=y[E]}if("GET"===r){var A=new URLSearchParams(g).toString();A&&(a+=(a.indexOf("?")>=0?"&":"?")+A)}else h.headers["Content-Type"]="application/json",m=JSON.stringify(g),h.body=m}else if("GET"!==r){var S=t instanceof HTMLFormElement?t:t.closest?t.closest("form"):null;S&&(h.headers["Content-Type"]="application/json",h.body=JSON.stringify(T(S)))}var N=t.getAttribute("data-headers");if(N){var q=L(N)||{};for(var O in q)h.headers[O]=q[O]}var M=function(){l._bInflight=!1,l._bReq===f&&(l._bReq=null),d(!1)};fetch(a,h).then(function(e){if(M(),t.dispatchEvent(new CustomEvent("laghu:after-request",{detail:{status:e.status,url:a}})),e.ok)return(e.headers.get("content-type")||"").indexOf("application/json")>=0?e.json().then(function(e){e&&"object"==typeof e&&!Array.isArray(e)?Object.assign(n,e):v(n,"result",e)}):e.text().then(function(e){var r=t.getAttribute("data-select");if(r){var a=document.createElement("template");a.innerHTML=c(e);var u=a.content.querySelector(r);e=u?u.outerHTML:""}var l=o?document.querySelector(o):t;l&&_(l,e,i,n)});t.dispatchEvent(new CustomEvent("laghu:request-error",{detail:{status:e.status,url:a}}))}).catch(function(n){M(),n&&"AbortError"===n.name||(t.dispatchEvent(new CustomEvent("laghu:request-error",{detail:{error:n}})),e&&"undefined"!=typeof console&&console.error("[laghu] fetch",n))})}(t,n,r,a,o,i)};N(t,u,l)}(n,o,r,a))})}function L(e){try{return JSON.parse(e)}catch(e){return null}}function T(e){var t={};return new FormData(e).forEach(function(e,n){n in t?(Array.isArray(t[n])||(t[n]=[t[n]]),t[n].push("string"==typeof e?e:String(e))):t[n]="string"==typeof e?e:String(e)}),t}function _(t,n,r,a){var o=String(r||"inner").toLowerCase();if("none"!==o){var i=document.activeElement&&document.activeElement.id?document.activeElement.id:null,u=document.activeElement,l=u&&"number"==typeof u.selectionStart?u.selectionStart:-1,f=u&&"number"==typeof u.selectionEnd?u.selectionEnd:-1;t.dispatchEvent(new CustomEvent("laghu:before-swap",{cancelable:!1,detail:{swap:o}}));var d=0===o.indexOf("morph"),s="undefined"!=typeof window?window.LaghuMorph:null;if(d){if(s&&s.morph)return s.morph(t,c(n),{}),q(t,a,{}),t.dispatchEvent(new CustomEvent("laghu:after-swap",{detail:{swap:o}})),void O(i,l,f);e&&"undefined"!=typeof console&&console.warn('[laghu] data-swap="morph" needs morph.js (window.LaghuMorph). Falling back to inner.'),o="inner"}if("delete"===o)return y(t),void(t.parentNode&&t.parentNode.removeChild(t));var v=document.createElement("template");v.innerHTML=c(n);var h=v.content,p=new Map;t.querySelectorAll("[data-preserve][id]").forEach(function(e){p.set(e.id,e)}),y(t),"inner"===o?(t.innerHTML="",t.appendChild(h)):"text"===o?t.textContent=v.content.textContent:"outer"===o?t.replaceWith(h):"append"===o||"beforeend"===o?t.appendChild(h):"prepend"===o||"afterbegin"===o?t.insertBefore(h,t.firstChild):"beforebegin"===o?t.parentNode&&t.parentNode.insertBefore(h,t):"afterend"===o?t.parentNode&&t.parentNode.insertBefore(h,t.nextSibling):(t.innerHTML="",t.appendChild(h)),p.forEach(function(e,n){var r="#";try{r="#"+("undefined"!=typeof CSS&&CSS&&CSS.escape?CSS.escape(n):String(n).replace(/([^a-zA-Z0-9_-])/g,"\\$1"))}catch(e){r='[id="'+String(n).replace(/"/g,"")+'"]'}var a=t.querySelector?t.querySelector(r):null;a||(a=document.getElementById(n)),a&&a!==e&&e.parentNode!==a&&a.replaceWith(e)}),q("outer"===o?t.parentNode instanceof Element?t.parentNode:document.body:t,a,{}),t.dispatchEvent(new CustomEvent("laghu:after-swap",{detail:{swap:o}})),O(i,l,f)}else t.dispatchEvent(new CustomEvent("laghu:after-swap",{detail:{swap:o}}))}function O(e,t,n){if(e){var r=document.getElementById(e);if(r&&r instanceof HTMLElement){try{r.focus({preventScroll:!0})}catch(e){try{r.focus()}catch(e){}}try{t>=0&&r.setSelectionRange&&r.setSelectionRange(t,n)}catch(e){}}}}var M=null;var x={version:"1.0.0",define:function(e,t){if(!e||e.indexOf("-")<0)throw new Error("[laghu] tag must contain hyphen: "+e);var n=t()||{},r=n.observed||Object.keys(n.state||{}).filter(function(e){var t=n.state[e];return"string"==typeof t||"number"==typeof t||"boolean"==typeof t}),a=class extends HTMLElement{connectedCallback(){if(!this._bInit){this._bInit=!0;var e=t()||{},n=e.state||{};for(var a in n)if(Object.prototype.hasOwnProperty.call(this,a))try{n[a]=this[a],delete this[a]}catch(e){}var o=this._bPend;if(o){for(var i in o)n[i]=i in n?A(o[i],n[i]):o[i];this._bPend=null}else for(var c=0;c<r.length;c++){var u=r[c];this.hasAttribute&&this.hasAttribute(u)&&u in n&&(n[u]=A(this.getAttribute(u),n[u]))}var l=h(n);this.state=l,this.actions=e.actions||{},q(this,l,e.actions||{});try{"function"==typeof e.onMount&&e.onMount.call(l)}catch(e){}this.classList.add("is-defined"),this.dispatchEvent(new CustomEvent("laghu:mount"))}}disconnectedCallback(){try{this.state}catch(e){}y(this),this.dispatchEvent(new CustomEvent("laghu:unmount")),this._bInit=!1}connectedMoveCallback(){}attributeChangedCallback(e,t,n){if(t!==n){var r=this.state;if(r&&e in r)r[e]=A(n,r[e]);else{var a=this._bPend||{};a[e]=n,this._bPend=a}}}};return a.observedAttributes=r,customElements.define(e,a),a},reactive:h,effect:l,eval:m,get:s,set:v,parseTrigger:C,swap:_,css:"laghu:not(:defined),[data-laghu]:not(:defined){visibility:hidden}laghu.is-defined,[data-laghu].is-defined{visibility:visible}",sanitize:function(e){"function"==typeof e&&(c=e)},hydrate:function(e,t,n){var r=h(t||E(e.getAttribute("data-scope")));return e._bScope=r,q(e,r,n||{}),r},destroyTree:y,store:function(e,n){if(void 0!==n){var r=h(n);return t.set(e,r),r}return t.get(e)},directive:function(e,t){n.set(e,t)}};window.Laghu=x,"undefined"!=typeof document&&document.addEventListener("DOMContentLoaded",function(){M||"undefined"==typeof MutationObserver||(M=new MutationObserver(function(e){e.forEach(function(e){e.addedNodes.forEach(function(e){if(e instanceof Element&&e.hasAttribute&&e.hasAttribute("data-scope")&&!e._bScope)try{var t=window.Laghu;t&&t.hydrate(e,E(e.getAttribute("data-scope")),{})}catch(e){}}),e.removedNodes.forEach(function(e){e instanceof Element&&y(e)})})})).observe(document.documentElement,{childList:!0,subtree:!0}),document.querySelectorAll("[data-scope]").forEach(function(e){var t=e;t._bScope||x.hydrate(t,E(t.getAttribute("data-scope")),{})})})}();
|
|
1
|
+
!function(){"use strict";var e="1.1.0",t="undefined"!=typeof window&&!0===window.__LAGHU_DEV__,n=new Map,r=new Map,a=new WeakMap,i=new WeakMap,o=new WeakSet,c=null,u=new Set,l=!1,f={href:1,src:1,action:1,formaction:1,cite:1,poster:1,data:1,srcset:1,srcdoc:1};function s(e){return"__proto__"===e||"constructor"===e||"prototype"===e}function d(e){var t=String(e).replace(/[\s\x00-\x1f]+/g,"").toLowerCase();return 0===t.indexOf("javascript:")||0===t.indexOf("data:text/html")||0===t.indexOf("vbscript:")}var v=function(e){var t=String(e);return t=(t=(t=(t=t.replace(/<script[\s\S]*?(<\/script\s*>|$)/gi,"")).replace(/<\/?(iframe|object|embed|link|meta|base)[^>]*>/gi,"")).replace(/\son[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi,"")).replace(/(href|src|xlink:href|action|formaction|poster|data)\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))/gi,function(e,t,n,r,a,i){return d(void 0!==r?r:void 0!==a?a:i)?t+'="#"':e})};function h(n){try{"undefined"!=typeof document&&document.dispatchEvent&&document.dispatchEvent(new CustomEvent("laghu:error",{detail:{error:String(n&&n.message||n),version:e}}))}catch(e){}t&&"undefined"!=typeof console&&console.error("[laghu]",n)}function p(){l=!1;var e=Array.from(u);u.clear();for(var t=0;t<e.length;t++)try{e[t]()}catch(e){h(e)}}var b=[],g=new WeakMap;function m(e){var t=!1,n=[],r=function(){if(!t){for(var a=0;a<n.length;a++)n[a].delete(r);n.length=0,b.push(r),c=r,g.set(r,n);try{e()}catch(e){h(e)}finally{b.pop(),c=b.length?b[b.length-1]:null}}};return r(),function(){t=!0;for(var e=0;e<n.length;e++)n[e].delete(r);n.length=0}}function y(e,t){if(c){var n=a.get(e);n||(n=new Map,a.set(e,n));var r=n.get(t);r||(r=new Set,n.set(t,r)),r.add(c);var i=g.get(c);i&&i.push(r)}}function E(e,t){var n=a.get(e);if(n){var r=n.get(t);r&&r.forEach(function(e){!function(e){u.add(e),l||(l=!0,Promise.resolve().then(p))}(e)})}}function A(e){return null!==e&&"object"==typeof e}function S(e,t){var n=Object.create(e);for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&Object.defineProperty(n,r,{value:t[r],writable:!0,enumerable:!0,configurable:!0});return n}function w(e,t){if(t){var r=String(t).split("."),a=r[0].trim();if(!s(a)){if("$store"===a){var i=r[1],o=n.get(i);if(!o)return;for(var c=o,u=2;u<r.length;u++){if(s(r[u]))return;if(!A(c))return;c=c[r[u]]}return c}if(a in Object(e)){for(var l=e[a],f=1;f<r.length;f++){if(s(r[f]))return;if(!A(l)&&"function"!=typeof l)return;l=l[r[f]]}return l}"string"==typeof a&&A(e)&&y(e,a)}}}function C(e,r,a){for(var i=String(r).split("."),o=0;o<i.length;o++)if(s(i[o].trim()))return void(t&&"undefined"!=typeof console&&console.warn('[laghu] blocked unsafe path "'+r+'"'));if("$store"!==i[0].trim()){for(var c=e,u=e;u&&u!==Object.prototype;){if(Object.prototype.hasOwnProperty.call(u,i[0])){c=u;break}u=Object.getPrototypeOf(u)}for(var l=c,f=0;f<i.length-1;f++){var d=i[f];A(l[d])||(l[d]={}),l=l[d]}l[i[i.length-1]]=a}else{var v=n.get(i[1]);if(!v)return;for(var h=v,p=2;p<i.length-1;p++)A(h[i[p]])||(h[i[p]]={}),h=h[i[p]];h[i[i.length-1]]=a}}function N(e){if(!A(e))return e;if(o.has(e))return e;var t=i.get(e);if(t)return t;var n=new Proxy(e,{get:function(e,t,n){"string"==typeof t&&y(e,t);var r=Reflect.get(e,t,n);return A(r)?N(r):r},set:function(e,t,n){var r=e[t],a=Reflect.set(e,t,n);if(r!==n&&"string"==typeof t){E(e,t);var o=i.get(e);o&&E(o,t)}return a},deleteProperty:function(e,t){var n=Reflect.deleteProperty(e,t);if("string"==typeof t){E(e,t);var r=i.get(e);r&&E(r,t)}return n}});return i.set(e,n),o.add(n),n}function O(e,t){for(var n=[],r=0,a=null,i="",o=0;o<e.length;o++){var c=e[o];a?(i+=c,c===a&&"\\"!==e[o-1]&&(a=null)):'"'!==c&&"'"!==c?("("!==c&&"["!==c||r++,")"!==c&&"]"!==c||r--,0!==r||e.slice(o,o+t.length)!==t?i+=c:(n.push(i),i="",o+=t.length-1)):(a=c,i+=c)}return n.push(i),n}function _(e,t){for(var n=0,r=null,a=e.length-1;a>0;a--){var i=e[a];if(r)i===r&&"\\"!==e[a-1]&&(r=null);else if('"'!==i&&"'"!==i){if(")"===i&&n++,"("===i&&n--,0===n&&t.indexOf(i)>=0)return a}else r=i}return-1}function q(e,n){var r=String(n).trim();if(r){var a=function(e){var t,n=0,r=null,a=-1;for(t=0;t<e.length;t++){var i=e[t];if(r)i===r&&"\\"!==e[t-1]&&(r=null);else if('"'!==i&&"'"!==i){if("("!==i&&"["!==i||n++,")"!==i&&"]"!==i||n--,"?"===i&&0===n){a=t;break}}else r=i}if(a<0)return null;for(n=0,r=null,t=a+1;t<e.length;t++){var o=e[t];if(r)o===r&&"\\"!==e[t-1]&&(r=null);else if('"'!==o&&"'"!==o){if("("!==o&&"["!==o||n++,")"!==o&&"]"!==o||n--,":"===o&&0===n)return[e.slice(0,a),e.slice(a+1,t),e.slice(t+1)]}else r=o}return null}(r);if(a)return q(e,a[0])?q(e,a[1]):q(e,a[2]);var i=O(r,"||");if(i.length>1){for(var o,c=0;c<i.length;c++)if(o=q(e,i[c]))return o;return o}var u=O(r,"&&");if(u.length>1){for(var l,f=0;f<u.length;f++)if(!(l=q(e,u[f])))return l;return l}var d=r.match(/^(.*?)(===|!==|>=|<=|==|!=|>|<)(.*)$/);if(d){var v=q(e,d[1]),h=q(e,d[3]),p=d[2];return">"===p?v>h:"<"===p?v<h:">="===p?v>=h:"<="===p?v<=h:"=="===p?v==h:"!="===p?v!=h:"==="===p?v===h:v!==h}var b=_(r,"+-");if(b>0&&r.slice(0,b).trim()&&r.slice(b+1).trim()){var g=r[b],m=q(e,r.slice(0,b)),y=q(e,r.slice(b+1));return"+"===g?m+y:m-y}var E=_(r,"*/%");if(E>0&&r.slice(0,E).trim()&&r.slice(E+1).trim()){var A=r[E],S=q(e,r.slice(0,E)),C=q(e,r.slice(E+1));return"*"===A?S*C:"/"===A?S/C:S%C}if("!"===r[0])return!q(e,r.slice(1));if("-"===r[0])return-q(e,r.slice(1));if("("===r[0]){var N,k=0,T=null,L=-1;for(N=0;N<r.length;N++){var x=r[N];if(T)x===T&&"\\"!==r[N-1]&&(T=null);else if('"'!==x&&"'"!==x){if("("===x&&k++,")"===x&&0===--k){L=N;break}}else T=x}if(L===r.length-1)return q(e,r.slice(1,-1))}if('"'===r[0]&&'"'===r[r.length-1]||"'"===r[0]&&"'"===r[r.length-1])return r.slice(1,-1);if(/^-?\d+(\.\d+)?$/.test(r))return Number(r);if("true"===r)return!0;if("false"===r)return!1;if("null"===r)return null;if("undefined"!==r){if(")"===r.charAt(r.length-1)){var M=r.indexOf("(");if(M>0){var $,j=0,R=null,H=-1;for($=M;$<r.length;$++){var P=r[$];if(R)P===R&&"\\"!==r[$-1]&&(R=null);else if('"'!==P&&"'"!==P){if("("===P&&j++,")"===P&&0===--j){H=$;break}}else R=P}if(H===r.length-1){var I=r.slice(0,M);if(/^[\w.$]+$/.test(I)){var U=w(e,I);if("function"==typeof U){var B=r.slice(M+1,H).trim(),F=B?O(B,",").map(function(t){return q(e,t)}):[];return U.apply(e,F)}return void(t&&"undefined"!=typeof console&&console.warn('[laghu] unknown method "'+r+'"'))}}}}var D=r.match(/^(.*)\[(\d+|"(?:[^"]*)"|'(?:[^']*)')\]$/);if(D&&D[1]){var J=q(e,D[1]);if(null==J)return;var G=D[2];if("string"==typeof(G='"'===G[0]||"'"===G[0]?G.slice(1,-1):Number(G))&&s(G))return;return J[G]}return w(e,r)}}}function k(e,t,n){var r=String(n).trim();if(r){var a=r.match(/^([\w.$]+)\+\+$/);if(a)C(e,a[1],Number(q(e,a[1])||0)+1);else{var i=r.match(/^([\w.$]+)--$/);if(i)C(e,i[1],Number(q(e,i[1])||0)-1);else{var o=r.match(/^([\w.$]+)\s*=\s*(.+)$/);if(!o||/^(==|===|!=|!==|>=|<=)/.test(r.slice(o[1].length))){var c=r.replace(/\(\)$/,""),u=t[c]||w(e,c);if("function"!=typeof u)if(/^[A-Za-z_$][\w.$-]*$/.test(r))T(r);else{var l=q(e,r);"function"==typeof l?l.call(e):void 0===l&&T(r)}else u.call(e)}else C(e,o[1],q(e,o[2]))}}}}function T(e){t&&"undefined"!=typeof console&&console.warn('[laghu] unknown action "'+e+'"'),"undefined"!=typeof CustomEvent&&"undefined"!=typeof document&&document.dispatchEvent&&document.dispatchEvent(new CustomEvent("laghu:unknown-action",{detail:{name:e}}))}function L(e,t){var n=e._bcu;n||(n=[],e._bcu=n),n.push(t)}function x(e,t){var n=e,r=n._bH||(n._bH={});return!!r[t]||(r[t]=1,!1)}function M(e){e._bcu&&(e._bcu.forEach(function(e){try{e()}catch(e){}}),e._bcu=[]),e._bH=void 0;for(var t=e.querySelectorAll?e.querySelectorAll("*"):[],n=0;n<t.length;n++){var r=t[n];if(r._bcu&&(r._bcu.forEach(function(e){try{e()}catch(e){}}),r._bcu=[]),r._bH=void 0,r._bReq){try{r._bReq.abort()}catch(e){}r._bReq=null}}var a=e;if(a._bReq){try{a._bReq.abort()}catch(e){}a._bReq=null}}function $(e){if(!e)return{};try{return JSON.parse(e)}catch(n){return t&&"undefined"!=typeof console&&console.warn("[laghu] data-scope must be JSON. Got: "+e),{}}}function j(e,t){if(null==e)return t;if("boolean"==typeof t)return"false"!==e&&"0"!==e;if("number"==typeof t){var n=Number(e);return isNaN(n)?t:n}return"true"===e||"false"!==e&&(""!==e&&!isNaN(Number(e))&&/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):e)}function R(e){var t=String(e).trim().match(/^(\d+(?:\.\d+)?)(ms|s)?$/);if(!t)return 0;var n=parseFloat(t[1]);return"s"===t[2]?1e3*n:n}function H(e){var t=e.tagName;return"FORM"===t?"submit":"INPUT"===t||"SELECT"===t||"TEXTAREA"===t?"change":"click"}function P(e,t){var n,r={on:"",delay:0,throttle:0,once:!1,changed:!1,queue:"last",from:"",every:0,load:!1,revealed:!1},a=String(e||"").trim();if(!a)return r.on=H(t),r;(n=a.match(/every\s+(\d+(?:\.\d+)?(?:ms|s)?)/))&&(r.every=R(n[1]),a=a.replace(n[0]," ")),(n=a.match(/delay:(\S+)/))&&(r.delay=R(n[1]),a=a.replace(n[0]," ")),(n=a.match(/throttle:(\S+)/))&&(r.throttle=R(n[1]),a=a.replace(n[0]," ")),(n=a.match(/queue:(first|last|all|none)/))&&(r.queue=n[1],a=a.replace(n[0]," ")),(n=a.match(/from:(\S+)/))&&(r.from=n[1],a=a.replace(n[0]," ")),/\bonce\b/.test(a)&&(r.once=!0,a=a.replace(/\bonce\b/," ")),/\bchanged\b/.test(a)&&(r.changed=!0,a=a.replace(/\bchanged\b/," ")),/\bload\b/.test(a)&&(r.load=!0,a=a.replace(/\bload\b/," ")),/\brevealed\b/.test(a)&&(r.revealed=!0,a=a.replace(/\brevealed\b/," "));var i=a.trim().split(/\s+/).filter(Boolean)[0]||"";return r.on=i||H(t),r}function I(e,t,n){var r=P(t,e),a={timer:0,lastFire:0,lastVal:null,done:!1},i=function(t){if(!a.done){var i=e;if(r.changed&&("value"in i||"checked"in i)){var c=!("type"in i)||"checkbox"!==i.type&&"radio"!==i.type?String(i.value):String(i.checked)+":"+String(i.value);if(c===a.lastVal)return;a.lastVal=c}var u=Date.now();if(!(r.throttle&&u-a.lastFire<r.throttle)){var l=function(){a.lastFire=Date.now(),n(t),r.once&&(a.done=!0,o())};r.delay?(clearTimeout(a.timer),a.timer=setTimeout(l,r.delay)):l()}}},o=function(){},c=e;if("document"===r.from&&"undefined"!=typeof document)c=document;else if("body"===r.from&&"undefined"!=typeof document)c=document.body;else if(r.from&&"undefined"!=typeof document){var u=document,l=function(e){var t=e.target;t&&t.closest&&t.closest(r.from)&&i(e)};return u.addEventListener(r.on,l),L(e,o=function(){u.removeEventListener(r.on,l),clearTimeout(a.timer)}),o}if(r.every){var f=setInterval(function(){n(new CustomEvent("laghu:poll"))},r.every);return L(e,o=function(){clearInterval(f),clearTimeout(a.timer)}),o}if(r.load){if("undefined"!=typeof document&&"loading"===document.readyState){var s=function(){i(new CustomEvent("laghu:load"))};document.addEventListener("DOMContentLoaded",s,{once:!0}),o=function(){document.removeEventListener("DOMContentLoaded",s)}}else{var d=function(){i(new CustomEvent("laghu:load"))};"undefined"!=typeof queueMicrotask?queueMicrotask(d):setTimeout(d,0),o=function(){}}return L(e,o),o}if(r.revealed){if("undefined"!=typeof IntersectionObserver){var v=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(i(new CustomEvent("laghu:revealed")),r.once&&v.disconnect())})});v.observe(e),o=function(){v.disconnect(),clearTimeout(a.timer)}}else i(new CustomEvent("laghu:revealed")),o=function(){};return L(e,o),o}return c.addEventListener(r.on,i),L(e,o=function(){c.removeEventListener(r.on,i),clearTimeout(a.timer)}),o}function U(e,n,a){a=a||{};var i=n;try{A(i)&&!("$error"in Object(i))&&C(i,"$error",null)}catch(e){}e instanceof Element&&e.hasAttribute&&e.hasAttribute("data-scope")&&!e._bScope&&(e._bScope=i),e.querySelectorAll("[data-bind],[data-text]").forEach(function(e){var t=e;if(!x(t,"bind")){var n=t.getAttribute("data-bind")||t.getAttribute("data-text")||"",r=m(function(){t.textContent=String(q(i,n)??"")});L(t,r)}}),e.querySelectorAll("[data-html]").forEach(function(e){var t=e;if(!x(t,"html")){var n=t.getAttribute("data-html")||"",r=m(function(){t.innerHTML=v(String(q(i,n)??""))});L(t,r)}}),Array.prototype.forEach.call(e.querySelectorAll("*"),function(e){var n=e;Array.prototype.forEach.call(n.attributes||[],function(e){var r=e.name;if(0===r.indexOf("data-bind:")){if(x(n,"a:"+r))return;var a=r.slice(10),o=e.value,c=m(function(){var e=q(i,o);!1===e||null==e?n.removeAttribute(a):f[a.toLowerCase()]&&d(e)?(n.removeAttribute(a),t&&"undefined"!=typeof console&&console.warn('[laghu] blocked scriptable URL for "'+a+'"')):n.setAttribute(a,String(e))});L(n,c)}else if(0===r.indexOf("data-class:")){if(x(n,"a:"+r))return;var u=r.slice(11),l=e.value,s=m(function(){n.classList.toggle(u,!!q(i,l))});L(n,s)}else if(0===r.indexOf("data-style:")){if(x(n,"a:"+r))return;var v=r.slice(11),h=e.value,p=m(function(){var e=q(i,h);n.style[v]=!1===e||null==e?"":String(e)});L(n,p)}})}),e.querySelectorAll("[data-model]").forEach(function(e){var t=e;if(!x(t,"model")){var n=t.getAttribute("data-model")||"",r="checkbox"===t.type,a="radio"===t.type,o="SELECT"===t.tagName&&t.multiple,c=m(function(){var e=q(i,n);if(document.activeElement!==t||r||a||"checkbox"===t.type||t.value!==String(e??""))if(r)Array.isArray(e)?t.checked=e.indexOf(t.value)>=0:t.checked=!!e;else if(a)t.checked=String(e??"")===String(t.value);else if(o){var c=Array.isArray(e)?e.map(String):[];Array.prototype.forEach.call(t.options||[],function(e){e.selected=c.indexOf(String(e.value))>=0})}else"SELECT"!==t.tagName&&void 0===t.value||(t.value=null==e?"":String(e))});L(t,c);var u=r||a||"SELECT"===t.tagName?"change":"input",l=function(){if(r){var e=q(i,n);if(Array.isArray(e)){var a=e.slice(),c=a.indexOf(t.value);t.checked&&c<0&&a.push(t.value),!t.checked&&c>=0&&a.splice(c,1),C(i,n,a)}else C(i,n,t.checked)}else if(o){var u=[];Array.prototype.forEach.call(t.selectedOptions||[],function(e){u.push(e.value)}),C(i,n,u)}else"number"===t.type||"number"===t.getAttribute("data-type")?C(i,n,""===t.value?null:Number(t.value)):C(i,n,t.value)};t.addEventListener(u,l),L(t,function(){t.removeEventListener(u,l)})}}),e.querySelectorAll("[data-show]").forEach(function(e){var t=e;if(!x(t,"show")){var n=t.getAttribute("data-show")||"",r=m(function(){t.hidden=!q(i,n)});L(t,r)}}),Array.prototype.forEach.call(e.querySelectorAll("[data-if]"),function(e){var t=e;if("TEMPLATE"!==t.tagName&&!x(t,"if")){var n=t.getAttribute("data-if")||"",r=m(function(){t.hidden=!q(i,n)});L(t,r)}}),Array.prototype.forEach.call(e.querySelectorAll("template[data-if]"),function(e){var t=e,n=t.getAttribute("data-if")||"";if(!x(t,"tif")){var r=document.createComment("b-if");t.parentNode&&t.parentNode.insertBefore(r,t);var o=null,c=m(function(){var e=!!q(i,n);if(e&&!o)o=t.content.cloneNode(!0),r.parentNode&&r.parentNode.insertBefore(o,r.nextSibling),r.parentNode&&r.parentNode instanceof Element&&U(r.parentNode,i,a);else if(!e&&o){for(var c=r.nextSibling;c&&c!==t;){var u=c.nextSibling;if(c instanceof Element&&M(c),c.parentNode&&c.parentNode.removeChild(c),o&&c===o.lastChild)break;c=u}o=null}});L(t,c)}}),Array.prototype.forEach.call(e.querySelectorAll("template[data-for]"),function(e){var n=e,r=n.getAttribute("data-for")||"";if(!x(n,"tfor")){var o=r.match(/^\s*(\w+)(?:\s*,\s*(\w+))?\s+in\s+(.+)\s*$/);if(o){var c=o[1],u=o[2],l=o[3],f=n.getAttribute("data-key")||"",s=document.createComment("b-for");n.parentNode&&n.parentNode.insertBefore(s,n);var d=new Map,v=m(function(){var e=q(i,l)||[];Array.isArray(e)||(t&&"undefined"!=typeof console&&console.warn("[laghu] data-for not array: "+l),e=[]);var r=s.parentNode;if(r){for(var o=new Set,v=[],h=0;h<e.length;h++){var p=e[h],b=h;if(f){var g=q(p,f);if(null==g){var m={};m[c]=p,g=q(S(i,m),f)}null!=g&&(b=g)}o.add(b),v.push(b);var y=d.get(b);if(y){var E=y.scope[c],A=u?y.scope[u]:-1;if(y.scope[c]=p,u&&(y.scope[u]=h),E!==p||u&&A!==h){var w=y.nodes[0];w instanceof Element&&(M(w),U(w,y.scope,a))}}else{var C={};C[c]=p,u&&(C[u]=h);var N=S(i,C),O=n.content.cloneNode(!0),_=document.createElement("span");_.style.display="contents",_.setAttribute("data-bkey",String(b)),_.appendChild(O),r.insertBefore(_,s),U(_,N,a),d.set(b,{nodes:[_],scope:N})}}for(var k=[],T=r.childNodes,L=0;L<T.length;L++){var x=T[L];1===x.nodeType&&x.hasAttribute("data-bkey")&&k.push(x.getAttribute("data-bkey"))}var $=k.length===v.length;if($)for(var j=0;j<v.length;j++)if(k[j]!==String(v[j])){$=!1;break}if(!$)for(var R=0;R<v.length;R++){var H=d.get(v[R]);H&&r.insertBefore(H.nodes[0],s)}d.forEach(function(e,t){o.has(t)||(e.nodes.forEach(function(e){e instanceof Element&&M(e),e.parentNode&&e.parentNode.removeChild(e)}),d.delete(t))})}});L(n,v)}else t&&"undefined"!=typeof console&&console.warn('[laghu] bad data-for "'+r+'"')}}),e.querySelectorAll("[data-click]").forEach(function(e){var t=e;if(!x(t,"click")){var n=t.getAttribute("data-click")||"",r=t.getAttribute("data-trigger")||"click";I(t,r,function(){k(i,a||{},n)})}}),Array.prototype.forEach.call(e.querySelectorAll("*"),function(e){var t=e;Array.prototype.forEach.call(t.attributes||[],function(e){if(0===e.name.indexOf("data-on:")){if(x(t,"on:"+e.name))return;var n=e.name.slice(8).split("."),r=n[0],o=n.slice(1),c=e.value,u=t.getAttribute("data-trigger")||r;I(t,u,function(n){if(o.indexOf("prevent")>=0&&n.preventDefault(),o.indexOf("stop")>=0&&n.stopPropagation(),k(S(i,{$event:n}),a||{},c),o.indexOf("once")>=0)try{t.removeAttribute(e.name)}catch(e){}})}})}),e.querySelectorAll("[data-init]").forEach(function(e){var t=e;if(!x(t,"init")){var n=t.getAttribute("data-init")||"";try{k(i,a||{},n)}catch(e){h(e)}}}),r.forEach(function(t,n){e.querySelectorAll("[data-"+n+"]").forEach(function(e){var r=e;if(!x(r,"dir:"+n))try{var a=t({el:r,get:function(){return q(i,r.getAttribute("data-"+n)||"")},effect:m,scope:i});"function"==typeof a&&L(r,a)}catch(e){h(e)}})}),Array.prototype.forEach.call(e.querySelectorAll("*"),function(e){var t=e,n="",r="";t.hasAttribute("data-get")?(n="GET",r=t.getAttribute("data-get")||""):t.hasAttribute("data-post")?(n="POST",r=t.getAttribute("data-post")||""):t.hasAttribute("data-put")?(n="PUT",r=t.getAttribute("data-put")||""):t.hasAttribute("data-patch")?(n="PATCH",r=t.getAttribute("data-patch")||""):t.hasAttribute("data-delete")&&(n="DELETE",r=t.getAttribute("data-delete")||""),(r||t.hasAttribute("data-get")||t.hasAttribute("data-post")||t.hasAttribute("data-put")||t.hasAttribute("data-patch")||t.hasAttribute("data-delete"))&&(t.hasAttribute("data-method")&&(n=String(t.getAttribute("data-method")||n).toUpperCase()),n||(n="GET"),x(t,"fetch")||function(e,t,n,r,a){var i=e.getAttribute("data-target")||"",o=e.getAttribute("data-swap")||"inner",c=e.getAttribute("data-trigger")||H(e);e._bActions=a||{};var u=!1;try{u="$error"in Object(t)}catch(e){}u||C(t,"$error",null);var l=function(){!function(e,t,n,r,a,i){var o=P(e.getAttribute("data-trigger")||H(e),e),c=e;c._bReqs||(c._bReqs=new Set);if(c._bN>0&&("first"===o.queue||"none"===o.queue))return;"last"===o.queue&&(c._bReqs.forEach(function(e){try{e.abort()}catch(e){}}),c._bReqs.clear());var u="undefined"!=typeof AbortController?new AbortController:null;u&&c._bReqs.add(u);c._bN=(c._bN||0)+1;var l=!1,f=R(e.getAttribute("data-timeout")||""),s=0;f>0&&u&&(s=setTimeout(function(){l=!0;try{u&&u.abort()}catch(e){}},f));var d=c._bActions||{},p=function(t){var n=e.getAttribute("data-indicator"),r=n?document.querySelector(n):null;r&&r instanceof HTMLElement&&(r.hidden=!t),e instanceof HTMLElement&&(t?(e.setAttribute("data-request",""),e.setAttribute("data-pending","")):(e.removeAttribute("data-request"),e.removeAttribute("data-pending")),e.hasAttribute("data-disable")&&e.toggleAttribute("disabled",t))};p(!0);var b=new CustomEvent("laghu:before-request",{bubbles:!0,cancelable:!0,detail:{url:r,method:n}});if(!e.dispatchEvent(b))return void M();var g={method:n,headers:{Accept:"application/json, text/html"},signal:u?u.signal:void 0},m=e.getAttribute("data-vals"),y=null;if(m){var E="form"===m.trim()&&e instanceof HTMLFormElement?F(e):B(m)||{},A=e instanceof HTMLFormElement?e:e.closest?e.closest("form"):null;if(A&&"form"!==m.trim()){var S=F(A);for(var w in S)E[w]=S[w]}if("GET"===n){var N=[];for(var O in E)if(Array.isArray(E[O]))for(var _=0;_<E[O].length;_++)N.push(encodeURIComponent(O)+"="+encodeURIComponent(String(E[O][_])));else N.push(encodeURIComponent(O)+"="+encodeURIComponent(String(E[O])));N.length&&(r+=(r.indexOf("?")>=0?"&":"?")+N.join("&"))}else g.headers["Content-Type"]="application/json",y=JSON.stringify(E),g.body=y}else if("GET"!==n){var q=e instanceof HTMLFormElement?e:e.closest?e.closest("form"):null;q&&(g.headers["Content-Type"]="application/json",g.body=JSON.stringify(F(q)))}var k=e.getAttribute("data-headers");if(k){var T=B(k)||{};for(var L in T)g.headers[L]=T[L]}var x=!1;function M(){x||(x=!0,s&&clearTimeout(s),u&&c._bReqs.delete(u),c._bN=Math.max(0,(c._bN||1)-1),p(!1))}fetch(r,g).then(function(n){return M(),e.dispatchEvent(new CustomEvent("laghu:after-request",{bubbles:!0,detail:{status:n.status,url:r}})),n.ok?(n.headers.get("content-type")||"").toLowerCase().indexOf("json")>=0?n.json().then(function(e){C(t,"$error",null),e&&"object"==typeof e&&!Array.isArray(e)?Object.assign(t,e):C(t,"result",e)}):n.text().then(function(n){var r=e.getAttribute("data-select");if(r){var o=document.createElement("template");o.innerHTML=v(n);var c=o.content.querySelector(r);n=c?c.outerHTML:""}var u=a?document.querySelector(a):e;u&&D(u,n,i,t,d)}):(C(t,"$error",{status:n.status,url:r}),void e.dispatchEvent(new CustomEvent("laghu:request-error",{bubbles:!0,detail:{status:n.status,url:r}})))}).catch(function(n){M(),n&&"AbortError"===n.name&&!l||(C(t,"$error",{message:String(n&&n.message||n),timeout:l}),e.dispatchEvent(new CustomEvent("laghu:request-error",{bubbles:!0,detail:{error:n,timeout:l}})),h(n))})}(e,t,n,r,i,o)};I(e,c,l)}(t,i,n,r,a))})}function B(e){try{return JSON.parse(e)}catch(e){return null}}function F(e){var t={};return new FormData(e).forEach(function(e,n){n in t?(Array.isArray(t[n])||(t[n]=[t[n]]),t[n].push("string"==typeof e?e:String(e))):t[n]="string"==typeof e?e:String(e)}),t}function D(e,n,r,a,i){i=i||{};var o=String(r||"inner").toLowerCase();if("none"!==(o=String(r||"inner").toLowerCase())){var c=document.activeElement&&document.activeElement.id?document.activeElement.id:null,u=document.activeElement,l=u&&"number"==typeof u.selectionStart?u.selectionStart:-1,f=u&&"number"==typeof u.selectionEnd?u.selectionEnd:-1;e.dispatchEvent(new CustomEvent("laghu:before-swap",{bubbles:!0,cancelable:!1,detail:{swap:o}}));var s=0===o.indexOf("morph"),d="undefined"!=typeof window?window.LaghuMorph:null;if(s){if(d&&d.morph)return d.morph(e,v(n),{mode:"morph:outer"===o?"outer":"inner"}),U(e,a,i),e.dispatchEvent(new CustomEvent("laghu:after-swap",{bubbles:!0,detail:{swap:o}})),void J(c,l,f);t&&"undefined"!=typeof console&&console.warn('[laghu] data-swap="morph" needs morph.js (window.LaghuMorph). Falling back to inner.'),o="inner"}if("delete"===o)return M(e),void(e.parentNode&&e.parentNode.removeChild(e));var h=document.createElement("template");h.innerHTML=v(n);var p=h.content,b=new Map;e.querySelectorAll("[data-preserve][id]").forEach(function(e){b.set(e.id,e)}),M(e),"inner"===o?(e.innerHTML="",e.appendChild(p)):"text"===o?e.textContent=h.content.textContent:"outer"===o?e.replaceWith(p):"append"===o||"beforeend"===o?e.appendChild(p):"prepend"===o||"afterbegin"===o?e.insertBefore(p,e.firstChild):"beforebegin"===o?e.parentNode&&e.parentNode.insertBefore(p,e):"afterend"===o?e.parentNode&&e.parentNode.insertBefore(p,e.nextSibling):(e.innerHTML="",e.appendChild(p)),b.forEach(function(t,n){var r="#";try{r="#"+("undefined"!=typeof CSS&&CSS&&CSS.escape?CSS.escape(n):String(n).replace(/([^a-zA-Z0-9_-])/g,"\\$1"))}catch(e){r='[id="'+String(n).replace(/"/g,"")+'"]'}var a=e.querySelector?e.querySelector(r):null;a||(a=document.getElementById(n)),a&&a!==t&&t.parentNode!==a&&a.replaceWith(t)}),U("outer"===o?e.parentNode instanceof Element?e.parentNode:document.body:e,a,i),e.dispatchEvent(new CustomEvent("laghu:after-swap",{bubbles:!0,detail:{swap:o}})),J(c,l,f)}else e.dispatchEvent(new CustomEvent("laghu:after-swap",{bubbles:!0,detail:{swap:o}}))}function J(e,t,n){if(e){var r=document.getElementById(e);if(r&&r instanceof HTMLElement){try{r.focus({preventScroll:!0})}catch(e){try{r.focus()}catch(e){}}try{t>=0&&r.setSelectionRange&&r.setSelectionRange(t,n)}catch(e){}}}}var G=null;var W={version:e,define:function(e,t){if(!e||e.indexOf("-")<0)throw new Error("[laghu] tag must contain hyphen: "+e);var n=t()||{},r=n.observed||Object.keys(n.state||{}).filter(function(e){var t=n.state[e];return"string"==typeof t||"number"==typeof t||"boolean"==typeof t}),a=class extends HTMLElement{connectedCallback(){if(!this._bInit){this._bInit=!0;var e=t()||{},n=e.state||{};for(var a in n)if(Object.prototype.hasOwnProperty.call(this,a))try{n[a]=this[a],delete this[a]}catch(e){}var i=this._bPend;if(i){for(var o in i)n[o]=o in n?j(i[o],n[o]):i[o];this._bPend=null}else for(var c=0;c<r.length;c++){var u=r[c];this.hasAttribute&&this.hasAttribute(u)&&u in n&&(n[u]=j(this.getAttribute(u),n[u]))}var l=N(n);this.state=l,this.actions=e.actions||{},"function"==typeof e.onUnmount&&(this._bOnUnmount=e.onUnmount),U(this,l,e.actions||{});try{"function"==typeof e.onMount&&e.onMount.call(l)}catch(e){h(e)}this.classList.add("is-defined"),this.dispatchEvent(new CustomEvent("laghu:mount"))}}disconnectedCallback(){try{var e=this.state,t=this._bOnUnmount;"function"==typeof t&&e&&t.call(e)}catch(e){h(e)}M(this),this.dispatchEvent(new CustomEvent("laghu:unmount")),this._bInit=!1}connectedMoveCallback(){}attributeChangedCallback(e,t,n){if(t!==n){var r=this.state;if(r&&e in r)r[e]=j(n,r[e]);else{var a=this._bPend||{};a[e]=n,this._bPend=a}}}};return a.observedAttributes=r,customElements.define(e,a),a},reactive:N,effect:m,eval:q,get:w,set:C,parseTrigger:P,swap:D,css:"laghu:not(:defined),[data-laghu]:not(:defined){visibility:hidden}laghu.is-defined,[data-laghu].is-defined{visibility:visible}",sanitize:function(e){"function"==typeof e&&(v=e)},hydrate:function(e,t,n){var r=N(t||$(e.getAttribute("data-scope")));return e._bScope=r,U(e,r,n||{}),r},destroyTree:M,store:function(e,t){if(void 0!==t){var r=N(t);return n.set(e,r),r}return n.get(e)},computed:function(e){var t=N({v:void 0});return m(function(){t.v=e()}),function(){return t.v}},watch:function(e,t,n){var r,a=!1;return m(function(){var i=q(e,t);if(!a)return a=!0,void(r=i);if(i!==r){var o=r;r=i,n(i,o)}})},persist:function(e,t){var r=(t=t||{}).key||"laghu:"+e,a=void 0===t.version?1:t.version,i=void 0===t.debounce?300:t.debounce,o=n.get(e);if(!o)return null;try{var c="undefined"!=typeof localStorage?localStorage.getItem(r):null;if(c){var u=JSON.parse(c);u&&u.v===a&&u.state&&"object"==typeof u.state&&Object.assign(o,u.state)}}catch(e){h(e)}var l=0;return m(function(){JSON.stringify(o),"undefined"!=typeof localStorage&&(l&&clearTimeout(l),l=setTimeout(function(){try{localStorage.setItem(r,JSON.stringify({v:a,state:o}))}catch(e){h(e)}},i))}),o},directive:function(e,t){r.set(e,t)}};window.Laghu=W,"undefined"!=typeof document&&document.addEventListener("DOMContentLoaded",function(){G||"undefined"==typeof MutationObserver||(G=new MutationObserver(function(e){var t=new Set;e.forEach(function(e){e.addedNodes.forEach(function(e){e instanceof Element&&t.add(e)})}),e.forEach(function(e){e.addedNodes.forEach(function(e){if(e instanceof Element&&e.hasAttribute&&e.hasAttribute("data-scope")&&!e._bScope)try{var t=window.Laghu;t&&t.hydrate(e,$(e.getAttribute("data-scope")),{})}catch(e){h(e)}}),e.removedNodes.forEach(function(e){e instanceof Element&&!t.has(e)&&M(e)})})})).observe(document.documentElement,{childList:!0,subtree:!0}),document.querySelectorAll("[data-scope]").forEach(function(e){var t=e;t._bScope||W.hydrate(t,$(t.getAttribute("data-scope")),{})})})}();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(){"use strict";var n={path:"",params:{}},e=[];function t(n){var e=[],t=String(n).split("/").map(function(n){return":"===n.charAt(0)?(e.push(n.slice(1)),"([^/]+)"):"*"===n?(e.push("wild"),"(.*)"):n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}).join("/"),r=new RegExp("^"+t+"$");return function(n){var t=n.match(r);if(!t)return null;for(var o={},a=0;a<e.length;a++)o[e[a]]=decodeURIComponent(t[a+1]||"");return o}}var r={version:"1.1.0",route:function(r){var o=(r=r||{}).target||"#view",a=r.scope||{},i=r.actions||{},u=(r.routes||[]).map(function(n){return{match:t(n.path),render:n.render}});function c(){var t="undefined"!=typeof location&&location.hash||"#/",c="#"===t.charAt(0)?t.slice(1):t;c||(c="/");var f,h=n,d=null,l=null;for(f=0;f<u.length;f++)if(d=u[f].match(c)){l=u[f];break}var p={path:c,params:d||{}};if("function"!=typeof r.beforeEach||!1!==r.beforeEach(p,h)){n=p;var s="";try{l?s=l.render(d,p)||"":"function"==typeof r.notfound&&(s=r.notfound(p)||"")}catch(n){return void("undefined"!=typeof console&&console.error("[laghu-router]",n))}var v="undefined"!=typeof window&&window.Laghu||null,w="undefined"!=typeof document?document.querySelector(o):null;if(v&&w&&v.swap(w,String(s),"inner",a,i),"function"==typeof r.afterEach)try{r.afterEach(p,h)}catch(n){}for(var g=0;g<e.length;g++)try{e[g](p,h)}catch(n){}}}function f(){c()}return"undefined"!=typeof window&&window.addEventListener("hashchange",f),c(),function(){"undefined"!=typeof window&&window.removeEventListener("hashchange",f)}},compile:t,current:function(){return n},navigate:function(n){"undefined"!=typeof location&&(location.hash="#"+n)},afterEach:function(n){e.push(n)}};window.LaghuRouter=r}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "laghu",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Zero-build drop-in micro-framework: Light-DOM Web Components + Proxy reactivity + JSON-into-state fetch",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
"dist/",
|
|
13
13
|
"src/",
|
|
14
14
|
"README.md",
|
|
15
|
-
"LICENSE"
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"CHANGELOG.md",
|
|
17
|
+
"SECURITY.md"
|
|
16
18
|
],
|
|
17
19
|
"keywords": [
|
|
18
20
|
"frontend",
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
"homepage": "https://github.com/prosubodh/laghu#readme",
|
|
32
34
|
"scripts": {
|
|
33
35
|
"check": "tsc --noEmit -p jsconfig.json",
|
|
34
|
-
"test": "node test/run.mjs",
|
|
36
|
+
"test": "node test/run.mjs && node test/examples.mjs && node test/apps.mjs && node test/router.mjs",
|
|
35
37
|
"size": "node scripts/size.mjs",
|
|
36
38
|
"prepublishOnly": "npm run check && npm run test && npm run size"
|
|
37
39
|
},
|