ajax.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. function AjaxErrorBadStatusCode(message) { this.name = 'AjaxErrorBadStatusCode'; this.message = message; };
  2. AjaxErrorBadStatusCode.prototype = new Error();
  3. var ajax = {};
  4. ajax.create = function() {
  5. if(typeof XMLHttpRequest !== 'undefined') {
  6. return new XMLHttpRequest();
  7. };
  8. var versions = [
  9. "MSXML2.XmlHttp.6.0",
  10. "MSXML2.XmlHttp.5.0",
  11. "MSXML2.XmlHttp.4.0",
  12. "MSXML2.XmlHttp.3.0",
  13. "MSXML2.XmlHttp.2.0",
  14. "Microsoft.XmlHttp"
  15. ];
  16. var xhr;
  17. for(var i = 0; i < versions.length; i++) {
  18. try {
  19. xhr = new ActiveXObject(versions[i]);
  20. break;
  21. } catch(e) {
  22. // Silent
  23. };
  24. };
  25. return xhr;
  26. };
  27. ajax.send = function(url, callback, method, data, async) {
  28. if(async === undefined) {
  29. async = true;
  30. };
  31. var a = ajax.create();
  32. a.open(method, url, async);
  33. a.onreadystatechange = function() {
  34. // a.readyState:
  35. // 0 - UNSENT
  36. // 1 - OPENED
  37. // 2 - HEADERS_RECEIVED
  38. // 3 - LOADING
  39. // 4 - DONE
  40. callback(method, data, a.readyState, a.status, a.responseText);
  41. };
  42. if(method == 'POST') {
  43. a.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  44. };
  45. a.send(data);
  46. };
  47. ajax.get = function(url, data, callback, async) {
  48. var query = [];
  49. for(var key in data) {
  50. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  51. };
  52. ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async);
  53. };
  54. ajax.getJSON = function(url, data, callbackSuccess, callbackError, async) {
  55. ajax.get(url, data, function(method, data, readyState, status, responseText) {
  56. if(readyState == 4) {
  57. if(status == 200) {
  58. try {
  59. var r = JSON.parse(responseText);
  60. if(callbackSuccess) {
  61. callbackSuccess(method, data, readyState, status, r);
  62. };
  63. } catch(e) {
  64. if(callbackError) {
  65. callbackError(method, data, readyState, status, e);
  66. };
  67. };
  68. } else {
  69. if(callbackError) {
  70. var e = new AjaxErrorBadStatusCode('Bad status code '+status);
  71. callbackError(method, data, readyState, status, e);
  72. };
  73. };
  74. };
  75. }, async);
  76. };
  77. ajax.post = function(url, data, callback, async) {
  78. var query = [];
  79. for (var key in data) {
  80. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  81. };
  82. ajax.send(url, callback, 'POST', query.join('&'), async);
  83. };
  84. // TODO: rework later (remove duplicated code)
  85. ajax.hasClass = function(obj, className) {
  86. return !!obj.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
  87. };
  88. ajax.addClass = function(obj, className) {
  89. if(!ajax.hasClass(obj, className)) obj.className += " " + className;
  90. };
  91. ajax.removeClass = function(obj, className) {
  92. if(ajax.hasClass(obj, className)) {
  93. var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
  94. obj.className = obj.className.replace(reg, ' ').trim();
  95. };
  96. };
  97. ajax.loadTag = function(tag, url, func) {
  98. if(typeof window[func] === 'function') {
  99. if(!ajax.hasClass(tag, 'loading')) {
  100. ajax.addClass(tag, 'loading');
  101. var body = document.getElementsByTagName('body');
  102. if(body.length >= 1) { ajax.addClass(body[0], 'loading'); };
  103. ajax.getJSON(url, {}, function(method, data, readyState, status, responseData) {
  104. var body = document.getElementsByTagName('body');
  105. if(body.length >= 1) { ajax.removeClass(body[0], 'loading'); };
  106. try {
  107. var resp = window[func](tag, responseData);
  108. tag.innerHTML = resp;
  109. } catch(e) {
  110. console.log('ajax.loadTag', 'e', e);
  111. };
  112. ajax.removeClass(tag, 'loading');
  113. }, function(method, data, readyState, status, responseData) {
  114. ajax.removeClass(tag, 'loading');
  115. var body = document.getElementsByTagName('body');
  116. if(body.length >= 1) { ajax.removeClass(body[0], 'loading'); };
  117. });
  118. };
  119. };
  120. };
  121. ajax.processTag = function(tag) {
  122. var get = tag.getAttribute('data-ajax-get');
  123. var func = tag.getAttribute('data-ajax-func');
  124. var delay = tag.getAttribute('data-ajax-delay');
  125. if(get && get != null && func && func != null) {
  126. if(delay == null) {
  127. ajax.loadTag(tag, get, func);
  128. } else {
  129. setTimeout(function() {
  130. ajax.loadTag(tag, get, func);
  131. }, delay);
  132. };
  133. };
  134. };
  135. ajax.processTags = function() {
  136. var tags = document.querySelectorAll('[data-ajax-get]');
  137. for(var key in tags) if(tags.hasOwnProperty(key)) {
  138. var tag = tags[key];
  139. ajax.processTag(tag);
  140. };
  141. };
  142. ajax.reloadTag = function(tag) {
  143. ajax.processTag(tag);
  144. };
  145. ajax.reloadTagById = function(id) {
  146. var tag = document.getElementById(id)
  147. if(tag != null) {
  148. ajax.reloadTag(tag);
  149. };
  150. };
  151. if(window.attachEvent) {
  152. window.attachEvent('onload', ajax.processTags);
  153. } else if(window.addEventListener) {
  154. window.addEventListener('load', ajax.processTags, false);
  155. };