strudel
Version:
A front-end framework for back-end powered web.
6 lines • 14.6 kB
JavaScript
/*!
* Strudel.js v1.0.5
* (c) 2016-2019 Mateusz Łuczak
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.Strudel={})}(this,function(t){"use strict";var p=function(t,e,n){console.error(t)},r={"/^\\.[\\w\\-]+$/":function(t){return document.getElementsByClassName(t.substring(1))},"/^\\w+$/":function(t){return document.getElementsByTagName(t)},"/^\\#[\\w\\-]+$/":function(t){return document.getElementById(t.substring(1))},"/^</":function(t){return(new s).generate(t)}},o=function(t,e){return(e||document).querySelectorAll(t)},i={events:function(t,e){if(t._e)for(var n in t._e)t._e[n].forEach(function(t){new s(e).on(n,t)})},select:function(t,e){new s(t).is("select")&&(e.value=t.value)},textarea:function(t,e){new s(t).is("textarea")&&(e.value=t.value)}},s=function t(e,n){if(e instanceof t)return e;"string"==typeof e&&(e=function(t,e){if(t=t.replace(/^\s*/,"").replace(/\s*$/,""),e)return o(t,e);for(var n in r)if(e=n.split("/"),new RegExp(e[1],e[2]).test(t))return r[n](t);return o(t)}(e,n)),(e&&e.nodeName||e&&e===window)&&(e=[e]),this._nodes=this.slice(e)},e={length:{configurable:!0}};function c(t,e){return new s(t,e)}e.length.get=function(){return this._nodes.length},s.prototype.array=function(o){var i=this;return this._nodes.reduce(function(t,e,n){var r;return o?((r=o.call(i,e,n))||(r=!1),"string"==typeof r&&(r=new s(r)),r instanceof s&&(r=r._nodes)):r=e.innerHTML,t.concat(!1!==r?r:[])},[])},s.prototype.str=function(e,n){return function(t){return"function"==typeof t?t.call(this,e,n):t.toString()}},s.prototype.is=function(t){return 0<this.filter(t).length},s.prototype.filter=function(e){var t=function(t){return t.matches=t.matches||t.msMatchesSelector||t.webkitMatchesSelector,t.matches(e||"*")};return"function"==typeof e&&(t=e),e instanceof s&&(t=function(t){return-1!==e._nodes.indexOf(t)}),new s(this._nodes.filter(t))},s.prototype.eq=function(t){return new s(this._nodes[t])||!1},s.prototype.get=function(t){return(t||0===t)&&t<=this._nodes.length?this._nodes[t]:this._nodes},s.prototype.first=function(){return this._nodes[0]||!1},s.prototype.index=function(t){var e=this.children()._nodes,n=t instanceof HTMLElement?t:t.first();return Array.prototype.indexOf.call(e,n)},s.prototype.slice=function(t){return t&&0!==t.length&&"string"!=typeof t&&"[object Function]"!==t.toString()?t.length?[].slice.call(t._nodes||t):[t]:[]},s.prototype.unique=function(){return new s(this._nodes.reduce(function(t,e){return null!=e&&!1!==e&&-1===t.indexOf(e)?t.concat(e):t},[]))},s.prototype.children=function(t){return this.map(function(t){return this.slice(t.children)}).filter(t)},s.prototype.generate=function(t){return/^\s*<t(h|r|d)/.test(t)?new s(document.createElement("table")).html(t).children()._nodes:/^\s*</.test(t)?new s(document.createElement("div")).html(t).children()._nodes:document.createTextNode(t)},s.prototype.args=function(t,e,n){return"function"==typeof t&&(t=t(e,n)),"string"!=typeof t&&(t=this.slice(t).map(this.str(e,n))),t.toString().split(/[\s,]+/).filter(function(t){return t.length})},s.prototype.each=function(t){return this._nodes.forEach(t.bind(this)),this},s.prototype.eacharg=function(n,r){return this.each(function(e,t){this.args(n,e,t).forEach(function(t){r.call(this,e,t)},this)})},s.prototype.isInPage=function(t){return t!==document.body&&document.body.contains(t)},s.prototype.map=function(t){return t?new s(this.array(t)).unique():this},s.prototype.adjacent=function(i,t,n){return"number"==typeof t&&(t=0===t?[]:new Array(t).join().split(",").map(Number.call,Number)),this.each(function(r,o){var e=document.createDocumentFragment();new s(t||{}).map(function(t,e){var n="function"==typeof i?i.call(this,t,e,r,o):i;return"string"==typeof n?this.generate(n):new s(n)}).each(function(t){this.isInPage(t)?e.appendChild(new s(t).clone().first()):e.appendChild(t)}),n.call(this,r,e)})},s.prototype.getAll=function(t){return new s([t].concat(new s("*",t)._nodes))},s.prototype.clone=function(){return this.map(function(t){var e=t.cloneNode(!0),r=this.getAll(e);return this.getAll(t).each(function(t,e){for(var n in i)i[n](t,r._nodes[e])}),e})},s.prototype.html=function(e){return void 0===e?this.first().innerHTML||"":this.each(function(t){t.innerHTML=e})},s.prototype.text=function(e){return void 0===e?this.first().textContent||"":this.each(function(t){t.textContent=e})},s.prototype.remove=function(){return this.each(function(t){t.parentNode.removeChild(t)})},s.prototype.parent=function(t){return this.map(function(t){return t.parentNode}).filter(t)},s.prototype.closest=function(e){return this.map(function(t){do{if(new s(t).is(e))return t}while((t=t.parentNode)&&t!==document)})},s.prototype.append=function(t,e){return this.adjacent(t,e,function(t,e){t.appendChild(e)})},s.prototype.prepend=function(t,e){return this.adjacent(t,e,function(t,e){t.insertBefore(e,t.firstChild)})},s.prototype.find=function(o){return this.map(function(t){var e,n=">"===o[0];n&&(e=!0,t.id||(e=!1,t.id="strudel-"+Math.random().toString(36).substr(2,9)),o="#"+t.id+o);var r=new s(o||"*",t);return n&&!e&&t.removeAttribute("id"),r})},s.prototype.addClass=function(t){return this.eacharg(arguments,function(t,e){t.classList.add(e)})},s.prototype.toggleClass=function(t){return this.eacharg(arguments,function(t,e){t.classList.toggle(e)})},s.prototype.removeClass=function(t){return this.eacharg(arguments,function(t,e){t.classList.remove(e)})},s.prototype.on=function(t,e,r){var n=e;if("string"==typeof e){var o=e;e=function(e){var n=arguments,t=new s(e.currentTarget);(t.is(o)?t:t.find(o)).each(function(t){if(t===e.target||t.contains(e.target)){try{Object.defineProperty(e,"currentTarget",{get:function(){return t}})}catch(t){}r.apply(t,n)}})},n=r}var i=function(t){return e.apply(this,[t].concat(t.detail||[]))};return this.eacharg(t,function(t,e){t.addEventListener(e,i),t._e=t._e||{},t._e[e]=t._e[e]||[],t._e[e].push({providedHandler:n,eventHandler:i})})},s.prototype.off=function(t,c){return void 0===t&&void 0===c&&this.each(function(r){var t=function(n){r._e[n].forEach(function(t){var e=t.eventHandler;r.removeEventListener(n,e)})};for(var e in r._e)t(e);r._e={}}),this.eacharg(t,function(o,i){new s(o._e?o._e[i]:[]).each(function(t,e){var n=t.providedHandler,r=t.eventHandler;c?c===n&&(o.removeEventListener(i,r),o._e[i]=o._e[i].slice(0,e).concat(o._e[i].slice(e+1))):(o.removeEventListener(i,r),o._e[i]=[])})})},s.prototype.trigger=function(t){var o=this.slice(arguments).slice(1);return this.eacharg(t,function(t,e){var n,r={bubbles:!0,cancelable:!0,detail:o};try{n=new window.CustomEvent(e,r)}catch(t){(n=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,o)}t.dispatchEvent(n)})},s.prototype.attr=function(n,t,r){if(r=r?"data-":"",void 0!==t){var e=n;(n={})[e]=t}return"object"==typeof n?this.each(function(t){for(var e in n)null!==n[e]?t.setAttribute(r+e,n[e]):t.removeAttribute(r+e)}):this.length?this.first().getAttribute(r+n):""},s.prototype.prop=function(n,t){if(void 0!==t){var e=n;(n={})[e]=t}return"object"==typeof n?this.each(function(t){for(var e in n)t[e]=n[e]}):this.length?this.first()[n]:""},s.prototype.data=function(t,e){return t?this.attr(t,e,!0):this.first().dataset},Object.defineProperties(s.prototype,e);var u=["constructor","$teardown","$on","$off","$emit"],a=function(t){return"function"==typeof t||!1},n=function(r){return function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t,e){t.__decorators__||(t.__decorators__=[]),t.__decorators__.push(function(t){return r(t,e,n)})}}},f=function(t,e){return[t,e].reduce(function(e,n){return Object.keys(n).forEach(function(t){e[t]=n[t]}),e})},l=function(){this._registry={},this._registrationQueue={},this._isRegistrationScheduled=!1};l.prototype.getData=function(){return f(this._registry,this._registrationQueue)},l.prototype.getRegisteredSelectors=function(){return Object.keys(this._registry)},l.prototype.getSelectorsFromRegistrationQueue=function(){return Object.keys(this._registrationQueue)},l.prototype.setSelectorsAsRegistered=function(){this._registry=f(this._registry,this._registrationQueue),this._registrationQueue={}},l.prototype.getComponent=function(t){return this._registrationQueue[t]||this._registry[t]},l.prototype.registerComponent=function(t,e){var n=this;this._registry[t]||this._registrationQueue[t]||(this._registrationQueue[t]=e,this._isRegistrationScheduled||(this._isRegistrationScheduled=!0,window.requestAnimationFrame(function(){n._isRegistrationScheduled=!1,c(document).trigger("content:loaded")})))};var d=new l,h="strudel-init",y={initializedClassName:h,initializedSelector:"."+h,devtools:!1,productionTip:!1},g={},_=function(){};_.getEvents=function(){return g},_.removeAllListeners=function(){Object.keys(g).forEach(function(t){delete g[t]})},_.prototype.$on=function(t,e){g[t]||(g[t]=[]),g[t].push(e)},_.prototype.$off=function(t,r){var e=g[t];if(e&&e.length){var n=e.reduce(function(t,e,n){return a(e)&&e===r?t=n:t},-1);if(-1<n)return e.splice(n,1),g[t]=e,!0}return!1},_.prototype.$emit=function(t){for(var e=[],n=arguments.length-1;0<n--;)e[n]=arguments[n+1];var r=g[t];if(r&&r.length){try{r.forEach(function(t){t.apply(void 0,e)})}catch(t){p(t,this.constructor)}return!0}return!1};var v=function(o){function t(t){var r=this;void 0===t&&(t={});var e=t.element,n=t.data;o.call(this),this.mixins=this.mixins||[];try{this.mixins.forEach(function(t){var e,n;a(t.beforeInit)&&t.beforeInit.call(r),e=r,n=t,Object.keys(n).forEach(function(t){e[t]||(e[t]=n[t])})}),this.beforeInit(),this.$element=e,this.$data=n,this.__decorators__&&(this.__decorators__.forEach(function(t){t(r)}),delete this.__decorators__),this.mixins.forEach(function(t){a(t.init)&&t.init.call(r)}),this.init()}catch(t){p(t,this.constructor)}this.$element.addClass(y.initializedClassName)}return o&&(t.__proto__=o),((t.prototype=Object.create(o&&o.prototype)).constructor=t).prototype.beforeInit=function(){},t.prototype.init=function(){},t.prototype.beforeDestroy=function(){},t.prototype.destroy=function(){},t.prototype.$teardown=function(){var e=this;try{this.mixins.forEach(function(t){a(t.beforeDestroy)&&t.beforeDestroy.call(e)}),this.beforeDestroy(),this.$element.off(),this.$element.removeClass(y.initializedClassName),delete this.$element.first().__strudel__,delete this.$element,this.mixins.forEach(function(t){a(t.destroy)&&t.destroy.call(e)}),this.destroy()}catch(t){p(t,this.constructor)}},t}(_),m=function(t,e){if(!t.prototype)return t;var n,r,o,i,c=function(n){function t(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];n.apply(this,t)}return n&&(t.__proto__=n),(t.prototype=Object.create(n&&n.prototype)).constructor=t}(v);return n=t,r=c.prototype,o=n.prototype,i="object"==typeof n?n:new n,Object.getOwnPropertyNames(i).forEach(function(t){var e=Object.getOwnPropertyDescriptor(i,t);e.writable=!0,Object.defineProperty(r,t,e)}),Object.getOwnPropertyNames(o).forEach(function(t){-1!==u.indexOf(t)||Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(o,t))}),Object.defineProperty(c.prototype,"_selector",{value:e}),Object.defineProperty(c.prototype,"isStrudelClass",{value:!0}),Object.defineProperty(c.prototype,"name",{value:t.name}),d.registerComponent(e,c),c};function b(e){return function(t){return m(t,e)}}var E=n(function(n,r,t){var e,o,i;t&&t[0]&&(o=(e=t)[0],i=e[1]),n._events||(n._events=[]);var c,s,u,a,f=function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];try{n[r].apply(this,t)}catch(t){p(t,n.constructor)}};if(o){var l=i?o+" "+i:o;n._events[l]=f,c=n.$element,s=o,u=i,a=f.bind(n),u?c.on(s,u,a):c.on(s,a)}}),w=n(function(t,e,n){n&&n[0]&&(t[e]=t.$element.find(n[0])),t._els||(t._els=[]),t._els[e]=e}),S=n(function(r,o){var i=r.init||function(){};"init"!==o&&(r.init=function(){for(var t,e=[],n=arguments.length;n--;)e[n]=arguments[n];return(t=r[o]).apply.apply(t,[this].concat(e)),i.apply.apply(i,[this].concat(e))})})(),O="1.0.5",C=y.initializedClassName,j=y.initializedSelector,N={components:d.getData()},A=Object.freeze({VERSION:O,INIT_CLASS:C,INIT_SELECTOR:j,options:N,config:y,Component:b,Evt:E,El:w,OnInit:S,EventEmitter:_,createDecorator:n,element:c,$:c}),$=function(t){this.registry=t};$.prototype.unlink=function(n){void 0===n&&(n=document),this.registry.getRegisteredSelectors().forEach(function(t){var e=Array.prototype.slice.call(n.querySelectorAll(t));n!==document&&c(n).is(y.initializedSelector)&&e.push(n),[].forEach.call(e,function(t){t.__strudel__&&t.__strudel__.$teardown()})})},$.prototype.link=function(e){var i=this;void 0===e&&(e=document);var t=e===document,n=t?this.registry.getSelectorsFromRegistrationQueue():this.registry.getRegisteredSelectors();0!==n.length&&(n.forEach(function(o){var t=Array.prototype.slice.call(e.querySelectorAll(o));e!==document&&c(e).is(o)&&t.push(e),[].forEach.call(t,function(t){if(!t.__strudel__){var e=c(t),n=e.data(),r=i.registry.getComponent(o);t.__strudel__=new r({element:e,data:n})}})}),t&&this.registry.setSelectorsAsRegistered())};var L,x,I,T={childList:!0,subtree:!0},R=window.__STRUDEL_DEVTOOLS_GLOBAL_HOOK__,P=new $(d),D=c(document),z=function(t){var e=t.nodeName,n=t.nodeType;return"SCRIPT"!==e&&"svg"!==e&&1===n},k=function(t){var e,n;P.link(((e=t)&&0<e.length&&(n=z(e[0])?e[0]:e[0].first()),n)),D.trigger("strudel:loaded")},H=function(t){var e=d.getRegisteredSelectors();Array.prototype.slice.call(t.addedNodes).filter(function(t){return z(t)}).forEach(function(n){e.some(function(t){var e=t+":not("+y.initializedSelector+")";return c(n).is(e)||c(n).find(e).length})&&k([n])})},Q=function(t){Array.prototype.slice.call(t.removedNodes).filter(function(t){return z(t)&&(c(t).is(y.initializedSelector)||c(t).find(y.initializedSelector).length)}).forEach(function(t){var e=t.querySelector(y.initializedSelector);e&&Array.prototype.slice.call(e).forEach(function(t){P.unlink(t)}),P.unlink(t)})};v.prototype.getInstance=function(){return A},/comp|inter|loaded/.test(document.readyState)&&setTimeout(k,0),setTimeout(function(){y.devtools&&R&&R.emit("init",A)},0),D.on("content:loaded",function(t){k(t.detail)}),L=D._nodes[0].body,x=H,I=Q,new MutationObserver(function(t){var i,c;i=x,c=I,t.forEach(function(t){var e,n,r,o;n=c,"childList"===(e=t).type&&0<e.removedNodes.length&&n(e),o=i,"childList"===(r=t).type&&0<r.addedNodes.length&&o(r)})}).observe(L,T),t.VERSION=O,t.INIT_CLASS=C,t.INIT_SELECTOR=j,t.options=N,t.config=y,t.Component=b,t.Evt=E,t.El=w,t.OnInit=S,t.EventEmitter=_,t.createDecorator=n,t.element=c,t.$=c,Object.defineProperty(t,"__esModule",{value:!0})});