-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcondi_loader.min.js
1 lines (1 loc) · 2.13 KB
/
condi_loader.min.js
1
"use strict";try{var CondiLoader=function(e,n){self=this,void 0===n&&(n={}),n.baseCSS=void 0!==n.baseCSS?n.baseCSS:"",n.baseJS=void 0!==n.baseJS?n.baseJS:"",n.skipDoubleLoad=void 0===n.skipDoubleLoad||n.skipDoubleLoad,this.loading_js=[],this.is_abs_url=function(e){return 0===e.indexOf("//")||-1!==e.indexOf("://")},this.load_css=function(e,o){return new Promise(function(s,t){var i=document.createElement("link");i.rel="preload",i.as="style",self.is_abs_url(e)||(e=n.baseCSS+e),i.href=e,null!=o&&(i.media=o),i.onload=function(){i.rel="stylesheet",s()},i.onerror=function(n){t("Error loading CSS: "+e)},document.head.appendChild(i)})},this.load_js=function(e){if(n.skipDoubleLoad&&void 0!==self.loading_js[e])return self.loading_js[e];var o=new Promise(function(o,s){var t=document.createElement("script"),i=e;self.is_abs_url(e)||(i=n.baseJS+e),t.src=i,t.async=!0,t.onload=function(){o()},t.onerror=function(n){s("Error loading JS: "+e)},document.head.appendChild(t)});return n.skipDoubleLoad&&(self.loading_js[e]=o),o},this.need_load=function(e){var n=!0;if(void 0!==e.sel){"string"==typeof e.sel&&(e.sel=new Array(e.sel));for(var o=0;o<e.sel.length;o++)n=n&&document.querySelectorAll(e.sel[o]).length>0}if(void 0!==e.xpath){"string"==typeof e.xpath&&(e.xpath=new Array(e.xpath));for(o=0;o<e.xpath.length;o++)n=n&&document.querySelectorAll(e.xpath[o]).length>0}return n},this.load_item=function(e){if(self.need_load(e)){var n=Promise.resolve();if("string"==typeof e.css&&(e.css=new Array(e.css)),void 0!==e.css)for(var o=0;o<e.css.length;o++)n.then(n=self.load_css(e.css[o],e.media));if("string"==typeof e.js&&(e.js=new Array(e.js)),void 0!==e.js)for(o=0;o<e.js.length;o++)n.then(n=self.load_js(e.js[o]));"function"==typeof e.init&&n.then(e.init),void 0!==e.event&&n.then(function(){document.dispatchEvent(new CustomEvent(e.event))}),n.catch(function(n){console.log("Item "+e.name+" processing error! "+n)})}},this.process=function(){e.forEach(function(e,n){e.name=e.name||"Unnamed item #"+n,self.load_item(e)})},document.addEventListener("DOMContentLoaded",this.process),"complete"===document.readyState&&this.process()}}catch(e){console.log("CondiLoader general exception!",e)}