Files
sysPass/public/js/app-requests.min.js
2020-01-08 13:18:03 +01:00

6 lines
2.1 KiB
JavaScript

sysPass.Requests=function(c){var e=c.log,b=[],h={type:"json",url:"",method:"post",callback:"",async:!0,data:"",cache:!1,processData:!0,contentType:"application/x-www-form-urlencoded; charset=UTF-8",timeout:0,addHistory:!1,hash:"",useLoading:!0,useFullLoading:!1};Object.seal(h);var l={get:function(){return b},add:function(a){var k=""===a.hash?c.util.hash.md5(JSON.stringify(a)):a.hash;if(0<b.length&&b[b.length-1].hash===k)return b;e.info("history:add");a.hash=k;b.push(a);15<=b.length&&b.splice(0,10);
return b},del:function(){e.info("history:del");if("undefined"!==typeof b.pop())return b[b.length-1]},reset:function(){e.info("history:reset");b=[]},length:function(){return b.length}},g=function(a){return-1===a.indexOf("http")&&-1===a.indexOf("https")?c.config.APP_ROOT+a:a};return{getRequestOpts:function(){return Object.create(h)},getActionCall:function(a,b,m){e.info("getActionCall");var d={},f=a.method.toUpperCase();"POST"!==f&&"GET"!==f||!c.config.CSRF||(d={"X-CSRF":c.config.CSRF});return $.ajax({dataType:a.type,
url:g(a.url),method:a.method,async:a.async,data:a.data,cache:a.cache,processData:a.processData,contentType:a.contentType,timeout:a.timeout,headers:d,beforeSend:function(){!0===a.useLoading&&c.theme.loading.show(a.useFullLoading)},success:function(c){if("function"!==typeof b)return!0;!0===a.addHistory&&(a.callback=b,l.add(a));b(c)},error:function(b,f,d){"function"!==typeof m?(f=void 0!==c.config.LANG[1]?c.config.LANG[1]:"An error occurred",f+="<p>"+d+"</p><p>"+b.responseText+"</p>",e.error(f),"html"===
a.type&&$("#content").html(c.msg.html.error(f)),c.msg.error(f)):m()},complete:function(b){!0===a.useLoading&&c.theme.loading.hide();void 0!==c.theme&&("html"===a.type||void 0!==b.responseJSON&&void 0!==b.responseJSON.data&&void 0!==b.responseJSON.data.html)&&c.theme.ajax.complete()}})},getActionEvent:function(a,b,c){var d=new EventSource(g(a.url));d.addEventListener("message",function(a){a=JSON.parse(a.data);e.debug(a);1===a.end?(e.info("getActionEvent:Ending"),d.close(),"function"===typeof c&&c(a)):
"function"===typeof b&&b(a)});d.addEventListener("error",function(a){e.error("getActionEvent:Error occured");d.close()});return d},getUrl:g,history:l}};