/branches/jsdoc_tk_gui/setup/workingDirectory/Webeo/ao/ajax/Ajax.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 213 lines · 72 code · 36 blank · 105 comment · 20 complexity · ce702f2e311846658b5a19f2bea2e5ba MD5 · raw file

  1. /**
  2. * @fileoverview
  3. * The Ajax Object class allow you to create simple Ajax Requests and to manage their response easily
  4. *
  5. *@author Sébastien Bordes => Sebastien dot Bordes at emukina dot fr
  6. *@version 1.0
  7. */
  8. ek.register("ao.ajax.Ajax");
  9. /** STATICS PROPERTY */
  10. /**
  11. * Static field used to determine the state of the AjaxRequest : uninitialized
  12. * @type int
  13. */
  14. Ajax.READY_STATE_UNINITIALIZED = 0 ;
  15. /**
  16. * Static field used to determine the state of the AjaxRequest : loading
  17. * @type int
  18. */
  19. Ajax.READY_STATE_LOADING = 1 ;
  20. /**
  21. * Static field used to determine the state of the AjaxRequest : loaded
  22. * @type int
  23. */
  24. Ajax.READY_STATE_LOADED = 2 ;
  25. /**
  26. * Static field used to determine the state of the AjaxRequest : interactive
  27. * @type int
  28. */
  29. Ajax.READY_STATE_INTERACTIVE = 3 ;
  30. /**
  31. * Static field used to determine the state of the AjaxRequest : complete
  32. * @type int
  33. */
  34. Ajax.READY_STATE_COMPLETE = 4 ;
  35. /** PRIVATE PROPERTY */
  36. /**
  37. * This object is the link with the main manager
  38. * @private
  39. * @type Object (AjaxManager or another component)
  40. */
  41. Ajax.prototype.manager = null;
  42. /**
  43. * The XMLHttpRequest object
  44. * @private
  45. * @type XMLHttpRequest
  46. */
  47. Ajax.prototype.query = null;
  48. /**
  49. * The load method to call when XMLHttpRequest is ready
  50. * @private
  51. * @type Function
  52. */
  53. Ajax.prototype.onload = null;
  54. /**
  55. * The method to call when XMLHttpRequest has failed
  56. * @private
  57. * @type Function
  58. */
  59. Ajax.prototype.onerror = Ajax.prototype.defaultError ;
  60. /**
  61. * The url to call
  62. * @private
  63. * @type String
  64. */
  65. Ajax.prototype.url = null ;
  66. /**
  67. * The method of the transport, GET or POST
  68. * @private
  69. * @type String
  70. */
  71. Ajax.prototype.method = "GET" ;
  72. /**
  73. * Parameters concatenate into a single string
  74. * @private
  75. * @type String
  76. */
  77. Ajax.prototype.params = null;
  78. /**
  79. * The content-type of the transaction,
  80. * @private
  81. * @type String
  82. */
  83. Ajax.prototype.contentType = null ;
  84. /**
  85. * Construct an Ajax object
  86. * @class
  87. * This class is used to create an Ajax object
  88. * @constructor
  89. *@env Client
  90. *@param Method:onload
  91. *@param Method:onerror
  92. *@param String:url
  93. *@param String:method
  94. *@param String:params
  95. *@param String:contentType
  96. * @return a new Ajax object
  97. */
  98. function Ajax (manager, onload, onerror, url, method, contentType, params){
  99. this.manager = manager;
  100. if (window.XMLHttpRequest){
  101. this.query=new XMLHttpRequest();
  102. } else if (window.ActiveXObject){
  103. this.query=new ActiveXObject("Microsoft.XMLHTTP");
  104. }
  105. this.onload = (onload != undefined ) ? onload : null;
  106. this.onerror = (onerror != undefined ) ? onerror : defaultError ;
  107. this.url = (url != undefined ) ? url : null ;
  108. this.method = (method != undefined ) ? method : null ;
  109. this.params = (params != undefined ) ? params : null;
  110. this.contentType = (contentType != undefined ) ? contentType : null ;
  111. if (!contentType && method=="POST"){
  112. this.contentType='application/x-www-form-urlencoded';
  113. }
  114. }
  115. /**
  116. * Set the params to send with the Ajax request
  117. * @public
  118. * @params params: Pameters to send
  119. */
  120. Ajax.prototype.setParams = function (params){
  121. this.params = params;
  122. }
  123. /**
  124. * Send the Ajax Request whit hte XMLHttpRequest object
  125. *@public
  126. *@param params Al parameters concatenate into a single String
  127. */
  128. Ajax.prototype.sendRequest = function (params){
  129. if (params){
  130. this.params = params;
  131. }
  132. if (this.query){
  133. try{
  134. var loader=this;
  135. this.query.onreadystatechange=function(){
  136. Ajax.onReadyState.call(loader);
  137. }
  138. this.query.open(this.method,this.url,true);
  139. if (this.contentType){
  140. this.query.setRequestHeader('Content-Type', this.contentType);
  141. }
  142. this.query.send(this.params);
  143. }catch (err){
  144. this.onerror.call(this);
  145. }
  146. }
  147. }
  148. /**
  149. * Call the load method when the response was received
  150. *@env Client
  151. *@isExpertDefault no
  152. */
  153. Ajax.onReadyState = function (){
  154. var req = this.query;
  155. var ready = req.readyState;
  156. //TODO Manage ALL STATE TO MANAGE A PROGRESSION BAR
  157. if (ready == Ajax.READY_STATE_COMPLETE){
  158. var httpStatus = req.status;
  159. if (httpStatus == 200 || httpStatus == 0){
  160. this.onload.call(this);
  161. }else{
  162. this.onerror.call(this);
  163. }
  164. }
  165. }
  166. /**
  167. * Display an error message if the request has failed
  168. * @private
  169. * @env Client
  170. * @isExpertDefault no
  171. */
  172. Ajax.prototype.defaultError = function (){
  173. alert("La requ?te n'a pas pu aboutir ! "
  174. +"ReadyState:"+this.req.readyState
  175. +"Status: "+this.req.status
  176. +"Headers: "+this.req.getAllResponseHeaders());
  177. }