litespeed.js
Version:
Lite & fast micro javascript framework that is easy to learn
1 lines • 14 kB
JavaScript
;var container=function(){var e=[],t={context:"",of:""},r=function(t,r,n,o){if("string"!=typeof t)throw new Error("var name must be of type string");if("function"!=typeof r&&"object"!=typeof r)throw new Error('var object "'+t+'" must be of type function or object');if("boolean"!=typeof n)throw new Error('var singelton "'+t+'" must be of type boolean');return o&&window.localStorage.setItem(t,JSON.stringify(r)),e[t]={name:t,object:r,singleton:n,instance:null},this},n={},o=function(t){var r=void 0!==e[t]?e[t]:null;if(null===r){if(n[t])return n[t];var o=window.localStorage.getItem(t);return o?(o=JSON.parse(o),n[t]=o,o):null}if(null===r.instance){var a="function"==typeof r.object?this.resolve(r.object):r.object;return r.singleton&&(r.instance=a),a}return r.instance},a=function(e){var t=this,r=/^function\s*[^\(]*\(\s*([^\)]*)\)/m,n=e.toString(),o=n.match(r)[1].split(",");return e.apply(e,o.map(function(e){return t.get(e.trim())}))},i=function(e){function t(){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e,r){e=e.replace(t.of,t.context).split(".");for(var n=this.get(e.shift());e.length>1;){if(void 0==n)return null;n=n[e.shift()]}if(void 0!==r)return n[e.shift()]=r;if(void 0==n)return null;var o=e.shift();return void 0==o?n:n[o]}),s=function(){return t},l=function(e,r){return t={context:e,of:r},this},c={set:r,get:o,resolve:a,path:i,setScope:l,getScope:s};return r("container",c,!0),c}();container.set("http",function(e){var t=[],r=[],n=function(t,r,n){var o,a=e.createElement("a"),i=/(?:\?|&|&)+([^=]+)(?:=([^&]*))*/g,s=[];for(a.href=t,r=encodeURIComponent(r);o=i.exec(a.search);)r!=o[1]&&s.push(o[1]+(o[2]?"="+o[2]:""));return s.push(r+(n?"="+encodeURIComponent(n):"")),a.search=s.join("&"),a.href},o=function(o,a,i,s,l){var c;if(-1==["GET","POST","PUT","DELETE","TRACE","HEAD","OPTIONS","CONNECT","PATCH"].indexOf(o))throw new Error("var method must contain a valid HTTP method name");if("string"!=typeof a)throw new Error("var url must be of type string");if("object"!=typeof i)throw new Error("var headers must be of type object");if("string"!=typeof a)throw new Error("var url must be of type string");for(c=0;c<t.length;c++)a=n(a,t[c].key,t[c].value);return new Promise(function(t,n){var u=new XMLHttpRequest;for(u.open(o,a,!0),u.setRequestHeader("Ajax","1"),c=0;c<r.length;c++)u.setRequestHeader(r[c].key,r[c].value);for(var p in i)i.hasOwnProperty(p)&&u.setRequestHeader(p,i[p]);u.onload=function(){4==u.readyState&&200==u.status?t(u.response):(e.dispatchEvent(new CustomEvent("http-"+o.toLowerCase()+"-"+u.status)),n(new Error(u.statusText)))},l&&(u.addEventListener("progress",l),u.upload.addEventListener("progress",l,!1)),u.onerror=function(){n(new Error("Network Error"))},u.send(s)})};return{get:function(e){return o("GET",e,{},"")},post:function(e,t,r){return o("POST",e,t,r)},put:function(e,t,r){return o("PUT",e,t,r)},"delete":function(e){return o("DELETE",e,{},"")},addGlobalParam:function(e,r){t.push({key:e,value:r})},addGlobalHeader:function(e,t){r.push({key:e,value:t})}}},!0),container.set("cookie",function(e){function t(t){var r="; "+e.cookie,n=r.split("; "+t+"=");return 2==n.length?n.pop().split(";").shift():void 0}function r(t,r,n){var o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3);var a="expires="+o.toUTCString();e.cookie=t+"="+r+";"+a+";path=/"}return{get:t,set:r}},!0),String.prototype.replaceAll=function(e,t){var r=this;return r.replace(new RegExp(e,"g"),t)},Object.path=function(e,t,r){for(t=t.split(".");t.length>1;){if(void 0==e)return null;e=e[t.shift()]}return void 0!==r?e[t.shift()]=r:void 0==e?null:e[t.shift()]},Array.prototype.includes||(Array.prototype.includes=function(e){if(null==this)throw new TypeError("Array.prototype.includes called on null or undefined");var t=Object(this),r=parseInt(t.length,10)||0;if(0===r)return!1;var n,o=parseInt(arguments[1],10)||0;o>=0?n=o:(n=r+o,0>n&&(n=0));for(var a;r>n;){if(a=t[n],e===a||e!==e&&a!==a)return!0;n++}return!1}),container.set("view",function(e,t){var r={},n=function(e,t,r,n,o){(!t.$lsStop||o)&&(r.set("element",t,!0),r.setScope(n.context,n.of),r.resolve(e.controller),r.setScope("",""),!0!==e.repeat&&t.removeAttribute(e.selector),o&&(t.$lsStop=!0))};return{stock:r,add:function(e){if("object"!=typeof e)throw new Error("var object must be of type object");var t={selector:"",controller:function(){},template:"",repeat:!1,"protected":!1};for(var n in t)t.hasOwnProperty(n)&&(n in e||(e[n]=t[n]));if(!e.selector)throw new Error("View component is missing a selector attribute");return r[e.selector]=e,this},render:function(o,a,i){var s=this,l=o?o.childNodes:[];a=a||{context:"",of:""};for(var c=0;c<l.length;c++){var u=l[c],p=!0;if(1===u.nodeType){if(u.attributes&&u.attributes.length)for(var d=0;d<u.attributes.length;d++){var f=u.attributes.length,v=u.attributes[d];if(r[v.nodeName]){var h=r[v.nodeName];void 0!=h.nested&&!1===h.nested&&(p=!1),"function"==typeof h.template&&(h.template=t.resolve(h.template)),h.template?e.get(h.template).then(function(e,r){return function(o){e.innerHTML=o,function(e,t,r,o,a){n(e,t,r,o,a)}(r,e,t,a,i),s.render(e,a,i)}}(u,h),function(e){throw new Error("Failed to load comp template: "+e.message)}):(!function(e,t,r,o,a){n(e,t,r,o,a)}(h,u,t,a,i),f!=u.attributes.length&&d--)}}!0===u.$lsSkip&&(p=!1),!1!==p&&this.render(u,a,i)}}}}},!0),container.set("form",function(){function e(e,t){switch(t){case"int":case"integer":e=parseInt(e);break;case"string":e=e.toString();break;case"json":e=e?JSON.parse(e):[];break;case"bool":case"boolean":e=!!e}return e}function t(r,n){var n=n||{},o=r.getAttribute("name"),a=r.getAttribute("type"),i=r.getAttribute("data-cast-to"),s=n;o&&"FORM"!=r.tagName&&("FIELDSET"==r.tagName?"object"==i?(void 0==n[o]&&(n[o]={}),s=n[o]):(Array.isArray(n[o])||(n[o]=[]),n[o].push({}),s=n[o][n[o].length-1]):""!=r.value&&("SELECT"==r.tagName?n[o]=r.children[r.selectedIndex].value:"radio"==a?r.checked&&(n[o]=r.value):"checkbox"==a?(Array.isArray(n[o])||(n[o]=[]),r.checked&&n[o].push(r.value)):void 0!=r.value&&(n[o]=r.value),n[o]=e(n[o],i)));for(var l=0;l<r.children.length;l++)Array.isArray(s)?(s.push({}),t(r.children[l],s[s.length])):t(r.children[l],s);return n}return{toJson:t}},!0),container.set("state",function(e){var t=[],r=null,n=null,o=function(){return r},a=function(){return n},i=function(e,t){return e in m.params?m.params[e]:t},s=function(){return m.params},l=function(e,t){return m.params[e]=t,this},c=function(){m.params=h(e.location.search)},u=function(){return e.location.href},p=function(e,r){if("string"!=typeof e)throw new Error("var path must be of type string");if("object"!=typeof r)throw new Error("var view must be of type object");return t[t.length++]={path:e,view:r},this},d=function g(e){t.sort(function(e,t){return t.path.length-e.path.length}),t.sort(function(e,t){return t.path.split("/").length-e.path.split("/").length});for(var o=0;o<t.length;o++){var a=t[o],g=new RegExp(a.path.replace(/:[^\s\/]+/g,"([\\w-]+)"));if(e.match(g))return n=r,r=a,a}return null},f=function(t){e.history.pushState({},"",t),e.dispatchEvent(new PopStateEvent("popstate",{}))},v=function(){f(e.location.href)},h=function(e){var t;if(e){var r=location.href.indexOf("?");if(-1==r)return[];t=location.href.substr(r+1)}else t=location.search.substr(1);var n={};return t.split("&").forEach(function(e){if(e){e=e.split("+").join(" ");var t=e.indexOf("="),r=t>-1?e.substr(0,t):e,o=t>-1?decodeURIComponent(e.substr(t+1)):"",a=r.indexOf("[");if(-1==a)n[decodeURIComponent(r)]=o;else{var i=r.indexOf("]"),s=decodeURIComponent(r.substring(a+1,i));r=decodeURIComponent(r.substring(0,a)),n[r]||(n[r]=[]),s?n[r][s]=o:n[r].push(o)}}}),n},m={setParam:l,getParam:i,getParams:s,reset:c,change:f,reload:v,getURL:u,add:p,match:d,getCurrent:o,getPrevious:a,params:{}};return m},!0),container.set("expression",function(e){var t=/(\{{.*?\}})/gi;return{parse:function(r){return r.replace(t,function(t){var r=t.substring(2,t.length-2).replace("['",".").replace("']","").trim(),n=e.path(r);return n=null==n||void 0==n?"":n,n="object"==typeof n?JSON.stringify(n):n})}}},!0),container.set("window",window,!0).set("document",window.document,!0).set("element",window.document,!0);var app=function(e){return{run:function(t){try{container.get("http").addGlobalParam("version",e),this.view.render(t.document)}catch(r){var n=container.resolve(this.error);n(r)}},error:function(){return function(e){console.error("error",e.message,e.stack,e.toString())}},container:container,view:this.container.get("view")}};container.get("view").add({selector:"data-ls-init",template:!1,repeat:!0,controller:function(e,t,r,n,o){var a={selector:"data-ls-scope",template:!1,repeat:!0,controller:function(){},state:!0},i=function(i){if(t.scrollTo(0,0),o.reset(),a["protected"]=void 0!==i.view["protected"]?i.view["protected"]:!1,a["protected"]&&null==o.getPrevious())throw new Error("CSRF protection");a.template=void 0!==i.view.template?i.view.template:null,a.controller=void 0!==i.view.controller?i.view.controller:function(){},a.state=void 0!==i.view.state?i.view.state:!0,r.dispatchEvent(new CustomEvent("state-change")),null==o.getPrevious()?a.template="":(a.nested=!1,n.render(e)),r.dispatchEvent(new CustomEvent("state-changed"))},s=function(e,t){if((t.nodeName||t.tagName).toLowerCase()===e.toLowerCase())return t;for(;t=t.parentNode;)if((t.nodeName||t.tagName).toLowerCase()===e.toLowerCase())return t;return null};n.add(a),r.addEventListener("click",function(e){var r=s("a",e.target);if(!r)return!1;if(!r.href)return!1;if(e.metaKey)return!1;if(r.hasAttribute("target")&&"_blank"==r.getAttribute("target"))return!1;if(r.hostname!=t.location.hostname)return!1;var n=o.match(r.pathname);return null===n?!1:(e.preventDefault(),t.location==r.href?!1:(n.view.state=void 0===n.view.state?!0:n.view.state,!0===n.view.state&&t.history.pushState({},"Unknown",r.href),i(n),!0))}),t.addEventListener("popstate",function(){i(o.match(t.location.pathname))}),i(o.match(t.location.pathname))}}),container.get("view").add({selector:"data-ls-print",template:!1,repeat:!0,controller:function(e,t){var r=t.parse("{{"+e.dataset.lsPrint+"}}"),n=e.getAttribute("data-default")||"";if(r=r||n,"INPUT"==e.tagName||"SELECT"==e.tagName||"BUTTON"==e.tagName||"TEXTAREA"==e.tagName){var o=e.getAttribute("type");"radio"==o||"checkbox"==o?r.toString()==e.value||r.indexOf(e.value)>-1?e.setAttribute("checked","checked"):e.removeAttribute("checked"):(e.value!=r&&(e.value=r),e.dispatchEvent(new window.Event("change")))}else e.innerText!=r&&(e.innerText=r)}}),container.get("view").add({selector:"data-ls-rerender",template:!1,repeat:!0,controller:function(e,t,r,n,o){var a=e.dataset.lsRerender||"";a=a.trim().split(",");for(var i=0;i<a.length;i++)""!=a[i]&&o.addEventListener(a[i],function(){t.render(e)})}}),container.get("view").add({selector:"data-ls-selected",template:!1,repeat:!0,controller:function controller(element,expression){var result=!!eval(expression.parse(element.dataset.lsSelected));element.$lsSkip=!result,result?element.classList.add("selected"):element.classList.remove("selected")}}),container.get("view").add({selector:"data-ls-eval",template:!1,controller:function controller(element,expression){var statement=expression.parse(element.dataset.lsEval);eval(statement)}}),container.get("view").add({selector:"data-ls-loop",template:!1,repeat:!0,nested:!1,controller:function(e,t,r,n){var o=r.path(e.dataset.lsLoop);if(o=null==o?[]:o,e.template=e.template?e.template:1==e.children.length?e.children[0].innerHTML:"",e.clone||(e.clone=1==e.children.length?e.children[0]:n.document.createElement("li")),e.innerHTML="",o instanceof Array&&"object"!=typeof o)throw new Error("Reference '"+path+"' value must be array or object. "+typeof o+" given");var a=[],i=r.getScope();e.style.visibility=0==o.length?"hidden":"visible";for(var s in o)if(o.hasOwnProperty(s)){a[s]=a[s]||e.clone.cloneNode(!0),a[s]=e.clone.cloneNode(!0),a[s].innerHTML=e.template,e.appendChild(a[s]),function(r){var n=e.dataset.lsLoop+"."+r;t.render(a[s],{context:n.replace(i.of,i.context),of:e.dataset.lsAs},!0)}(s);var l=function(r){var n=function o(n){!function(r){var o=e.dataset.lsLoop+"."+r;t.render(n.target,{context:o.replace(i.of,i.context),of:e.dataset.lsAs},!0)}(r),a[r].removeEventListener("template-loaded",o,!1)};return n}(s);a[s].addEventListener("template-loaded",l,!1)}}}),container.get("view").add({selector:"data-ls-template",template:!1,repeat:!0,controller:function(e,t,r,n,o){var a=n.parse(e.dataset.lsTemplate),i=e.dataset.type||"url";e.innerHTML="";var s=function(e,r){r.innerHTML=e,t.render(r),r.dispatchEvent(new CustomEvent("template-loaded",{bubbles:!0,cancelable:!1}))};if("script"==i){var l=o.getElementById(a);return void(l&&l.innerHTML?s(l.innerHTML,e):e.innerHTML='<span style="color: red">Missing template "'+a+'"</span>')}r.get(a).then(function(e){return function(t){s(t,e)}}(e),function(){throw new Error("Failed loading template")})}}),container.get("view").add({selector:"data-ls-href",template:!1,repeat:!0,controller:function(e,t){e.href=t.parse(e.dataset.lsHref)}}),container.get("view").add({selector:"data-ls-if",template:!1,repeat:!0,controller:function controller(element,expression){var result=!!eval(expression.parse(element.dataset.lsIf));element.$lsSkip=!result,result?(element.style.removeProperty("display"),element.style.removeProperty("visibility")):(element.style.visibility="hidden",element.style.display="none")}}),container.get("view").add({selector:"data-ls-hide",template:!1,repeat:!0,controller:function controller(element,expression){element.style.display=eval(expression.parse(element.dataset.lsHide))?"inherit":"none"}}),container.get("view").add({selector:"data-ls-class",template:!1,repeat:!0,controller:function(e,t){e.className=t.parse(e.dataset.lsClass)}}),container.get("view").add({selector:"data-ls-src",template:!1,repeat:!0,controller:function(e,t){e.addEventListener("error",function(){e.style.opacity=0}),e.addEventListener("load",function(){e.style.opacity=1});var r=t.parse(e.dataset.lsSrc);r!=e.src&&""!=r&&(e.src=r)}});