SMMP/public_html/js/lib/min/page-manager.js

1 line
3.9 KiB
JavaScript
Raw Normal View History

function pageManagerClass(){}var ptrPageManagerClass;pageManagerClass.prototype={loaded:null,depJS:null,depCSS:null,xhr:[],activeXHR:null,page:null,vars:[],root:"",path:"",jsPath:"js",cssPath:"css",pagelist:null,container:null,refresher:function(){},ajax:function(t,e,s,i){var h;h=this.xhr.push(window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest"))-1,this.activeXHR=this.xhr[h],this.xhr[h].addEventListener("readystatechange",function(t){if(void 0!=this.xhr[t]&&4===this.xhr[t].readyState){[0,200].indexOf(this.xhr[t].status)>-1?e(this.xhr[t].responseText):e(),this.xhr[t]=null;for(var s=!0,t=0;t<this.xhr.length;t++)if(null!==this.xhr[t]){s=!1;break}s&&(this.xhr=[])}}.bind(this,h),!1);var n="string"==typeof s&&/^POST|GET$/i.test(s)?s.toUpperCase():"POST",a="POST"==n&&"object"==typeof i&&i instanceof FormData?i:null;return this.xhr[h].open(n,t,!0),this.xhr[h].send(a),this},explodeURL:function(t){if(t=arguments.length>=1?t:document.URL,null!=this.pagelist&&/^(?:(?:https?:\/\/)?[^\/]+)\/([a-z0-9_]+)(?:\/|((?:\/\w+)+)\/?)?(#.*)?$/i.test(t)){var e=RegExp.$2.split("/").slice(1);return this.pagelist.indexOf(RegExp.$1)>-1?{page:RegExp.$1,var:e}:null}return null},loadDependencies:function(){"object"==typeof this.depCSS&&this.depCSS instanceof Element&&this.depCSS.parentNode==document.head&&document.head.removeChild(this.depCSS),"object"==typeof this.depJS&&this.depJS instanceof Element&&this.depJS.parentNode==document.head&&document.head.removeChild(this.depJS),this.ajax(this.root+this.path+"/"+this.cssPath+"/"+this.page+".css",function(t){return null==t?void console.warn("[loadDependencies_Error] - ("+this.root+this.path+"/"+this.cssPath+"/"+this.page+".css)"):(this.depCSS=document.createElement("link"),this.depCSS.rel="stylesheet",this.depCSS.type="text/css",this.depCSS.innerHTML=t,void document.head.appendChild(this.depCSS))}.bind(this),"GET"),this.ajax(this.root+this.path+"/"+this.jsPath+"/"+this.page+".js",function(t){return null==t?void console.warn("[loadDependencies_Error] - ("+this.root+this.path+"/"+this.jsPath+"/"+this.page+".js)"):(this.depJS=document.createElement("script"),this.depJS.type="text/javascript",this.depJS.innerHTML=t,void document.head.appendChild(this.depJS))}.bind(this),"GET")},updateURL:function(){this.vars.length>0?window.history.pushState(this.page,this.page,this.root+"/"+this.page+"/"+this.vars.join("/")+"/"):window.history.pushState(this.page,this.page,this.root+"/"+this.page+"/")},setPage:function(t,e,s,i,h){var n="object"==typeof i&&i instanceof Array?i:null;if(null!=n)for(var a=0;a<n.length&&(n="string"==typeof n[a]&&/^[a-z0-9_]+$/i.test(n[a])?n:null,null!=n);a++);if(this.pagelist=null!=n?n:this.pagelist,this.root="string"==typeof h?h:this.root,this.path="string"==typeof e?e:this.path,this.container="object"==typeof s&&s instanceof Element?s:this.container,this.pagelist&&this.container){if(t===!0){for(var r=new FormData,a=0;a<this.vars.length;a++)r.append(this.vars[a],null);return this.updateURL(),this.ajax(this.root+this.path+"/"+this.page+".php",function(t){this.container.innerHTML=t,this.loadDependencies(),this.refresher.apply(this),this.updateURL()}.bind(this),"POST",r),this}if("string"==typeof t&&this.pagelist.indexOf(t)>-1){this.page=t;for(var r=new FormData,a=0;a<this.vars.length;a++)r.append(this.vars[a],null);this.ajax(this.root+this.path+"/"+this.page+".php",function(t){this.container.innerHTML=t,this.loadDependencies()}.bind(this),"POST",r),this.updateURL()}else{var o=this.explodeURL();if(null!=o){var r=new FormData;this.vars.length=0;for(var a=0;a<o.var.length;a++)this.vars[a]=o.var[a],r.append(this.vars[a],null);if(o.page==this.page)return this;this.page=o.page,this.ajax(this.root+this.path+"/"+this.page+".php",function(t){this.container.innerHTML=t,this.loadDependencies()}.bind(this),"POST",r),this.updateURL()}else this.setPage(this.pagelist[0])}}else console.warn("pagelist et container manquant");return this},refresh:function(t){return t instanceof Function?void(this.refresher=t):this.setPage(!0)}};