vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB

10 years ago
  1. /**
  2. * @class Ajax
  3. *
  4. * Perform asynchronous call to the server.
  5. *
  6. * documentation:
  7. * http://ajaxpatterns.org/HTTP_Streaming
  8. * http://ajaxpatterns.org/XMLHttpRequest_Call
  9. * http://www.skill-guru.com/blog/2011/02/04/adding-access-control-allow-origin-to-server-for-cross-domain-scripting/
  10. *
  11. * @author Jos de Jong, Almende, 2011
  12. */
  13. var Ajax = function() {
  14. this.isBusy = true;
  15. this.timer = undefined;
  16. this.req = undefined;
  17. this.callback = undefined;
  18. // for long poll
  19. this.pollCallback = undefined;
  20. this.pollInterval = 1000; // milliseconds
  21. this.lastResponseLength = 0;
  22. }
  23. /**
  24. * Make a request
  25. * @param {string} method The call method: typically "GET" or "POST"
  26. * @param {string} url The url to be called, for example "mydata.php"
  27. * @param {method} callback The callback method, which will be called when
  28. * the response is received. The response is passed
  29. * as a plain text (string) parameter to this method:
  30. * callback(response);
  31. */
  32. Ajax.prototype.request = function(method, url, callback)
  33. {
  34. var me = this;
  35. this.isBusy = true;
  36. this.callback = callback;
  37. this.req = (XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
  38. this.req.onreadystatechange = function() { me._checkReadyState(); };
  39. this.req.open(method, url, true);
  40. this.req.send(null);
  41. }
  42. /**
  43. * Make a long poll request.
  44. * This poll can be stopped via Ajax.abort();
  45. * @param {string} method The call method: typically "GET" or "POST"
  46. * @param {string} url The url to be called, for example "mydata.php"
  47. * @param {method} callback The callback method, which will be called
  48. * repeatedly, each time that new data is received.
  49. * The newly received data is passed
  50. * as a plain text (string) parameter to this method:
  51. * callback(response);
  52. */
  53. Ajax.prototype.requestLongPoll = function(method, url, callback)
  54. {
  55. this.request(method, url);
  56. var me = this;
  57. this.pollCallback = callback;
  58. this.lastResponseLength = 0;
  59. this.timer = setInterval(function() {me._checkResponse();}, this.pollInterval);
  60. }
  61. /**
  62. * Cancel a current request
  63. */
  64. Ajax.prototype.abort = function() {
  65. this.isBusy = false;
  66. if (this.timer) {
  67. clearInterval(this.timer)
  68. this.timer = undefined;
  69. }
  70. if (this.req) {
  71. this.req.abort();
  72. this.req = undefined;
  73. }
  74. }
  75. /**
  76. * The callback method which is called when a response is received.
  77. */
  78. Ajax.prototype._checkReadyState = function()
  79. {
  80. switch(this.req.readyState)
  81. {
  82. case 1: break;
  83. case 2: break;
  84. case 3: break;
  85. case 4:
  86. if (this.callback) {
  87. this.callback(this.req.responseText);
  88. }
  89. // reset all variables
  90. this.abort();
  91. }
  92. }
  93. /**
  94. * Callback function executed repeatedly during a long poll.
  95. * The currently received response data is checked, and all new data is passed
  96. * to the callback function.
  97. */
  98. Ajax.prototype._checkResponse = function() {
  99. var len = this.req.responseText.length;
  100. if (len > this.lastResponseLength) {
  101. var newData = this.req.responseText.substring(this.lastResponseLength);
  102. // TODO: clear the current responseText here, to prevent the response
  103. // from growing infinitely?
  104. if (this.pollCallback) {
  105. this.pollCallback(newData);
  106. }
  107. this.lastResponseLength = len;
  108. }
  109. }
  110. /**
  111. * Set the interval for long polling
  112. * @param {number} interval Interval in milliseconds
  113. */
  114. Ajax.prototype.setPollInterval = function(interval) {
  115. this.pollInterval = interval;
  116. }
  117. /**
  118. * get the poll interval
  119. */
  120. Ajax.prototype.getPollInterval = function() {
  121. return this.pollInterval;
  122. }