ajax.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. ajax.postJSON = function(url, data, callbackSuccess, callbackError, async) {
  85. ajax.post(url, data, function(method, data, readyState, status, responseText) {
  86. if(readyState == 4) {
  87. if(status == 200) {
  88. try {
  89. var r = JSON.parse(responseText);
  90. if(callbackSuccess) {
  91. callbackSuccess(method, data, readyState, status, r);
  92. };
  93. } catch(e) {
  94. if(callbackError) {
  95. callbackError(method, data, readyState, status, e);
  96. };
  97. };
  98. } else {
  99. if(callbackError) {
  100. var e = new AjaxErrorBadStatusCode('Bad status code '+status);
  101. callbackError(method, data, readyState, status, e);
  102. };
  103. };
  104. };
  105. }, async);
  106. };
  107. ajax.loadTag = function(tag, url, func, field) {
  108. if((typeof window[func] === 'function') || field != null) {
  109. if(!!!tag.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  110. tag.className += " loading";
  111. ajax.getJSON(url, {}, function(method, data, readyState, status, responseData) {
  112. try {
  113. if(typeof window[func] === 'function') {
  114. var html = window[func](tag, responseData);
  115. tag.innerHTML = html;
  116. } else if(field != null) {
  117. tag.innerHTML = responseData[field];
  118. };
  119. } catch(e) {
  120. console.log('ajax.loadTag', 'e', e);
  121. };
  122. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  123. }, function(method, data, readyState, status, responseData) {
  124. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  125. });
  126. };
  127. };
  128. };
  129. ajax.processTag = function(tag) {
  130. var get = tag.getAttribute('data-ajax-get');
  131. var func = tag.getAttribute('data-ajax-func');
  132. var field = tag.getAttribute('data-ajax-field');
  133. var delay = tag.getAttribute('data-ajax-delay');
  134. if((get && get != null) && ((func && func != null) || (field && field != null))) {
  135. if(delay == null) {
  136. ajax.loadTag(tag, get, func, field);
  137. } else {
  138. setTimeout(function() {
  139. ajax.loadTag(tag, get, func, field);
  140. }, delay);
  141. };
  142. };
  143. };
  144. ajax.processTags = function() {
  145. var tags = document.querySelectorAll('[data-ajax-get]');
  146. for(var key in tags) if(tags.hasOwnProperty(key)) {
  147. var tag = tags[key];
  148. ajax.processTag(tag);
  149. };
  150. };
  151. ajax.reloadTag = function(tag) {
  152. ajax.processTag(tag);
  153. };
  154. ajax.reloadTagById = function(id) {
  155. var tag = document.getElementById(id)
  156. if(tag != null) {
  157. ajax.reloadTag(tag);
  158. };
  159. };
  160. ajax.processFormSubmit = function(event) {
  161. event.preventDefault();
  162. var form = event.target;
  163. var func = form.getAttribute('data-ajax-func');
  164. if(func && func != null && typeof window[func] === 'function') {
  165. if(!!!form.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  166. form.className += " loading";
  167. var data = {};
  168. var inputs = form.querySelectorAll("input,select,textarea");
  169. for(var i=0,m=inputs.length-1; i<=m; i++) {
  170. data[inputs[i].name] = inputs[i].value;
  171. };
  172. var ajaxFunc = ajax.get;
  173. if(form.method == "post") { ajaxFunc = ajax.post; };
  174. ajaxFunc(form.action, data, function(method, data, readyState, status, responseText) {
  175. if(readyState == 4) {
  176. var error = (status == 200);
  177. var responseData = responseText;
  178. try {
  179. var responseData = JSON.parse(responseText);
  180. } catch(e) {
  181. console.log('ajax.processFormSubmit', 'e', e);
  182. };
  183. try {
  184. window[func](form, responseData, error, status);
  185. } catch(e) {
  186. console.log('ajax.processFormSubmit', 'e', e);
  187. };
  188. form.className = form.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  189. };
  190. });
  191. };
  192. };
  193. };
  194. ajax.processForm = function(form) {
  195. var is = form.getAttribute('data-ajax-form');
  196. var func = form.getAttribute('data-ajax-func');
  197. if((is && is != null && is == "true") && (func && func != null)) {
  198. if(window.attachEvent) {
  199. form.attachEvent('onsubmit', ajax.processFormSubmit);
  200. } else if(window.addEventListener) {
  201. form.addEventListener('submit', ajax.processFormSubmit, false);
  202. };
  203. };
  204. };
  205. ajax.processForms = function() {
  206. var forms = document.querySelectorAll('[data-ajax-form]');
  207. for(var key in forms) if(forms.hasOwnProperty(key)) {
  208. var form = forms[key];
  209. ajax.processForm(form);
  210. };
  211. };
  212. if(window.attachEvent) {
  213. window.attachEvent('onload', ajax.processTags);
  214. window.attachEvent('onload', ajax.processForms);
  215. } else if(window.addEventListener) {
  216. window.addEventListener('load', ajax.processTags, false);
  217. window.addEventListener('load', ajax.processForms, false);
  218. };