ajax.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. $(function(){
  2. var root = window || {};
  3. root.GravAjax = function (url, settings) {
  4. settings = typeof settings === 'undefined' ? typeof url === 'string' ? {} : url : settings;
  5. settings.url = typeof settings.url === 'undefined' && typeof url === 'string' ? url : settings.url;
  6. var callbacks = {
  7. success: typeof settings.success !== 'undefined' ? typeof settings.success === 'function' ? [ settings.success ] : settings.success : [],
  8. error: typeof settings.error !== 'undefined' ? typeof settings.error === 'function' ? [ settings.error ] : settings.error : []
  9. };
  10. if (settings.toastErrors) {
  11. callbacks.error.push(root.GravAjax.toastErrorHandler);
  12. delete settings.toastErrors;
  13. }
  14. delete settings.success;
  15. delete settings.error;
  16. var deferred = $.Deferred(),
  17. jqxhr = $.ajax(settings);
  18. jqxhr.done(function (response, status, xhr) {
  19. var responseObject = {
  20. response: response,
  21. status: status,
  22. xhr: xhr
  23. };
  24. switch (response.status) {
  25. case "unauthenticated":
  26. document.location.href = GravAdmin.config.base_url_relative;
  27. throw "Logged out";
  28. break;
  29. case "unauthorized":
  30. responseObject.response.message = responseObject.response.message || "Unauthorized.";
  31. root.GravAjax.errorHandler(deferred, callbacks, responseObject);
  32. break;
  33. case "error":
  34. responseObject.response.message = responseObject.response.message || "Unknown error.";
  35. root.GravAjax.errorHandler(deferred, callbacks, responseObject);
  36. break;
  37. case "success":
  38. root.GravAjax.successHandler(deferred, callbacks, responseObject);
  39. break;
  40. default:
  41. responseObject.response.message = responseObject.response.message || "Invalid AJAX response.";
  42. root.GravAjax.errorHandler(deferred, callbacks, responseObject);
  43. break;
  44. }
  45. });
  46. jqxhr.fail(function (xhr, status, error) {
  47. var response = {
  48. status: 'error',
  49. message: error
  50. };
  51. root.GravAjax.errorHandler(deferred, callbacks, { xhr: xhr, status: status, response: response});
  52. });
  53. root.GravAjax.jqxhr = jqxhr;
  54. return deferred;
  55. };
  56. root.GravAjax.successHandler = function (promise, callbacks, response) {
  57. callbacks = callbacks.success;
  58. for (var i = 0; i < callbacks.length; i++) {
  59. if (typeof callbacks[i] === 'function') {
  60. callbacks[i](response.response, response.status, response.xhr);
  61. }
  62. }
  63. promise.resolve(response.response, response.status, response.xhr);
  64. };
  65. root.GravAjax.errorHandler = function (promise, callbacks, response) {
  66. callbacks = callbacks.error;
  67. for (var i = 0; i < callbacks.length; i++) {
  68. if (typeof callbacks[i] === 'function') {
  69. callbacks[i](response.xhr, response.status, response.response.message);
  70. }
  71. }
  72. promise.reject(response.xhr, response.status, response.response.message);
  73. };
  74. root.GravAjax.toastErrorHandler = function (xhr, status, error) {
  75. if (status !== 'abort') {
  76. toastr.error(error);
  77. }
  78. };
  79. });