diff --git a/dist/dist.js b/dist/dist.js index 28981b6..a857436 100644 --- a/dist/dist.js +++ b/dist/dist.js @@ -1 +1 @@ -!function(t){var e={};function r(s){if(e[s])return e[s].exports;var o=e[s]={i:s,l:!1,exports:{}};return t[s].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=t,r.c=e,r.d=function(t,e,s){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(s,o,function(e){return t[e]}.bind(null,o));return s},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e);var o=class{constructor(){}view(){return s.markup("nav",{attrs:{class:"navbar navbar-light bg-light",id:"navbar"},children:[s.markup("span",{attrs:{},children:[s.markup("img",{attrs:{src:"images/sling.png",width:"30px",height:"30px",class:"d-inline-block align-top",style:"margin-right:0.5rem;margin-top:0.3125rem;margin-bottom:0.3125rem;"},children:[]}),s.markup("span",{attrs:{class:"navbar-brand"},children:[s.textNode("Sling")]}),s.markup("div",{attrs:{style:"display:inline-flex;"},children:[s.markup("ul",{attrs:{class:"navbar-nav"},children:[s.markup("li",{attrs:{class:"nav-item"},children:[s.textNode("v"+s.version)]})]})]})]})]})}};var n=class{constructor(t,e){this.text=t,this.completed=e}};const i="cookieNoteData";var a=class{constructor(){}getNoteCookie(){return this.getCookie(i)}setNoteCookie(t){this.setCookie(i,JSON.stringify(t),31)}setCookie(t,e,r){var s=new Date;s.setTime(s.getTime()+24*r*60*60*1e3);var o="expires="+s.toUTCString();document.cookie=t+"="+e+";"+o+";path=/"}getCookie(t){for(var e=t+"=",r=decodeURIComponent(document.cookie).split(";"),s=0;s{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}updateNote(t,e){let r=s.getState();r.getNotes().forEach(r=>{r===t&&(r.text=e.target.value)}),s.setState(r),(new a).setNoteCookie(r)}view(){return s.markup("div",{attrs:{id:"divTodoList"},children:[s.markup("div",{attrs:{style:"width:50%;margin:auto;padding:1rem;"},children:[...Array.from(s.getState().getNotes(),t=>s.markup("div",{attrs:{class:"input-group mb-3 animEnter",style:"width:100%;"},children:[s.markup("div",{attrs:{class:"input-group-prepend"},children:[s.markup("div",{attrs:{class:"input-group-text"},children:[s.markup("input",{attrs:{type:"checkbox",...t.completed&&{checked:"true"},onchange:this.completeNote.bind(this,t)}})]}),s.markup("img",{attrs:{src:"images/cat-typing.gif",width:"50px",height:"50px"}})]}),s.markup("input",{attrs:{value:t.text,class:"form-control",...t.completed&&{readonly:"true"},oninput:this.updateNote.bind(this,t),style:"margin-left:1px;"}})]}))]})]})}};var c=class{constructor(){}view(){return s.markup("div",{attrs:{},children:[s.markup("h4",{attrs:{style:"text-align:center;padding:1rem;font-family:Arial;line-height:58px;font-size:54px;font-weight:300;"},children:[s.textNode("Todo App")]})]})}};var d=class{constructor(){}slOnDestroy(){console.log("Destroy completed list component")}completeNote(t){let e=s.getState();e.getNotes().forEach(e=>{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}view(){return s.markup("div",{attrs:{id:"divTodoList"},children:[s.markup("div",{attrs:{style:"width:50%;margin:auto;padding:1rem;"},children:[...Array.from(s.getState().getNotes().filter(t=>!!t.completed),t=>s.markup("div",{attrs:{class:"input-group mb-3 animEnter",style:"width:100%;"},children:[s.markup("div",{attrs:{class:"input-group-prepend"},children:[s.markup("div",{attrs:{class:"input-group-text"},children:[s.markup("input",{attrs:{type:"checkbox",...t.completed&&{checked:"true"},onchange:this.completeNote.bind(this,t)}})]})]}),s.markup("input",{attrs:{value:t.text,class:"form-control",...t.completed&&{readonly:"true"}}})]}))]})]})}};var p=class{constructor(){this.routeString=""}slOnInit(){this.performRouteAction(s.getRouteSegments()[0])}routeToAll(){let t=new u;s.mount("divTodoList",t),s.route("all"),s.autoUpdate("divTodoList",t)}routeToCompleted(){let t=new d;s.mount("divTodoList",t),s.route("completed"),s.autoUpdate("divTodoList",t)}performRouteAction(t){switch(this.routeString=t,t){case"all":this.routeToAll();break;case"completed":this.routeToCompleted();break;default:this.routeString="all",this.routeToAll()}}completeNote(t){let e=s.getState();e.getNotes().forEach(e=>{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}view(){return s.markup("ul",{attrs:{class:"nav",style:"width:50%;margin:auto;",id:"navTodoList"},children:[s.markup("li",{attrs:{class:"nav-item"},children:[s.markup("a",{attrs:{..."all"!==this.routeString&&{class:"nav-link"},..."all"===this.routeString&&{class:"nav-link textBold"},onclick:this.performRouteAction.bind(this,"all"),style:"cursor:pointer;"},children:[s.textNode("All")]})]}),s.markup("li",{attrs:{class:"nav-item"},children:[s.markup("a",{attrs:{..."completed"!==this.routeString&&{class:"nav-link"},..."completed"===this.routeString&&{class:"nav-link textBold"},onclick:this.performRouteAction.bind(this,"completed"),style:"cursor:pointer;"},children:[s.textNode("Completed")]})]})]})}};var m=class{constructor(){this.notes=[]}getNotes(){return this.notes}setNotes(t){this.notes=t}addNotes(t){t.notes.forEach(t=>{this.notes.push(t)})}};let h=(new a).getNoteCookie(),g=new m;h.length>0&&g.addNotes(JSON.parse(h)),s.setState(g),s.addRoute("all",{component:new u,root:"divTodoList"}),s.addRoute("completed",{component:new d,root:"divTodoList"});let v=new o;s.mount("divNavbar",v);let f=new l;s.mount("divNoteInput",f);let k=new p;s.mount("divNoteNav",k);let N=new c;s.mount("divTodoHeader",N),s.autoUpdate("navTodoList",k);let b=s.stream();b.push(1),b.push(2);const y=function(t){return t.map(t=>2*t)};b.log(),b.transform(y),b.log(),b.push(3);b.transform((function(t){return t.map(t=>2*t)})),b.log();b.transform((function(t){return t.filter(t=>t>6)})),b.log(),b.transform(y),b.log();b.transform((function(t){return t.reduce((t,e)=>t+e)})),b.log(),b.transform(y).transform(y),b.log(),b.clearTransformers(),b.from([1,2]),b.transform(y).transform(y),b.log()}]); \ No newline at end of file +!function(t){var e={};function s(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,s),o.l=!0,o.exports}s.m=t,s.c=e,s.d=function(t,e,r){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(s.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)s.d(r,o,function(e){return t[e]}.bind(null,o));return r},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e,r){"use strict";r.r(e);var o=class{constructor(){}view(){return s.markup("nav",{attrs:{class:"navbar navbar-light bg-light",id:"navbar"},children:[s.markup("span",{attrs:{},children:[s.markup("img",{attrs:{src:"images/sling.png",width:"30px",height:"30px",class:"d-inline-block align-top",style:"margin-right:0.5rem;margin-top:0.3125rem;margin-bottom:0.3125rem;"},children:[]}),s.markup("span",{attrs:{class:"navbar-brand"},children:[s.textNode("Sling")]}),s.markup("div",{attrs:{style:"display:inline-flex;"},children:[s.markup("ul",{attrs:{class:"navbar-nav"},children:[s.markup("li",{attrs:{class:"nav-item"},children:[s.textNode("v"+s.version)]})]})]})]})]})}};var i=class{constructor(t,e){this.text=t,this.completed=e}};const n="cookieNoteData";var a=class{constructor(){}getNoteCookie(){return this.getCookie(n)}setNoteCookie(t){this.setCookie(n,JSON.stringify(t),31)}setCookie(t,e,s){var r=new Date;r.setTime(r.getTime()+24*s*60*60*1e3);var o="expires="+r.toUTCString();document.cookie=t+"="+e+";"+o+";path=/"}getCookie(t){for(var e=t+"=",s=decodeURIComponent(document.cookie).split(";"),r=0;r{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}updateNote(t,e){let r=s.getState();r.getNotes().forEach(s=>{s===t&&(s.text=e.target.value)}),s.setState(r),(new a).setNoteCookie(r)}view(){return s.markup("div",{attrs:{id:"divTodoList"},children:[s.markup("div",{attrs:{style:"width:50%;margin:auto;padding:1rem;"},children:[...Array.from(s.getState().getNotes(),t=>s.markup("div",{attrs:{class:"input-group mb-3 animEnter",style:"width:100%;"},children:[s.markup("div",{attrs:{class:"input-group-prepend"},children:[s.markup("div",{attrs:{class:"input-group-text"},children:[s.markup("input",{attrs:{type:"checkbox",...t.completed&&{checked:"true"},onchange:this.completeNote.bind(this,t)}})]}),s.markup("img",{attrs:{src:"images/cat-typing.gif",width:"50px",height:"50px"}})]}),s.markup("input",{attrs:{value:t.text,class:"form-control",...t.completed&&{readonly:"true"},oninput:this.updateNote.bind(this,t),style:"margin-left:1px;"}})]}))]})]})}};var c=class{constructor(){}view(){return s.markup("div",{attrs:{},children:[s.markup("h4",{attrs:{style:"text-align:center;padding:1rem;font-family:Arial;line-height:58px;font-size:54px;font-weight:300;"},children:[s.textNode("Todo App")]})]})}};var u=class{constructor(){}slOnDestroy(){console.log("Destroy completed list component")}completeNote(t){let e=s.getState();e.getNotes().forEach(e=>{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}view(){return s.markup("div",{attrs:{id:"divTodoList"},children:[s.markup("div",{attrs:{style:"width:50%;margin:auto;padding:1rem;"},children:[...Array.from(s.getState().getNotes().filter(t=>!!t.completed),t=>s.markup("div",{attrs:{class:"input-group mb-3 animEnter",style:"width:100%;"},children:[s.markup("div",{attrs:{class:"input-group-prepend"},children:[s.markup("div",{attrs:{class:"input-group-text"},children:[s.markup("input",{attrs:{type:"checkbox",...t.completed&&{checked:"true"},onchange:this.completeNote.bind(this,t)}})]})]}),s.markup("input",{attrs:{value:t.text,class:"form-control",...t.completed&&{readonly:"true"}}})]}))]})]})}};var p=class{constructor(){this.routeString=""}slOnInit(){this.performRouteAction(s.getRouteSegments()[0])}routeToAll(){let t=new d;s.mount("divTodoList",t),s.route("all"),s.autoUpdate("divTodoList",t)}routeToCompleted(){let t=new u;s.mount("divTodoList",t),s.route("completed"),s.autoUpdate("divTodoList",t)}performRouteAction(t){switch(this.routeString=t,t){case"all":this.routeToAll();break;case"completed":this.routeToCompleted();break;default:this.routeString="all",this.routeToAll()}}completeNote(t){let e=s.getState();e.getNotes().forEach(e=>{e===t&&(e.completed=!t.completed)}),s.setState(e),(new a).setNoteCookie(e)}view(){return s.markup("ul",{attrs:{class:"nav",style:"width:50%;margin:auto;",id:"navTodoList"},children:[s.markup("li",{attrs:{class:"nav-item"},children:[s.markup("a",{attrs:{..."all"!==this.routeString&&{class:"nav-link"},..."all"===this.routeString&&{class:"nav-link textBold"},onclick:this.performRouteAction.bind(this,"all"),style:"cursor:pointer;"},children:[s.textNode("All")]})]}),s.markup("li",{attrs:{class:"nav-item"},children:[s.markup("a",{attrs:{..."completed"!==this.routeString&&{class:"nav-link"},..."completed"===this.routeString&&{class:"nav-link textBold"},onclick:this.performRouteAction.bind(this,"completed"),style:"cursor:pointer;"},children:[s.textNode("Completed")]})]})]})}};var m=class{constructor(){this.notes=[]}getNotes(){return this.notes}setNotes(t){this.notes=t}addNotes(t){t.notes.forEach(t=>{this.notes.push(t)})}};let h=(new a).getNoteCookie(),g=new m;h.length>0&&g.addNotes(JSON.parse(h)),s.setState(g),s.addRoute("all",{component:new d,root:"divTodoList"}),s.addRoute("completed",{component:new u,root:"divTodoList"});let v=new o;s.mount("divNavbar",v);let k=new l;s.mount("divNoteInput",k);let f=new p;s.mount("divNoteNav",f);let N=new c;s.mount("divTodoHeader",N),s.autoUpdate("navTodoList",f)}]); \ No newline at end of file diff --git a/dist/sling-full.min.js b/dist/sling-full.min.js index a610e87..ad6ca98 100644 --- a/dist/sling-full.min.js +++ b/dist/sling-full.min.js @@ -1 +1 @@ -new function(t){let e={},n=new Map,r=[],o={segmentArr:[],routeMap:new Map,params:null};Sling=s=function(){},s.version=1.5;const i=({tagName:t,attrs:e,children:n})=>{let r,s=document.createElement(t);for(let[t,n]of Object.entries(e))"function"==(r=typeof n)?s[t]=n:s.setAttribute(t,n);for(let t of n){let e=u(t);s.appendChild(e)}return s},l=(t,e)=>{if(!e)return t=>{t.remove()};if("string"==typeof t||"string"==typeof e)return t!==e?t=>{let n=u(e);return t.replaceWith(n),n}:t=>void 0;if(t.tagName!==e.tagName)return t=>{let n=u(e);return t.replaceWith(n),n};let n=((t,e,n,r)=>{let s,o,i,l=[];if(n===r)for(let[t,n]of Object.entries(e))"function"==(s=typeof n)?l.push(e=>(e[t]=n,e)):l.push(e=>(o=e.hasAttribute(t),i=e.getAttribute(t),o&&i!==n?e.setAttribute(t,n):o||e.setAttribute(t,n),e));else{for(let[t,n]of Object.entries(e))"function"==(s=typeof n)?l.push(e=>(e[t]=n,e)):l.push(e=>(e.setAttribute(t,n),e));for(let n in t)n in e||l.push(t=>(t[n]=!1,t.removeAttribute(n),t))}return t=>{for(let e of l)e(t)}})(t.attrs,e.attrs,t.attrsStr,e.attrsStr),r=((t,e)=>{let n=[];t.forEach((t,r)=>{n.push(l(t,e[r]))});let r=[];for(let n of e.slice(t.length))r.push(t=>(t.appendChild(u(n)),t));return t=>{for(let[e,r]of((t,e)=>{let n=[];for(let r=0;r(n(t),r(t),t)},u=t=>"string"==typeof t?document.createTextNode(t):i(t),a=()=>{let e=t.location.href.split("#")[1];e&&(o.segmentArr=e.split("/"))},d=(t,e)=>{let n=(t=>(t.slOnInit&&t.slOnInit(),"string"==typeof(t=t.view())?document.createTextNode(t):i(t)))(e);return t.replaceWith(n),n};s.setState=t=>{e=t},s.getState=()=>e,s.textNode=t=>String(t),s.markup=(t,{attrs:e={},children:n=[]}={})=>{let r="";for(let[t,n]of Object.entries(e))r+=t+n;return{tagName:t,attrs:e,children:n,attrsStr:r}},s.mount=(t,e)=>{let n=t,r=document.getElementById(t);if(null!==r)return d(r,e);throw"Element ID '"+n+"' is invalid."},s.update=(t,e)=>{t=document.getElementById(t);let n=e.view.bind(e)();t=l(t,n)(t),t=n},s.clearAutoUpdate=t=>{let e=t;if(null===(t=document.getElementById(t)))throw"Element ID '"+e+"' is invalid.";n.has(t.id)&&(clearInterval(n.get(t.id)),n.delete(t.id))},s.autoUpdate=(t,e,r=17)=>{let s=t=document.getElementById(t),o=e.view.bind(e)(),i=l(s,o);t=i(t),s=o;let u=setInterval(()=>{let n=e.view.bind(e)(),r=l(s,n);t=r(t),s=n},r);n.has(t.id)&&(clearInterval(n.get(t.id)),n.delete(t.id)),n.set(t.id,u)},s.getRouteSegments=()=>o.segmentArr,s.getRouteParams=()=>o.params,s.getRoute=()=>t.location.href.split("#")[1],s.addRoute=function(t,e){o.routeMap.set(new RegExp("^"+t.replace(/:[^\/]+/g,"([^\\/]+)")+"$"),e)},s.route=function(e,i={}){o.params=i,o.routeMap.forEach((o,i)=>{if(i.test(e)){r.forEach(t=>{t()}),r=[],t.location.hash=e,a();let i=document.getElementById(o.root),l=!1;n.has(i.id)&&(clearInterval(n.get(i.id)),n.delete(i.id),l=!0),d(i,o.component),o.component.slOnDestroy&&r.push(o.component.slOnDestroy),l&&s.autoUpdate(o.root,o.component)}})},a()}(this),s.request=function(t,e,n={}){var r=new XMLHttpRequest;return new Promise((function(s,o){if(r.onreadystatechange=function(){4===r.readyState&&(r.status>=200&&r.status<300?s(r):o({status:r.status,statusText:r.statusText}))},r.open(e||"GET",t,!0),r.setRequestHeader("Content-Type",n.contentType||"application/json"),r.timeout=n.timeout||0,r.withCredentials=n.withCredentials||!1,n.header)for(let[t,e]of Object.entries(n.headers))r.setRequestHeader(t,e);n.body?r.send(n.body):r.send()}))};const requestWithBody=(t,e,n={})=>{let r={body:n};return s.request(t,e,r)};s.get=(t,e={})=>requestWithBody(t,"GET",e),s.post=(t,e={})=>requestWithBody(t,"POST",e),s.put=(t,e={})=>requestWithBody(t,"PUT",e),s.patch=(t,e={})=>requestWithBody(t,"PATCH",e),s.delete=(t,e={})=>requestWithBody(t,"DELETE",e),s.stream=()=>{const t=[],e=[],n={};let r=0;const s=function(){t.forEach(t=>{let n=e.slice(t.end,r),s=n.length;if(s>0){n=t.fn(n),Array.isArray(n)||(n=[n]),e.splice(t.end,n.length,...n);let r=s-n.length;r&&e.splice(e.length-r,r),t.end=e.length}})};return n.push=function(t){return e.push(t),r++,s(),this},n.transform=function(e){return t.push({fn:e,end:0}),s(),this},n.call=function(t){return t(e),this},n.getData=function(){return JSON.parse(JSON.stringify(e))},n.clearTransformers=function(){return t.splice(0,t.length),this},n.from=function(t){return e.splice(0,e.length),t.forEach(t=>{e.push(t)}),r=e.length,s(),this},n}; \ No newline at end of file +new function(t){let e={},n=new Map,r=[],i={segmentArr:[],routeMap:new Map,params:null};Sling=s=function(){},s.version=1.6;const o=({tagName:t,attrs:e,children:n})=>{let r,s=document.createElement(t);for(let[t,n]of Object.entries(e))"function"==(r=typeof n)?s[t]=n:s.setAttribute(t,n);for(let t of n){let e=l(t);s.appendChild(e)}return s},u=(t,e)=>{if(!e)return t=>{t.remove()};if("string"==typeof t||"string"==typeof e)return t!==e?t=>{let n=l(e);return t.replaceWith(n),n}:t=>void 0;if(t.tagName!==e.tagName)return t=>{let n=l(e);return t.replaceWith(n),n};let n=((t,e,n,r)=>{let s,i,o,u=[];if(n===r)for(let[t,n]of Object.entries(e))"function"==(s=typeof n)?u.push(e=>(e[t]=n,e)):u.push(e=>(i=e.hasAttribute(t),o=e.getAttribute(t),i&&o!==n?e.setAttribute(t,n):i||e.setAttribute(t,n),e));else{for(let[t,n]of Object.entries(e))"function"==(s=typeof n)?u.push(e=>(e[t]=n,e)):u.push(e=>(e.setAttribute(t,n),e));for(let n in t)n in e||u.push(t=>(t[n]=!1,t.removeAttribute(n),t))}return t=>{for(let e of u)e(t)}})(t.attrs,e.attrs,t.attrsStr,e.attrsStr),r=((t,e)=>{let n=[];t.forEach((t,r)=>{n.push(u(t,e[r]))});let r=[];for(let n of e.slice(t.length))r.push(t=>(t.appendChild(l(n)),t));return t=>{for(let[e,r]of((t,e)=>{let n=[];for(let r=0;r(n(t),r(t),t)},l=t=>"string"==typeof t?document.createTextNode(t):o(t),a=()=>{let e=t.location.href.split("#")[1];e&&(i.segmentArr=e.split("/"))},c=(t,e)=>{let n=(t=>(t.slOnInit&&t.slOnInit(),"string"==typeof(t=t.view())?document.createTextNode(t):o(t)))(e);return t.replaceWith(n),n};s.setState=t=>{e=t},s.getState=()=>e,s.textNode=t=>String(t),s.markup=(t,{attrs:e={},children:n=[]}={})=>{let r="";for(let[t,n]of Object.entries(e))r+=t+n;return{tagName:t,attrs:e,children:n,attrsStr:r}},s.mount=(t,e)=>{let n=t,r=document.getElementById(t);if(null!==r)return c(r,e);throw"Element ID '"+n+"' is invalid."},s.update=(t,e)=>{t=document.getElementById(t);let n=e.view.bind(e)();t=u(t,n)(t),t=n},s.clearAutoUpdate=t=>{let e=t;if(null===(t=document.getElementById(t)))throw"Element ID '"+e+"' is invalid.";n.has(t.id)&&(clearInterval(n.get(t.id)),n.delete(t.id))},s.autoUpdate=(t,e,r=17)=>{let s=t=document.getElementById(t),i=e.view.bind(e)(),o=u(s,i);t=o(t),s=i;let l=setInterval(()=>{let n=e.view.bind(e)(),r=u(s,n);t=r(t),s=n},r);n.has(t.id)&&(clearInterval(n.get(t.id)),n.delete(t.id)),n.set(t.id,l)},s.getRouteSegments=()=>i.segmentArr,s.getRouteParams=()=>i.params,s.getRoute=()=>t.location.href.split("#")[1],s.addRoute=function(t,e){i.routeMap.set(new RegExp("^"+t.replace(/:[^\/]+/g,"([^\\/]+)")+"$"),e)},s.route=function(e,o={}){i.params=o,i.routeMap.forEach((i,o)=>{if(o.test(e)){r.forEach(t=>{t()}),r=[],t.location.hash=e,a();let o=document.getElementById(i.root),u=!1;n.has(o.id)&&(clearInterval(n.get(o.id)),n.delete(o.id),u=!0),c(o,i.component),i.component.slOnDestroy&&r.push(i.component.slOnDestroy),u&&s.autoUpdate(i.root,i.component)}})},a()}(this),s.request=function(t,e,n={}){var r=new XMLHttpRequest;return new Promise((function(s,i){if(r.onreadystatechange=function(){4===r.readyState&&(r.status>=200&&r.status<300?s(r):i({status:r.status,statusText:r.statusText}))},r.open(e||"GET",t,!0),r.setRequestHeader("Content-Type",n.contentType||"application/json"),r.timeout=n.timeout||0,r.withCredentials=n.withCredentials||!1,n.header)for(let[t,e]of Object.entries(n.headers))r.setRequestHeader(t,e);n.body?r.send(n.body):r.send()}))};const requestWithBody=(t,e,n={})=>{let r={body:n};return s.request(t,e,r)};s.get=(t,e={})=>requestWithBody(t,"GET",e),s.post=(t,e={})=>requestWithBody(t,"POST",e),s.put=(t,e={})=>requestWithBody(t,"PUT",e),s.patch=(t,e={})=>requestWithBody(t,"PATCH",e),s.delete=(t,e={})=>requestWithBody(t,"DELETE",e),s.observable=t=>{const e=["pop","push","reverse","shift","unshift","splice","sort","map","filter","fill","copyWithin"],n=e.slice(0,4),r={},s=[];let i=t,o=0;const u=function(t){0===o?(s.forEach(e=>{e(t)}),o++):o=0};return r.subscribe=function(t){return s.push(t),i=new Proxy(i,{set:function(t,e,n){return"length"===e?(t[e]=n,!0):!1===isNaN(e)?(t[e]=n,u(t),!0):void 0}}),e.forEach((function(t){Object.defineProperty(i,t,{writable:!0,value:function(){let e=i;return n.indexOf(t)>-1?Array.prototype[t].apply(e,arguments):e=Array.prototype[t].apply(e,arguments),u(e),e}})})),this},r.clearSubscription=function(t){return s=s.filter(e=>e!==t),this},r.clearSubscriptions=function(){return s.splice(0,s.length),this},r.getData=function(){return i},r},s.stream=()=>{const t=[],e=[],n={},r=[];let s=0;const i=function(){t.forEach(t=>{let n=e.slice(t.end,s),r=n.length;if(r>0){n=t.fn(n),Array.isArray(n)||(n=[n]),e.splice(t.end,n.length,...n);let s=r-n.length;s&&e.splice(e.length-s,s),t.end=e.length}})},o=function(){r.forEach(t=>{t(e)})};return n.push=function(t){return e.push(t),s++,i(),o(),this},n.subscribe=function(t){return r.push(t),this},n.clearSubscription=function(t){return r=r.filter(e=>e!==t),this},n.clearSubscriptions=function(){return r.splice(0,r.length),this},n.transform=function(e){return t.push({fn:e,end:0}),i(),o(),this},n.call=function(t){return t(e),this},n.getData=function(){return JSON.parse(JSON.stringify(e))},n.clearTransformers=function(){return t.splice(0,t.length),this},n.from=function(t){return e.splice(0,e.length),t.forEach(t=>{e.push(t)}),s=e.length,i(),o(),this},n}; \ No newline at end of file diff --git a/dist/sling-reactive.min.js b/dist/sling-reactive.min.js index c98f7ec..6a750f1 100644 --- a/dist/sling-reactive.min.js +++ b/dist/sling-reactive.min.js @@ -1 +1 @@ -s.stream=()=>{const n=[],t=[],e={};let r=0;const s=function(){n.forEach(n=>{let e=t.slice(n.end,r),s=e.length;if(s>0){e=n.fn(e),Array.isArray(e)||(e=[e]),t.splice(n.end,e.length,...e);let r=s-e.length;r&&t.splice(t.length-r,r),n.end=t.length}})};return e.push=function(n){return t.push(n),r++,s(),this},e.transform=function(t){return n.push({fn:t,end:0}),s(),this},e.call=function(n){return n(t),this},e.getData=function(){return JSON.parse(JSON.stringify(t))},e.clearTransformers=function(){return n.splice(0,n.length),this},e.from=function(n){return t.splice(0,t.length),n.forEach(n=>{t.push(n)}),r=t.length,s(),this},e}; \ No newline at end of file +s.observable=t=>{const n=["pop","push","reverse","shift","unshift","splice","sort","map","filter","fill","copyWithin"],r=n.slice(0,4),e={},i=[];let s=t,c=0;const u=function(t){0===c?(i.forEach(n=>{n(t)}),c++):c=0};return e.subscribe=function(t){return i.push(t),s=new Proxy(s,{set:function(t,n,r){return"length"===n?(t[n]=r,!0):!1===isNaN(n)?(t[n]=r,u(t),!0):void 0}}),n.forEach((function(t){Object.defineProperty(s,t,{writable:!0,value:function(){let n=s;return r.indexOf(t)>-1?Array.prototype[t].apply(n,arguments):n=Array.prototype[t].apply(n,arguments),u(n),n}})})),this},e.clearSubscription=function(t){return i=i.filter(n=>n!==t),this},e.clearSubscriptions=function(){return i.splice(0,i.length),this},e.getData=function(){return s},e},s.stream=()=>{const t=[],n=[],r={},e=[];let i=0;const s=function(){t.forEach(t=>{let r=n.slice(t.end,i),e=r.length;if(e>0){r=t.fn(r),Array.isArray(r)||(r=[r]),n.splice(t.end,r.length,...r);let i=e-r.length;i&&n.splice(n.length-i,i),t.end=n.length}})},c=function(){e.forEach(t=>{t(n)})};return r.push=function(t){return n.push(t),i++,s(),c(),this},r.subscribe=function(t){return e.push(t),this},r.clearSubscription=function(t){return e=e.filter(n=>n!==t),this},r.clearSubscriptions=function(){return e.splice(0,e.length),this},r.transform=function(n){return t.push({fn:n,end:0}),s(),c(),this},r.call=function(t){return t(n),this},r.getData=function(){return JSON.parse(JSON.stringify(n))},r.clearTransformers=function(){return t.splice(0,t.length),this},r.from=function(t){return n.splice(0,n.length),t.forEach(t=>{n.push(t)}),i=n.length,s(),c(),this},r}; \ No newline at end of file diff --git a/dist/sling.min.js b/dist/sling.min.js index 2c8ce44..fa7da6a 100644 --- a/dist/sling.min.js +++ b/dist/sling.min.js @@ -1 +1 @@ -new function(t){let e={},r=new Map,n=[],o={segmentArr:[],routeMap:new Map,params:null};Sling=s=function(){},s.version=1.5;const l=({tagName:t,attrs:e,children:r})=>{let n,o=document.createElement(t);for(let[t,r]of Object.entries(e))"function"===(n=typeof r)?o[t]=r:o.setAttribute(t,r);for(let t of r){let e=u(t);o.appendChild(e)}return o},i=(t,e)=>{let r=[];t.forEach((t,n)=>{r.push(a(t,e[n]))});let n=[];for(let r of e.slice(t.length))n.push(t=>(t.appendChild(u(r)),t));return t=>{for(let[e,n]of((t,e)=>{let r=[];for(let n=0;n{if(!e)return t=>{t.remove()};if("string"==typeof t||"string"==typeof e)return t!==e?t=>{let r=u(e);return t.replaceWith(r),r}:t=>void 0;if(t.tagName!==e.tagName)return t=>{let r=u(e);return t.replaceWith(r),r};let r=((t,e,r,n)=>{let o,l,s,i=[];if(r===n)for(let[t,r]of Object.entries(e))"function"===(o=typeof r)?i.push(e=>(e[t]=r,e)):i.push(e=>(l=e.hasAttribute(t),s=e.getAttribute(t),l&&s!==r?e.setAttribute(t,r):l||e.setAttribute(t,r),e));else{for(let[t,r]of Object.entries(e))"function"===(o=typeof r)?i.push(e=>(e[t]=r,e)):i.push(e=>(e.setAttribute(t,r),e));for(let r in t)r in e||i.push(t=>(t[r]=!1,t.removeAttribute(r),t))}return t=>{for(let e of i)e(t)}})(t.attrs,e.attrs,t.attrsStr,e.attrsStr),n=i(t.children,e.children);return t=>(r(t),n(t),t)},u=t=>"string"==typeof t?document.createTextNode(t):l(t),d=()=>{let e=t.location.href.split("#")[1];e&&(o.segmentArr=e.split("/"))},c=(t,e)=>{let r=(t=>(t.slOnInit&&t.slOnInit(),"string"==typeof(t=t.view())?document.createTextNode(t):l(t)))(e);return t.replaceWith(r),r};s.setState=t=>{e=t},s.getState=()=>e,s.textNode=t=>String(t),s.markup=(t,{attrs:e={},children:r=[]}={})=>{let n="";for(let[t,r]of Object.entries(e))n+=t+r;return{tagName:t,attrs:e,children:r,attrsStr:n}},s.mount=(t,e)=>{let r=t,n=document.getElementById(t);if(null!==n)return c(n,e);throw"Element ID '"+r+"' is invalid."},s.update=(t,e)=>{t=document.getElementById(t);let r=e.view.bind(e)();t=a(t,r)(t),t=r},s.clearAutoUpdate=t=>{let e=t;if(null===(t=document.getElementById(t)))throw"Element ID '"+e+"' is invalid.";r.has(t.id)&&(clearInterval(r.get(t.id)),r.delete(t.id))},s.autoUpdate=(t,e,n=17)=>{let o=t=document.getElementById(t),l=e.view.bind(e)(),s=a(o,l);t=s(t),o=l;let i=setInterval(()=>{let r=e.view.bind(e)(),n=a(o,r);t=n(t),o=r},n);r.has(t.id)&&(clearInterval(r.get(t.id)),r.delete(t.id)),r.set(t.id,i)},s.getRouteSegments=()=>o.segmentArr,s.getRouteParams=()=>o.params,s.getRoute=()=>t.location.href.split("#")[1],s.addRoute=function(t,e){o.routeMap.set(new RegExp("^"+t.replace(/:[^\/]+/g,"([^\\/]+)")+"$"),e)},s.route=function(e,l={}){o.params=l,o.routeMap.forEach((o,l)=>{if(l.test(e)){n.forEach(t=>{t()}),n=[],t.location.hash=e,d();let l=document.getElementById(o.root),i=!1;r.has(l.id)&&(clearInterval(r.get(l.id)),r.delete(l.id),i=!0),c(l,o.component),o.component.slOnDestroy&&n.push(o.component.slOnDestroy),i&&s.autoUpdate(o.root,o.component)}})},d()}(this); \ No newline at end of file +new function(t){let e={},r=new Map,n=[],o={segmentArr:[],routeMap:new Map,params:null};Sling=s=function(){},s.version=1.6;const l=({tagName:t,attrs:e,children:r})=>{let n,o=document.createElement(t);for(let[t,r]of Object.entries(e))"function"===(n=typeof r)?o[t]=r:o.setAttribute(t,r);for(let t of r){let e=u(t);o.appendChild(e)}return o},i=(t,e)=>{let r=[];t.forEach((t,n)=>{r.push(a(t,e[n]))});let n=[];for(let r of e.slice(t.length))n.push(t=>(t.appendChild(u(r)),t));return t=>{for(let[e,n]of((t,e)=>{let r=[];for(let n=0;n{if(!e)return t=>{t.remove()};if("string"==typeof t||"string"==typeof e)return t!==e?t=>{let r=u(e);return t.replaceWith(r),r}:t=>void 0;if(t.tagName!==e.tagName)return t=>{let r=u(e);return t.replaceWith(r),r};let r=((t,e,r,n)=>{let o,l,s,i=[];if(r===n)for(let[t,r]of Object.entries(e))"function"===(o=typeof r)?i.push(e=>(e[t]=r,e)):i.push(e=>(l=e.hasAttribute(t),s=e.getAttribute(t),l&&s!==r?e.setAttribute(t,r):l||e.setAttribute(t,r),e));else{for(let[t,r]of Object.entries(e))"function"===(o=typeof r)?i.push(e=>(e[t]=r,e)):i.push(e=>(e.setAttribute(t,r),e));for(let r in t)r in e||i.push(t=>(t[r]=!1,t.removeAttribute(r),t))}return t=>{for(let e of i)e(t)}})(t.attrs,e.attrs,t.attrsStr,e.attrsStr),n=i(t.children,e.children);return t=>(r(t),n(t),t)},u=t=>"string"==typeof t?document.createTextNode(t):l(t),d=()=>{let e=t.location.href.split("#")[1];e&&(o.segmentArr=e.split("/"))},c=(t,e)=>{let r=(t=>(t.slOnInit&&t.slOnInit(),"string"==typeof(t=t.view())?document.createTextNode(t):l(t)))(e);return t.replaceWith(r),r};s.setState=t=>{e=t},s.getState=()=>e,s.textNode=t=>String(t),s.markup=(t,{attrs:e={},children:r=[]}={})=>{let n="";for(let[t,r]of Object.entries(e))n+=t+r;return{tagName:t,attrs:e,children:r,attrsStr:n}},s.mount=(t,e)=>{let r=t,n=document.getElementById(t);if(null!==n)return c(n,e);throw"Element ID '"+r+"' is invalid."},s.update=(t,e)=>{t=document.getElementById(t);let r=e.view.bind(e)();t=a(t,r)(t),t=r},s.clearAutoUpdate=t=>{let e=t;if(null===(t=document.getElementById(t)))throw"Element ID '"+e+"' is invalid.";r.has(t.id)&&(clearInterval(r.get(t.id)),r.delete(t.id))},s.autoUpdate=(t,e,n=17)=>{let o=t=document.getElementById(t),l=e.view.bind(e)(),s=a(o,l);t=s(t),o=l;let i=setInterval(()=>{let r=e.view.bind(e)(),n=a(o,r);t=n(t),o=r},n);r.has(t.id)&&(clearInterval(r.get(t.id)),r.delete(t.id)),r.set(t.id,i)},s.getRouteSegments=()=>o.segmentArr,s.getRouteParams=()=>o.params,s.getRoute=()=>t.location.href.split("#")[1],s.addRoute=function(t,e){o.routeMap.set(new RegExp("^"+t.replace(/:[^\/]+/g,"([^\\/]+)")+"$"),e)},s.route=function(e,l={}){o.params=l,o.routeMap.forEach((o,l)=>{if(l.test(e)){n.forEach(t=>{t()}),n=[],t.location.hash=e,d();let l=document.getElementById(o.root),i=!1;r.has(l.id)&&(clearInterval(r.get(l.id)),r.delete(l.id),i=!0),c(l,o.component),o.component.slOnDestroy&&n.push(o.component.slOnDestroy),i&&s.autoUpdate(o.root,o.component)}})},d()}(this); \ No newline at end of file diff --git a/dist/todo.html b/dist/todo.html index b639382..045639e 100644 --- a/dist/todo.html +++ b/dist/todo.html @@ -3,7 +3,6 @@ Demo App - diff --git a/src/todo.html b/src/todo.html index cd9bede..045639e 100644 --- a/src/todo.html +++ b/src/todo.html @@ -3,8 +3,6 @@ Demo App - -