apprun 3.33.10 → 3.35.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/WHATSNEW.md +50 -3
- package/apprun.d.ts +2 -2
- package/dist/apprun-dev-tools.js +1 -1
- package/dist/apprun-dev-tools.js.map +1 -1
- package/dist/apprun-html.esm.js +1 -1
- package/dist/apprun-html.esm.js.map +1 -1
- package/dist/apprun-html.js +1 -1
- package/dist/apprun-html.js.map +1 -1
- package/dist/apprun-play-html.esm.js +1 -1
- package/dist/apprun-play-html.esm.js.map +1 -1
- package/dist/apprun-play.js +1 -1
- package/dist/apprun-play.js.map +1 -1
- package/dist/apprun.esm.js +1 -1
- package/dist/apprun.esm.js.map +1 -1
- package/dist/apprun.js +1 -1
- package/dist/apprun.js.map +1 -1
- package/esm/app.js +1 -1
- package/esm/app.js.map +1 -1
- package/esm/apprun.js +24 -20
- package/esm/apprun.js.map +1 -1
- package/esm/router.js +3 -0
- package/esm/router.js.map +1 -1
- package/index.html +1 -0
- package/jsx-runtime.js +1 -1
- package/jsx-runtime.js.map +1 -1
- package/package.json +1 -1
- package/src/app.ts +6 -5
- package/src/apprun.ts +27 -21
- package/src/router.ts +2 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class t{constructor(){this._events={}}on(t,i,s={}){this._events[t]=this._events[t]||[],this._events[t].push({fn:i,options:s})}off(t,i){const s=this._events[t]||[];this._events[t]=s.filter((t=>t.fn!==i))}find(t){return this._events[t]}run(t,...i){const s=this.getSubscribers(t,this._events);return console.assert(s&&s.length>0,"No subscriber for event: "+t),s.forEach((s=>{const{fn:e,options:n}=s;return n.delay?this.delay(t,e,i,n):Object.keys(n).length>0?e.apply(this,[...i,n]):e.apply(this,i),!s.options.once})),s.length}once(t,i,s={}){this.on(t,i,Object.assign(Object.assign({},s),{once:!0}))}delay(t,i,s,e){e._t&&clearTimeout(e._t),e._t=setTimeout((()=>{clearTimeout(e._t),Object.keys(e).length>0?i.apply(this,[...s,e]):i.apply(this,s)}),e.delay)}runAsync(t,...i){const s=this.getSubscribers(t,this._events);console.assert(s&&s.length>0,"No subscriber for event: "+t);const e=s.map((t=>{const{fn:s,options:e}=t;return Object.keys(e).length>0?s.apply(this,[...i,e]):s.apply(this,i)}));return Promise.all(e)}query(t,...i){return this.runAsync(t,...i)}getSubscribers(t,i){const s=i[t]||[];return i[t]=s.filter((t=>!t.options.once)),Object.keys(i).filter((i=>i.endsWith("*")&&t.startsWith(i.replace("*","")))).sort(((t,i)=>i.length-t.length)).forEach((e=>s.push(...i[e].map((i=>Object.assign(Object.assign({},i),{options:Object.assign(Object.assign({},i.options),{event:t})})))))),s}}let i;const s="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};s.app&&s._AppRunVersions?i=s.app:(i=new t,s.app=i,s._AppRunVersions="AppRun-3");var e=i;const n=(t,i)=>(i?t.state[i]:t.state)||"",o=(t,i,s)=>{if(i){const e=t.state||{};e[i]=s,t.setState(e)}else t.setState(s)},r=(t,i)=>{if(Array.isArray(t))return t.map((t=>r(t,i)));{let{type:s,tag:h,props:c,children:l}=t;return h=h||s,l=l||(null==c?void 0:c.children),c&&Object.keys(c).forEach((t=>{t.startsWith("$")&&(((t,i,s,r)=>{if(t.startsWith("$on")){const s=i[t];if(t=t.substring(1),"boolean"==typeof s)i[t]=i=>r.run?r.run(t,i):e.run(t,i);else if("string"==typeof s)i[t]=t=>r.run?r.run(s,t):e.run(s,t);else if("function"==typeof s)i[t]=t=>r.setState(s(r.state,t));else if(Array.isArray(s)){const[n,...o]=s;"string"==typeof n?i[t]=t=>r.run?r.run(n,...o,t):e.run(n,...o,t):"function"==typeof n&&(i[t]=t=>r.setState(n(r.state,...o,t)))}}else if("$bind"===t){const e=i.type||"text",h="string"==typeof i[t]?i[t]:i.name;if("input"===s)switch(e){case"checkbox":i.checked=n(r,h),i.onclick=t=>o(r,h||t.target.name,t.target.checked);break;case"radio":i.checked=n(r,h)===i.value,i.onclick=t=>o(r,h||t.target.name,t.target.value);break;case"number":case"range":i.value=n(r,h),i.oninput=t=>o(r,h||t.target.name,Number(t.target.value));break;default:i.value=n(r,h),i.oninput=t=>o(r,h||t.target.name,t.target.value)}else"select"===s?(i.value=n(r,h),i.onchange=t=>{t.target.multiple||o(r,h||t.target.name,t.target.value)}):"option"===s?(i.selected=n(r,h),i.onclick=t=>o(r,h||t.target.name,t.target.selected)):"textarea"===s&&(i.innerHTML=n(r,h),i.oninput=t=>o(r,h||t.target.name,t.target.value))}else e.run("$",{key:t,tag:s,props:i,component:r})})(t,c,h,i),delete c[t])})),l&&r(l,i),t}};function h(t,...i){return l(i)}const c="_props";function l(t){const i=[],s=t=>{null!=t&&""!==t&&!1!==t&&i.push("function"==typeof t||"object"==typeof t?t:`${t}`)};return t&&t.forEach((t=>{Array.isArray(t)?t.forEach((t=>s(t))):s(t)})),i}function a(t,i,...s){const e=l(s);if("string"==typeof t)return{tag:t,props:i,children:e};if(Array.isArray(t))return t;if(void 0===t&&s)return e;if(Object.getPrototypeOf(t).i)return{tag:t,props:i,children:e};if("function"==typeof t)return t(i,e);throw new Error(`Unknown tag in vdom ${t}`)}const u=new WeakMap,d=(t,i,s={})=>{if(null==i||!1===i)return;!function(t,i,s={}){if(null==i||!1===i)return;if(i=g(i,s),!t)return;const e="SVG"===t.nodeName;Array.isArray(i)?f(t,i,e):f(t,[i],e)}("string"==typeof t&&t?document.getElementById(t)||document.querySelector(t):t,i=r(i,s),s)};function p(t,i,s){3!==i._op&&(s=s||"svg"===i.tag,!function(t,i){const s=t.nodeName,e=`${i.tag||""}`;return s.toUpperCase()===e.toUpperCase()}(t,i)?t.parentNode.replaceChild(b(i,s),t):(!(2&i._op)&&f(t,i.children,s),!(1&i._op)&&m(t,i.props,s)))}function f(t,i,s){var e,n;const o=(null===(e=t.childNodes)||void 0===e?void 0:e.length)||0,r=(null==i?void 0:i.length)||0,h=Math.min(o,r);for(let e=0;e<h;e++){const n=i[e];if(3===n._op)continue;const o=t.childNodes[e];if("string"==typeof n)o.textContent!==n&&(3===o.nodeType?o.nodeValue=n:t.replaceChild(v(n),o));else if(n instanceof HTMLElement||n instanceof SVGElement)t.insertBefore(n,o);else{const i=n.props&&n.props.key;if(i)if(o.key===i)p(t.childNodes[e],n,s);else{const r=u[i];if(r){const i=r.nextSibling;t.insertBefore(r,o),i?t.insertBefore(o,i):t.appendChild(o),p(t.childNodes[e],n,s)}else t.replaceChild(b(n,s),o)}else p(t.childNodes[e],n,s)}}let c=(null===(n=t.childNodes)||void 0===n?void 0:n.length)||0;for(;c>h;)t.removeChild(t.lastChild),c--;if(r>h){const e=document.createDocumentFragment();for(let t=h;t<i.length;t++)e.appendChild(b(i[t],s));t.appendChild(e)}}const y=t=>{const i=document.createElement("section");return i.insertAdjacentHTML("afterbegin",t),Array.from(i.children)};function v(t){if(0===(null==t?void 0:t.indexOf("_html:"))){const i=document.createElement("div");return i.insertAdjacentHTML("afterbegin",t.substring(6)),i}return document.createTextNode(null!=t?t:"")}function b(t,i){if(t instanceof HTMLElement||t instanceof SVGElement)return t;if("string"==typeof t)return v(t);if(!t.tag||"function"==typeof t.tag)return v(JSON.stringify(t));const s=(i=i||"svg"===t.tag)?document.createElementNS("http://www.w3.org/2000/svg",t.tag):document.createElement(t.tag);return m(s,t.props,i),t.children&&t.children.forEach((t=>s.appendChild(b(t,i)))),s}function m(t,i,s){const e=t[c]||{};i=function(t,i){i.class=i.class||i.className,delete i.className;const s={};return t&&Object.keys(t).forEach((t=>s[t]=null)),Object.keys(i).forEach((t=>s[t]=i[t])),s}(e,i||{}),t[c]=i;for(const e in i){const n=i[e];if(e.startsWith("data-")){const i=e.substring(5).replace(/-(\w)/g,(t=>t[1].toUpperCase()));t.dataset[i]!==n&&(n||""===n?t.dataset[i]=n:delete t.dataset[i])}else if("style"===e)if(t.style.cssText&&(t.style.cssText=""),"string"==typeof n)t.style.cssText=n;else for(const i in n)t.style[i]!==n[i]&&(t.style[i]=n[i]);else if(e.startsWith("xlink")){const i=e.replace("xlink","").toLowerCase();null==n||!1===n?t.removeAttributeNS("http://www.w3.org/1999/xlink",i):t.setAttributeNS("http://www.w3.org/1999/xlink",i,n)}else e.startsWith("on")?n&&"function"!=typeof n?"string"==typeof n&&(n?t.setAttribute(e,n):t.removeAttribute(e)):t[e]=n:/^id$|^class$|^list$|^readonly$|^contenteditable$|^role|-|^for$/g.test(e)||s?t.getAttribute(e)!==n&&(n?t.setAttribute(e,n):t.removeAttribute(e)):t[e]!==n&&(t[e]=n);"key"===e&&n&&(u[n]=t)}i&&"function"==typeof i.ref&&window.requestAnimationFrame((()=>i.ref(t)))}function g(t,i,s=0){var e;if("string"==typeof t)return t;if(Array.isArray(t))return t.map((t=>g(t,i,s++)));let n=t;if(t&&"function"==typeof t.tag&&Object.getPrototypeOf(t.tag).i&&(n=function(t,i,s){const{tag:e,props:n,children:o}=t;let r=`_${s}`,h=n&&n.id;h?r=h:h=`_${s}${Date.now()}`;let c="section";n&&n.as&&(c=n.as,delete n.as),i.v||(i.v={});let l=i.v[r];if(l&&l instanceof e&&l.element)l.renderState(l.state);else{const t=document.createElement(c);l=i.v[r]=new e(Object.assign(Object.assign({},n),{children:o})).mount(t,{render:!0})}if(l.mounted){const t=l.mounted(n,o,l.state);void 0!==t&&l.setState(t)}return m(l.element,n,!1),l.element}(t,i,s)),n&&Array.isArray(n.children)){const t=null===(e=n.props)||void 0===e?void 0:e._component;if(t){let i=0;n.children=n.children.map((s=>g(s,t,i++)))}else n.children=n.children.map((t=>g(t,i,s++)))}return n}const w=(t,i={})=>class extends HTMLElement{constructor(){super()}get component(){return this._component}get state(){return this._component.state}static get observedAttributes(){return(i.observedAttributes||[]).map((t=>t.toLowerCase()))}connectedCallback(){if(this.isConnected&&!this._component){const s=i||{};this._shadowRoot=s.shadow?this.attachShadow({mode:"open"}):this;const e=s.observedAttributes||[],n=e.reduce(((t,i)=>{const s=i.toLowerCase();return s!==i&&(t[s]=i),t}),{});this._attrMap=t=>n[t]||t;const o={};Array.from(this.attributes).forEach((t=>o[this._attrMap(t.name)]=t.value)),e.forEach((t=>{void 0!==this[t]&&(o[t]=this[t]),Object.defineProperty(this,t,{get:()=>o[t],set(i){this.attributeChangedCallback(t,o[t],i)},configurable:!0,enumerable:!0})})),requestAnimationFrame((()=>{const i=this.children?Array.from(this.children):[];if(this._component=new t(Object.assign(Object.assign({},o),{children:i})).mount(this._shadowRoot,s),this._component._props=o,this._component.dispatchEvent=this.dispatchEvent.bind(this),this._component.mounted){const t=this._component.mounted(o,i,this._component.state);void 0!==t&&(this._component.state=t)}this.on=this._component.on.bind(this._component),this.run=this._component.run.bind(this._component),!1!==s.render&&this._component.run(".")}))}}disconnectedCallback(){var t,i,s,e;null===(i=null===(t=this._component)||void 0===t?void 0:t.unload)||void 0===i||i.call(t),null===(e=null===(s=this._component)||void 0===s?void 0:s.unmount)||void 0===e||e.call(s),this._component=null}attributeChangedCallback(t,s,e){if(this._component){const n=this._attrMap(t);this._component._props[n]=e,this._component.run("attributeChanged",n,s,e),e!==s&&!1!==i.render&&window.requestAnimationFrame((()=>{this._component.run(".")}))}}};var $=(t,i,s)=>{"undefined"!=typeof customElements&&customElements.define(t,w(i,s))};const S={meta:new WeakMap,defineMetadata(t,i,s){this.meta.has(s)||this.meta.set(s,{}),this.meta.get(s)[t]=i},getMetadataKeys(t){return t=Object.getPrototypeOf(t),this.meta.get(t)?Object.keys(this.meta.get(t)):[]},getMetadata(t,i){return i=Object.getPrototypeOf(i),this.meta.get(i)?this.meta.get(i)[t]:null}};const A=new Map;e.find("get-components")||e.on("get-components",(t=>t.components=A));const j=t=>t;class x{renderState(t,i=null){if(!this.view)return;let s=i||this.view(t);if(e.debug&&e.run("debug",{component:this,_:s?".":"-",state:t,vdom:s,el:this.element}),"object"!=typeof document)return;const n="string"==typeof this.element&&this.element?document.getElementById(this.element)||document.querySelector(this.element):this.element;if(!n)return;const o="_c";this.unload?n._component===this&&n.getAttribute(o)===this.tracking_id||(this.tracking_id=(new Date).valueOf().toString(),n.setAttribute(o,this.tracking_id),"undefined"!=typeof MutationObserver&&(this.observer||(this.observer=new MutationObserver((t=>{t[0].oldValue!==this.tracking_id&&document.body.contains(n)||(this.unload(this.state),this.observer.disconnect(),this.observer=null)}))),this.observer.observe(document.body,{childList:!0,subtree:!0,attributes:!0,attributeOldValue:!0,attributeFilter:[o]}))):n.removeAttribute&&n.removeAttribute(o),n._component=this,!i&&s&&(s=r(s,this),this.options.transition&&document&&document.startViewTransition?document.startViewTransition((()=>e.render(n,s,this))):e.render(n,s,this)),this.rendered&&this.rendered(this.state)}setState(t,i={render:!0,history:!1}){const s=async t=>{try{for(;;){const{value:s,done:e}=await t.next();if(e)break;this.setState(s,i)}}catch(t){console.error("Error in async iterator:",t)}},e=t;if(null==e?void 0:e[Symbol.asyncIterator])this.setState(s(e[Symbol.asyncIterator]()),i);else if((null==e?void 0:e[Symbol.iterator])&&"function"==typeof e.next)for(const t of e)this.setState(t,i);else if(t&&t instanceof Promise)Promise.resolve(t).then((s=>{this.setState(s,i),this._state=t}));else{if(this._state=t,null==t)return;this.state=t,!1!==i.render&&(i.transition&&document&&document.startViewTransition?document.startViewTransition((()=>this.renderState(t))):this.renderState(t)),!1!==i.history&&this.enable_history&&(this._history=[...this._history,t],this._history_idx=this._history.length-1),"function"==typeof i.callback&&i.callback(this.state)}}constructor(i,s,e,n){this.state=i,this.view=s,this.update=e,this.options=n,this._app=new t,this._actions=[],this._global_events=[],this._history=[],this._history_idx=-1,this._history_prev=()=>{this._history_idx--,this._history_idx>=0?this.setState(this._history[this._history_idx],{render:!0,history:!1}):this._history_idx=0},this._history_next=()=>{this._history_idx++,this._history_idx<this._history.length?this.setState(this._history[this._history_idx],{render:!0,history:!1}):this._history_idx=this._history.length-1},this.start=(t=null,i)=>{if(this.mount(t,Object.assign({render:!0},i)),this.mounted&&"function"==typeof this.mounted){const t=this.mounted({},[],this.state);void 0!==t&&this.setState(t)}return this}}mount(t=null,i){var s,n;return console.assert(!this.element,"Component already mounted."),this.options=i=Object.assign(Object.assign({},this.options),i),this.element=t,this.global_event=i.global_event,this.enable_history=!!i.history,this.enable_history&&(this.on(i.history.prev||"history-prev",this._history_prev),this.on(i.history.next||"history-next",this._history_next)),i.route&&(this.update=this.update||{},this.update[i.route]||(this.update[i.route]=j)),this.add_actions(),this.state=null!==(n=null!==(s=this.state)&&void 0!==s?s:this.model)&&void 0!==n?n:{},"function"==typeof this.state&&(this.state=this.state()),this.setState(this.state,{render:!!i.render,history:!0}),e.debug&&(A.get(t)?A.get(t).push(this):A.set(t,[this])),this}is_global_event(t){return t&&(this.global_event||this._global_events.indexOf(t)>=0||t.startsWith("#")||t.startsWith("/")||t.startsWith("@"))}add_action(t,i,s={}){i&&"function"==typeof i&&(s.global&&this._global_events.push(t),this.on(t,((...n)=>{e.debug&&e.run("debug",{component:this,_:">",event:t,p:n,current_state:this.state,options:s});const o=i(this.state,...n);e.debug&&e.run("debug",{component:this,_:"<",event:t,p:n,newState:o,state:this.state,options:s}),this.setState(o,s)}),s))}add_actions(){const t=this.update||{};S.getMetadataKeys(this).forEach((i=>{if(i.startsWith("apprun-update:")){const s=S.getMetadata(i,this);t[s.name]=[this[s.key].bind(this),s.options]}}));const i={};Array.isArray(t)?t.forEach((t=>{const[s,e,n]=t;s.toString().split(",").forEach((t=>i[t.trim()]=[e,n]))})):Object.keys(t).forEach((s=>{const e=t[s];("function"==typeof e||Array.isArray(e))&&s.split(",").forEach((t=>i[t.trim()]=e))})),i["."]||(i["."]=j),Object.keys(i).forEach((t=>{const s=i[t];"function"==typeof s?this.add_action(t,s):Array.isArray(s)&&this.add_action(t,s[0],s[1])}))}run(t,...i){if(this.state instanceof Promise)return Promise.resolve(this.state).then((s=>{this.state=s,this.run(t,...i)}));{const s=t.toString();return this.is_global_event(s)?e.run(s,...i):this._app.run(s,...i)}}on(t,i,s){const n=t.toString();return this._actions.push({name:n,fn:i}),this.is_global_event(n)?e.on(n,i,s):this._app.on(n,i,s)}runAsync(t,...i){const s=t.toString();return this.is_global_event(s)?e.runAsync(s,...i):this._app.runAsync(s,...i)}query(t,...i){return this.runAsync(t,...i)}unmount(){var t;null===(t=this.observer)||void 0===t||t.disconnect(),this._actions.forEach((t=>{const{name:i,fn:s}=t;this.is_global_event(i)?e.off(i,s):this._app.off(i,s)}))}}x.i=!0;const O="//",_="///",k=t=>{if(t||(t="#"),t.startsWith("#")){const[i,...s]=t.split("/");e.run(i,...s)||e.run(_,i,...s),e.run(O,i,...s)}else if(t.startsWith("/")){const[i,s,...n]=t.split("/");e.run("/"+s,...n)||e.run(_,"/"+s,...n),e.run(O,"/"+s,...n)}else e.run(t)||e.run(_,t),e.run(O,t)};if(!e.start){e.h=e.createElement=a,e.render=d,e.Fragment=h,e.webComponent=$,e.safeHTML=y,e.start=(t,i,s,e,n)=>{const o=Object.assign({render:!0,global_event:!0},n),r=new x(i,s,e);return n&&n.rendered&&(r.rendered=n.rendered),n&&n.mounted&&(r.mounted=n.mounted),r.start(t,o),r};const t=t=>{};e.on("$",t),e.on("debug",(i=>t)),e.on(O,t),e.on("#",t),e.route=k,e.on("route",(t=>e.route&&e.route(t))),"object"==typeof document&&document.addEventListener("DOMContentLoaded",(()=>{e.route===k&&(window.onpopstate=()=>k(location.hash),document.body.hasAttribute("apprun-no-init")||e["no-init-route"]||k(location.hash))})),"object"==typeof window&&(window.Component=x,window._React=window.React,window.React=e,window.on=function(t,i={}){return function(s,e){const n=t?t.toString():e;S.defineMetadata(`apprun-update:${n}`,{name:n,key:e,options:i},s)}},window.customElement=function(t,i){return function(s){return $(t,s,i),s}},window.safeHTML=y),e.use_render=(t,i=0)=>{e.render=0===i?(i,s)=>t(s,i):(i,s)=>t(i,s)},e.use_react=(t,i)=>{e.h=e.createElement=t.createElement,e.Fragment=t.Fragment,e.render=(t,s)=>i.render(s,t),t.version&&t.version.startsWith("18")&&(e.render=(t,s)=>{t&&s&&(t._root||(t._root=i.createRoot(t)),t._root.render(s))})},e.use_prettyLink=()=>{window.onload=()=>e.route(location.pathname),window.addEventListener("popstate",(()=>e.route(location.pathname))),document.body.addEventListener("click",(t=>{const i=t.target,s="A"===i.tagName?i:i.closest("a");s&&s.origin===location.origin&&(t.preventDefault(),history.pushState(null,"",s.pathname),e.route(s.pathname))}))}}
|
|
1
|
+
class t{constructor(){this._events={}}on(t,i,s={}){this._events[t]=this._events[t]||[],this._events[t].push({fn:i,options:s})}off(t,i){const s=this._events[t]||[];this._events[t]=s.filter((t=>t.fn!==i))}find(t){return this._events[t]}run(t,...i){const s=this.getSubscribers(t,this._events);return console.assert(s&&s.length>0,"No subscriber for event: "+t),s.forEach((s=>{const{fn:e,options:n}=s;return n.delay?this.delay(t,e,i,n):Object.keys(n).length>0?e.apply(this,[...i,n]):e.apply(this,i),!s.options.once})),s.length}once(t,i,s={}){this.on(t,i,Object.assign(Object.assign({},s),{once:!0}))}delay(t,i,s,e){e._t&&clearTimeout(e._t),e._t=setTimeout((()=>{clearTimeout(e._t),Object.keys(e).length>0?i.apply(this,[...s,e]):i.apply(this,s)}),e.delay)}runAsync(t,...i){const s=this.getSubscribers(t,this._events);console.assert(s&&s.length>0,"No subscriber for event: "+t);const e=s.map((t=>{const{fn:s,options:e}=t;return Object.keys(e).length>0?s.apply(this,[...i,e]):s.apply(this,i)}));return Promise.all(e)}query(t,...i){return this.runAsync(t,...i)}getSubscribers(t,i){const s=i[t]||[];return i[t]=s.filter((t=>!t.options.once)),Object.keys(i).filter((i=>i.endsWith("*")&&t.startsWith(i.replace("*","")))).sort(((t,i)=>i.length-t.length)).forEach((e=>s.push(...i[e].map((i=>Object.assign(Object.assign({},i),{options:Object.assign(Object.assign({},i.options),{event:t})})))))),s}}let i;const s="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};s.app&&s._AppRunVersions?i=s.app:(i=new t,s.app=i,s._AppRunVersions="AppRun-3.3.11");var e=i;const n=(t,i)=>(i?t.state[i]:t.state)||"",o=(t,i,s)=>{if(i){const e=t.state||{};e[i]=s,t.setState(e)}else t.setState(s)},r=(t,i)=>{if(Array.isArray(t))return t.map((t=>r(t,i)));{let{type:s,tag:h,props:c,children:l}=t;return h=h||s,l=l||(null==c?void 0:c.children),c&&Object.keys(c).forEach((t=>{t.startsWith("$")&&(((t,i,s,r)=>{if(t.startsWith("$on")){const s=i[t];if(t=t.substring(1),"boolean"==typeof s)i[t]=i=>r.run?r.run(t,i):e.run(t,i);else if("string"==typeof s)i[t]=t=>r.run?r.run(s,t):e.run(s,t);else if("function"==typeof s)i[t]=t=>r.setState(s(r.state,t));else if(Array.isArray(s)){const[n,...o]=s;"string"==typeof n?i[t]=t=>r.run?r.run(n,...o,t):e.run(n,...o,t):"function"==typeof n&&(i[t]=t=>r.setState(n(r.state,...o,t)))}}else if("$bind"===t){const e=i.type||"text",h="string"==typeof i[t]?i[t]:i.name;if("input"===s)switch(e){case"checkbox":i.checked=n(r,h),i.onclick=t=>o(r,h||t.target.name,t.target.checked);break;case"radio":i.checked=n(r,h)===i.value,i.onclick=t=>o(r,h||t.target.name,t.target.value);break;case"number":case"range":i.value=n(r,h),i.oninput=t=>o(r,h||t.target.name,Number(t.target.value));break;default:i.value=n(r,h),i.oninput=t=>o(r,h||t.target.name,t.target.value)}else"select"===s?(i.value=n(r,h),i.onchange=t=>{t.target.multiple||o(r,h||t.target.name,t.target.value)}):"option"===s?(i.selected=n(r,h),i.onclick=t=>o(r,h||t.target.name,t.target.selected)):"textarea"===s&&(i.innerHTML=n(r,h),i.oninput=t=>o(r,h||t.target.name,t.target.value))}else e.run("$",{key:t,tag:s,props:i,component:r})})(t,c,h,i),delete c[t])})),l&&r(l,i),t}};function h(t,...i){return l(i)}const c="_props";function l(t){const i=[],s=t=>{null!=t&&""!==t&&!1!==t&&i.push("function"==typeof t||"object"==typeof t?t:`${t}`)};return t&&t.forEach((t=>{Array.isArray(t)?t.forEach((t=>s(t))):s(t)})),i}function a(t,i,...s){const e=l(s);if("string"==typeof t)return{tag:t,props:i,children:e};if(Array.isArray(t))return t;if(void 0===t&&s)return e;if(Object.getPrototypeOf(t).i)return{tag:t,props:i,children:e};if("function"==typeof t)return t(i,e);throw new Error(`Unknown tag in vdom ${t}`)}const u=new WeakMap,d=(t,i,s={})=>{if(null==i||!1===i)return;!function(t,i,s={}){if(null==i||!1===i)return;if(i=g(i,s),!t)return;const e="SVG"===t.nodeName;Array.isArray(i)?f(t,i,e):f(t,[i],e)}("string"==typeof t&&t?document.getElementById(t)||document.querySelector(t):t,i=r(i,s),s)};function p(t,i,s){3!==i._op&&(s=s||"svg"===i.tag,!function(t,i){const s=t.nodeName,e=`${i.tag||""}`;return s.toUpperCase()===e.toUpperCase()}(t,i)?t.parentNode.replaceChild(b(i,s),t):(!(2&i._op)&&f(t,i.children,s),!(1&i._op)&&m(t,i.props,s)))}function f(t,i,s){var e,n;const o=(null===(e=t.childNodes)||void 0===e?void 0:e.length)||0,r=(null==i?void 0:i.length)||0,h=Math.min(o,r);for(let e=0;e<h;e++){const n=i[e];if(3===n._op)continue;const o=t.childNodes[e];if("string"==typeof n)o.textContent!==n&&(3===o.nodeType?o.nodeValue=n:t.replaceChild(v(n),o));else if(n instanceof HTMLElement||n instanceof SVGElement)t.insertBefore(n,o);else{const i=n.props&&n.props.key;if(i)if(o.key===i)p(t.childNodes[e],n,s);else{const r=u[i];if(r){const i=r.nextSibling;t.insertBefore(r,o),i?t.insertBefore(o,i):t.appendChild(o),p(t.childNodes[e],n,s)}else t.replaceChild(b(n,s),o)}else p(t.childNodes[e],n,s)}}let c=(null===(n=t.childNodes)||void 0===n?void 0:n.length)||0;for(;c>h;)t.removeChild(t.lastChild),c--;if(r>h){const e=document.createDocumentFragment();for(let t=h;t<i.length;t++)e.appendChild(b(i[t],s));t.appendChild(e)}}const y=t=>{const i=document.createElement("section");return i.insertAdjacentHTML("afterbegin",t),Array.from(i.children)};function v(t){if(0===(null==t?void 0:t.indexOf("_html:"))){const i=document.createElement("div");return i.insertAdjacentHTML("afterbegin",t.substring(6)),i}return document.createTextNode(null!=t?t:"")}function b(t,i){if(t instanceof HTMLElement||t instanceof SVGElement)return t;if("string"==typeof t)return v(t);if(!t.tag||"function"==typeof t.tag)return v(JSON.stringify(t));const s=(i=i||"svg"===t.tag)?document.createElementNS("http://www.w3.org/2000/svg",t.tag):document.createElement(t.tag);return m(s,t.props,i),t.children&&t.children.forEach((t=>s.appendChild(b(t,i)))),s}function m(t,i,s){const e=t[c]||{};i=function(t,i){i.class=i.class||i.className,delete i.className;const s={};return t&&Object.keys(t).forEach((t=>s[t]=null)),Object.keys(i).forEach((t=>s[t]=i[t])),s}(e,i||{}),t[c]=i;for(const e in i){const n=i[e];if(e.startsWith("data-")){const i=e.substring(5).replace(/-(\w)/g,(t=>t[1].toUpperCase()));t.dataset[i]!==n&&(n||""===n?t.dataset[i]=n:delete t.dataset[i])}else if("style"===e)if(t.style.cssText&&(t.style.cssText=""),"string"==typeof n)t.style.cssText=n;else for(const i in n)t.style[i]!==n[i]&&(t.style[i]=n[i]);else if(e.startsWith("xlink")){const i=e.replace("xlink","").toLowerCase();null==n||!1===n?t.removeAttributeNS("http://www.w3.org/1999/xlink",i):t.setAttributeNS("http://www.w3.org/1999/xlink",i,n)}else e.startsWith("on")?n&&"function"!=typeof n?"string"==typeof n&&(n?t.setAttribute(e,n):t.removeAttribute(e)):t[e]=n:/^id$|^class$|^list$|^readonly$|^contenteditable$|^role|-|^for$/g.test(e)||s?t.getAttribute(e)!==n&&(n?t.setAttribute(e,n):t.removeAttribute(e)):t[e]!==n&&(t[e]=n);"key"===e&&n&&(u[n]=t)}i&&"function"==typeof i.ref&&window.requestAnimationFrame((()=>i.ref(t)))}function g(t,i,s=0){var e;if("string"==typeof t)return t;if(Array.isArray(t))return t.map((t=>g(t,i,s++)));let n=t;if(t&&"function"==typeof t.tag&&Object.getPrototypeOf(t.tag).i&&(n=function(t,i,s){const{tag:e,props:n,children:o}=t;let r=`_${s}`,h=n&&n.id;h?r=h:h=`_${s}${Date.now()}`;let c="section";n&&n.as&&(c=n.as,delete n.as),i.v||(i.v={});let l=i.v[r];if(l&&l instanceof e&&l.element)l.renderState(l.state);else{const t=document.createElement(c);l=i.v[r]=new e(Object.assign(Object.assign({},n),{children:o})).mount(t,{render:!0})}if(l.mounted){const t=l.mounted(n,o,l.state);void 0!==t&&l.setState(t)}return m(l.element,n,!1),l.element}(t,i,s)),n&&Array.isArray(n.children)){const t=null===(e=n.props)||void 0===e?void 0:e._component;if(t){let i=0;n.children=n.children.map((s=>g(s,t,i++)))}else n.children=n.children.map((t=>g(t,i,s++)))}return n}const w=(t,i={})=>class extends HTMLElement{constructor(){super()}get component(){return this._component}get state(){return this._component.state}static get observedAttributes(){return(i.observedAttributes||[]).map((t=>t.toLowerCase()))}connectedCallback(){if(this.isConnected&&!this._component){const s=i||{};this._shadowRoot=s.shadow?this.attachShadow({mode:"open"}):this;const e=s.observedAttributes||[],n=e.reduce(((t,i)=>{const s=i.toLowerCase();return s!==i&&(t[s]=i),t}),{});this._attrMap=t=>n[t]||t;const o={};Array.from(this.attributes).forEach((t=>o[this._attrMap(t.name)]=t.value)),e.forEach((t=>{void 0!==this[t]&&(o[t]=this[t]),Object.defineProperty(this,t,{get:()=>o[t],set(i){this.attributeChangedCallback(t,o[t],i)},configurable:!0,enumerable:!0})})),requestAnimationFrame((()=>{const i=this.children?Array.from(this.children):[];if(this._component=new t(Object.assign(Object.assign({},o),{children:i})).mount(this._shadowRoot,s),this._component._props=o,this._component.dispatchEvent=this.dispatchEvent.bind(this),this._component.mounted){const t=this._component.mounted(o,i,this._component.state);void 0!==t&&(this._component.state=t)}this.on=this._component.on.bind(this._component),this.run=this._component.run.bind(this._component),!1!==s.render&&this._component.run(".")}))}}disconnectedCallback(){var t,i,s,e;null===(i=null===(t=this._component)||void 0===t?void 0:t.unload)||void 0===i||i.call(t),null===(e=null===(s=this._component)||void 0===s?void 0:s.unmount)||void 0===e||e.call(s),this._component=null}attributeChangedCallback(t,s,e){if(this._component){const n=this._attrMap(t);this._component._props[n]=e,this._component.run("attributeChanged",n,s,e),e!==s&&!1!==i.render&&window.requestAnimationFrame((()=>{this._component.run(".")}))}}};var $=(t,i,s)=>{"undefined"!=typeof customElements&&customElements.define(t,w(i,s))};const S={meta:new WeakMap,defineMetadata(t,i,s){this.meta.has(s)||this.meta.set(s,{}),this.meta.get(s)[t]=i},getMetadataKeys(t){return t=Object.getPrototypeOf(t),this.meta.get(t)?Object.keys(this.meta.get(t)):[]},getMetadata(t,i){return i=Object.getPrototypeOf(i),this.meta.get(i)?this.meta.get(i)[t]:null}};const A=new Map;e.find("get-components")||e.on("get-components",(t=>t.components=A));const j=t=>t;class x{renderState(t,i=null){if(!this.view)return;let s=i||this.view(t);if(e.debug&&e.run("debug",{component:this,_:s?".":"-",state:t,vdom:s,el:this.element}),"object"!=typeof document)return;const n="string"==typeof this.element&&this.element?document.getElementById(this.element)||document.querySelector(this.element):this.element;if(!n)return;const o="_c";this.unload?n._component===this&&n.getAttribute(o)===this.tracking_id||(this.tracking_id=(new Date).valueOf().toString(),n.setAttribute(o,this.tracking_id),"undefined"!=typeof MutationObserver&&(this.observer||(this.observer=new MutationObserver((t=>{t[0].oldValue!==this.tracking_id&&document.body.contains(n)||(this.unload(this.state),this.observer.disconnect(),this.observer=null)}))),this.observer.observe(document.body,{childList:!0,subtree:!0,attributes:!0,attributeOldValue:!0,attributeFilter:[o]}))):n.removeAttribute&&n.removeAttribute(o),n._component=this,!i&&s&&(s=r(s,this),this.options.transition&&document&&document.startViewTransition?document.startViewTransition((()=>e.render(n,s,this))):e.render(n,s,this)),this.rendered&&this.rendered(this.state)}setState(t,i={render:!0,history:!1}){const s=async t=>{try{for(;;){const{value:s,done:e}=await t.next();if(e)break;this.setState(s,i)}}catch(t){console.error("Error in async iterator:",t)}},e=t;if(null==e?void 0:e[Symbol.asyncIterator])this.setState(s(e[Symbol.asyncIterator]()),i);else if((null==e?void 0:e[Symbol.iterator])&&"function"==typeof e.next)for(const t of e)this.setState(t,i);else if(t&&t instanceof Promise)Promise.resolve(t).then((s=>{this.setState(s,i),this._state=t}));else{if(this._state=t,null==t)return;this.state=t,!1!==i.render&&(i.transition&&document&&document.startViewTransition?document.startViewTransition((()=>this.renderState(t))):this.renderState(t)),!1!==i.history&&this.enable_history&&(this._history=[...this._history,t],this._history_idx=this._history.length-1),"function"==typeof i.callback&&i.callback(this.state)}}constructor(i,s,e,n){this.state=i,this.view=s,this.update=e,this.options=n,this._app=new t,this._actions=[],this._global_events=[],this._history=[],this._history_idx=-1,this._history_prev=()=>{this._history_idx--,this._history_idx>=0?this.setState(this._history[this._history_idx],{render:!0,history:!1}):this._history_idx=0},this._history_next=()=>{this._history_idx++,this._history_idx<this._history.length?this.setState(this._history[this._history_idx],{render:!0,history:!1}):this._history_idx=this._history.length-1},this.start=(t=null,i)=>{if(this.mount(t,Object.assign({render:!0},i)),this.mounted&&"function"==typeof this.mounted){const t=this.mounted({},[],this.state);void 0!==t&&this.setState(t)}return this}}mount(t=null,i){var s,n;return console.assert(!this.element,"Component already mounted."),this.options=i=Object.assign(Object.assign({},this.options),i),this.element=t,this.global_event=i.global_event,this.enable_history=!!i.history,this.enable_history&&(this.on(i.history.prev||"history-prev",this._history_prev),this.on(i.history.next||"history-next",this._history_next)),i.route&&(this.update=this.update||{},this.update[i.route]||(this.update[i.route]=j)),this.add_actions(),this.state=null!==(n=null!==(s=this.state)&&void 0!==s?s:this.model)&&void 0!==n?n:{},"function"==typeof this.state&&(this.state=this.state()),this.setState(this.state,{render:!!i.render,history:!0}),e.debug&&(A.get(t)?A.get(t).push(this):A.set(t,[this])),this}is_global_event(t){return t&&(this.global_event||this._global_events.indexOf(t)>=0||t.startsWith("#")||t.startsWith("/")||t.startsWith("@"))}add_action(t,i,s={}){i&&"function"==typeof i&&(s.global&&this._global_events.push(t),this.on(t,((...n)=>{e.debug&&e.run("debug",{component:this,_:">",event:t,p:n,current_state:this.state,options:s});const o=i(this.state,...n);e.debug&&e.run("debug",{component:this,_:"<",event:t,p:n,newState:o,state:this.state,options:s}),this.setState(o,s)}),s))}add_actions(){const t=this.update||{};S.getMetadataKeys(this).forEach((i=>{if(i.startsWith("apprun-update:")){const s=S.getMetadata(i,this);t[s.name]=[this[s.key].bind(this),s.options]}}));const i={};Array.isArray(t)?t.forEach((t=>{const[s,e,n]=t;s.toString().split(",").forEach((t=>i[t.trim()]=[e,n]))})):Object.keys(t).forEach((s=>{const e=t[s];("function"==typeof e||Array.isArray(e))&&s.split(",").forEach((t=>i[t.trim()]=e))})),i["."]||(i["."]=j),Object.keys(i).forEach((t=>{const s=i[t];"function"==typeof s?this.add_action(t,s):Array.isArray(s)&&this.add_action(t,s[0],s[1])}))}run(t,...i){if(this.state instanceof Promise)return Promise.resolve(this.state).then((s=>{this.state=s,this.run(t,...i)}));{const s=t.toString();return this.is_global_event(s)?e.run(s,...i):this._app.run(s,...i)}}on(t,i,s){const n=t.toString();return this._actions.push({name:n,fn:i}),this.is_global_event(n)?e.on(n,i,s):this._app.on(n,i,s)}runAsync(t,...i){const s=t.toString();return this.is_global_event(s)?e.runAsync(s,...i):this._app.runAsync(s,...i)}query(t,...i){return this.runAsync(t,...i)}unmount(){var t;null===(t=this.observer)||void 0===t||t.disconnect(),this._actions.forEach((t=>{const{name:i,fn:s}=t;this.is_global_event(i)?e.off(i,s):this._app.off(i,s)}))}}x.i=!0;const O="//",_="///",k=t=>{if(e.lastUrl!==t)if(e.lastUrl=t,t||(t="#"),t.startsWith("#")){const[i,...s]=t.split("/");e.run(i,...s)||e.run(_,i,...s),e.run(O,i,...s)}else if(t.startsWith("/")){const[i,s,...n]=t.split("/");e.run("/"+s,...n)||e.run(_,"/"+s,...n),e.run(O,"/"+s,...n)}else e.run(t)||e.run(_,t),e.run(O,t)};if(!e.start){e.version="3.35.0",e.h=e.createElement=a,e.render=d,e.Fragment=h,e.webComponent=$,e.safeHTML=y,e.start=(t,i,s,e,n)=>{const o=Object.assign({render:!0,global_event:!0},n),r=new x(i,s,e);return n&&n.rendered&&(r.rendered=n.rendered),n&&n.mounted&&(r.mounted=n.mounted),r.start(t,o),r};const t=t=>{};e.on("debug",(i=>t)),e.on(O,t),e.on(_,t),e.route=k,e.on("route",(t=>e.route&&e.route(t))),"object"==typeof document&&document.addEventListener("DOMContentLoaded",(()=>{const t=document.body.hasAttribute("apprun-no-init")||e["no-init-route"]||!1,i=e.find("#")||e.find("#/")||!1;window.addEventListener("hashchange",(()=>k(location.hash))),window.addEventListener("popstate",(()=>k(location.pathname))),i?t&&k(location.hash):(t&&k(location.pathname),document.body.addEventListener("click",(t=>{const i=t.target,s="A"===i.tagName?i:i.closest("a");s&&s.origin===location.origin&&(t.preventDefault(),history.pushState(null,"",s.pathname),k(s.pathname))})))})),"object"==typeof window&&(window.Component=x,window._React=window.React,window.React=e,window.on=function(t,i={}){return function(s,e){const n=t?t.toString():e;S.defineMetadata(`apprun-update:${n}`,{name:n,key:e,options:i},s)}},window.customElement=function(t,i){return function(s){return $(t,s,i),s}},window.safeHTML=y),e.use_render=(t,i=0)=>{e.render=0===i?(i,s)=>t(s,i):(i,s)=>t(i,s)},e.use_react=(t,i)=>{e.h=e.createElement=t.createElement,e.Fragment=t.Fragment,e.render=(t,s)=>i.render(s,t),t.version&&t.version.startsWith("18")&&(e.render=(t,s)=>{t&&s&&(t._root||(t._root=i.createRoot(t)),t._root.render(s))})}}
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2019 Google LLC
|