/modules/luci-base/htdocs/luci-static/resources/xhr.js

https://gitlab.com/jiangming1399/luci · JavaScript · 262 lines · 209 code · 45 blank · 8 comment · 44 complexity · 7a2974bf797109c15570e764b5203279 MD5 · raw file

  1. /*
  2. * xhr.js - XMLHttpRequest helper class
  3. * (c) 2008-2010 Jo-Philipp Wich
  4. */
  5. XHR = function()
  6. {
  7. this.reinit = function()
  8. {
  9. if (window.XMLHttpRequest) {
  10. this._xmlHttp = new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject) {
  13. this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  14. }
  15. else {
  16. alert("xhr.js: XMLHttpRequest is not supported by this browser!");
  17. }
  18. }
  19. this.busy = function() {
  20. if (!this._xmlHttp)
  21. return false;
  22. switch (this._xmlHttp.readyState)
  23. {
  24. case 1:
  25. case 2:
  26. case 3:
  27. return true;
  28. default:
  29. return false;
  30. }
  31. }
  32. this.abort = function() {
  33. if (this.busy())
  34. this._xmlHttp.abort();
  35. }
  36. this.get = function(url,data,callback,timeout)
  37. {
  38. this.reinit();
  39. var xhr = this._xmlHttp;
  40. var code = this._encode(data);
  41. url = location.protocol + '//' + location.host + url;
  42. if (code)
  43. if (url.substr(url.length-1,1) == '&')
  44. url += code;
  45. else
  46. url += '?' + code;
  47. xhr.open('GET', url, true);
  48. if (!isNaN(timeout))
  49. xhr.timeout = timeout;
  50. xhr.onreadystatechange = function()
  51. {
  52. if (xhr.readyState == 4) {
  53. var json = null;
  54. if (xhr.getResponseHeader("Content-Type") == "application/json") {
  55. try {
  56. json = eval('(' + xhr.responseText + ')');
  57. }
  58. catch(e) {
  59. json = null;
  60. }
  61. }
  62. callback(xhr, json);
  63. }
  64. }
  65. xhr.send(null);
  66. }
  67. this.post = function(url,data,callback,timeout)
  68. {
  69. this.reinit();
  70. var xhr = this._xmlHttp;
  71. var code = this._encode(data);
  72. xhr.onreadystatechange = function()
  73. {
  74. if (xhr.readyState == 4)
  75. callback(xhr);
  76. }
  77. xhr.open('POST', url, true);
  78. if (!isNaN(timeout))
  79. xhr.timeout = timeout;
  80. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  81. xhr.send(code);
  82. }
  83. this.cancel = function()
  84. {
  85. this._xmlHttp.onreadystatechange = function(){};
  86. this._xmlHttp.abort();
  87. }
  88. this.send_form = function(form,callback,extra_values)
  89. {
  90. var code = '';
  91. for (var i = 0; i < form.elements.length; i++)
  92. {
  93. var e = form.elements[i];
  94. if (e.options)
  95. {
  96. code += (code ? '&' : '') +
  97. form.elements[i].name + '=' + encodeURIComponent(
  98. e.options[e.selectedIndex].value
  99. );
  100. }
  101. else if (e.length)
  102. {
  103. for (var j = 0; j < e.length; j++)
  104. if (e[j].name) {
  105. code += (code ? '&' : '') +
  106. e[j].name + '=' + encodeURIComponent(e[j].value);
  107. }
  108. }
  109. else
  110. {
  111. code += (code ? '&' : '') +
  112. e.name + '=' + encodeURIComponent(e.value);
  113. }
  114. }
  115. if (typeof extra_values == 'object')
  116. for (var key in extra_values)
  117. code += (code ? '&' : '') +
  118. key + '=' + encodeURIComponent(extra_values[key]);
  119. return(
  120. (form.method == 'get')
  121. ? this.get(form.getAttribute('action'), code, callback)
  122. : this.post(form.getAttribute('action'), code, callback)
  123. );
  124. }
  125. this._encode = function(obj)
  126. {
  127. obj = obj ? obj : { };
  128. obj['_'] = Math.random();
  129. if (typeof obj == 'object')
  130. {
  131. var code = '';
  132. var self = this;
  133. for (var k in obj)
  134. code += (code ? '&' : '') +
  135. k + '=' + encodeURIComponent(obj[k]);
  136. return code;
  137. }
  138. return obj;
  139. }
  140. }
  141. XHR.get = function(url, data, callback)
  142. {
  143. (new XHR()).get(url, data, callback);
  144. }
  145. XHR.poll = function(interval, url, data, callback, post)
  146. {
  147. if (isNaN(interval) || interval < 1)
  148. interval = 5;
  149. if (!XHR._q)
  150. {
  151. XHR._t = 0;
  152. XHR._q = [ ];
  153. XHR._r = function() {
  154. for (var i = 0, e = XHR._q[0]; i < XHR._q.length; e = XHR._q[++i])
  155. {
  156. if (!(XHR._t % e.interval) && !e.xhr.busy())
  157. e.xhr[post ? 'post' : 'get'](e.url, e.data, e.callback, e.interval * 1000 - 5);
  158. }
  159. XHR._t++;
  160. };
  161. }
  162. var e = {
  163. interval: interval,
  164. callback: callback,
  165. url: url,
  166. data: data,
  167. xhr: new XHR()
  168. };
  169. XHR._q.push(e);
  170. XHR.run();
  171. return e;
  172. }
  173. XHR.stop = function(e)
  174. {
  175. for (var i = 0; XHR._q && XHR._q[i]; i++) {
  176. if (XHR._q[i] === e) {
  177. e.xhr.cancel();
  178. XHR._q.splice(i, 1);
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. XHR.halt = function()
  185. {
  186. if (XHR._i)
  187. {
  188. /* show & set poll indicator */
  189. try {
  190. document.getElementById('xhr_poll_status').style.display = '';
  191. document.getElementById('xhr_poll_status_on').style.display = 'none';
  192. document.getElementById('xhr_poll_status_off').style.display = '';
  193. } catch(e) { }
  194. window.clearInterval(XHR._i);
  195. XHR._i = null;
  196. }
  197. }
  198. XHR.run = function()
  199. {
  200. if (XHR._r && !XHR._i)
  201. {
  202. /* show & set poll indicator */
  203. try {
  204. document.getElementById('xhr_poll_status').style.display = '';
  205. document.getElementById('xhr_poll_status_on').style.display = '';
  206. document.getElementById('xhr_poll_status_off').style.display = 'none';
  207. } catch(e) { }
  208. /* kick first round manually to prevent one second lag when setting up
  209. * the poll interval */
  210. XHR._r();
  211. XHR._i = window.setInterval(XHR._r, 1000);
  212. }
  213. }
  214. XHR.running = function()
  215. {
  216. return !!(XHR._r && XHR._i);
  217. }