NxTIC/subDir/public_html/js/lib/min/api.js

5 lines
1.5 KiB
JavaScript

function APIClass(b,c){this.target=b;this.subdir=c||""}
APIClass.prototype={xhr:[],buffer:null,optionalParams:[],getTarget:function(){var b="/"+this.target.replace(/ /g,"_").replace(/\//g," ").trim().replace(/ /g,"/")+"/",c=this.subdir.replace(/ /g,"_").replace(/\//g," ").trim().replace(/ /g,"/");return c?"/"+c+b:b},send:function(b,c,g){b.hasOwnProperty("path")||c({ModuleError:4});for(var a=0;a<this.xhr.length;a++)4==this.xhr[a].readyState&&this.xhr.splice(a,1);this.xhr.push(null);a=this.xhr.length-1;this.optionalParams[a]=[];if(3<arguments.length)for(var e=
3;e<arguments.length;e++)this.optionalParams[a].push(arguments[e]);this.xhr[a]=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest");var f=this;console.log("api request",b);this.xhr[a].onreadystatechange=function(){if(4==f.xhr[a].readyState)if(f.buffer=f.xhr[a].responseText,console.log("api response",JSON.parse(f.xhr[a].responseText)),-1<[0,200].indexOf(f.xhr[a].status))try{c(JSON.parse(f.xhr[a].responseText),f.optionalParams[a])}catch(b){c({ModuleError:-1,ErrorDescription:"Erreur au niveau de api.js"},
f.optionalParams[a]),console.warn(b)}else c({ModuleError:3})};var e=new FormData,d;for(d in b)"path"==d?e.append(d,b[d]):b[d]instanceof File?e.append(d,b[d]):e.append(d,JSON.stringify(b[d]));this.xhr[a].open("POST",this.getTarget(),!0);g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].setRequestHeader("X-Requested-With","XMLHttpRequest");this.xhr[a].send(e)}};