ajax.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. function AjaxErrorBadStatusCode(message) { this.name = 'AjaxErrorBadStatusCode'; this.message = message; };
  2. AjaxErrorBadStatusCode.prototype = new Error();
  3. var ajax = {loaded: false};
  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 == 'PUT' || method == 'POST') {
  43. a.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  44. };
  45. a.send(data);
  46. };
  47. ajax.del = 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, 'DELETE', null, async);
  53. };
  54. ajax.delJSON = function(url, data, callbackSuccess, callbackError, async) {
  55. ajax.del(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.get = 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 + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async);
  83. };
  84. ajax.getJSON = function(url, data, callbackSuccess, callbackError, async) {
  85. ajax.get(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.put = function(url, data, callback, async) {
  108. var query = [];
  109. for (var key in data) {
  110. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  111. };
  112. ajax.send(url, callback, 'PUT', query.join('&'), async);
  113. };
  114. ajax.putJSON = function(url, data, callbackSuccess, callbackError, async) {
  115. ajax.put(url, data, function(method, data, readyState, status, responseText) {
  116. if(readyState == 4) {
  117. if(status == 200) {
  118. try {
  119. var r = JSON.parse(responseText);
  120. if(callbackSuccess) {
  121. callbackSuccess(method, data, readyState, status, r);
  122. };
  123. } catch(e) {
  124. if(callbackError) {
  125. callbackError(method, data, readyState, status, e);
  126. };
  127. };
  128. } else {
  129. if(callbackError) {
  130. var e = new AjaxErrorBadStatusCode('Bad status code '+status);
  131. callbackError(method, data, readyState, status, e);
  132. };
  133. };
  134. };
  135. }, async);
  136. };
  137. ajax.post = function(url, data, callback, async) {
  138. var query = [];
  139. for (var key in data) {
  140. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  141. };
  142. ajax.send(url, callback, 'POST', query.join('&'), async);
  143. };
  144. ajax.postJSON = function(url, data, callbackSuccess, callbackError, async) {
  145. ajax.post(url, data, function(method, data, readyState, status, responseText) {
  146. if(readyState == 4) {
  147. if(status == 200) {
  148. try {
  149. var r = JSON.parse(responseText);
  150. if(callbackSuccess) {
  151. callbackSuccess(method, data, readyState, status, r);
  152. };
  153. } catch(e) {
  154. if(callbackError) {
  155. callbackError(method, data, readyState, status, e);
  156. };
  157. };
  158. } else {
  159. if(callbackError) {
  160. var e = new AjaxErrorBadStatusCode('Bad status code '+status);
  161. callbackError(method, data, readyState, status, e);
  162. };
  163. };
  164. };
  165. }, async);
  166. };
  167. ajax.loadTag = function(tag, url, func, field) {
  168. if((typeof window[func] === 'function') || field != null) {
  169. if(!!!tag.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  170. tag.className += " loading";
  171. ajax.getJSON(url, {}, function(method, data, readyState, status, responseData) {
  172. try {
  173. if(typeof window[func] === 'function') {
  174. var html = window[func](tag, responseData);
  175. tag.innerHTML = html;
  176. } else if(field != null) {
  177. tag.innerHTML = responseData[field];
  178. };
  179. } catch(e) {
  180. console.log('ajax.loadTag', 'e', e);
  181. };
  182. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  183. }, function(method, data, readyState, status, responseData) {
  184. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  185. });
  186. };
  187. };
  188. };
  189. ajax.processTag = function(tag) {
  190. var get = tag.getAttribute('data-ajax-get');
  191. var func = tag.getAttribute('data-ajax-func');
  192. var field = tag.getAttribute('data-ajax-field');
  193. var delay = tag.getAttribute('data-ajax-delay');
  194. if((get && get != null) && ((func && func != null) || (field && field != null))) {
  195. if(delay == null) {
  196. ajax.loadTag(tag, get, func, field);
  197. } else {
  198. setTimeout(function() {
  199. ajax.loadTag(tag, get, func, field);
  200. }, delay);
  201. };
  202. };
  203. };
  204. ajax.processTags = function() {
  205. var tags = document.querySelectorAll('[data-ajax-get]');
  206. for(var key in tags) if(tags.hasOwnProperty(key)) {
  207. var tag = tags[key];
  208. ajax.processTag(tag);
  209. };
  210. };
  211. ajax.reloadTag = function(tag) {
  212. ajax.processTag(tag);
  213. };
  214. ajax.reloadTagById = function(id) {
  215. var tag = document.getElementById(id)
  216. if(tag != null) {
  217. ajax.reloadTag(tag);
  218. };
  219. };
  220. ajax.processFormSubmit = function(event) {
  221. if(!event) var event = window.event;
  222. event.preventDefault();
  223. var form = event.target;
  224. var func = form.getAttribute('data-ajax-func');
  225. var put = form.getAttribute('data-put');
  226. if(func && func != null && typeof window[func] === 'function') {
  227. if(!!!form.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  228. form.className += " loading";
  229. var data = {};
  230. var inputs = form.querySelectorAll("input,select,textarea");
  231. for(var i=0,m=inputs.length-1; i<=m; i++) {
  232. data[inputs[i].name] = inputs[i].value;
  233. };
  234. var ajaxFunc = ajax.get;
  235. if(form.method == "post") { ajaxFunc = ajax.post; };
  236. if(put && put != null && put == "true") { ajaxFunc = ajax.put; };
  237. ajaxFunc(form.action, data, function(method, data, readyState, status, responseText) {
  238. if(readyState == 4) {
  239. var error = (status != 200);
  240. var responseData = responseText;
  241. try {
  242. var responseData = JSON.parse(responseText);
  243. } catch(e) {
  244. console.log('ajax.processFormSubmit', 'e', e);
  245. };
  246. try {
  247. window[func](form, responseData, error, status);
  248. } catch(e) {
  249. console.log('ajax.processFormSubmit', 'e', e);
  250. };
  251. form.className = form.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  252. };
  253. });
  254. };
  255. };
  256. };
  257. ajax.processForm = function(form) {
  258. var is = form.getAttribute('data-ajax-form');
  259. var func = form.getAttribute('data-ajax-func');
  260. if((is && is != null && is == "true") && (func && func != null)) {
  261. if(window.attachEvent) {
  262. form.attachEvent('onsubmit', ajax.processFormSubmit);
  263. } else if(window.addEventListener) {
  264. form.addEventListener('submit', ajax.processFormSubmit, false);
  265. };
  266. };
  267. };
  268. ajax.processForms = function() {
  269. var forms = document.querySelectorAll('[data-ajax-form]');
  270. for(var key in forms) if(forms.hasOwnProperty(key)) {
  271. var form = forms[key];
  272. ajax.processForm(form);
  273. };
  274. };
  275. ajax.processCheckboxClick = function(event) {
  276. if(!event) var event = window.event;
  277. var checkbox = event.target;
  278. var on = checkbox.getAttribute('data-ajax-on');
  279. var off = checkbox.getAttribute('data-ajax-off');
  280. var func = checkbox.getAttribute('data-ajax-func');
  281. var box = checkbox.parentNode;
  282. if(!!box.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  283. event.preventDefault();
  284. event.stopPropagation();
  285. return false;
  286. };
  287. box.className += " loading";
  288. ajax.post(checkbox.checked ? on : off, {}, function(method, data, readyState, status, responseText) {
  289. if(readyState == 4) {
  290. var error = (status != 200);
  291. var responseData = responseText;
  292. try {
  293. var responseData = JSON.parse(responseText);
  294. } catch(e) {
  295. console.log('ajax.processCheckboxClick', 'e', e);
  296. };
  297. try {
  298. window[func](checkbox, responseData, error, status);
  299. } catch(e) {
  300. console.log('ajax.processCheckboxClick', 'e', e);
  301. };
  302. if(error) {
  303. checkbox.checked = !checkbox.checked;
  304. };
  305. box.className = box.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  306. };
  307. });
  308. return true;
  309. };
  310. ajax.processCheckbox = function(checkbox) {
  311. var is = checkbox.getAttribute('data-ajax-checkbox');
  312. var on = checkbox.getAttribute('data-ajax-on');
  313. var off = checkbox.getAttribute('data-ajax-off');
  314. var func = checkbox.getAttribute('data-ajax-func');
  315. if((is && is != null && is == "true") && (on && on != null) && (off && off != null) && (func && func != null)) {
  316. if(window.attachEvent) {
  317. checkbox.attachEvent('onclick', ajax.processCheckboxClick);
  318. } else if(window.addEventListener) {
  319. checkbox.addEventListener('click', ajax.processCheckboxClick, false);
  320. };
  321. };
  322. };
  323. ajax.processCheckboxes = function() {
  324. var checkboxes = document.querySelectorAll('[data-ajax-checkbox]');
  325. for(var key in checkboxes) if(checkboxes.hasOwnProperty(key)) {
  326. var checkbox = checkboxes[key];
  327. ajax.processCheckbox(checkbox);
  328. };
  329. };
  330. ajax.load = function() {
  331. if(!ajax.loaded) {
  332. ajax.loaded = true;
  333. ajax.processTags();
  334. ajax.processForms();
  335. ajax.processCheckboxes();
  336. };
  337. };
  338. if(window.attachEvent) {
  339. window.attachEvent('onload', ajax.load);
  340. } else if(window.addEventListener) {
  341. window.addEventListener('load', ajax.load, false);
  342. };