Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. // xhr request object
  2.  
  3. var XHR = {
  4.  
  5. indicator: 'load_indicator', // the element id of the load indicator
  6. active: false, // the state of the request system
  7. pending: 0, // the current number of uncomplete requests
  8. current: {}, // a place to keep track open requests
  9.  
  10.  
  11. create: function() {
  12.  
  13. if (XHR.active == false) if ($(XHR.indicator)) $(XHR.indicator).className = 'loading_active';
  14. XHR.pending++; // increment the pending requests counter
  15. XHR.active = true; // set the request handler state to active
  16. },
  17.  
  18.  
  19. complete: function(key) {
  20.  
  21. XHR.pending--; // reduce the number of pending requests by 1
  22.  
  23. // if there are no more pending requests
  24. if (XHR.pending == 0)
  25. {
  26. XHR.active = false; // set the request handler state to inactive
  27. if ($(XHR.indicator)) $(XHR.indicator).className = 'loading_inactive'; // hide the load indicator
  28. }
  29.  
  30. this.current[ key ]['stop'] = parseInt(new Date().getTime().toString().substring(0, 10));
  31. setTimeout('XHR.cleanUp()', 30000);
  32. },
  33.  
  34.  
  35. init: function(url) {
  36.  
  37. var parts = url.split("?"); // split the url into address / parameter data
  38. var url = parts[0]; // the url is in part 0
  39. var params = {}; // set params to nothing
  40.  
  41. if (parts[1]) // if there appears to be a parameter string, handle it
  42. {
  43. params = parts[1].toQueryParams(); // convert the parameter string to an object
  44.  
  45. if (params['f'] != undefined) // if there appears to be a form id
  46. {
  47. if ($(params['f'])) // if the form actually exists
  48. {
  49. var form_params = $(params['f']).serialize(true); // serialize the form
  50. for (i in form_params) params[i] = form_params[i]; // add the form params to the other params
  51. }
  52. }
  53. }
  54.  
  55. params['requested_via_ajax'] = true; // add a value to signify that the request if being handled using ajax
  56.  
  57. var start = parseInt(new Date().getTime().toString().substring(0, 10));
  58. var key = md5(start+url); // handle for this request
  59.  
  60. // add the request to the current object
  61. this.current[ key ] = { start:start, stop:0, url:url, params:params, ajax:null, response:null };
  62.  
  63. return key;
  64. },
  65.  
  66. // perform an ajax request
  67. load: function(url, flag, function) {
  68.  
  69. var key = this.init(url); // parse the url and create a new record
  70. var flag = flag || 'update'; // default action is update
  71.  
  72. XHR.current[ key ]['ajax'] = new Ajax.Request(XHR.current[ key ]['url'], {
  73. parameters: XHR.current[ key ]['params'],
  74. onCreate: function() { XHR.create(); },
  75. onSuccess: function(transport) {
  76.  
  77. XHR.current[ key ]['response'] = transport.responseText;
  78.  
  79. if (flag == 'json')
  80. return transport.responseText;
  81. else
  82. XHR.update(key); // otherwise use the response to update the document
  83. },
  84. onFailure: function() { alert('failed!') },
  85. onComplete: function() {
  86. XHR.complete(key);
  87. return XHR.current[ key ]['response'];
  88. }
  89. });
  90. },
  91.  
  92.  
  93. update: function(key) {
  94.  
  95. var element = 'viewport'; // set the default element to update to the viewport div
  96. var content = false; // set the content to false
  97. var data = false; // set the data to false
  98. var js = false; // set the js to false
  99.  
  100. // check to see if the element to update was provided in the parameter string
  101. if (XHR.current[ key ] != undefined)
  102. if (XHR.current[ key ]['params']['e'] != undefined) element = XHR.current[ key ]['params']['e'];
  103.  
  104. // if the request returns JSON data
  105. if (XHR.current[ key ]['response'].isJSON())
  106. {
  107. // evaluate the json response
  108. var json = XHR.current[ key ]['response'].evalJSON();
  109.  
  110. // if an element is supplied in the response it supercedes the element from the parameter string
  111. if (json['e'] != undefined) element = json['e'];
  112.  
  113. // if there is content in the response update the appropriate element with the content
  114. if (json['content'] != undefined) if ($(element)) $(element).update(json['content']).show();
  115.  
  116. // if there is data in
  117. if (json['data'] != undefined) data = jason['data'];
  118.  
  119. // if there is any javascript, supply it with any response data and evaluate it
  120. if (json['js'] != undefined) eval("var data = "+data+";"+json['js']);
  121. }
  122.  
  123. // the response is not JSON
  124. else
  125. {
  126. // update the contents of the element, then show it
  127. if ($(element)) $(element).update(XHR.current[ key ]['response']).show();
  128. }
  129.  
  130. return true;
  131. },
  132.  
  133. // clear completed requests from XHR.current
  134. cleanUp: function() {
  135.  
  136. var now = parseInt(new Date().getTime().toString().substring(0, 10));
  137.  
  138. for (key in XHR.current)
  139. {
  140. if (now - XHR.current[ key ]['stop'] > 10) delete XHR.current[ key ];
  141. }
  142.  
  143. }
  144. }
Add Comment
Please, Sign In to add comment