-
Notifications
You must be signed in to change notification settings - Fork 2
/
skeleton.min.js
1 lines (1 loc) · 19.3 KB
/
skeleton.min.js
1
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},Skeleton=function(){function e(t){function n(e){var n=Object.assign({},t.defaults)||{};this.get=function(e){return n[e]||null},this.set=function(){if(2===arguments.length)n[arguments[0]]=arguments[1];else{if(1!==arguments.length)throw new Error("Error on setting a value");var e=arguments[0];for(var t in e)n[t]=e[t]}},this.toJSON=function(){return n};for(var r in e)this.set(r,e[r]);t&&t.init&&t.init.call(this)}if(!(this instanceof e))return new e(t);if(!t||!t.defaults)throw new Error('A "defaults" field must be passed');for(var r in t)"init"!==r&&"defaults"!==r&&(n.prototype[r]=t[r]);return n}function t(e){function o(e,t){if(e){if(!A[e])throw new Error("The type passed is not a possible type");A[e].forEach(function(e){return e(t)})}else A.push.forEach(function(e){return e(t)}),A.remove.forEach(function(e){return e(t)})}function i(e,t){e instanceof S||(e=new S(e));var n=g();e.set("index",n);var r=e.toJSON();j[n]=e,s(r,t),o("push",r)}function a(e,t){var n='[data-id="'+e+'"]',r=I.querySelector(n);if(!r)throw new Error('Make sure your you set a "data-id" attribute to each model');I.replaceChild(t,r)}function s(e,t){var n=r(c(f(e),e));if("push"===t)I.appendChild(n);else{if("unshift"!==t)throw new Error('unknown method passed to "_updateSingleModelView"');I.insertBefore(n,I.childNodes[0])}}function u(e){var t='[data-id="'+e+'"]',n=I.querySelector(t);n&&n.remove()}function l(e){var t=e||L.models(),n="";return t.forEach(function(e){n+=c(f(e),e)}),n}function c(e,t){var o=void 0;"string"==typeof e&&(o=r(e));var i=o.querySelectorAll("[data-loop]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(r,o){var i=r.getAttribute("data-loop").trim(),a=t[i];if(!a)throw new Error(i+" attribute does not appear in model");if(!Array.isArray(a))throw new Error(i+"'s value must be an array");var s=n(r),u="";a.forEach(function(e){u+=s.replace(E,function(t,n){return-1!==n.indexOf("|")?d(e,n):w(e,n)})}),e=e.replace(s,u)}),e):e}function f(e){var t=T;return t=t.replace(b,function(t,n){return-1!==n.indexOf("|")?d(e,n):w(e,n)}),t=p(e,t),t=h(e,t),t=m(e,t),t=v(e,t),t=y(e,t)}function d(e,t){var n=t.split("|"),r=n[0].trim(),o=n[1].trim(),i=w(e,r);if(!i)throw new Error('Please check the expression "'+r+'" you passed in the template');if(x[o])return x[o](i);throw new Error('The filter you are using does not exist. Please use "addFilter" function to create it.')}function p(e,t){var o=r(t),i=o.querySelectorAll("[data-checked]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(t){var n=t.getAttribute("data-checked").trim(),r=!!e[n];r?t.setAttribute("checked","checked"):t.getAttribute("checked")&&t.removeAttribute("checked")}),n(o)):t}function h(e,t){var o=r(t),i=o.querySelectorAll("[data-hide]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(t){var n=t.getAttribute("data-hide").trim(),r=!!e[n];r?t.style.display="none":t.style.display=""}),n(o)):t}function m(e,t){var o=r(t),i=o.querySelectorAll("[data-show]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(t){var n=t.getAttribute("data-show").trim(),r=!!e[n];r?t.style.display="":t.style.display="none"}),n(o)):t}function v(e,t){var o=r(t),i=o.querySelectorAll("[data-style]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(t){var n=t.getAttribute("data-style").trim(),r=void 0;try{r=JSON.parse(n)}catch(o){throw new Error("data-style attribute must be passed as a stringified json object")}Object.keys(r).forEach(function(n){var o=r[n].split("?");if(2!==o.length)throw new Error("data-style needs an expression to evaluate");var i=o[0].trim(),a=o[1].split(":");if(2!==a.length)throw new Error("Error on data-style attribute");var s=e[i]?a[0]:a[1];t.style[n]=s})}),n(o)):t}function y(e,t){var o=r(t),i=o.querySelectorAll("[data-class]");return i&&i.length?(Array.prototype.slice.call(i).forEach(function(t){var n=t.getAttribute("data-class").trim(),r=void 0;try{r=JSON.parse(n)}catch(o){throw new Error("data-class attribute must be passed as a stringified json object")}Object.keys(r).forEach(function(n){var o=r[n].trim(),i=void 0;i="!"===o.charAt(0)?!e[o.substring(1).trim()]:e[o],i?t.classList.add(n):t.classList.remove(n)})}),n(o)):t}function w(e,t){if("this"===t)return e;var n=t.split(".");if(1===n.length)return e[t];for(var r=e[n[0].trim()],o=1;o<n.length;o++)r=r[n[o].trim()];return r}function g(){return k++}if(!(this instanceof t))return new t(e);var b=/{{\s*((\w+\.?\w+?)*\s*\|?\s*\w+)\s*}}/g,E=/{{\s*#\s*((\w+\.?\w+?)*\s*\|?\s*\w+)\s*}}/g,k=0,A={push:[],remove:[],filter:[],sort:[],pushAll:[],removeAll:[],edit:[]},x={upper:function(e){return e.toUpperCase()},lower:function(e){return e.toLowerCase()},capitalize:function(e){return e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()},currency:function(e){return"$"+e},json:function(e){try{e=JSON.stringify(e)}catch(t){throw new Error("The argument passed can not be stringified to a json string")}return e}},S=e&&e.model,I=document.getElementById(e&&e.element),T=void 0;if(e&&e.template&&e.templateId)throw new Error('Only "template" or "templateId" attribute should be supplied, not both');if(!e.template&&!e.templateId)throw new Error("You must pass a template string, or a templateId of a template element");if(e.template)if("string"==typeof e.template)T=e.template;else if(e.template.templateId){var N=document.getElementById(e.template.templateId);if(!N)throw new Error('Please provide "templateId" attribute');T=N.innerHTML}if(e.templateId){var O=document.getElementById(e.templateId);if(!O)throw new Error('Please provide "templateId" attribute');T=O.innerHTML}if(!S)throw new Error("A model must be supplied");if(!I)throw new Error("An element must be supplied, provided by its id");if(!T)throw new Error("A template id or string must be supplied");var L=this,j={};this.getCollection=function(){return Object.keys(j).map(function(e){return j[e]})},this.models=function(){return Object.keys(j).map(function(e){return j[e].toJSON()})},this.pushAll=function(e){if(!e||!Array.isArray(e))throw new Error("pushAll method must receive an array as an argument");e.forEach(function(e){e.index=g(),j[e.index]=new S(e)}),I.innerHTML+=l(),o("pushAll",this.models())},this.push=function(e){if(!e)throw new Error("push method must receive a model as an argument");i(e,"push")},this.unshift=function(e){if(!e)throw new Error("unshift method must receive a model as an argument");i(e,"unshift")},this.remove=function(e){if(j[e]){var t=j[e];return delete j[e],u(e),o("remove",t),t}},this.lastIndex=function(){return 0===this.size()?-1:k-1},this.firstIndex=function(){var e=Object.keys(j);return e.length?Math.min.apply(null,e):-1},this.removeAll=function(){j={},I.innerHTML="",o("removeAll")},this.get=function(e){return j[e]?j[e].toJSON():null},this.getModel=function(e){return j[e]?j[e]:null},this.size=function(){return Object.keys(j).length},this.filter=function(e){var t=this.models().filter(e);return I.innerHTML=l(t),o("filter",t),t},this.sort=function(e){e=e||function(e,t){return e.index-t.index};var t=this.models().sort(e),n={};return t.forEach(function(e){n[e.index]=j[e.index]}),j=n,I.innerHTML=l(t),o("sort",t),t},this.forEach=function(e){this.models().forEach(e)},this.edit=function(e,t){if(t){var n=j[e];for(var i in t)n.set(i,t[i]);var s=n.toJSON(),u=c(f(s),s),l=r(u);a(e,l),o("edit",s)}},this.addFilter=function(e,t){if("string"!=typeof e)throw new Error("Filter name must be a string");if("function"!=typeof t)throw new Error("Filter callback must be a function");x[e]=t},this.subscribe=function(){function e(e,t){for(var n=0;n<A[e].length;n++)if(A[e][n]===t){A[e].splice(n,1);break}}var t=arguments;if(1===arguments.length&&"function"==typeof arguments[0]){var n=function(){var n=t[0];return A.push.push(n),A.remove.push(n),{v:function(){e("push",n),e("remove",n)}}}();if("object"===("undefined"==typeof n?"undefined":_typeof(n)))return n.v}else{if(2!==arguments.length)throw new Error('You should pass a callback function or a type "push" or "remove" and a callback to subscribe');var r=function(){var n=t[0],r=t[1];if(Array.isArray(n))return n.forEach(function(e){if(!A[e])throw new Error("type "+e+' is not a possible type. possible types: "push", "remove", "filter", "sort", "edit", "pushAll", "removeAll"');A[e].push(r)}),{v:function(){return n.forEach(function(t){return e(t,r)})}};if(A[n])return A[n].push(r),{v:function(){return e(n,r)}};throw new Error("type "+n+' is not a possible type. possible types: "push", "remove", "filter", "sort", "edit", "pushAll", "removeAll"')}();if("object"===("undefined"==typeof r?"undefined":_typeof(r)))return r.v}}}function n(e){var t=document.createElement("div");return t.appendChild(e),t.innerHTML}function r(e){var t=document.createElement("div");return t.innerHTML=e,t.firstElementChild}function o(){if(!(this instanceof o))return new o;if(!window)throw new Error("I only run on the browser!");window.onpopstate=function(){return n()};var e={},t={},n=function(){var n=void 0,r=0,o=location.pathname,i=o.split("/").slice(1);for(var a in t){if(1===r)break;if(n=t[a],a=a.split(","),a.length===i.length)for(var s=0;s<a.length;s++){var u=a[s];if(":"!=u.charAt(0)){if(u===i[s]){r=1;continue}r=0,e={},n=null;break}r=1,e[u.slice(1)]=i[s]}}if(!n)throw new Error("No handler set for this route!");n.call(null,e),e={}};this.path=function(e,n){t[e.split("/").slice(1)]=n},this.visit=function(e){return history.pushState(null,null,e),n()}}function i(e){try{return e=JSON.stringify(e)}catch(t){return e}}function a(e){try{return e=JSON.parse(e)}catch(t){return e}}function s(e,t){var n=arguments.length<=2||void 0===arguments[2]?"keyup":arguments[2],r=document.getElementById(e);if(!r)throw new Error("The id '"+e+"' does not match any dom element");if("INPUT"!==r.nodeName&&"TEXTAREA"!==r.nodeName)throw new Error("The id '"+e+"' must match an input or textarea element");v[e]=r,t&&r.addEventListener(n,t)}function u(e){if(!e.name)throw new Error('Skeleton.form must recieve a "name" field with the form\'s name');var t=e.name,n=document.querySelector("form[name='"+t+"']");if(!n)throw new Error("No form element with name "+t);var r={};r.name=e.name;var o=e.inputs;if(o)for(var i in o){var a=o[i],s=n.querySelector("#"+a);if(!s)throw new Error("No element with id "+a);if("INPUT"!==s.nodeName&&"TEXTAREA"!==s.nodeName)throw new Error("The id '"+a+"' must match an input or textarea element");r[i]=s}if(!e.submit)throw new Error('"submit" button id or event key code and input field must be supplied');if(!e.onSubmit)throw new Error('"onSubmit" method must be supplied');if(y[t]=r,e.submit.input&&e.submit.keyCode)!function(){var n=e.submit.input,r=e.submit.keyCode;if(!n)throw new Error("Please supply input element to trigger submit event on by its field");if(!r)throw new Error("Please supply keyCode to fire event on");y[t][n].addEventListener("keydown",function(n){n.keyCode===r&&(n.preventDefault(),e.onSubmit.call(y[t],n))})}();else{var u=document.getElementById(e.submit);if(!u)throw new Error("Id passed as submit button id does not exist");u.addEventListener("click",function(n){n.preventDefault(),e.onSubmit.call(y[t],n)})}}function l(e){var t=document.getElementById(e);if(!t)throw new Error(e+" id does not match any element");return{to:function(){var e=Array.prototype.slice.call(arguments),n=e.map(function(e){var t=document.getElementById(e);if(!t||"INPUT"!==t.nodeName&&"TEXTAREA"!==t.nodeName)throw new Error(e+" id does not match any element or the element it matches is not input or textarea element");return t});return{exec:function(e){for(var r=arguments.length<=1||void 0===arguments[1]?"keyup":arguments[1],o=0;o<n.length;o++){var i=n[o];i.addEventListener(r,function(r){var o=n.map(function(e){var t=e.value;return t?t:""});t.textContent=e.apply(null,o)})}}}}}}function c(){return this instanceof c?(this.arr=function(e){return Array.isArray(e)},this.str=function(e){return"string"==typeof e},this.func=function(e){return"function"==typeof e},this.num=function(e){return"number"==typeof e},this.obj=function(e){return-1!==Object.prototype.toString.call(e).indexOf("object")},this["null"]=function(e){return null===e},this.undef=function(e){return void 0===e},this.none=function(e){return this["null"](e)||this.undef(e)},this.hex=function(e){return/^#([0-9A-Z]{6}$|[0-9A-Z]{3}$)/i.test(e)},this.rgb=function(e){return/^rgb\(\s*\w\s*\,\s*\w\s*\,\s*\w\s*\)/i.test(e)},this.rgba=function(e){return/^rgba\(\s*\w\s*\,\s*\w\s*\,\s*\w\s*\)/i.test(e)},this.color=function(e){return this.hex(e)||this.rgb(e)||this.rgba(e)},void(this.html=function(e){return e instanceof HTMLCollection||e instanceof NodeList})):new c}function f(){if(!(this instanceof f))return new f;var e=this,t={overlay:{bgcolor:"black",opacity:"0.8"},popup:{width:"400",height:"400",bgcolor:"white"}};this.setDefaults=function(e){var n=e.overlay,r=e.popup;if(n)for(var o in n)t.overlay[o]=n[o];if(r)for(var i in r)t.popup[i]=r[i]},this.open=function(t,n){switch(document.getElementById("skeleton-popup")&&this.close(),this.openOverlay(),document.body.innerHTML+=t,document.getElementById("skeleton-overlay").addEventListener("click",e.close),n.type){case"confirm":document.getElementById("confirm-yes-label").addEventListener("click",n.approve),document.getElementById("confirm-no-label").addEventListener("click",n.regret);break;case"message":document.getElementById("close-message-popup").addEventListener("click",e.close);break;default:throw new Error("Only confirm and message popups are supported")}},this.close=function(){var t=document.getElementById("skeleton-popup");t&&(document.getElementById("skeleton-overlay")&&e.closeOverlay(),t.remove())},this.openOverlay=function(){var e=t.overlay,n=e.bgcolor,r=e.opacity,o="<div id='skeleton-overlay' style='background-color: "+n+"; opacity: "+r+"; height: 100%; width: 100%; position: fixed; top: 0; left: 0; bottom: 0; right: 0;'></div>";document.body.innerHTML+=o},this.closeOverlay=function(){var e=document.getElementById("skeleton-overlay");e&&e.remove()},this.message=function(e){var n=e.height||t.popup.height,r=e.width||t.popup.width,o=t.popup.bgcolor||"white",i=e.closeMessage||"Close",a=['<div id="skeleton-popup" style="height: '+n+"px; width: "+r+"px; background-color: "+o+'; z-index: 100; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto;">',"<h2>"+e.title+"</h2>","<p>"+e.body+"</p>",'<button id="close-message-popup">'+i+"</button>","</div>"].join("");e.type="message",this.open(a,e)},this.confirm=function(e){var n=e.height||t.popup.height,r=e.width||t.popup.width,o=t.popup.bgcolor||"white",i=e.yesLabel||"Yes",a=e.noLabel||"No",s=['<div id="skeleton-popup" style="height: '+n+"px; width: "+r+"px; background-color: "+o+'; z-index: 100; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto;">',"<h2>"+e.title+"</h2>","<p>"+e.body+"</p>",'<button id="confirm-yes-label">'+i+"</button>",'<button id="confirm-no-label">'+a+"</button>","</div>"].join("");e.type="confirm",this.open(s,e)}}function d(){if(!(this instanceof d))return new d;var e={};this.on=function(t,n){e[t]?e[t].push(n):e[t]=[n]},this.dispose=function(t){e[t]&&delete e[t]},this.emit=function(t){if(!t)throw new Error("First argument must be an event");if(!e[t])throw new Error("No handler for '"+t+"' event!");var n=[];arguments.length>1&&(n=Array.prototype.slice.call(arguments,1)),e[t].forEach(function(e){return e.apply(null,n)})}}function p(e){if("object"!==("undefined"==typeof e?"undefined":_typeof(e)))throw new Error('The parameter provided to "Skeleton.network" must be an object');var t=e.online,n=e.offline;t||(t={}),n||(n={});var r=document.createElement("div"),o=document.createTextNode(n.message||"Lost Internet Connection...");if(r.appendChild(o),r.id="skeleton-offline-popup",Object.assign(r.style,{position:"fixed",zIndex:1e3,padding:"15px",left:0,right:0,opacity:1,width:n.width||"100%",height:n.height||"60px",color:n.color||"black",textAlign:n.textAlign||"center",backgroundColor:n.backgroundColor||"#c61313",fontSize:n.fontSize||"20px",fontWeight:n.fontWeight||"700",fontFamily:n.fontFamily||"Verdana, sans-serif",border:n.border||"none"}),!n.position||n.position&&"top"===n.position.trim())r.style.top=0;else if("middle"===n.position)Object.assign(r.style,{top:0,bottom:0,margin:"auto"});else{if("bottom"!==n.position)throw new Error('position can be "top", "bottom" or "middle"');r.style.bottom=0}window.addEventListener("offline",function(){var e=document.getElementById("skeleton-online-popup");e&&e.remove(),document.body.insertBefore(r,document.body.childNodes[0])});var i=document.createElement("div"),a=document.createTextNode(t.message||"Internet Connection is back");if(i.appendChild(a),i.id="skeleton-online-popup",Object.assign(i.style,{position:"fixed",zIndex:1e3,padding:"15px",left:0,right:0,opacity:1,width:t.width||"100%",height:t.height||"60px",color:t.color||"white",textAlign:t.textAlign||"center",backgroundColor:t.backgroundColor||"#328134",fontSize:t.fontSize||"20px",fontWeight:t.fontWeight||"700",fontFamily:t.fontFamily||"Verdana, sans-serif",border:t.border||"none"}),!t.position||t.position&&"top"===t.position.trim())i.style.top=0;else if("middle"===t.position)Object.assign(i.style,{top:0,bottom:0,margin:"auto"});else{if("bottom"!==t.position)throw new Error('position can be "top", "bottom" or "middle"');i.style.bottom=0}window.addEventListener("online",function(){var e=document.getElementById("skeleton-offline-popup");e&&e.remove();var t=i.cloneNode(!0);document.body.insertBefore(t,document.body.childNodes[0]);var n=document.getElementById("skeleton-online-popup");window.setTimeout(function(){var e=window.setInterval(function(){n.style.opacity<=0&&(window.clearInterval(e),n.remove()),n.style.opacity-=.15},120)},2e3)})}function h(){var e=document.cookie,t=e.split(";"),n={};return t.forEach(function(e){var t=e.split("=");2===t.length&&(n[t[0].trim()]=t[1].trim())}),n}var m={save:function(){if(window.localStorage)if(2===arguments.length){var e=arguments[0],t=arguments[1];if("string"!=typeof e)throw new Error("First item must be a string");t=i(t),window.localStorage.setItem(e,t)}else{if(1!==arguments.length||"object"!==_typeof(arguments[0]))throw new Error("Method save must get key an value, or an object of keys and values");var n=arguments[0];for(var r in n){var o=n[r];o=i(o),window.localStorage.setItem(r,o)}}},fetch:function(e){if(window.localStorage){var t=window.localStorage.getItem(e);return t?a(t):null}},clear:function(){window.localStorage&&window.localStorage.clear()}},v={},y={};return s.get=function(e){var t=v[e];if(!t)throw new Error("The id '"+e+"' was not set to be cached. Please use the 'input' function first");return t.value},s.set=function(e,t){var n=v[e];if(!n)throw new Error("The id '"+e+"' was not set to be cached. Please use the 'input' function first");n.value=t},s.clear=function(e){e?v[e].value="":Object.keys(v).forEach(function(e){return s.set(e,"")})},u.clear=function(e){var t=y[e];if(!t)throw new Error("The name "+e+" is not recognized as a form name");for(var n in t){var r=t[n];!r.nodeName||"INPUT"!==r.nodeName&&"TEXTAREA"!==r.nodeName||(r.value="")}},h.get=function(e){var t=h();return t[e]?t[e]:null},h.set=function(e,t){var n=arguments.length<=2||void 0===arguments[2]?1:arguments[2],r=new Date;r.setTime(r.getTime()+24*n*60*60*1e3);var o="expires="+r.toUTCString();document.cookie=e+"="+t+";"+o},h["delete"]=function(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"},{Model:e,List:t,Router:o,Event:d,Popup:f,Type:c,network:p,storage:m,cookies:h,form:u,input:s,bind:l}}();