ajax.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. function ajax_request(method, action, data, callback, options) {
  2. var ajax_response_action = function(data, req_code) {
  3. if (typeof options === 'object' && options.replacecallback === true && typeof callback === 'function') {
  4. callback(data, req_code);
  5. } else {
  6. // Use just JSON data
  7. data = data.responseJSON;
  8. // Check if data object is valid
  9. if (typeof data === 'object') {
  10. // Check if we are doing notifications
  11. if (typeof data.notify === 'object') {
  12. if (typeof parent.notify === 'function') {
  13. var notifyString = (data.notify.html?data.notify.html:'Ajax Complete!;')
  14. var notifyIcon = (data.notify.icon?data.notify.icon:'bullhorn;')
  15. var notifyType = (data.notify.type?data.notify.type:'success;')
  16. var notifyLength = (data.notify.length?data.notify.length:4000)
  17. var notifyLayout = (data.notify.layout?data.notify.layout:'bar')
  18. var notifyEffect = (data.notify.effect?data.notify.effect:'slidetop')
  19. parent.notify(notifyString, notifyIcon, notifyType, notifyLength, notifyLayout, notifyEffect);
  20. } else {
  21. console.log(data.notify);
  22. }
  23. }
  24. // Callback
  25. if (typeof data.callback === 'string') {
  26. eval(data.callback);
  27. }
  28. // Internal Process Function
  29. var scopefunctions = function(scopeObj, data) {
  30. // Reload?
  31. if (data.reload === true) {
  32. scopeObj.location.href = scopeObj.location.href;
  33. }
  34. // Navigate?
  35. if (typeof data.goto === 'string') {
  36. if (!/^http(s)?:\/\//i.test(data.goto)) {
  37. if (/^\//.test(data.goto)) {
  38. data.goto = scopeObj.location.origin + data.goto;
  39. } else {
  40. var currentLoc = scopeObj.location.pathname.split('/');
  41. currentLoc.splice(-1);
  42. data.goto = currentLoc.join('/') + '/' + data.goto;
  43. }
  44. }
  45. scopeObj.location.href = data.goto;
  46. }
  47. // Replace body with
  48. if (typeof data.content === 'string') {
  49. scopeObj.document.getElementsByTagName('body').innerHTML = data.content;
  50. }
  51. }
  52. // Tab Scope
  53. if (typeof data.tab === 'object') {
  54. scopefunctions(window, data.tab);
  55. }
  56. // Page Functions
  57. if (typeof data.parent === 'object') {
  58. scopefunctions(window.parent, data.parent);
  59. }
  60. } else {
  61. // Dunno what this is
  62. console.log(data);
  63. }
  64. // Custom Callback (in addition to default actions);
  65. if (typeof callback !== 'undefined') {
  66. if (typeof callback === 'function') {
  67. callback(data, req_code);
  68. } else {
  69. console.log('Specified callback is not valid');
  70. console.log(callback);
  71. }
  72. }
  73. }
  74. };
  75. // Data must be an object
  76. if (typeof data !== 'object') { data = {}; }
  77. var ajax_settings = {
  78. async: true,
  79. accepts: 'application/json',
  80. cache: false,
  81. complete: ajax_response_action,
  82. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  83. crossDomain: false,
  84. data: data,
  85. dataType: 'json',
  86. error: function (xhr, req_code, error) {
  87. console.log(xhr);
  88. console.log(req_code);
  89. console.log(error);
  90. },
  91. headers: {
  92. action: action,
  93. },
  94. method: method,
  95. };
  96. var result = $.ajax('ajax.php?a='+action, ajax_settings);
  97. console.log(result);
  98. return result;
  99. }