upd: fixed page-manager (dependencies' file content was downloaded in Ajax but never used, only checks if the file exists, so now <script> or <style> tags are filled with the downloaded content -> so more efficient)

This commit is contained in:
xdrm-brackets 2018-02-08 11:39:06 +01:00
parent 0c0f34c7d4
commit 3b2c707618
2 changed files with 24 additions and 35 deletions

View File

@ -1,19 +1 @@
/***************************************/ 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)}};
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){null!=t?(this.depCSS=document.createElement("link"),this.depCSS.rel="stylesheet",this.depCSS.type="text/css",this.depCSS.href=this.root+this.path+"/"+this.cssPath+"/"+this.page+".css",document.head.appendChild(this.depCSS)):console.warn("[loadDependencies_Error] - ("+this.root+this.path+"/"+this.cssPath+"/"+this.page+".css)")}.bind(this)),this.ajax(this.root+this.path+"/"+this.jsPath+"/"+this.page+".js",function(t){null!=t?(this.depJS=document.createElement("script"),this.depJS.type="text/javascript",this.depJS.src=this.root+this.path+"/"+this.jsPath+"/"+this.page+".js",document.head.appendChild(this.depJS)):console.warn("[loadDependencies_Error] - ("+this.root+this.path+"/"+this.jsPath+"/"+this.page+".js)")}.bind(this))},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 p=this.explodeURL()
if(null!=p){var r=new FormData
this.vars.length=0
for(var a=0;a<p.var.length;a++)this.vars[a]=p.var[a],r.append(this.vars[a],null)
if(p.page==this.page)return this
this.page=p.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)}}

View File

@ -155,26 +155,33 @@ pageManagerClass.prototype = {
// si le fichier css existe // si le fichier css existe
this.ajax(this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css', function(e){ this.ajax(this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css', function(e){
if( e != null ){ // on charge la dépendance CSS si le fichier existe if( e == null ){ // on charge la dépendance CSS si le fichier existe
console.warn('[loadDependencies_Error] - ('+this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css)');
return;
}
this.depCSS = document.createElement('link'); this.depCSS = document.createElement('link');
this.depCSS.rel = 'stylesheet'; this.depCSS.rel = 'stylesheet';
this.depCSS.type = 'text/css'; this.depCSS.type = 'text/css';
this.depCSS.href = this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css'; // this.depCSS.href = this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css';
this.depCSS.innerHTML = e;
document.head.appendChild(this.depCSS); document.head.appendChild(this.depCSS);
}else }.bind(this), 'GET');
console.warn('[loadDependencies_Error] - ('+this.root+this.path+'/'+this.cssPath+'/'+this.page+'.css)');
}.bind(this));
// si le fichier js existe // si le fichier js existe
this.ajax(this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js', function(e){ this.ajax(this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js', function(e){
if( e != null ){ // on charge la dépendance JS si le fichier existe if( e == null ){ // on charge la dépendance JS si le fichier existe
console.warn('[loadDependencies_Error] - ('+this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js)');
return;
}
this.depJS = document.createElement('script'); this.depJS = document.createElement('script');
this.depJS.type = 'text/javascript'; this.depJS.type = 'text/javascript';
this.depJS.src = this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js'; // this.depJS.src = this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js';
this.depJS.innerHTML = e;
document.head.appendChild(this.depJS); document.head.appendChild(this.depJS);
}else
console.warn('[loadDependencies_Error] - ('+this.root+this.path+'/'+this.jsPath+'/'+this.page+'.js)'); }.bind(this), 'GET');
}.bind(this));
}, },
/* ======================================================================= /* =======================================================================