simplyview 3.0.1 → 3.0.2

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.
@@ -792,9 +792,7 @@
792
792
  }
793
793
  }
794
794
  if (!matches) {
795
- if (currentItem) {
796
- return t;
797
- }
795
+ return t;
798
796
  }
799
797
  };
800
798
  let template = Array.from(templates).find(templateMatches);
@@ -888,26 +886,26 @@
888
886
  let lastKey = 0;
889
887
  let skipped = 0;
890
888
  context.list = value;
891
- for (let item of items) {
892
- let currentKey = parseInt(item.getAttribute(attribute + "-key"));
889
+ for (let item2 of items) {
890
+ let currentKey = parseInt(item2.getAttribute(attribute + "-key"));
893
891
  if (currentKey > lastKey) {
894
892
  context.index = lastKey;
895
- el.insertBefore(this.applyTemplate(context), item);
893
+ el.insertBefore(this.applyTemplate(context), item2);
896
894
  } else if (currentKey < lastKey) {
897
- item.remove();
895
+ item2.remove();
898
896
  } else {
899
- let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
900
- if (item.matches(`[${attribute}]`)) {
901
- bindings.unshift(item);
897
+ let bindings = Array.from(item2.querySelectorAll(`[${attribute}]`));
898
+ if (item2.matches(`[${attribute}]`)) {
899
+ bindings.unshift(item2);
902
900
  }
903
901
  let needsReplacement = bindings.find((b) => {
904
902
  let databind = b.getAttribute(attribute);
905
903
  return databind.substr(0, 5) !== "#root" && databind.substr(0, path.length) !== path;
906
904
  });
907
905
  if (!needsReplacement) {
908
- if (item.$bindTemplate) {
906
+ if (item2.$bindTemplate) {
909
907
  let newTemplate = this.findTemplate(templates, value[lastKey]);
910
- if (newTemplate != item.$bindTemplate) {
908
+ if (newTemplate != item2.$bindTemplate) {
911
909
  needsReplacement = true;
912
910
  if (!newTemplate) {
913
911
  skipped++;
@@ -917,7 +915,7 @@
917
915
  }
918
916
  if (needsReplacement) {
919
917
  context.index = lastKey;
920
- el.replaceChild(this.applyTemplate(context), item);
918
+ el.replaceChild(this.applyTemplate(context), item2);
921
919
  }
922
920
  }
923
921
  lastKey++;
@@ -949,59 +947,38 @@
949
947
  const value = context.value;
950
948
  const attribute = this.options.attribute;
951
949
  context.list = value;
952
- let list = Object.entries(value);
953
- let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
954
- let current = 0;
955
- let skipped = 0;
956
- for (let item of items) {
957
- if (current >= list.length) {
958
- break;
959
- }
960
- let key = list[current][0];
961
- current++;
962
- let keypath = path + "." + key;
963
- let needsReplacement;
964
- const databind = item.getAttribute(attribute);
965
- if (databind && databind.substr(0, keypath.length) != keypath) {
966
- needsReplacement = true;
967
- } else {
968
- let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
969
- needsReplacement = bindings.find((b) => {
970
- const db = b.getAttribute(attribute);
971
- return db.substr(0, 5) !== "#root" && db.substr(0, keypath.length) !== keypath;
972
- });
973
- if (!needsReplacement) {
974
- if (item.$bindTemplate) {
975
- let newTemplate = this.findTemplate(templates, value[key]);
976
- if (newTemplate != item.$bindTemplate) {
977
- needsReplacement = true;
978
- if (!newTemplate) {
979
- skipped++;
980
- }
981
- }
950
+ let items = Array.from(el.querySelectorAll(":scope > [" + attribute + "-key]"));
951
+ for (let key in context.list) {
952
+ context.index = key;
953
+ let item2 = items.shift();
954
+ if (!item2) {
955
+ el.appendChild(this.applyTemplate(context));
956
+ continue;
957
+ }
958
+ if (item2.getAttribute[attribute + "-key"] != key) {
959
+ items.unshift(item2);
960
+ let outOfOrderItem = el.querySelector(":scope > [" + attribute + '-key="' + key + '"]');
961
+ if (!outOfOrderItem) {
962
+ let clone = this.applyTemplate(context);
963
+ if (clone.firstElementChild) {
964
+ el.insertBefore(clone, item2);
982
965
  }
966
+ continue;
967
+ } else {
968
+ el.insertBefore(outOfOrderItem, item2);
969
+ item2 = outOfOrderItem;
970
+ items = items.filter((i) => i != outOfOrderItem);
983
971
  }
984
972
  }
985
- if (needsReplacement) {
986
- context.index = key;
973
+ let newTemplate = this.findTemplate(templates, value[key]);
974
+ if (newTemplate != item2.$bindTemplate) {
987
975
  let clone = this.applyTemplate(context);
988
- el.replaceChild(clone, item);
976
+ el.replaceChild(clone, item2);
989
977
  }
990
978
  }
991
- items = el.querySelectorAll(":scope > [" + attribute + "-key]");
992
- let length = items.length + skipped;
993
- if (length > list.length) {
994
- while (length > list.length) {
995
- let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
996
- child?.remove();
997
- length--;
998
- }
999
- } else if (length < list.length) {
1000
- while (length < list.length) {
1001
- context.index = list[length][0];
1002
- el.appendChild(this.applyTemplate(context));
1003
- length++;
1004
- }
979
+ while (items.length) {
980
+ item = items.shift();
981
+ item.remove();
1005
982
  }
1006
983
  }
1007
984
  function transformLiteralByTemplates(context) {
@@ -1080,7 +1057,11 @@
1080
1057
  const el = context.element;
1081
1058
  const value = context.value;
1082
1059
  if (!matchValue(el.innerHTML, value)) {
1083
- el.innerHTML = "" + value;
1060
+ if (typeof value == "undefined" || value == null) {
1061
+ el.innerHTML = "";
1062
+ } else {
1063
+ el.innerHTML = "" + value;
1064
+ }
1084
1065
  }
1085
1066
  }
1086
1067
 
@@ -1,2 +1,2 @@
1
- (()=>{function q(t){return new M(t)}var M=class{constructor(e={}){this.root=e.root||"/",this.app=e.app,this.clear(),e.routes&&this.load(e.routes)}load(e){Y(e,this.routeInfo)}clear(){this.routeInfo=[],this.listeners={match:{},call:{},finish:{}}}match(e,n){let r={path:e,options:n};r=this.runListeners("match",r),e=r.path?r.path:e;let i;if(!e)return this.match(document.location.pathname+document.location.hash)?!0:this.match(document.location.pathname);e=b(e);for(let l of this.routeInfo)if(i=l.match.exec(e),i&&i.length){var s={};return l.params.forEach((f,o)=>{f=="*"&&(f="remainder"),s[f]=i[o+1]}),Object.assign(s,n),r.route=l,r.params=s,r=this.runListeners("call",r),s=r.params?r.params:s,r.result=l.action.call(l,s),this.runListeners("finish",r),r.result}return e&&e[e.length-1]!="/"?this.match(e+"/",n):!1}runListeners(e,n){if(Object.keys(this.listeners[e]))return Object.keys(this.listeners[e]).forEach(r=>{var i=$(r);if(i.exec(n.path)){var s;for(let l of this.listeners[e][r])s=l.call(this.app,n),s&&(n=s)}}),n}handleEvents(){globalThis.addEventListener("popstate",()=>{this.match(b(document.location.pathname+document.location.hash,this.root))===!1&&this.match(b(document.location.pathname,this.root))}),globalThis.document.addEventListener("click",e=>{if(!e.ctrlKey&&e.which==1){for(var n=e.target;n&&n.tagName!="A";)n=n.parentElement;if(n&&n.pathname&&n.hostname==globalThis.location.hostname&&!n.link&&!n.dataset.simplyCommand){let r=b(n.pathname+n.hash,this.root);if(this.has(r)||(r=b(n.pathname,this.root)),this.has(r)){let i=this.runListeners("goto",{path:r});return i.path&&this.goto(i.path),e.preventDefault(),!1}}}})}goto(e){return history.pushState({},"",X(e)),this.match(e)}has(e){e=b(e,this.root);for(let r of this.routeInfo){var n=r.match.exec(e);if(n&&n.length)return!0}return!1}addListener(e,n,r){if(["goto","match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][n]||(this.listeners[e][n]=[]),this.listeners[e][n].push(r)}removeListener(e,n,r){if(["match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][n]&&(this.listeners[e][n]=this.listeners[e][n].filter(i=>i!=r))}init(e){e.root&&(this.root=e.root)}};function b(t,e="/"){return(t.substring(0,e.length)==e||e[e.length-1]=="/"&&t.length==e.length-1&&t==e.substring(0,t.length))&&(t=t.substring(e.length)),t[0]!="/"&&t[0]!="#"&&(t="/"+t),t}function X(t,e){return t=b(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),e+t}function $(t){return new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)"))}function Y(t){let e=[],n=Object.keys(t),r=/:(\w+|\*)/g;for(let i of n){let s=[],l=[];do s=r.exec(i),s&&l.push(s[1]);while(s);e.push({match:$(i),params:l,action:t[i]})}return e}var P=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),this.$handlers=e.handlers||_,e.commands&&Object.assign(this,e.commands);let n=r=>{let i=Z(r,this.$handlers);if(!i)return;if(!this[i.name]){console.error("simply.command: undefined command "+i.name,i.source);return}if(this[i.name].call(e.app,i.source,i.value)===!1)return r.preventDefault(),r.stopPropagation(),!1};e.app.container.addEventListener("click",n),e.app.container.addEventListener("submit",n),e.app.container.addEventListener("change",n),e.app.container.addEventListener("input",n)}};function K(t={}){return new P(t)}function Z(t,e){var n=t.target.closest("[data-simply-command]");if(n){for(let r of e)if(n.matches(r.match))return r.check(n,t)?{name:n.dataset.simplyCommand,source:n,value:r.get(n)}:null}return null}var _=[{match:"input,select,textarea",get:function(t){if(t.tagName==="SELECT"&&t.multiple){let e=[];for(let n of t.options)n.selected&&e.push(n.value);return e}return t.dataset.simplyValue||t.value},check:function(t,e){return e.type=="change"||t.dataset.simplyImmediate&&e.type=="input"}},{match:"a,button",get:function(t){return t.dataset.simplyValue||t.href||t.value},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}},{match:"form",get:function(t){let e={};for(let n of Array.from(t.elements)){if(n.tagName=="INPUT"&&(n.type=="checkbox"||n.type=="radio")&&!n.checked)return;e[n.name]&&!Array.isArray(e[n.name])&&(e[n.name]=[e[n.name]]),Array.isArray(e[n.name])?e[n.name].push(n.value):e[n.name]=n.value}return e},check:function(t,e){return e.type=="submit"}},{match:"*",get:function(t){return t.dataset.simplyValue},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}}];function U(t){if(t.app){let e={get:(n,r)=>n[r].bind(t.app)};return new Proxy(t.actions,e)}else return t}var O=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),Object.assign(this,e.keys);let n=r=>{if(r.isComposing||r.keyCode===229||r.defaultPrevented||!r.target)return;let i="default";r.target.closest("[data-simply-keyboard]")&&(i=r.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard);let s="";r.ctrlKey&&r.keyCode!=17&&(s+="Control+"),r.metaKey&&r.keyCode!=224&&(s+="Meta+"),r.altKey&&r.keyCode!=18&&(s+="Alt+"),r.shiftKey&&r.keyCode!=16&&(s+="Shift+"),s+=r.key,this[i]&&this[i][s]&&this[i][s].call(e.app,r)};e.app.container.addEventListener("keydown",n)}};function D(t={}){return new O(t)}var I=Symbol("iterate");Symbol.xRay||(Symbol.xRay=Symbol("xRay"));var ee={get:(t,e,n)=>{if(e===Symbol.xRay)return t;let r=t?.[e];return B(n,e),typeof r=="function"?Array.isArray(t)?(...i)=>{let s=t.length,l=r.apply(n,i);return s!=t.length&&w(n,y("length",{was:s,now:t.length})),l}:t instanceof Set||t instanceof Map?(...i)=>{let s=t.size,l=r.apply(t,i);return s!=t.size&&w(n,y("size",{was:s,now:t.size})),["set","add","clear","delete"].includes(e)&&w(n,y({entries:{},forEach:{},has:{},keys:{},values:{},[Symbol.iterator]:{}})),l}:t instanceof HTMLElement||t instanceof Number||t instanceof String||t instanceof Boolean?r.bind(t):r.bind(n):r&&typeof r=="object"?C(r):r},set:(t,e,n,r)=>{n=n?.[Symbol.xRay]||n;let i=t[e];return i!==n&&(t[e]=n,w(r,y(e,{was:i,now:n}))),typeof i>"u"&&w(r,y(I,{})),!0},has:(t,e)=>{let n=g.get(t);return n&&B(n,e),Object.hasOwn(t,e)},deleteProperty:(t,e)=>{if(typeof t[e]<"u"){let n=t[e];delete t[e];let r=g.get(t);w(r,y(e,{delete:!0,was:n}))}return!0},defineProperty:(t,e,n)=>{if(typeof t[e]>"u"){let r=g.get(t);w(r,y(I,{}))}return Object.defineProperty(t,e,n)},ownKeys:t=>{let e=g.get(t);return B(e,I),Reflect.ownKeys(t)}},g=new WeakMap;function C(t){return g.has(t)||g.set(t,new Proxy(t,ee)),g.get(t)}var V=new Set,te=0;function w(t,e={}){let n=[];if(e.forEach((r,i)=>{let s=ie(t,i);if(s?.length){for(let l of s)ne(l,y(i,r));n=n.concat(s)}}),n=new Set(n.filter(Boolean)),n)if(te)V=V.union(n);else{let r=k[k.length-1];for(let i of Array.from(n))i!=r&&i?.needsUpdate&&i(),re(i)}}function y(t,e){let n=new Map;if(typeof t=="object")for(let r in t)n.set(r,t[r]);else n.set(t,e);return n}function ne(t,e){t.context?e.forEach((n,r)=>{t.context.set(r,n)}):t.context=e,t.needsUpdate=!0}function re(t){delete t.context,delete t.needsUpdate}function B(t,e){let n=k[k.length-1];n&&se(t,e,n)}var v=new WeakMap,S=new WeakMap;function ie(t,e){let n=v.get(t);return n?Array.from(n.get(e)||[]):[]}function se(t,e,n){v.has(t)||v.set(t,new Map);let r=v.get(t);r.has(e)||r.set(e,new Set),r.get(e).add(n),S.has(n)||S.set(n,new Map);let i=S.get(n);i.has(e)||i.set(e,new Set),i.get(e).add(t)}function F(t){let e=S.get(t);e&&e.forEach(n=>{n.forEach(r=>{let i=v.get(r);i.has(n)&&i.get(n).delete(t)})})}var k=[],z=[],W=new WeakMap,L=[];function G(t){let e=W.get(t)?.deref();if(!e)return;F(e);let n=e.fn;g.remove(n),W.delete(t)}function J(t,e){if(z.findIndex(l=>t==l)!==-1)throw new Error("Recursive update() call",{cause:t});z.push(t);let n=g.get(t);n||(n=C({current:null}),g.set(t,n));let r=!1,i=!0;return function l(){if(L.findIndex(o=>o==n)!==-1)throw new Error("Cyclical dependency in update() call",{cause:t});if(r&&r>Date.now()){i=!0;return}F(l),k.push(l),L.push(n);let f;try{f=t(l,k,L)}finally{i=!1,k.pop(),L.pop(),f instanceof Promise?f.then(o=>{n.current=o}):n.current=f}r=Date.now()+e,globalThis.setTimeout(()=>{i&&l()},e)}(),n}var x=class{constructor(e){this.bindings=new Map;let n={container:document.body,attribute:"data-bind",transformers:[],defaultTransformers:[le]};if(!e?.root)throw new Error("bind needs at least options.root set");this.options=Object.assign({},n,e);let r=this.options.attribute,i=u=>{this.bindings.set(u,J(()=>{let c={templates:u.querySelectorAll(":scope > template"),path:this.getBindingPath(u)};c.value=R(this.options.root,c.path),c.element=u,s(c)},100))},s=u=>{let c=this.options.defaultTransformers||[];u.element.dataset.transform&&u.element.dataset.transform.split(" ").filter(Boolean).forEach(h=>{this.options.transformers[h]?c.push(this.options.transformers[h]):console.warn("No transformer with name "+h+" configured",{cause:u.element})});let a;for(let h of c)a=((m,p)=>d=>p.call(this,d,m))(a,h);a(u)},l=u=>{for(let c of u)i(c)},f=u=>{for(let c of u)if(c.type=="childList"&&c.addedNodes){for(let a of c.addedNodes)if(a instanceof HTMLElement){let h=Array.from(a.querySelectorAll(`[${r}]`));a.matches(`[${r}]`)&&h.unshift(a),h.length&&l(h)}}};this.observer=new MutationObserver(u=>{f(u)}),this.observer.observe(e.container,{subtree:!0,childList:!0});let o=this.options.container.querySelectorAll("["+this.options.attribute+"]:not(template)");o.length&&l(o)}applyTemplate(e){let n=e.path,r=e.templates,i=e.list,s=e.index,l=e.parent,f=i?i[s]:e.value,o=this.findTemplate(r,f);if(!o){let h=new DocumentFragment;return h.innerHTML="<!-- no matching template -->",h}let u=o.content.cloneNode(!0);if(!u.children?.length)throw new Error("template must contain a single html element",{cause:o});if(u.children.length>1)throw new Error("template must contain a single root node",{cause:o});let c=u.querySelectorAll("["+this.options.attribute+"]"),a=this.options.attribute;for(let h of c){let m=h.getAttribute(a);m.substring(0,6)=="#root."?h.setAttribute(a,m.substring(6)):m=="#value"&&s!=null?h.setAttribute(a,n+"."+s):s!=null?h.setAttribute(a,n+"."+s+"."+m):h.setAttribute(a,l+"."+m)}return typeof s<"u"&&u.children[0].setAttribute(a+"-key",s),u.children[0].$bindTemplate=o,u}getBindingPath(e){return e.getAttribute(this.options.attribute)}findTemplate(e,n){let r=l=>{let f=this.getBindingPath(l),o;f?f.substr(0,6)=="#root."?o=R(this.options.root,f):o=R(n,f):o=n;let u=""+o,c=l.getAttribute(this.options.attribute+"-match");if(c){if(c==="#empty"&&!o)return l;if(c==="#notempty"&&o||u.match(c))return l}if(!c&&o)return l},i=Array.from(e).find(r),s=i?.getAttribute("rel");if(s){let l=document.querySelector("template#"+s);if(!l)throw new Error("Could not find template with id "+s);i=l}return i}destroy(){this.bindings.forEach(e=>{G(e)}),this.bindings=new Map,this.observer.disconnect()}};function Q(t){return new x(t)}function E(t,e){return t=="#empty"&&!e||e=="#empty"&&!t||""+t==""+e}function R(t,e){let n=e.split("."),r=t,i,s;for(;n.length&&r;){if(i=n.shift(),i=="#key")return s;if(i=="#value")return r;i=="#root"?r=t:(i=decodeURIComponent(i),r=r[i],s=i)}return r}function le(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute;return Array.isArray(s)&&n?.length?ae.call(this,t):typeof s=="object"&&n?.length?oe.call(this,t):n?.length?fe.call(this,t):e.tagName=="INPUT"?ce.call(this,t):e.tagName=="BUTTON"?ue.call(this,t):e.tagName=="SELECT"?he.call(this,t):e.tagName=="A"?me.call(this,t):de.call(this,t),t}function ae(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute,f=e.querySelectorAll(":scope > ["+l+"-key]"),o=0,u=0;t.list=s;for(let a of f){let h=parseInt(a.getAttribute(l+"-key"));if(h>o)t.index=o,e.insertBefore(this.applyTemplate(t),a);else if(h<o)a.remove();else{let m=Array.from(a.querySelectorAll(`[${l}]`));a.matches(`[${l}]`)&&m.unshift(a);let p=m.find(d=>{let T=d.getAttribute(l);return T.substr(0,5)!=="#root"&&T.substr(0,i.length)!==i});if(!p&&a.$bindTemplate){let d=this.findTemplate(n,s[o]);d!=a.$bindTemplate&&(p=!0,d||u++)}p&&(t.index=o,e.replaceChild(this.applyTemplate(t),a))}if(o++,o>=s.length)break}f=e.querySelectorAll(":scope > ["+l+"-key]");let c=f.length+u;if(c>s.length)for(;c>s.length;)e.querySelectorAll(":scope > :not(template)")?.[c-1]?.remove(),c--;else if(c<s.length)for(;c<s.length;)t.index=c,e.appendChild(this.applyTemplate(t)),c++}function oe(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute;t.list=s;let f=Object.entries(s),o=e.querySelectorAll(":scope > ["+l+"-key]"),u=0,c=0;for(let h of o){if(u>=f.length)break;let m=f[u][0];u++;let p=i+"."+m,d,T=h.getAttribute(l);if(T&&T.substr(0,p.length)!=p)d=!0;else if(d=Array.from(h.querySelectorAll(`[${l}]`)).find(A=>{let j=A.getAttribute(l);return j.substr(0,5)!=="#root"&&j.substr(0,p.length)!==p}),!d&&h.$bindTemplate){let A=this.findTemplate(n,s[m]);A!=h.$bindTemplate&&(d=!0,A||c++)}if(d){t.index=m;let N=this.applyTemplate(t);e.replaceChild(N,h)}}o=e.querySelectorAll(":scope > ["+l+"-key]");let a=o.length+c;if(a>f.length)for(;a>f.length;)e.querySelectorAll(":scope > :not(template)")?.[a-1]?.remove(),a--;else if(a<f.length)for(;a<f.length;)t.index=f[a][0],e.appendChild(this.applyTemplate(t)),a++}function fe(t){let e=t.element,n=t.templates,r=t.value,i=this.options.attribute,s=e.querySelector(":scope > :not(template)"),l=this.findTemplate(n,r);if(t.parent=e.parentElement?.closest(`[${i}]`)?.getAttribute(i)||"#root",s)if(l){if(s?.$bindTemplate!=l){let f=this.applyTemplate(t);e.replaceChild(f,s)}}else e.removeChild(s);else if(l){let f=this.applyTemplate(t);e.appendChild(f)}}function ce(t){let e=t.element,n=t.value;e.type=="checkbox"||e.type=="radio"?E(e.value,n)?e.checked=!0:e.checked=!1:E(e.value,n)||(e.value=""+n)}function ue(t){let e=t.element,n=t.value;E(e.value,n)||(e.value=""+n)}function he(t){let e=t.element,n=t.value;if(e.multiple){if(Array.isArray(n))for(let r of e.options)n.indexOf(r.value)===!1?r.selected=!1:r.selected=!0}else{let r=e.options.find(i=>E(i.value,n));r&&(r.selected=!0)}}function me(t){let e=t.element,n=t.value;n?.innerHTML&&!E(e.innerHTML,n.innerHTML)&&(e.innerHTML=""+n.innerHTML),n?.href&&!E(e.href,n.href)&&(e.href=""+n.href)}function de(t){let e=t.element,n=t.value;E(e.innerHTML,n)||(e.innerHTML=""+n)}var H=class{constructor(e={}){this.container=e.container||document.body,e.state||(e.state={}),this.state=C(e.state),e.commands&&(this.commands=K({app:this,container:this.container,commands:e.commands})),e.keys&&(this.keys=D({app:this,keys:e.keys})),e.routes&&(this.routes=q({app:this,routes:e.routes})),e.actions&&(this.actions=U({app:this,actions:e.actions}));let n={container:this.container,root:this.state};e.defaultTransformers&&(n.defaultTransformers=e.defaultTransformers),e.transformers&&(n.transformers=e.transformers),this.bind=Q(n)}};function Me(t={}){return new H(t)}})();
1
+ (()=>{function R(t){return new C(t)}var C=class{constructor(e={}){this.root=e.root||"/",this.app=e.app,this.clear(),e.routes&&this.load(e.routes)}load(e){J(e,this.routeInfo)}clear(){this.routeInfo=[],this.listeners={match:{},call:{},finish:{}}}match(e,n){let r={path:e,options:n};r=this.runListeners("match",r),e=r.path?r.path:e;let i;if(!e)return this.match(document.location.pathname+document.location.hash)?!0:this.match(document.location.pathname);e=y(e);for(let l of this.routeInfo)if(i=l.match.exec(e),i&&i.length){var s={};return l.params.forEach((c,o)=>{c=="*"&&(c="remainder"),s[c]=i[o+1]}),Object.assign(s,n),r.route=l,r.params=s,r=this.runListeners("call",r),s=r.params?r.params:s,r.result=l.action.call(l,s),this.runListeners("finish",r),r.result}return e&&e[e.length-1]!="/"?this.match(e+"/",n):!1}runListeners(e,n){if(Object.keys(this.listeners[e]))return Object.keys(this.listeners[e]).forEach(r=>{var i=j(r);if(i.exec(n.path)){var s;for(let l of this.listeners[e][r])s=l.call(this.app,n),s&&(n=s)}}),n}handleEvents(){globalThis.addEventListener("popstate",()=>{this.match(y(document.location.pathname+document.location.hash,this.root))===!1&&this.match(y(document.location.pathname,this.root))}),globalThis.document.addEventListener("click",e=>{if(!e.ctrlKey&&e.which==1){for(var n=e.target;n&&n.tagName!="A";)n=n.parentElement;if(n&&n.pathname&&n.hostname==globalThis.location.hostname&&!n.link&&!n.dataset.simplyCommand){let r=y(n.pathname+n.hash,this.root);if(this.has(r)||(r=y(n.pathname,this.root)),this.has(r)){let i=this.runListeners("goto",{path:r});return i.path&&this.goto(i.path),e.preventDefault(),!1}}}})}goto(e){return history.pushState({},"",G(e)),this.match(e)}has(e){e=y(e,this.root);for(let r of this.routeInfo){var n=r.match.exec(e);if(n&&n.length)return!0}return!1}addListener(e,n,r){if(["goto","match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][n]||(this.listeners[e][n]=[]),this.listeners[e][n].push(r)}removeListener(e,n,r){if(["match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][n]&&(this.listeners[e][n]=this.listeners[e][n].filter(i=>i!=r))}init(e){e.root&&(this.root=e.root)}};function y(t,e="/"){return(t.substring(0,e.length)==e||e[e.length-1]=="/"&&t.length==e.length-1&&t==e.substring(0,t.length))&&(t=t.substring(e.length)),t[0]!="/"&&t[0]!="#"&&(t="/"+t),t}function G(t,e){return t=y(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),e+t}function j(t){return new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)"))}function J(t){let e=[],n=Object.keys(t),r=/:(\w+|\*)/g;for(let i of n){let s=[],l=[];do s=r.exec(i),s&&l.push(s[1]);while(s);e.push({match:j(i),params:l,action:t[i]})}return e}var S=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),this.$handlers=e.handlers||X,e.commands&&Object.assign(this,e.commands);let n=r=>{let i=Q(r,this.$handlers);if(!i)return;if(!this[i.name]){console.error("simply.command: undefined command "+i.name,i.source);return}if(this[i.name].call(e.app,i.source,i.value)===!1)return r.preventDefault(),r.stopPropagation(),!1};e.app.container.addEventListener("click",n),e.app.container.addEventListener("submit",n),e.app.container.addEventListener("change",n),e.app.container.addEventListener("input",n)}};function x(t={}){return new S(t)}function Q(t,e){var n=t.target.closest("[data-simply-command]");if(n){for(let r of e)if(n.matches(r.match))return r.check(n,t)?{name:n.dataset.simplyCommand,source:n,value:r.get(n)}:null}return null}var X=[{match:"input,select,textarea",get:function(t){if(t.tagName==="SELECT"&&t.multiple){let e=[];for(let n of t.options)n.selected&&e.push(n.value);return e}return t.dataset.simplyValue||t.value},check:function(t,e){return e.type=="change"||t.dataset.simplyImmediate&&e.type=="input"}},{match:"a,button",get:function(t){return t.dataset.simplyValue||t.href||t.value},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}},{match:"form",get:function(t){let e={};for(let n of Array.from(t.elements)){if(n.tagName=="INPUT"&&(n.type=="checkbox"||n.type=="radio")&&!n.checked)return;e[n.name]&&!Array.isArray(e[n.name])&&(e[n.name]=[e[n.name]]),Array.isArray(e[n.name])?e[n.name].push(n.value):e[n.name]=n.value}return e},check:function(t,e){return e.type=="submit"}},{match:"*",get:function(t){return t.dataset.simplyValue},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}}];function q(t){if(t.app){let e={get:(n,r)=>n[r].bind(t.app)};return new Proxy(t.actions,e)}else return t}var M=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),Object.assign(this,e.keys);let n=r=>{if(r.isComposing||r.keyCode===229||r.defaultPrevented||!r.target)return;let i="default";r.target.closest("[data-simply-keyboard]")&&(i=r.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard);let s="";r.ctrlKey&&r.keyCode!=17&&(s+="Control+"),r.metaKey&&r.keyCode!=224&&(s+="Meta+"),r.altKey&&r.keyCode!=18&&(s+="Alt+"),r.shiftKey&&r.keyCode!=16&&(s+="Shift+"),s+=r.key,this[i]&&this[i][s]&&this[i][s].call(e.app,r)};e.app.container.addEventListener("keydown",n)}};function K(t={}){return new M(t)}var O=Symbol("iterate");Symbol.xRay||(Symbol.xRay=Symbol("xRay"));var Y={get:(t,e,n)=>{if(e===Symbol.xRay)return t;let r=t?.[e];return P(n,e),typeof r=="function"?Array.isArray(t)?(...i)=>{let s=t.length,l=r.apply(n,i);return s!=t.length&&g(n,p("length",{was:s,now:t.length})),l}:t instanceof Set||t instanceof Map?(...i)=>{let s=t.size,l=r.apply(t,i);return s!=t.size&&g(n,p("size",{was:s,now:t.size})),["set","add","clear","delete"].includes(e)&&g(n,p({entries:{},forEach:{},has:{},keys:{},values:{},[Symbol.iterator]:{}})),l}:t instanceof HTMLElement||t instanceof Number||t instanceof String||t instanceof Boolean?r.bind(t):r.bind(n):r&&typeof r=="object"?L(r):r},set:(t,e,n,r)=>{n=n?.[Symbol.xRay]||n;let i=t[e];return i!==n&&(t[e]=n,g(r,p(e,{was:i,now:n}))),typeof i>"u"&&g(r,p(O,{})),!0},has:(t,e)=>{let n=d.get(t);return n&&P(n,e),Object.hasOwn(t,e)},deleteProperty:(t,e)=>{if(typeof t[e]<"u"){let n=t[e];delete t[e];let r=d.get(t);g(r,p(e,{delete:!0,was:n}))}return!0},defineProperty:(t,e,n)=>{if(typeof t[e]>"u"){let r=d.get(t);g(r,p(O,{}))}return Object.defineProperty(t,e,n)},ownKeys:t=>{let e=d.get(t);return P(e,O),Reflect.ownKeys(t)}},d=new WeakMap;function L(t){return d.has(t)||d.set(t,new Proxy(t,Y)),d.get(t)}var $=new Set,Z=0;function g(t,e={}){let n=[];if(e.forEach((r,i)=>{let s=te(t,i);if(s?.length){for(let l of s)_(l,p(i,r));n=n.concat(s)}}),n=new Set(n.filter(Boolean)),n)if(Z)$=$.union(n);else{let r=b[b.length-1];for(let i of Array.from(n))i!=r&&i?.needsUpdate&&i(),ee(i)}}function p(t,e){let n=new Map;if(typeof t=="object")for(let r in t)n.set(r,t[r]);else n.set(t,e);return n}function _(t,e){t.context?e.forEach((n,r)=>{t.context.set(r,n)}):t.context=e,t.needsUpdate=!0}function ee(t){delete t.context,delete t.needsUpdate}function P(t,e){let n=b[b.length-1];n&&ne(t,e,n)}var E=new WeakMap,A=new WeakMap;function te(t,e){let n=E.get(t);return n?Array.from(n.get(e)||[]):[]}function ne(t,e,n){E.has(t)||E.set(t,new Map);let r=E.get(t);r.has(e)||r.set(e,new Set),r.get(e).add(n),A.has(n)||A.set(n,new Map);let i=A.get(n);i.has(e)||i.set(e,new Set),i.get(e).add(t)}function V(t){let e=A.get(t);e&&e.forEach(n=>{n.forEach(r=>{let i=E.get(r);i.has(n)&&i.get(n).delete(t)})})}var b=[],U=[],D=new WeakMap,v=[];function z(t){let e=D.get(t)?.deref();if(!e)return;V(e);let n=e.fn;d.remove(n),D.delete(t)}function W(t,e){if(U.findIndex(l=>t==l)!==-1)throw new Error("Recursive update() call",{cause:t});U.push(t);let n=d.get(t);n||(n=L({current:null}),d.set(t,n));let r=!1,i=!0;return function l(){if(v.findIndex(o=>o==n)!==-1)throw new Error("Cyclical dependency in update() call",{cause:t});if(r&&r>Date.now()){i=!0;return}V(l),b.push(l),v.push(n);let c;try{c=t(l,b,v)}finally{i=!1,b.pop(),v.pop(),c instanceof Promise?c.then(o=>{n.current=o}):n.current=c}r=Date.now()+e,globalThis.setTimeout(()=>{i&&l()},e)}(),n}var I=class{constructor(e){this.bindings=new Map;let n={container:document.body,attribute:"data-bind",transformers:[],defaultTransformers:[re]};if(!e?.root)throw new Error("bind needs at least options.root set");this.options=Object.assign({},n,e);let r=this.options.attribute,i=a=>{this.bindings.set(a,W(()=>{let u={templates:a.querySelectorAll(":scope > template"),path:this.getBindingPath(a)};u.value=B(this.options.root,u.path),u.element=a,s(u)},100))},s=a=>{let u=this.options.defaultTransformers||[];a.element.dataset.transform&&a.element.dataset.transform.split(" ").filter(Boolean).forEach(h=>{this.options.transformers[h]?u.push(this.options.transformers[h]):console.warn("No transformer with name "+h+" configured",{cause:a.element})});let f;for(let h of u)f=((m,T)=>k=>T.call(this,k,m))(f,h);f(a)},l=a=>{for(let u of a)i(u)},c=a=>{for(let u of a)if(u.type=="childList"&&u.addedNodes){for(let f of u.addedNodes)if(f instanceof HTMLElement){let h=Array.from(f.querySelectorAll(`[${r}]`));f.matches(`[${r}]`)&&h.unshift(f),h.length&&l(h)}}};this.observer=new MutationObserver(a=>{c(a)}),this.observer.observe(e.container,{subtree:!0,childList:!0});let o=this.options.container.querySelectorAll("["+this.options.attribute+"]:not(template)");o.length&&l(o)}applyTemplate(e){let n=e.path,r=e.templates,i=e.list,s=e.index,l=e.parent,c=i?i[s]:e.value,o=this.findTemplate(r,c);if(!o){let h=new DocumentFragment;return h.innerHTML="<!-- no matching template -->",h}let a=o.content.cloneNode(!0);if(!a.children?.length)throw new Error("template must contain a single html element",{cause:o});if(a.children.length>1)throw new Error("template must contain a single root node",{cause:o});let u=a.querySelectorAll("["+this.options.attribute+"]"),f=this.options.attribute;for(let h of u){let m=h.getAttribute(f);m.substring(0,6)=="#root."?h.setAttribute(f,m.substring(6)):m=="#value"&&s!=null?h.setAttribute(f,n+"."+s):s!=null?h.setAttribute(f,n+"."+s+"."+m):h.setAttribute(f,l+"."+m)}return typeof s<"u"&&a.children[0].setAttribute(f+"-key",s),a.children[0].$bindTemplate=o,a}getBindingPath(e){return e.getAttribute(this.options.attribute)}findTemplate(e,n){let r=l=>{let c=this.getBindingPath(l),o;c?c.substr(0,6)=="#root."?o=B(this.options.root,c):o=B(n,c):o=n;let a=""+o,u=l.getAttribute(this.options.attribute+"-match");if(u){if(u==="#empty"&&!o)return l;if(u==="#notempty"&&o||a.match(u))return l}if(!u)return l},i=Array.from(e).find(r),s=i?.getAttribute("rel");if(s){let l=document.querySelector("template#"+s);if(!l)throw new Error("Could not find template with id "+s);i=l}return i}destroy(){this.bindings.forEach(e=>{z(e)}),this.bindings=new Map,this.observer.disconnect()}};function F(t){return new I(t)}function w(t,e){return t=="#empty"&&!e||e=="#empty"&&!t||""+t==""+e}function B(t,e){let n=e.split("."),r=t,i,s;for(;n.length&&r;){if(i=n.shift(),i=="#key")return s;if(i=="#value")return r;i=="#root"?r=t:(i=decodeURIComponent(i),r=r[i],s=i)}return r}function re(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute;return Array.isArray(s)&&n?.length?ie.call(this,t):typeof s=="object"&&n?.length?se.call(this,t):n?.length?le.call(this,t):e.tagName=="INPUT"?ae.call(this,t):e.tagName=="BUTTON"?oe.call(this,t):e.tagName=="SELECT"?fe.call(this,t):e.tagName=="A"?ce.call(this,t):ue.call(this,t),t}function ie(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute,c=e.querySelectorAll(":scope > ["+l+"-key]"),o=0,a=0;t.list=s;for(let f of c){let h=parseInt(f.getAttribute(l+"-key"));if(h>o)t.index=o,e.insertBefore(this.applyTemplate(t),f);else if(h<o)f.remove();else{let m=Array.from(f.querySelectorAll(`[${l}]`));f.matches(`[${l}]`)&&m.unshift(f);let T=m.find(k=>{let N=k.getAttribute(l);return N.substr(0,5)!=="#root"&&N.substr(0,i.length)!==i});if(!T&&f.$bindTemplate){let k=this.findTemplate(n,s[o]);k!=f.$bindTemplate&&(T=!0,k||a++)}T&&(t.index=o,e.replaceChild(this.applyTemplate(t),f))}if(o++,o>=s.length)break}c=e.querySelectorAll(":scope > ["+l+"-key]");let u=c.length+a;if(u>s.length)for(;u>s.length;)e.querySelectorAll(":scope > :not(template)")?.[u-1]?.remove(),u--;else if(u<s.length)for(;u<s.length;)t.index=u,e.appendChild(this.applyTemplate(t)),u++}function se(t){let e=t.element,n=t.templates,r=n.length,i=t.path,s=t.value,l=this.options.attribute;t.list=s;let c=Array.from(e.querySelectorAll(":scope > ["+l+"-key]"));for(let o in t.list){t.index=o;let a=c.shift();if(!a){e.appendChild(this.applyTemplate(t));continue}if(a.getAttribute[l+"-key"]!=o){c.unshift(a);let f=e.querySelector(":scope > ["+l+'-key="'+o+'"]');if(f)e.insertBefore(f,a),a=f,c=c.filter(h=>h!=f);else{let h=this.applyTemplate(t);h.firstElementChild&&e.insertBefore(h,a);continue}}if(this.findTemplate(n,s[o])!=a.$bindTemplate){let f=this.applyTemplate(t);e.replaceChild(f,a)}}for(;c.length;)item=c.shift(),item.remove()}function le(t){let e=t.element,n=t.templates,r=t.value,i=this.options.attribute,s=e.querySelector(":scope > :not(template)"),l=this.findTemplate(n,r);if(t.parent=e.parentElement?.closest(`[${i}]`)?.getAttribute(i)||"#root",s)if(l){if(s?.$bindTemplate!=l){let c=this.applyTemplate(t);e.replaceChild(c,s)}}else e.removeChild(s);else if(l){let c=this.applyTemplate(t);e.appendChild(c)}}function ae(t){let e=t.element,n=t.value;e.type=="checkbox"||e.type=="radio"?w(e.value,n)?e.checked=!0:e.checked=!1:w(e.value,n)||(e.value=""+n)}function oe(t){let e=t.element,n=t.value;w(e.value,n)||(e.value=""+n)}function fe(t){let e=t.element,n=t.value;if(e.multiple){if(Array.isArray(n))for(let r of e.options)n.indexOf(r.value)===!1?r.selected=!1:r.selected=!0}else{let r=e.options.find(i=>w(i.value,n));r&&(r.selected=!0)}}function ce(t){let e=t.element,n=t.value;n?.innerHTML&&!w(e.innerHTML,n.innerHTML)&&(e.innerHTML=""+n.innerHTML),n?.href&&!w(e.href,n.href)&&(e.href=""+n.href)}function ue(t){let e=t.element,n=t.value;w(e.innerHTML,n)||(typeof n>"u"||n==null?e.innerHTML="":e.innerHTML=""+n)}var H=class{constructor(e={}){this.container=e.container||document.body,e.state||(e.state={}),this.state=L(e.state),e.commands&&(this.commands=x({app:this,container:this.container,commands:e.commands})),e.keys&&(this.keys=K({app:this,keys:e.keys})),e.routes&&(this.routes=R({app:this,routes:e.routes})),e.actions&&(this.actions=q({app:this,actions:e.actions}));let n={container:this.container,root:this.state};e.defaultTransformers&&(n.defaultTransformers=e.defaultTransformers),e.transformers&&(n.transformers=e.transformers),this.bind=F(n)}};function Le(t={}){return new H(t)}})();
2
2
  //# sourceMappingURL=simply.app.min.js.map