UNPKG

statelessjs

Version:

Front end javascript library for building web applications

2 lines (1 loc) 16.3 kB
!function(e,n,t){if(!e.stateless){var r=document.createElement("div"),o=function(e){var o={};if(!e)throw new Error("No element selected");var s={};Object.setPrototypeOf(o,s),Object.defineProperty(s,"parent",{enumerable:!1,configurable:!1,get:function(){return e.parentNode&&e.parentNode.scope}}),Object.defineProperty(s,"children",{enumerable:!1,configurable:!1,get:t(function(n,t){t=t||e.children;var r=[];return Array.prototype.forEach.call(t,function(e){e.scope!=o?r.push(e.scope):Array.prototype.push.apply(r,n(e.children))}),r})}),Object.defineProperty(s,"root",{enumerable:!1,configurable:!1,get:t(function(e,n){return n=n||o,n.parent?e(n.parent):n})});var i=function(e,n){Object.defineProperty(e,"scope",{enumerable:!1,configurable:!1,get:function(){return o}}),!1!==n&&Array.prototype.forEach.call(e.querySelectorAll("*"),function(e){i(e,!1)})};i(e);var u={};return s.on=n().args({scope:{on:"function"},addEventListener:"function"},"string","function").use(function(e,n,t){if(e.scope==o){var r=!1;(u[n]||(u[n]=[])).forEach(function(n){n.fn!=t&&n.fn.toString()!=t.toString()||n.el!=e||(r=!0)}),r?console.warn("listener is already registered"):(e.addEventListener(n,t),u[n].push({fn:t,el:e}))}else e.scope.on(e,n,t);return o}).args({0:"object",length:"number",forEach:"function"},"string","function").use(function(e,n,t){return e.forEach(function(e){o.on(e,n,t)}),o}).args("string","string","function").use(function(e,n,t){return"$"==e[0]?o.elements(e).forEach(function(e){o.on(e,n,t)}):console.warn("selector must begin with '$'"),o}).args("string","function").use(function(n,t){return o.on(e,n,t),o}).args().use(function(){return console.warn("on function inputs improperly formatted"),o}),s.off=n().args({scope:{off:"function"},addEventListener:"function"},"string","function").use(function(e,n,t){if(e.scope==o){var r=u[n],s=!1;r&&r.forEach(function(o,i){o.fn!=t&&o.fn.toString()!=t.toString()||o.el!=e||(s=!0,e.removeEventListener(n,o.fn),r.splice(i,1))}),s||console.warn("Listener is not currently registered")}else e.scope.off(e,n,t);return o}).args({0:"object",length:"number",forEach:"function"},"string","function").use(function(e,n,t){e.forEach(function(e){o.off(e,n,t)})}).args("string","string","function").use(function(e,n,t){return"$"==e[0]?o.elements(e).forEach(function(e){o.off(e,n,t)}):console.warn("selector must begin with '$'"),o}).args("string","function").use(function(n,t){return o.off(e,n,t),o}).args().use(function(){return console.warn("off function inputs improperly formatted"),o}),s.once=n().args({scope:{off:"function"},addEventListener:"function"},"string","function").use(function(e,n,t){if(e.scope==o){var r=function(s){t(s),o.off(e,n,r)};o.on(e,n,r)}else e.scope.once(e,n,t);return o}).args({0:"object",length:"number",forEach:"function"},"string","function").use(function(e,n,t){e.forEach(function(e){o.once(e,n,t)})}).args("string","string","function").use(function(e,n,t){return"$"==e[0]?o.elements(e).forEach(function(e){o.once(e,n,t)}):console.warn("selector must begin with '$'"),o}).args("string","function").use(function(n,t){return o.once(e,n,t),o}).args().use(function(){return console.warn("once function inputs improperly formatted"),o}),s.hasClass=n().args({className:"string"},"string").use(function(e,n){return!!e.className.match(new RegExp("(^"+n+"$|\\s"+n+"\\s|^"+n+"\\s|\\s"+n+"$)"))}).args("string","string").use(function(e,n){return"$"==e[0]?o.hasClass(o.element(e),n):(console.warn("selector must begin with '$'"),!1)}).args("string").use(function(n){return o.hasClass(e,n)}).args().use(function(e){return console.warn("malformed call"),!1}),s.addClass=n().args({scope:"object",className:"string"},"string","boolean").use(function(e,n,t){return e.scope==o?!t&&o.hasClass(e,n)||(e.className+=" "+n,e.className=e.className.replace(/(^\s+|\s+$)/g,"")):e.scope.addClass(e,n,t),o}).args({scope:"object",className:"string"},"string").use(function(e,n){return o.addClass(e,n,!1),o}).args({0:"object",length:"number",forEach:"function"},"string","boolean").use(function(e,n,t){e.forEach(function(e){o.addClass(e,n,t)})}).args("string","string","boolean").use(function(e,n,t){return"$"==e[0]?o.elements(e).forEach(function(e){o.addClass(e,n,t)}):console.warn("selector must begin with '$'"),o}).args({0:"object",length:"number",forEach:"function"},"string").use(function(e,n){e.forEach(function(e){o.addClass(e,n,!1)})}).args({scope:"object",className:"string"},"string").use(function(e,n){return o.addClass(e,n,!1),o}).args("string","string").use(function(e,n){return"$"==e[0]?o.elements(e).forEach(function(e){o.addClass(e,n,!1)}):console.warn("selector must begin with '$'"),o}).args("string","boolean").use(function(n,t){return o.addClass(e,n,t),o}).args("string").use(function(n){return o.addClass(e,n,!1),o}).args().use(function(){return console.warn("addClass function inputs improperly formatted"),o}),s.removeClass=n().args({scope:"object",className:"string"},"string","boolean").use(function(e,n,t){if(e.scope==o){var r=t?"g":"",s=new RegExp("(^"+n+"$|\\s"+n+"\\s|^"+n+"\\s|\\s"+n+"$)",r);e.className=e.className.replace(s," ").replace(/(^\s+|\s+$)/g,"").replace(/\s+/g," ")}else e.scope.removeClass(e,n,t);return o}).args({scope:"object",className:"string"},"string").use(function(e,n){return o.removeClass(e,n,!1),o}).args({0:"object",length:"number",forEach:"function"},"string","boolean").use(function(e,n,t){e.forEach(function(e){o.removeClass(e,n,t)})}).args("string","string","boolean").use(function(e,n,t){return"$"==e[0]?o.elements(e).forEach(function(e){o.removeClass(e,n,t)}):console.warn("selector must begin with '$'"),o}).args({0:"object",length:"number",forEach:"function"},"string").use(function(e,n){e.forEach(function(e){o.removeClass(e,n,!1)})}).args("string","string").use(function(e,n){return"$"==e[0]?o.elements(e).forEach(function(e){o.removeClass(e,n,!1)}):console.warn("selector must begin with '$'"),o}).args("string","boolean").use(function(n,t){return o.removeClass(e,n,t),o}).args("string").use(function(n){return o.removeClass(e,n,!1),o}).args().use(function(){return console.warn("removeClass function inputs improperly formatted"),o}),s.attr=n().args({scope:"object",getAttribute:"function",setAttribute:"function"},"string").use(function(e,n,t){return e.scope==o?void 0===t?e.getAttribute(n):(e.setAttribute(n,t),o):void 0===t?e.scope.attr(e,n):(e.scope.attr(e,n,t),o)}).args({0:"object",length:"number",forEach:"function"},"string").use(function(e,n,t){return e.forEach(function(e){o.attr(e,n,t)}),o}).args("string","string").use(function(n,t,r){if("$"==n[0]){var s=n,i=t;return void 0===(u=r)?o.attr(o.element(s),i):(o.elements(s).forEach(function(e){o.attr(e,i,u)}),o)}var i=n,u=t;return o.attr(e,i,u)}).args("string").use(function(n,t){return o.attr(e,n,t)}).args().use(function(){return console.warn("attr function inputs improperly formatted"),o}),s.css=n().args({scope:"object",style:"object"},"string").use(function(e,n,t){if(e.scope==o){if(void 0!==t){var r=o.attr(e,"style")||"",s=n+"[^;]*(;|$)";return replaceRule=new RegExp("((^(\\n|\\s)*|;)(\\s|\\n)*"+s+"|^"+s+")","gi"),replaceWith=n+":"+t+";",newRules=r.replace(replaceRule,";"+replaceWith).replace(/^;/,""),newRules==r?o.attr(e,"style",r+replaceWith):o.attr(e,"style",newRules),o}return getComputedStyle(e).getPropertyValue(n)}return t?(e.scope.css(e,n,t),o):e.scope.css(e,n)}).args({0:"object",length:"number",forEach:"function"},"string").use(function(e,n,t){return e.forEach(function(e){o.css(e,n,t)}),o}).args("string","string").use(function(n,t,r){if("$"==n[0]){var s=n,i=t,u=r;return o.elements(s).forEach(function(e){o.css(e,i,u)}),o}var i=n,u=t;return o.css(e,i,u)}).args("string").use(function(n){return o.css(e,n)}).args().use(function(){return console.warn("css function inputs improperly formatted"),o}),s.data=n().args({scope:"object",dataset:"object"},"string").use(function(e,n,t){return e.scope==o?void 0!==t?(e.dataset[n]=t,o):e.dataset[n]:e.scope.data(e,n)}).args("string","string").use(function(n,t,r){if("$"==n[0]){var s=n,i=t;return void 0===(u=r)?o.data(o.element(s),i):(o.elements(s).forEach(function(e){o.data(e,i,u)}),o)}var i=n,u=t;return o.data(e,i,u)}).args("string").use(function(n,t){return o.data(e,n,t)}).args().use(function(){return console.warn("data function inputs improperly formatted"),o}),s.html=n().args({scope:"object",appendChild:"function"},"string").use(function(e,n){return e.scope==o?(o.children.forEach(function(e){e.unlink()}),e.innerHTML=n,e.scope.include()):e.scope.html(e,n),o}).args("string","string").use(function(e,n){return"$"==e[0]&&o.html(o.element(e),n),o}).args({scope:"object",appendChild:"function"}).use(function(e){return e.scope==o?e.innerHTML:e.scope.html(e)}).args("string").use(function(n){if("$"==n[0]){var t=n;return o.html(o.element(t))}var r=n;return o.html(e,r),o}).args().use(function(e){return o.html(o.element())}),s.include=n().args({scope:"object",appendChild:"function"},"object").use(function(e,n){return e.scope!=o||n.scope?n.scope?console.warn("The item you are trying to include into this scope is already part of another scope. Try to append that scope to the current scope instead"):e.scope.include(e,n):(i(n),e.appendChild(n)),o}).args("string","object").use(function(e,n){return"$"==e[0]?o.include(o.element(e),n):console.warn("selector must begin with '$'"),o}).args("object").use(function(n){return o.include(e,n),o}).args().use(function(){return o.children.forEach(function(e){e.include()}),o.elements().forEach(function(e){e.scope||o.include(o.element(),e)}),o}),s.property=s.define=n().args("string",{get:"function",static:"undefined",asVar:"undefined"}).use(function(e,n){n.scope=o;var t={enumerable:void 0===n.enumerable||n.enumerable,configurable:!0,get:n.get};return n.set&&"function"==typeof n.set&&(t.set=n.set),t.get.bind&&(t.get=t.get.bind(n)),t.set&&t.set.bind&&(t.set=t.set.bind(n)),Object.defineProperty(o,e,t),o}).args("string",{get:"undefined",set:"undefined",asVar:"undefined",static:"!undefined"}).use(function(e,n){n.scope=o;var t={enumerable:void 0===n.enumerable||n.enumerable,configurable:!0,writable:!1,value:n.static};return t.value.bind&&(t.value=t.value.bind(n)),Object.defineProperty(o,e,t),o}).args("string",{get:"undefined",set:"undefined",asVar:"!undefined",static:"undefined"}).use(function(e,n){var t={enumerable:void 0===n.enumerable||n.enumerable,configurable:!0,writable:!0,value:n.asVar};return Object.defineProperty(o,e,t),o}).args().use(function(){return console.warn("property/define function inputs improperly formatted"),o}),s.element=function(n){return n?("$"==n[0]&&(n=n.replace(/^\$\s*/,"")),e.querySelector(n)):e},s.elements=function(n){if(n)return"$"==n[0]&&(n=n.replace(/^\$\s*/,"")),Array.prototype.slice.call(e.querySelectorAll(n),0);var t=Array.prototype.filter.call(e.querySelectorAll("*"),function(e){return e.scope==o});return t.unshift(e),t},s.append=s.appendChild=n().args({scope:{append:"function"},appendChild:"function"},{children:"object",root:"object",element:"function",unlink:"function"}).use(function(e,n){return e.scope==o?(n.unlink(),Object.setPrototypeOf(Object.getPrototypeOf(n),o),e.appendChild(n.element())):e.scope.append(e,n),o}).args("string",{children:"object",root:"object",element:"function",unlink:"function"}).use(function(e,n){return"$"==e[0]?o.append(o.element(e),n):console.warn("selector must begin with '$'"),o}).args({scope:{append:"function"},appendChild:"function"},"object").use(function(e,n){return e.scope==o?o.include(e,n):e.scope.include(e,n),o}).args("string","object").use(function(e,n){return"$"==e[0]?o.append(o.element(e),n):console.warn("selector must begin with '$'"),o}).args({scope:{append:"function"},appendChild:"function"},"string").use(function(e,n){return r.innerHTML=n,Array.prototype.slice.call(r.children,0).forEach(function(n){o.append(e,n)}),o}).args("string","string").use(function(e,n){if("$"==e[0]){var t=o.element(e);r.innerHTML=n,Array.prototype.slice.call(r.children,0).forEach(function(e){o.append(t,e)})}else console.warn("selector must begin with '$'");return o}).args({children:"object",root:"object",element:"function",unlink:"function"}).use(function(n){return o.append(e,n),o}).args("object").use(function(n){return e.scope==o&&o.append(e,n),o}).args("string").use(function(n){return r.innerHTML=n,Array.prototype.slice.call(r.children,0).forEach.call(r.children,function(n){o.append(e,n)}),o}).args().use(function(){return console.warn("append/appendChild function inputs improperly formatted"),o}),s.unlink=function(){return e.parentNode&&(Object.setPrototypeOf(Object.getPrototypeOf(o),Object.prototype),e.parentNode.removeChild(e)),o},s.render=function(){return o.unlink(),document.body.appendChild(e),o},o};e.stateless={};var s={},i={};Object.setPrototypeOf(e.stateless,i),Object.setPrototypeOf(i,s);var u=0,c=function(e){if(e.id){var n=e.className;e.className=e.id,n&&(e.className+=" "+n),e.removeAttribute("id")}},a=function(n){var t=u,r=n.id||t;return c(n),u++,Object.defineProperty(e.stateless,r,{enumerable:!1,configurable:!1,writable:!1,value:n}),e.stateless[t]||Object.defineProperty(e.stateless,t,{enumerable:!1,configurable:!1,writable:!1,value:n}),r};Object.defineProperty(s,"length",{enumerable:!1,configurable:!1,get:function(){return u}}),Object.defineProperty(s,"consume",{enumerable:!1,configurable:!1,writable:!1,value:function(n){if(n instanceof HTMLElement)n.parentElement&&n.parentElement.removeChild(n),a(n);else if("string"==typeof n)r.innerHTML=n,stateless.consume(r.children);else{if(!n.length||!n[0])throw new Error("Invalid inputs");for(var t=0;t<n.length;t++)stateless.consume(n[t])}return e.stateless}}),Object.defineProperty(s,"register",{enumerable:!1,configurable:!1,writable:!1,value:s.consume}),Object.defineProperty(s,"view",{enumerable:!1,configurable:!1,writable:!1,value:function(e){if(e instanceof HTMLElement)return e.parentElement&&e.parentElement.removeChild(e),c(e),o(e);if("string"!=typeof e)throw new Error("Invalid inputs");return r.innerHTML=e,1===r.children.length?stateless.view(r.children[0]):void 0}}),Object.defineProperty(s,"build",{enumerable:!1,configurable:!1,writable:!1,value:s.view}),Object.defineProperty(s,"instantiate",{enumerable:!1,configurable:!1,writable:!1,value:function(e){if(stateless[e])return o(stateless[e].cloneNode(!0));throw new Error(e+" cannot be found in the template library")}}),Object.defineProperty(s,"each",{enumerable:!1,configurable:!1,writable:!1,value:function(n){for(var t=0;t<u;t++)n(e.stateless[t],t);return e.stateless}});var l={},f=[];Object.defineProperty(s,"watch",{enumerable:!1,configurable:!1,writable:!1,value:n().args("string","function").use(function(e,n){return l[e]=l[e]||[],l[e].push(n),function(){l[e].forEach(function(t,r){t==n&&l[e].splice(r,1)})}}).args("function").use(function(e){return f.push(e),function(){f.forEach(function(n,t){n==e&&f.splice(t,1)})}}).args().use(function(){console.warn("watcher registration error")})}),Object.defineProperty(s,"emit",{enumerable:!1,configurable:!1,writable:!1,value:function(n,t){return t=t||{},l[n]&&l[n].forEach(function(e){e(t)}),f.forEach(function(e){e(n,t)}),e.stateless}}),Object.defineProperty(s,"plugin",{enumerable:!1,configurable:!1,writable:!1,value:n().args("string","!undefined").use(function(n,t){return i.hasOwnProperty(n)?console.warn("Plugin already defined"):(Object.defineProperty(i,n,{enumerable:!0,configurable:!0,writable:!0,value:t}),e.stateless)}).args("string","undefined").use(function(n){return i.hasOwnProperty(n)?i[n]:e.stateless}).args().use(function(){return console.warn("plugin error"),e.stateless})})}}(this,function(){var e=function(n,t){var r=!0;for(var o in t)"string"==typeof t[o]&&"!"===t[o][0]?typeof n[o]===t[o].substring(1)&&(r=!1):"object"==typeof n[o]&&"object"==typeof t[o]?r=e(n[o],t[o])&&r:typeof n[o]!==t[o]&&(r=!1);return r};return function(){var n=this,t=arguments;1===arguments.length&&(t=arguments[0]);var r=[],o=function(){};o.use=o.args=function(){return o};var s=function(){for(var n in r){var t=r[n];if(e(arguments,t.m))return t.e.apply(this,arguments)}},i=function(){var i=arguments;return{use:function(u){return 0==i.length&&delete s.args,r.push({m:i,e:u}),t.length&&e(t,i)?(u.apply(n,t),o):s}}};return s.args=i,s}}(),function(e){var n=function(){var t=Array.prototype.concat.call([n],Array.prototype.splice.call(arguments,0));return e.apply(null,t)};return n});