ajax.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // Show Apply
  25. if (data.show_apply === true) {
  26. $('#apply').show();
  27. }
  28. // Callback
  29. if (typeof data.callback === 'string') {
  30. eval(data.callback);
  31. }
  32. // Internal Process Function
  33. var scopefunctions = function(scopeObj, data) {
  34. // Reload?
  35. if (data.reload === true) {
  36. console.log(scopeObj.location.href );
  37. scopeObj.location.href = scopeObj.location.href;
  38. scopeObj.location.reload(true);
  39. }
  40. // Navigate?
  41. if (typeof data.goto === 'string') {
  42. if (!/^http(s)?:\/\//i.test(data.goto)) {
  43. if (/^\//.test(data.goto)) {
  44. data.goto = scopeObj.location.origin + data.goto;
  45. } else {
  46. var currentLoc = scopeObj.location.pathname.split('/');
  47. currentLoc.splice(-1);
  48. data.goto = currentLoc.join('/') + '/' + data.goto;
  49. }
  50. }
  51. scopeObj.location.href = data.goto;
  52. }
  53. // Replace body with
  54. if (typeof data.content === 'string') {
  55. scopeObj.document.getElementsByTagName('body').innerHTML = data.content;
  56. }
  57. }
  58. // Tab Scope
  59. if (typeof data.tab === 'object') {
  60. scopefunctions(window, data.tab);
  61. }
  62. // Page Functions
  63. if (typeof data.parent === 'object') {
  64. scopefunctions(window.parent, data.parent);
  65. }
  66. } else {
  67. // Dunno what this is
  68. console.log(data);
  69. }
  70. // Custom Callback (in addition to default actions);
  71. if (typeof callback !== 'undefined') {
  72. if (typeof callback === 'function') {
  73. callback(data, req_code);
  74. } else {
  75. console.log('Specified callback is not valid');
  76. console.log(callback);
  77. }
  78. }
  79. }
  80. };
  81. // Data must be an object
  82. if (typeof data !== 'object') { data = {}; }
  83. var ajax_settings = {
  84. async: true,
  85. accepts: 'application/json',
  86. cache: false,
  87. complete: ajax_response_action,
  88. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  89. crossDomain: false,
  90. data: data,
  91. dataType: 'json',
  92. error: function (xhr, req_code, error) {
  93. console.log(xhr);
  94. console.log(req_code);
  95. console.log(error);
  96. },
  97. headers: {
  98. action: action,
  99. },
  100. method: method,
  101. };
  102. var result = $.ajax('ajax.php?a='+action, ajax_settings);
  103. console.log(result);
  104. return result;
  105. }