mosaic 1.0.9 → 1.0.10

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/cjs/update.js CHANGED
@@ -33,6 +33,10 @@ Notes
33
33
 
34
34
  */
35
35
 
36
+ function unique(arr) {
37
+ return [...new Set(arr)]
38
+ }
39
+
36
40
  function configure(
37
41
  { update = {}, middleware = [], derivations = {}, initialState = {} },
38
42
  node
@@ -41,9 +45,11 @@ function configure(
41
45
  let state;
42
46
  let updatedCallback = () => {};
43
47
 
44
- let dollarKeys = Object.keys(update)
45
- .concat(...middleware.map(Object.keys))
46
- .filter((v) => v.startsWith("$"));
48
+ let dollarKeys = unique(
49
+ Object.keys(update)
50
+ .concat(...middleware.map(Object.keys))
51
+ .filter((v) => v.startsWith("$"))
52
+ );
47
53
 
48
54
  function updateState(o) {
49
55
  state = { ...o };
@@ -74,8 +80,8 @@ function configure(
74
80
  function dispatch(action) {
75
81
  const { type } = action;
76
82
 
77
- if (type.startsWith("$") && !(type in update)) {
78
- node.dispatchEvent(
83
+ if (type.startsWith("$")) {
84
+ node.parentNode.dispatchEvent(
79
85
  new CustomEvent(type, {
80
86
  detail: action,
81
87
  bubbles: true,
@@ -126,7 +132,9 @@ function configure(
126
132
  }
127
133
 
128
134
  for (let actionName of dollarKeys) {
129
- node.addEventListener(actionName, ({ detail }) => dispatch(detail));
135
+ node.addEventListener(actionName, ({ detail }) => {
136
+ dispatch(detail);
137
+ });
130
138
  }
131
139
 
132
140
  return {
package/dist/mosaic.js CHANGED
@@ -29,6 +29,10 @@ Notes
29
29
 
30
30
  */
31
31
 
32
+ function unique(arr) {
33
+ return [...new Set(arr)]
34
+ }
35
+
32
36
  function configure(
33
37
  { update = {}, middleware = [], derivations = {}, initialState = {} },
34
38
  node
@@ -37,9 +41,11 @@ function configure(
37
41
  let state;
38
42
  let updatedCallback = () => {};
39
43
 
40
- let dollarKeys = Object.keys(update)
41
- .concat(...middleware.map(Object.keys))
42
- .filter((v) => v.startsWith("$"));
44
+ let dollarKeys = unique(
45
+ Object.keys(update)
46
+ .concat(...middleware.map(Object.keys))
47
+ .filter((v) => v.startsWith("$"))
48
+ );
43
49
 
44
50
  function updateState(o) {
45
51
  state = { ...o };
@@ -70,8 +76,8 @@ function configure(
70
76
  function dispatch(action) {
71
77
  const { type } = action;
72
78
 
73
- if (type.startsWith("$") && !(type in update)) {
74
- node.dispatchEvent(
79
+ if (type.startsWith("$")) {
80
+ node.parentNode.dispatchEvent(
75
81
  new CustomEvent(type, {
76
82
  detail: action,
77
83
  bubbles: true,
@@ -122,7 +128,9 @@ function configure(
122
128
  }
123
129
 
124
130
  for (let actionName of dollarKeys) {
125
- node.addEventListener(actionName, ({ detail }) => dispatch(detail));
131
+ node.addEventListener(actionName, ({ detail }) => {
132
+ dispatch(detail);
133
+ });
126
134
  }
127
135
 
128
136
  return {
@@ -1 +1 @@
1
- const e=(e=[])=>e[e.length-1],t=e=>e.nodeType===e.TEXT_NODE&&""===e.nodeValue.trim(),n=(e,r,i=!0)=>{if(e){if(!t(e)){let t=r(e);if(!1===t)return;if(t?.nodeName)return n(t,r,i)}i&&n(e.firstChild,r,i),n(e.nextSibling,r,i)}},r=(t,n)=>{let[i,a]=((t,n)=>{let i=((e="")=>e.split(/(\[[^\]]+\])/).filter((e=>e)).reduce(((e,t)=>e+("["===t.charAt(0)?"."+t.replace(/\./g,":"):t)),""))(t).split(".").map((e=>{if("["===e.charAt(0)){let t=e.slice(1,-1).replace(/:/g,".");return r(t,n)}return e}));return[i.slice(0,-1).reduce(((e,t)=>e&&e[t]),n)||n,e(i)]})(t,n),l=i?.[a];return"function"==typeof l?l.bind(i):l},i=e=>{if("string"==typeof e){let t=document.createElement("template");return t.innerHTML=e.trim(),t.content}return"TEMPLATE"===e.nodeName?e.cloneNode(!0).content:"defs"===e.nodeName?e.firstElementChild.cloneNode(!0):void 0},a=e=>null===e||"object"!=typeof e,l=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),c=e=>e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})),s=(e,t,n)=>{t=l(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n="")),"string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t)},o=(e,t)=>{let n=c(e);return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const u=e=>{let t=0;return n(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},d=e=>t(e.nextSibling)?d(e.nextSibling):e.nextSibling;const p=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=u(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(d(e));r.push(t)}return r})(e,i),l=e;t.forEach(((e,t)=>{let i=-1===e?r(n[t][1],n[t][0]):a[e],c=(s=i).nodeType===s.DOCUMENT_FRAGMENT_NODE&&s.lastChild||s;var s;l.after(i),l=c})),e.setAttribute("length",t.length)},h=e=>e.trim().split(/({{[^{}]+}})/).filter((e=>e)).map((e=>{let t=e.match(/{{([^{}]+)}}/);if(!t)return{type:1,value:e,negated:!1};let n="!"===(e=t[1].trim()).charAt(0);return n&&(e=e.slice(1)),{type:2,value:e,negated:n}})),f=(e,t)=>t.reduce(((t,n)=>{let i,{type:a,value:l,negated:c}=n;if(1===a&&(i=l),2===a&&(i=r(l,e)),3===a){let t=n.args.map((t=>r(t,e)));i=r(n.method,e)?.(...t)}return c&&(i=!i),t||0===t?t+i:i}),""),b=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),m=e=>{let t=typeof e;return"string"===t?e.split(";").reduce(((e,t)=>{const[n,r]=t.split(":").map((e=>e.trim()));return n&&(e[n]=r),e}),{}):"object"===t?e:{}},y=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((l={...m(e.getAttribute("style")),...m(n)},Object.keys(l).reduce(((e,t)=>(e[b(t)]=l[t],e)),{})));else if("class"===t)switch(i=n,Object.prototype.toString.call(i).match(/\s(.+[^\]])/)[1]){case"Array":n=n.join(" ");break;case"Object":n=Object.keys(n).reduce(((e,t)=>(n[t]&&e.push(t),e)),[]).join(" ")}else if(!a(n))return e[(r=t,r.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})))]=n;var r,i,l;t=b(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let c=e.getAttribute(t);n!==c&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},g=(e=[])=>{let t=e;return{get:()=>t,push:e=>t.push(e),wrap:e=>t.reduce(((e,t)=>new Proxy(e,(({path:e,identifier:t,key:n,index:i,i:l,k:c})=>({get(a,s){let o=r(e,a);if(s===t)for(let e in o){let t=o[e];if(n){if(t[n]===c)return t}else if(e==l)return t}if(s===i)for(let e in o){let t=o[e];if(n){if(t[n]===c)return e}else if(e==l)return e}let u=n?o.find((e=>e[n]===c)):o[l];return u?.hasOwnProperty?.(s)?u[s]:Reflect.get(...arguments)},set(t,i,s){let o=r(e,t),u=n?o.find((e=>e[n]===c)):o[l];return u&&!a(u)?(u[i]=s,!0):Reflect.set(...arguments)}}))(t))),e)}},E=(t,{getState:a,dispatch:l},c,s,o)=>{const b={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=f(i,h(e));a!==l&&(t.textContent=l)}}),2:({value:e,node:t,name:n,context:i},{getState:a})=>({handler:()=>{let l=i?i.wrap(a()):a(),c=f(l,h(e));if(y(t,n,c),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=r(e,l);t.selected=n===c}}}),3:({node:e,path:t,context:n},{getState:i,dispatch:a})=>(e.addEventListener("input",(()=>{let r="checkbox"===e.getAttribute("type")?e.checked:e.value;if(r.trim?.().length&&!isNaN(r)&&(r=+r),n){let e=n.wrap(i());e[t]=r,a({type:"MERGE",payload:e})}else a({type:"SET",payload:{name:t,value:r,context:n}})})),{handler:()=>{let a=n?n.wrap(i()):i();((e,t)=>{if("SELECT"===e.nodeName)return void Array.from(e.querySelectorAll("option")).forEach((e=>{e.selected=t.includes(e.value)}));let n;switch(e.getAttribute("type")){case"checkbox":if(n=t,e.checked===n)break;n?e.setAttribute("checked",""):e.removeAttribute("checked");break;case"radio":if(n=t===e.getAttribute("value"),e.checked===n)break;e.checked=n,n?e.setAttribute("checked",""):e.removeAttribute("checked");break;default:if(e.value===t)break;e.setAttribute("value",e.value=t||"")}})(e,r(t,a))}}),4:({node:e,eventType:t,actionType:n,context:r},{dispatch:i,getState:a})=>(e.addEventListener(t,(e=>{let t={type:n,event:e,context:r?r.wrap(a()):a()};i(t)})),{handler:()=>{}}),5:({node:t,context:a,map:l,path:c,identifier:s,index:o,key:h,blockIndex:f,hydrate:b,pickupNode:m},{getState:y})=>{let E;t.$t=f-1;const v=(e,t,r,i)=>{n(e,k((e=>{if(e===i)return!1}),T(l,b,g((a?.get()||[]).concat({path:c,identifier:s,key:h,index:o,i:t,k:r}))),(e=>e.$t=f)))};const A=(e,n)=>{let r=e[h],a=i(t);var l;return v((l=a).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),a};if(b){let n=r(c,y()),i=(e=>{let t=e.getAttribute("length"),n=u(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=d(i),e.push(i);r.push(e)}return r})(t);i.forEach(((t,r)=>{let i=n[r]?.[h];v(t[0],r,i,e(t).nextSibling)})),m=e(e(i)).nextSibling}return{handler:()=>{let e=a?a.wrap(y()):y();const n=Object.entries(r(c,e)||[]),i=((e,t=[],n=[])=>{let r=n.map((([n,r])=>e?t.findIndex((([t,n])=>n[e]===r[e])):n in t?n:-1));if(t.length!==n.length||!r.every(((e,t)=>e===t)))return r})(h,E,n);i&&p(t,i,n,A),E=n.slice(0)},pickupNode:m}}},{bind:m,update:E}=(()=>{const e=(()=>{let e=new Set;return{publish:t=>{for(let t of e)t();t?.()},subscribe(t){e.add(t)}}})();return{bind(t){let n=b[t.type](t,{getState:a,dispatch:l});return e.subscribe(n.handler),n},update:t=>e.publish(t)}})();let v=0;const A=e=>{let t=0,r={};return n(e,(e=>{let n,i=[];switch(e.nodeType){case e.TEXT_NODE:{let t=e.textContent;t.match(/({{[^{}]+}})/)&&i.push({type:1,value:t});break}case e.ELEMENT_NODE:{let t=function(e){let t=e.getAttribute("each"),n=t?.match(/(.+)\s+in\s+(.+)/);if(!n)return t?{path:t.trim(),key:e.getAttribute("key")}:n;let[r,i,a]=n,l=i.match(/\(([^\)]+)\)/),[c,s]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:s||c,index:s?c:s,key:e.getAttribute("key")}}(e);if(t){let r,a=e.namespaceURI;if(a.endsWith("/svg")){e.removeAttribute("each");let t=document.createElementNS(a,"defs");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),r=A((e=t).firstChild)}else{if("TEMPLATE"!==e.nodeName){e.removeAttribute("each");let t=document.createElement("template");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),e=t}r=A(e.content.firstChild)}n=e.nextSibling,i.push({type:5,map:r,blockIndex:v++,...t,pickupNode:n});break}let r=e.attributes,a=r.length;for(;a--;){let{name:t,value:n}=r[a];if(":name"!==t||!n||"INPUT"!==e.nodeName&&"SELECT"!==e.nodeName&&"TEXTAREA"!==e.nodeName){if(t.startsWith(":on")){e.removeAttribute(t);let r=t.split(":on")[1];i.push({type:4,eventType:r,actionType:n})}else if(t.startsWith(":")){let r=t.slice(1),a=n||r;a.includes("{{")||(a=`{{${a}}}`),i.push({type:2,name:r,value:a}),e.removeAttribute(t)}}else i.push({type:3,path:n}),e.removeAttribute(t),e.setAttribute("name",n)}}}return i.length&&(r[t]=i),t++,n})),r},k=(...e)=>(...t)=>{for(let n of e){if(!1===n(...t))return!1}},T=(e,t=0,n)=>{let r=0;return i=>{let a;return r in e&&(e[r].forEach((e=>{let r=m({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let N=i(c),C=A(N);return n(N,T(C)),o?.(N),t.prepend(N),E(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>E(s)))},v=e=>{let t=document.createDocumentFragment();for(;e.firstChild;)t.appendChild(e.firstChild);return t},A=(e,t,n)=>customElements.define(e,class extends HTMLElement{async connectedCallback(){if(!this.initialised){let e=t(this);e instanceof Promise&&(e=await e);let{subscribe:r,shadow:i,initialState:u={},observe:d=[]}=e;this.connectedCallback=e.connectedCallback,this.disconnectedCallback=e.disconnectedCallback;const{dispatch:p,getState:h,onUpdate:f,flush:b}=function({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],c=()=>{},s=Object.keys(e).concat(...t.map(Object.keys)).filter((e=>e.startsWith("$")));function o(e){a={...e};for(let t in n)a[t]=n[t](e)}function u(){return{...a}}function d(e){l.push(e)}function p(n){const{type:r}=n;if(r.startsWith("$")&&!(r in e)&&i.dispatchEvent(new CustomEvent(r,{detail:n,bubbles:!0})),"SET"===r||"MERGE"===r)o(function(e,t){switch(t.type){case"SET":{const{name:n,value:r}=t.payload;return{...e,[n]:r}}case"MERGE":return{...e,...t.payload}}}(u(),n));else if(t.length){let i=t.slice(),a=t=>{if(i.length){let e=i.shift();t.type in e?e[t.type](t,a,{getState:u,dispatch:p,afterNextRender:d}):a(t)}else t.type in e&&o(e[t.type](u(),t))},l=i.shift();r in l?l[r](n,a,{getState:u,dispatch:p,afterNextRender:d}):a(n)}else r in e&&o(e[r](u(),n));c()}o(r);for(let e of s)i.addEventListener(e,(({detail:e})=>p(e)));return{dispatch:p,getState:u,onUpdate:function(e){c=e},flush:function(){l.forEach((e=>e())),l=[]}}}(e,this);u=h(),d=d.map(c);let m,y=d.map(l),g=this.setAttribute;this.setAttribute=(e,t)=>{if(y.includes(e)){let{name:n,value:r}=o(e,t);p({type:"SET",payload:{name:n,value:r}})}g.apply(this,[e,t])},y.forEach((e=>{let t,n=o(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||u[n],Object.defineProperty(this,n,{get:()=>h()[n],set(e){p({type:"SET",payload:{name:n,value:e}}),a(e)&&s(this,n,e)}}),this[n]=t})),i?this.attachShadow({mode:i}):m=e=>((e,t)=>{t.querySelectorAll("slot[name]").forEach((t=>{let n=t.attributes.name.value,r=e.querySelector(`[slot="${n}"]`);r?(r.removeAttribute("slot"),t.parentNode.replaceChild(r,t)):t.parentNode.replaceChild(v(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(v(e.innerHTML.trim()?e:n),n)})(this,e),f(E(this.shadowRoot||this,{getState:h,dispatch:p},n,(()=>{d.forEach((e=>{let t=h()[e];a(t)&&s(this,e,t)})),r?.(h()),b()}),m)),this.initialised=!0}this.connectedCallback?.()}disconnectedCallback(){this.disconnectedCallback?.()}});export{A as define};
1
+ function e({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],c=()=>{},s=(o=Object.keys(e).concat(...t.map(Object.keys)).filter((e=>e.startsWith("$"))),[...new Set(o)]);var o;function u(e){a={...e};for(let t in n)a[t]=n[t](e)}function d(){return{...a}}function p(e){l.push(e)}function h(n){const{type:r}=n;if(r.startsWith("$")&&i.parentNode.dispatchEvent(new CustomEvent(r,{detail:n,bubbles:!0})),"SET"===r||"MERGE"===r)u(function(e,t){switch(t.type){case"SET":{const{name:n,value:r}=t.payload;return{...e,[n]:r}}case"MERGE":return{...e,...t.payload}}}(d(),n));else if(t.length){let i=t.slice(),a=t=>{if(i.length){let e=i.shift();t.type in e?e[t.type](t,a,{getState:d,dispatch:h,afterNextRender:p}):a(t)}else t.type in e&&u(e[t.type](d(),t))},l=i.shift();r in l?l[r](n,a,{getState:d,dispatch:h,afterNextRender:p}):a(n)}else r in e&&u(e[r](d(),n));c()}u(r);for(let e of s)i.addEventListener(e,(({detail:e})=>{h(e)}));return{dispatch:h,getState:d,onUpdate:function(e){c=e},flush:function(){l.forEach((e=>e())),l=[]}}}const t=(e=[])=>e[e.length-1],n=e=>e.nodeType===e.TEXT_NODE&&""===e.nodeValue.trim(),r=(e,t,i=!0)=>{if(e){if(!n(e)){let n=t(e);if(!1===n)return;if(n?.nodeName)return r(n,t,i)}i&&r(e.firstChild,t,i),r(e.nextSibling,t,i)}},i=(e,n)=>{let[r,a]=((e,n)=>{let r=((e="")=>e.split(/(\[[^\]]+\])/).filter((e=>e)).reduce(((e,t)=>e+("["===t.charAt(0)?"."+t.replace(/\./g,":"):t)),""))(e).split(".").map((e=>{if("["===e.charAt(0)){let t=e.slice(1,-1).replace(/:/g,".");return i(t,n)}return e}));return[r.slice(0,-1).reduce(((e,t)=>e&&e[t]),n)||n,t(r)]})(e,n),l=r?.[a];return"function"==typeof l?l.bind(r):l},a=e=>{if("string"==typeof e){let t=document.createElement("template");return t.innerHTML=e.trim(),t.content}return"TEMPLATE"===e.nodeName?e.cloneNode(!0).content:"defs"===e.nodeName?e.firstElementChild.cloneNode(!0):void 0},l=e=>null===e||"object"!=typeof e,c=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),s=e=>e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})),o=(e,t,n)=>{t=c(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n="")),"string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t)},u=(e,t)=>{let n=s(e);return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const d=e=>{let t=0;return r(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},p=e=>n(e.nextSibling)?p(e.nextSibling):e.nextSibling;const h=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=d(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(p(e));r.push(t)}return r})(e,i),l=e;t.forEach(((e,t)=>{let i=-1===e?r(n[t][1],n[t][0]):a[e],c=(s=i).nodeType===s.DOCUMENT_FRAGMENT_NODE&&s.lastChild||s;var s;l.after(i),l=c})),e.setAttribute("length",t.length)},f=e=>e.trim().split(/({{[^{}]+}})/).filter((e=>e)).map((e=>{let t=e.match(/{{([^{}]+)}}/);if(!t)return{type:1,value:e,negated:!1};let n="!"===(e=t[1].trim()).charAt(0);return n&&(e=e.slice(1)),{type:2,value:e,negated:n}})),b=(e,t)=>t.reduce(((t,n)=>{let r,{type:a,value:l,negated:c}=n;if(1===a&&(r=l),2===a&&(r=i(l,e)),3===a){let t=n.args.map((t=>i(t,e)));r=i(n.method,e)?.(...t)}return c&&(r=!r),t||0===t?t+r:r}),""),m=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),y=e=>{let t=typeof e;return"string"===t?e.split(";").reduce(((e,t)=>{const[n,r]=t.split(":").map((e=>e.trim()));return n&&(e[n]=r),e}),{}):"object"===t?e:{}},g=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((a={...y(e.getAttribute("style")),...y(n)},Object.keys(a).reduce(((e,t)=>(e[m(t)]=a[t],e)),{})));else if("class"===t)switch(i=n,Object.prototype.toString.call(i).match(/\s(.+[^\]])/)[1]){case"Array":n=n.join(" ");break;case"Object":n=Object.keys(n).reduce(((e,t)=>(n[t]&&e.push(t),e)),[]).join(" ")}else if(!l(n))return e[(r=t,r.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})))]=n;var r,i,a;t=m(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let c=e.getAttribute(t);n!==c&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},E=(e=[])=>{let t=e;return{get:()=>t,push:e=>t.push(e),wrap:e=>t.reduce(((e,t)=>new Proxy(e,(({path:e,identifier:t,key:n,index:r,i:a,k:c})=>({get(l,s){let o=i(e,l);if(s===t)for(let e in o){let t=o[e];if(n){if(t[n]===c)return t}else if(e==a)return t}if(s===r)for(let e in o){let t=o[e];if(n){if(t[n]===c)return e}else if(e==a)return e}let u=n?o.find((e=>e[n]===c)):o[a];return u?.hasOwnProperty?.(s)?u[s]:Reflect.get(...arguments)},set(t,r,s){let o=i(e,t),u=n?o.find((e=>e[n]===c)):o[a];return u&&!l(u)?(u[r]=s,!0):Reflect.set(...arguments)}}))(t))),e)}},v=(e,{getState:n,dispatch:l},c,s,o)=>{const u={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=b(i,f(e));a!==l&&(t.textContent=l)}}),2:({value:e,node:t,name:n,context:r},{getState:a})=>({handler:()=>{let l=r?r.wrap(a()):a(),c=b(l,f(e));if(g(t,n,c),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=i(e,l);t.selected=n===c}}}),3:({node:e,path:t,context:n},{getState:r,dispatch:a})=>(e.addEventListener("input",(()=>{let i="checkbox"===e.getAttribute("type")?e.checked:e.value;if(i.trim?.().length&&!isNaN(i)&&(i=+i),n){let e=n.wrap(r());e[t]=i,a({type:"MERGE",payload:e})}else a({type:"SET",payload:{name:t,value:i,context:n}})})),{handler:()=>{let a=n?n.wrap(r()):r();((e,t)=>{if("SELECT"===e.nodeName)return void Array.from(e.querySelectorAll("option")).forEach((e=>{e.selected=t.includes(e.value)}));let n;switch(e.getAttribute("type")){case"checkbox":if(n=t,e.checked===n)break;n?e.setAttribute("checked",""):e.removeAttribute("checked");break;case"radio":if(n=t===e.getAttribute("value"),e.checked===n)break;e.checked=n,n?e.setAttribute("checked",""):e.removeAttribute("checked");break;default:if(e.value===t)break;e.setAttribute("value",e.value=t||"")}})(e,i(t,a))}}),4:({node:e,eventType:t,actionType:n,context:r},{dispatch:i,getState:a})=>(e.addEventListener(t,(e=>{let t={type:n,event:e,context:r?r.wrap(a()):a()};i(t)})),{handler:()=>{}}),5:({node:e,context:n,map:l,path:c,identifier:s,index:o,key:u,blockIndex:f,hydrate:b,pickupNode:m},{getState:y})=>{let g;e.$t=f-1;const v=(e,t,i,a)=>{r(e,k((e=>{if(e===a)return!1}),N(l,b,E((n?.get()||[]).concat({path:c,identifier:s,key:u,index:o,i:t,k:i}))),(e=>e.$t=f)))};const A=(t,n)=>{let r=t[u],i=a(e);var l;return v((l=i).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),i};if(b){let n=i(c,y()),r=(e=>{let t=e.getAttribute("length"),n=d(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=p(i),e.push(i);r.push(e)}return r})(e);r.forEach(((e,r)=>{let i=n[r]?.[u];v(e[0],r,i,t(e).nextSibling)})),m=t(t(r)).nextSibling}return{handler:()=>{let t=n?n.wrap(y()):y();const r=Object.entries(i(c,t)||[]),a=((e,t=[],n=[])=>{let r=n.map((([n,r])=>e?t.findIndex((([t,n])=>n[e]===r[e])):n in t?n:-1));if(t.length!==n.length||!r.every(((e,t)=>e===t)))return r})(u,g,r);a&&h(e,a,r,A),g=r.slice(0)},pickupNode:m}}},{bind:m,update:y}=(()=>{const e=(()=>{let e=new Set;return{publish:t=>{for(let t of e)t();t?.()},subscribe(t){e.add(t)}}})();return{bind(t){let r=u[t.type](t,{getState:n,dispatch:l});return e.subscribe(r.handler),r},update:t=>e.publish(t)}})();let v=0;const A=e=>{let t=0,n={};return r(e,(e=>{let r,i=[];switch(e.nodeType){case e.TEXT_NODE:{let t=e.textContent;t.match(/({{[^{}]+}})/)&&i.push({type:1,value:t});break}case e.ELEMENT_NODE:{let t=function(e){let t=e.getAttribute("each"),n=t?.match(/(.+)\s+in\s+(.+)/);if(!n)return t?{path:t.trim(),key:e.getAttribute("key")}:n;let[r,i,a]=n,l=i.match(/\(([^\)]+)\)/),[c,s]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:s||c,index:s?c:s,key:e.getAttribute("key")}}(e);if(t){let n,a=e.namespaceURI;if(a.endsWith("/svg")){e.removeAttribute("each");let t=document.createElementNS(a,"defs");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),n=A((e=t).firstChild)}else{if("TEMPLATE"!==e.nodeName){e.removeAttribute("each");let t=document.createElement("template");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),e=t}n=A(e.content.firstChild)}r=e.nextSibling,i.push({type:5,map:n,blockIndex:v++,...t,pickupNode:r});break}let n=e.attributes,a=n.length;for(;a--;){let{name:t,value:r}=n[a];if(":name"!==t||!r||"INPUT"!==e.nodeName&&"SELECT"!==e.nodeName&&"TEXTAREA"!==e.nodeName){if(t.startsWith(":on")){e.removeAttribute(t);let n=t.split(":on")[1];i.push({type:4,eventType:n,actionType:r})}else if(t.startsWith(":")){let n=t.slice(1),a=r||n;a.includes("{{")||(a=`{{${a}}}`),i.push({type:2,name:n,value:a}),e.removeAttribute(t)}}else i.push({type:3,path:r}),e.removeAttribute(t),e.setAttribute("name",r)}}}return i.length&&(n[t]=i),t++,r})),n},k=(...e)=>(...t)=>{for(let n of e){if(!1===n(...t))return!1}},N=(e,t=0,n)=>{let r=0;return i=>{let a;return r in e&&(e[r].forEach((e=>{let r=m({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let T=a(c),C=A(T);return r(T,N(C)),o?.(T),e.prepend(T),y(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>y(s)))},A=e=>{let t=document.createDocumentFragment();for(;e.firstChild;)t.appendChild(e.firstChild);return t},k=(t,n,r)=>customElements.define(t,class extends HTMLElement{async connectedCallback(){if(!this.initialised){let t=n(this);t instanceof Promise&&(t=await t);let{subscribe:i,shadow:a,initialState:d={},observe:p=[]}=t;this.connectedCallback=t.connectedCallback,this.disconnectedCallback=t.disconnectedCallback;const{dispatch:h,getState:f,onUpdate:b,flush:m}=e(t,this);d=f(),p=p.map(s);let y,g=p.map(c),E=this.setAttribute;this.setAttribute=(e,t)=>{if(g.includes(e)){let{name:n,value:r}=u(e,t);h({type:"SET",payload:{name:n,value:r}})}E.apply(this,[e,t])},g.forEach((e=>{let t,n=u(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||d[n],Object.defineProperty(this,n,{get:()=>f()[n],set(e){h({type:"SET",payload:{name:n,value:e}}),l(e)&&o(this,n,e)}}),this[n]=t})),a?this.attachShadow({mode:a}):y=e=>((e,t)=>{t.querySelectorAll("slot[name]").forEach((t=>{let n=t.attributes.name.value,r=e.querySelector(`[slot="${n}"]`);r?(r.removeAttribute("slot"),t.parentNode.replaceChild(r,t)):t.parentNode.replaceChild(A(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(A(e.innerHTML.trim()?e:n),n)})(this,e),b(v(this.shadowRoot||this,{getState:f,dispatch:h},r,(()=>{p.forEach((e=>{let t=f()[e];l(t)&&o(this,e,t)})),i?.(f()),m()}),y)),this.initialised=!0}this.connectedCallback?.()}disconnectedCallback(){this.disconnectedCallback?.()}});export{k as define};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosaic",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "The Web UI Framework",
5
5
  "main": "dist/mosaic.min.js",
6
6
  "browser": "dist/mosaic.min.js",
package/src/update.js CHANGED
@@ -29,6 +29,10 @@ Notes
29
29
 
30
30
  */
31
31
 
32
+ function unique(arr) {
33
+ return [...new Set(arr)]
34
+ }
35
+
32
36
  export function configure(
33
37
  { update = {}, middleware = [], derivations = {}, initialState = {} },
34
38
  node
@@ -37,9 +41,11 @@ export function configure(
37
41
  let state
38
42
  let updatedCallback = () => {}
39
43
 
40
- let dollarKeys = Object.keys(update)
41
- .concat(...middleware.map(Object.keys))
42
- .filter((v) => v.startsWith("$"))
44
+ let dollarKeys = unique(
45
+ Object.keys(update)
46
+ .concat(...middleware.map(Object.keys))
47
+ .filter((v) => v.startsWith("$"))
48
+ )
43
49
 
44
50
  function updateState(o) {
45
51
  state = { ...o }
@@ -70,8 +76,8 @@ export function configure(
70
76
  function dispatch(action) {
71
77
  const { type } = action
72
78
 
73
- if (type.startsWith("$") && !(type in update)) {
74
- node.dispatchEvent(
79
+ if (type.startsWith("$")) {
80
+ node.parentNode.dispatchEvent(
75
81
  new CustomEvent(type, {
76
82
  detail: action,
77
83
  bubbles: true,
@@ -122,7 +128,9 @@ export function configure(
122
128
  }
123
129
 
124
130
  for (let actionName of dollarKeys) {
125
- node.addEventListener(actionName, ({ detail }) => dispatch(detail))
131
+ node.addEventListener(actionName, ({ detail }) => {
132
+ dispatch(detail)
133
+ })
126
134
  }
127
135
 
128
136
  return {