-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturpial.es5.min.js
14 lines (14 loc) · 18.2 KB
/
turpial.es5.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
_______ _ _ _____ _____ _____ _ _ _____
|__ __| | | | __ \| __ \_ _| /\ | | | |/ ____|
| | | | | | |__) | |__) || | / \ | | | | (___
| | | | | | _ /| ___/ | | / /\ \ | | _ | |\___ \
| | | |__| | | \ \| | _| |_ / ____ \| |____ | |__| |____) |
|_| \____/|_| \_\_| |_____/_/ \_\______| \____/|_____/
* Turpial JS Library V. 1.0.0
* License: MIT.
* Copyright Yorman Maricuto, May 2019.
* @twitter: @MaricutoYorman, @Instagram: maricuto
* Micro-Library to create web components, multi-fetch elements, append styles, scripts, templating engine JSX
*/
"use strict";function _instanceof(t,e){return null!=e&&"undefined"!=typeof Symbol&&e[Symbol.hasInstance]?!!e[Symbol.hasInstance](t):t instanceof e}function _defineProperty(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _createForOfIteratorHelper(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var o=0,r=function(){};return{s:r,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,c=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return a=t.done,t},e:function(t){c=!0,i=t},f:function(){try{a||null==n.return||n.return()}finally{if(c)throw i}}}}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&_setPrototypeOf(t,e)}function _createSuper(t){var e=_isNativeReflectConstruct();return function(){var n,o=_getPrototypeOf(t);if(e){var r=_getPrototypeOf(this).constructor;n=Reflect.construct(o,arguments,r)}else n=o.apply(this,arguments);return _possibleConstructorReturn(this,n)}}function _possibleConstructorReturn(t,e){if(e&&("object"===_typeof(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return _assertThisInitialized(t)}function _assertThisInitialized(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function _wrapNativeSuper(t){var e="function"==typeof Map?new Map:void 0;return(_wrapNativeSuper=function(t){if(null===t||!_isNativeFunction(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return _construct(t,arguments,_getPrototypeOf(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),_setPrototypeOf(n,t)})(t)}function _construct(t,e,n){return(_construct=_isNativeReflectConstruct()?Reflect.construct:function(t,e,n){var o=[null];o.push.apply(o,e);var r=new(Function.bind.apply(t,o));return n&&_setPrototypeOf(r,n.prototype),r}).apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function _isNativeFunction(t){return-1!==Function.toString.call(t).indexOf("[native code]")}function _setPrototypeOf(t,e){return(_setPrototypeOf=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function _getPrototypeOf(t){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function _classCallCheck(t,e){if(!_instanceof(t,e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function _createClass(t,e,n){return e&&_defineProperties(t.prototype,e),n&&_defineProperties(t,n),t}var Turpial=function(){function t(){var e=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,t),this.birds=[],this.un=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return void 0===t?e:t},this.searchStr=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],o=t.search(e);return-1!==o&&(!0!==n||o)},this.replacement=function(t,e,n){return t.split(e).join(n)},this.find=function(t){return"string"==typeof t?document.getElementById(t):t},this.ext=".turpial.js",this.allowStateEvents=this.un(n.allowStateEvents,!1),this.loadModulesOnRoute=this.un(n.loadModulesOnRoute,!0),this.autoloader=this.un(n.autoloader,!1),this.autoloader_folder=this.un(n.autoloader_folder,""),this.cache=this.un(n.cache,"public"),this.public_path=this.un(n.public_path,""),this.core_path=this.un(n.core_path,""),this.folder=this.un(n.core_path,"/turpial/"),this.loader={},this.httpRequests=[],this.loader.show=this.un(n.loaderShow,null),this.loader.hide=this.un(n.loaderHide,null),this.views={},this.statusResources="loaded",this.resources={},this.myComponents=[],this.random_string=function(t){void 0===t&&(t=6);for(var e="",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=n.length,r=0;r<t;r++)e+=n.charAt(Math.floor(Math.random()*o));return e},this.selectData=function(t,e,n,o){o=turpial.un(o,!1);var r="";return t.forEach(function(t,i){if(t[e]==n)return!0===o?void(r=i):void(r=t)}),r},this.component={applyProps:function(t,n){var o=function(){var e=document.querySelectorAll(t);Array.prototype.slice.call(e).forEach(function(t){n(t)})},r=function(){if("loading"===e.statusResources)var t=0,n=setInterval(function(){return t>6e3?(console.warn("error loading resources and applying components"),void clearInterval(n)):(t+=20,"loaded"===e.statusResources?(clearInterval(n),void o()):void 0)},20);else o()};window.addEventListener("load",function(){r()}),"complete"!==document.readyState||r()},set:function(t){var n=e,o=n.component,r=t.props,i=t.tag;n.myComponents.push({tag:i,props:r});var a=n.un(t.extends,null);if(null!==a&&(a={extends:a}),void 0===window.customElements)return n.component.olderVerBrow=function(){},void o.applyProps(i,r);void 0===window.customElements.get(i)&&window.customElements.define(i,function(e){_inherits(r,_wrapNativeSuper(HTMLElement));var n=_createSuper(r);function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.props;return _classCallCheck(this,r),n.call(this,function(t){o.applyProps(i,t)}(e))}return r}(),a)}},this.view={},this.models={},this.models.sources={},this.controller={},this.urls={},this.filesLoaded={},this.inject=function(t){var n,o=_createForOfIteratorHelper(t);try{for(o.s();!(n=o.n()).done;){var r=n.value;"STYLE"===e.filesLoaded[r].tagName&&(e.filesLoaded[r].innerHTML=e.filesLoaded[r].text),document.head.appendChild(e.filesLoaded[r])}}catch(t){o.e(t)}finally{o.f()}},this.models.fetch=function(t){var n=e,o=t.type||"script",r=n.un(t.cancelOnResend,null),i=n.un(t.options,null),a=n.un(t.method,"GET"),c=n.un(t.attributes,[]);t.url=t.url||[],t.file=t.file||t.url,t.files=t.files||t.file;var s=t.files;void 0===t.ready&&(t.ready=function(){}),"string"==typeof s&&(s=[s]),document.head;var l=[],u=[],f=[];t.getString=function(t){return t.clone().text()},t.fetching=function(e){void 0!==n.filesLoaded[e]&&"script"===o&&n.filesLoaded[e].remove();var s=new XMLHttpRequest;s.open(a,e,!0);var p=[],d=[];if(null!==i){for(var h in i)d.push(i[h]);Object.keys(i).forEach(function(t,e){p.push([t,d[e]])}),p.forEach(function(t){s.setRequestHeader(t[0],t[1])})}if(s.onload=function(){if(s.status>=200&&s.status<400){var r,i=s.responseText;f.push(i),r="script"===o||"style"===o||"link"===o?o:"script";var a=document.createElement(r);c.map(function(t){a.setAttribute(t.name,t.content)}),"style"===o?a.type="text/css":"link"===o&&(a.rel="stylesheet",a.media="all",a.href=e),a.text=i,n.filesLoaded[e]=a,l.push(e)}else if("function"==typeof t.onerror&&s.status>=400)return t.onerror(s.status)},s.onerror=function(){n.filesLoaded[e]="",l.push("unloaded:"+e),u.push(e)},!0===r){var v="rq_"+n.un(t.id,n.random_string(4));if(void 0!==n.httpRequests[v])try{n.httpRequests[v].abort()}catch(t){console.warn("unable to cancel request.")}n.httpRequests[v]=s}"POST"!==a?s.send():s.send(t.data)},n.statusResources="loading",function(e){var r,i=_createForOfIteratorHelper(e);try{for(i.s();!(r=i.n()).done;){var a=r.value;t.fetching(a)}}catch(t){i.e(t)}finally{i.f()}var c=0,s=setInterval(function(){var r=!1;if(1e4===(c+=70)&&(r=!0,console.warn("¡Impossible to load all files.")),l.length!==e.length&&!0!==r);else{if(clearInterval(s),"script"===o||"style"===o||"link"===o)n.inject(e),t.ready();else if("text"===o){var i,a=[],u=_createForOfIteratorHelper(e);try{for(u.s();!(i=u.n()).done;){var f=i.value;a.push(n.filesLoaded[f].innerHTML)}}catch(t){u.e(t)}finally{u.f()}t.ready(a)}n.statusResources="loaded"}},70)}(s)},this.fetch=function(t){return e.models.fetch(t)},this.include=function(t){return e.models.fetch(t)},this.linkCSS=function(t){t.type="link";var n=e;t.type="style",t.url=t.url||[],t.file=t.file||t.url,t.files=t.files||t.file;var o=[];return t.files.map(function(t){var e=document.createElement("link");e.setAttribute("media","all"),e.setAttribute("rel","stylesheet"),e.href=t;var r=n.mount(document.head,e);o.push(r)}),t.ready(o),o},this.includeCSS=function(t){return t.type="style",e.models.fetch(t)},this.controller.views={path:function(t){var n=e.ext,o="".concat(e.folder,"views/"),r=t.views,i=e.resources,a=r.split("/"),c=o;if(1===a.length){var s=a[0];return c+="".concat(s).concat(n),i[s]={},c}var l=a.pop(),u=a.pop();return s=l,i[u]={},i[u][s]={},0===a.length?c+="".concat(u,"/").concat(s).concat(n):(a.forEach(function(t){c+="".concat(t,"/")}),c+="".concat(u,"/").concat(s).concat(n)),c}},this.view.load=function(t){(t=t||{}).ready=t.ready||function(){},t.folder=e.autoloader_folder||"";var n=e.ext,o=e.app.parameters,r=e.app.controller_name,i=e.app.action_name,a="".concat(e.folder).concat(t.folder).concat(r);if(t.module=turpial.un(t.module,null),"index"===r)var c="".concat(a).concat(n);else 0===o.length&&"index"!==r&&void 0===i?c="".concat(a,"/index").concat(n):0===o.length&&"undefined"!==i?c="".concat(a,"/").concat(i,"/index").concat(n):(c="".concat(a,"/").concat(i),o.forEach(function(t){c+="/".concat(t)}),c+=n);"string"==typeof t.module&&(t.ext=t.ext||n,c=e.core_path+t.module+t.ext);var s={file:c,options:t.options||{},ready:function(){t.ready()}};"function"==typeof t.error&&(s.error=function(e){t.error(e)}),e.DataView=s,""!==t.module&&!1!==t.module&&e.fetch(s)},this.controller.routes={getHost:function(t){!1!==(t=t||{}).loadModule&&!1!==e.loadModulesOnRoute||(t.relativeModules=!0);var n=window.location.href;n=(n=(n=(n=n.split("#"))[0]).split("?"))[0];var o=e.un(t.relativeModules,!1),r=n.search(e.public_path),i=n.slice(r,r+e.public_path.length),a=(i=n.split(i)[0]+i).slice(-1);return a="/"!==a?"/":"",1==o?"".concat(i).concat(a):(a=n.slice(-1),"".concat(n).concat(a="/"!==a?"/":""))},set:function(t){var n=e;n.app={},t=t||{};var o=window.location.href.split("?"),r=(o=(o=o[0]).split("#"))[0].search(n.public_path);if(r>0){var i=(o=o[0].substr(r+n.public_path.length)).split("/"),a=0,c=0;n.app.parameters=[],i.forEach(function(t){""!=t&&(0===a&&(n.app.controller_name=t),1===a?n.app.action_name=t:a>1&&(n.app.parameters[c++]=t),a++)}),n.app.controller_name=n.app.controller_name||"index",n.app.action_name=n.app.action_name||"";var s="".concat(n.app.controller_name,"/").concat(n.app.action_name,"/").concat(n.app.parameters.join("/"));s=(s=s.split("//").join("/")).split("///").join("/"),n.app.props=t,n.app.path=s,n.host=n.controller.routes.getHost(),n.app.host=n.host}else console.warn("bad_public_path_name")},change:function(t){var n=e,o=t.type||"pushState";t.loadModule=n.un(t.loadModule,!0);var r="".concat(n.controller.routes.getHost(t)).concat(t.path);if(r!==window.location.href){window.history[o](n.un(t.object),"",n.un(r)),!0===t.loadModule&&!0===n.loadModulesOnRoute&&n.urls.load(t);var i=n.un(t.title,!1);"string"==typeof i&&(document.title=i)}},go:function(t){if("number"==typeof t)window.history.go(t);else if("back"===t)window.history.back();else{if("forward"!==t)return;window.history.forward()}e.controller.routes.set(),e.urls.load()}},this.router=function(t,n){n=n||function(){},"number"!=typeof t&&"string"!=typeof t?(e.controller.routes.change(t),e.stateEvent(),n()):e.controller.routes.go(t)},this.routes=this.controller.routes.set,this.routes(),this.controller.components={},this.views.get=function(t){if("object"==_typeof(t)){"string"==typeof t.views&&(t.views=[t.views]);var n=[],o=e.controller.views;t.views.forEach(function(e,r){t.views=e,n[r]=o.path(t)}),Object.assign(t,{file:n,ready:t.ready}),e.fetch(t)}},this.urls={},this.urls.load=function(t){t=t||{};var n=e,o=n.app.controller_name,r=n.app.action_name,i=n.app.parameters,a=n.urls[o]||!1;if(t.module=turpial.un(t.module,null),"string"!=typeof t.module){if(!1!==a){n.un(a.loadController,!0);var c=n.un(a.loadAction,!0),s=n.un(a.loadParameters,1e3),l=n.un(n.urls[o][r],!1);return"function"==typeof a.self?"function"==typeof l&&!0===c?i.length>s?void a.self(function(){l(function(){})}):void a.self(function(){l(function(){n.view.load(t)})}):!1===c&&"string"==typeof r?void a.self(function(){l(function(){})}):void a.self(function(){n.view.load(t)}):void 0}n.view.load(t)}else n.view.load(t)},this.historyEvents={},this.URLNoHASH=function(t){return t.split("#")[0]},this.createHistoryEvent=function(t,e){t=this.URLNoHASH(window.location.href)+(t=t||""),this.historyEvents[t]=e},this.createHistoryEvent("",function(){}),this.stateEvent=function(){var t=e.historyEvents;"function"==typeof t[e.URLNoHASH(window.location.href)]&&t[e.URLNoHASH(window.location.href)]()},!0===this.allowStateEvents&&window.addEventListener("popstate",this.stateEvent),!0===n.autoloader&&(window.addEventListener("load",function(){e.urls.load()}),window.onpopstate=function(t){e.controller.routes.set(),e.urls.load(),function(){void 0===window.customElements&&Array.prototype.slice.call(this.myComponents).forEach(function(t){this.component.set({tag:t.tag,props:t.props})})}()}),this.template=function(t,e){return function(t,e){if("object"==_typeof(e=e||[])&&!Array.isArray(e)){var n=[];Object.values(e).map(function(t,o){n.push(_defineProperty({},Object.keys(e)[o],t))}),e=n}"object"==_typeof(t)&&(t=t.innerHTML);var o=t;return e.forEach(function(e){var n=Object.keys(e),r=Object.values(e);n.forEach(function(e,n){e="{{ ".concat(e," }}"),t.search(e)>=0&&(o=function(t,e,n){return t.split(e).join(n)}(o,e,r[n]))})}),o}(t,e)}}return _createClass(t,[{key:"map",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=this.birds[t];if("object"==_typeof(t)?n=t:void 0===n&&(n=this.find(t)),"object"==_typeof(e)){if(void 0===e[0])return n;n=n.children,e.forEach(function(t,o){n=void 0===e[o+1]?n[t]:n[t].children})}return n}},{key:"createMap",value:function(t){var e=(t=this.find(t)).getAttribute("id");if(void 0!==e){var n=document.getElementById(e);this.birds[e]=n}}},{key:"read",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return this.map(t,e)}},{key:"selectorApp",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return"object"==_typeof(e)&&!0===Number.isInteger(e[0])?this.map(t,e):e}},{key:"delete",value:function(t){(t=this.find(t)).remove()}},{key:"update",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"replace-selector",o=!(arguments.length>3&&void 0!==arguments[3])||arguments[3];if("string"==typeof e)var r=this.render(e),i=e;else r=e;if(null===r){var a=!1;r=e}else a=!0;var c=t;!0===o&&(n="replace-selector");var s=!1,l=c.parentNode;if("before"===n)l.insertBefore(r,c),s=r;else if("after"===n)l.insertBefore(r,c.nextSibling),s=r;else if("replace-selector"===n)l.replaceChild(r,c),s=r;else{if("inner"!==n)return console.warn("error-on-placement"),!1;!0===a?c.innerHTML=i:c.innerText=r,s=c}var u=c;return this.createMap(u),s}},{key:"insert",value:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"inner";return t=this.find(t),this.update(t,e,n,!1)}},{key:"render",value:function(t){var e=document.createElement("template");return e.innerHTML=t,e.content.firstElementChild}},{key:"mount",value:function(t,e){if("string"==typeof e&&(e=this.render(e)),null!==e){var n=(t=this.find(t)).appendChild(e),o=t;return this.createMap(o),n}console.warn("need to be HTML string or object")}},{key:"settings",value:function(t){if(void 0===t[0]){var e=[],n=0,o=[];for(var r in t)o.push(t[r]);o.forEach(function(t){e[n++]=t});var i=[];n=0,Object.keys(t).forEach(function(t){i[n++]=t});var a="";return n=0,i.forEach(function(t,n){a+=" ".concat(t,'="').concat(e[n],'"')}),a}}},{key:"createTag",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"";"object"==_typeof(e)&&(e=this.settings(e));var o="<".concat(t).concat(e,">");return o+=n,"br"!==t&&(o+="</".concat(t,">")),o}},{key:"html",value:function(t,e,n){var o=this.replacement,r=this;if("code"===t&&(n=o(n,"<","<"),n=o(n,">",">"),n=o(n," "," ")),void 0===n&&(n=e,e=""),"object"==_typeof(n)&&void 0!==n[0]){var i="";n.forEach(function(t,e){i+="".concat(t)});var a=r.createTag(t,e,i)}else"object"==_typeof(n)?(e=n,a=r.createTag(t,e,n="")):a=r.createTag(t,e,n);return a}},{key:"el",value:function(t,e,n){return this.html(t,e,n)}}]),t}();