var abp = abp || {}; (function ($) { if (!$) { return; } /* jquery enhancements ***************************************************/ // abp.ajax -> uses $.ajax ------------------------------------------------ abp.ajax = function (useroptions) { useroptions = useroptions || {}; var options = $.extend(true, {}, abp.ajax.defaultopts, useroptions); var oldbeforesendoption = options.beforesend; options.beforesend = function(xhr) { if (oldbeforesendoption) { oldbeforesendoption(xhr); } xhr.setrequestheader("pragma", "no-cache"); xhr.setrequestheader("cache-control", "no-cache"); xhr.setrequestheader("expires", "sat, 01 jan 2000 00:00:00 gmt"); }; options.success = undefined; options.error = undefined; return $.deferred(function ($dfd) { $.ajax(options) .done(function (data, textstatus, jqxhr) { if (data.__abp) { abp.ajax.handleresponse(data, useroptions, $dfd, jqxhr); } else { $dfd.resolve(data); useroptions.success && useroptions.success(data); } }).fail(function (jqxhr) { if (jqxhr.responsejson && jqxhr.responsejson.__abp) { abp.ajax.handleresponse(jqxhr.responsejson, useroptions, $dfd, jqxhr); } else { abp.ajax.handlenonabperrorresponse(jqxhr, useroptions, $dfd); } }); }); }; $.extend(abp.ajax, { defaultopts: { datatype: 'json', type: 'post', contenttype: 'application/json', headers: { 'x-requested-with': 'xmlhttprequest' } }, defaulterror: { message: 'an error has occurred!', details: 'error detail not sent by server.' }, defaulterror401: { message: 'you are not authenticated!', details: 'you should be authenticated (sign in) in order to perform this operation.' }, defaulterror403: { message: 'you are not authorized!', details: 'you are not allowed to perform this operation.' }, defaulterror404: { message: 'resource not found!', details: 'the resource requested could not found on the server.' }, logerror: function (error) { abp.log.error(error); }, showerror: function (error) { if (error.details) { return abp.message.error(error.details, error.message); } else { return abp.message.error(error.message || abp.ajax.defaulterror.message); } }, handletargeturl: function (targeturl) { if (!targeturl) { location.href = abp.apppath; } else { location.href = targeturl; } }, handlenonabperrorresponse: function (jqxhr, useroptions, $dfd) { if (useroptions.abphandleerror !== false) { switch (jqxhr.status) { case 401: abp.ajax.handleunauthorizedrequest( abp.ajax.showerror(abp.ajax.defaulterror401), abp.apppath ); break; case 403: abp.ajax.showerror(abp.ajax.defaulterror403); break; case 404: abp.ajax.showerror(abp.ajax.defaulterror404); break; default: abp.ajax.showerror(abp.ajax.defaulterror); break; } } $dfd.reject.apply(this, arguments); useroptions.error && useroptions.error.apply(this, arguments); }, handleunauthorizedrequest: function (messagepromise, targeturl) { if (messagepromise) { messagepromise.done(function () { abp.ajax.handletargeturl(targeturl); }); } else { abp.ajax.handletargeturl(targeturl); } }, handleresponse: function (data, useroptions, $dfd, jqxhr) { if (data) { if (data.success === true) { $dfd && $dfd.resolve(data.result, data, jqxhr); useroptions.success && useroptions.success(data.result, data, jqxhr); if (data.targeturl) { abp.ajax.handletargeturl(data.targeturl); } } else if (data.success === false) { var messagepromise = null; if (data.error) { if (useroptions.abphandleerror !== false) { messagepromise = abp.ajax.showerror(data.error); } } else { data.error = abp.ajax.defaulterror; } abp.ajax.logerror(data.error); $dfd && $dfd.reject(data.error, jqxhr); useroptions.error && useroptions.error(data.error, jqxhr); if (jqxhr.status === 401 && useroptions.abphandleerror !== false) { abp.ajax.handleunauthorizedrequest(messagepromise, data.targeturl); } } else { //not wrapped result $dfd && $dfd.resolve(data, null, jqxhr); useroptions.success && useroptions.success(data, null, jqxhr); } } else { //no data sent to back $dfd && $dfd.resolve(jqxhr); useroptions.success && useroptions.success(jqxhr); } }, blockui: function (options) { if (options.blockui) { if (options.blockui === true) { //block whole page abp.ui.setbusy(); } else { //block an element abp.ui.setbusy(options.blockui); } } }, unblockui: function (options) { if (options.blockui) { if (options.blockui === true) { //unblock whole page abp.ui.clearbusy(); } else { //unblock an element abp.ui.clearbusy(options.blockui); } } }, ajaxsendhandler: function (event, request, settings) { var token = abp.security.antiforgery.gettoken(); if (!token) { return; } if (!abp.security.antiforgery.shouldsendtoken(settings)) { return; } if (!settings.headers || settings.headers[abp.security.antiforgery.tokenheadername] === undefined) { request.setrequestheader(abp.security.antiforgery.tokenheadername, token); } } }); $(document).ajaxsend(function (event, request, settings) { return abp.ajax.ajaxsendhandler(event, request, settings); }); /* jquery plugin enhancements ********************************************/ /* jquery form plugin * http://www.malsup.com/jquery/form/ */ // abpajaxform -> uses ajaxform ------------------------------------------ if ($.fn.ajaxform) { $.fn.abpajaxform = function (useroptions) { useroptions = useroptions || {}; var options = $.extend({}, $.fn.abpajaxform.defaults, useroptions); options.beforesubmit = function () { abp.ajax.blockui(options); useroptions.beforesubmit && useroptions.beforesubmit.apply(this, arguments); }; options.success = function (data) { abp.ajax.handleresponse(data, useroptions); }; //todo: error? options.complete = function () { abp.ajax.unblockui(options); useroptions.complete && useroptions.complete.apply(this, arguments); }; return this.ajaxform(options); }; $.fn.abpajaxform.defaults = { method: 'post' }; } abp.event.on('abp.dynamicscriptsinitialized', function () { abp.ajax.defaulterror.message = abp.localization.abpweb('defaulterror'); abp.ajax.defaulterror.details = abp.localization.abpweb('defaulterrordetail'); abp.ajax.defaulterror401.message = abp.localization.abpweb('defaulterror401'); abp.ajax.defaulterror401.details = abp.localization.abpweb('defaulterrordetail401'); abp.ajax.defaulterror403.message = abp.localization.abpweb('defaulterror403'); abp.ajax.defaulterror403.details = abp.localization.abpweb('defaulterrordetail403'); abp.ajax.defaulterror404.message = abp.localization.abpweb('defaulterror404'); abp.ajax.defaulterror404.details = abp.localization.abpweb('defaulterrordetail404'); }); })(jquery);