PageRenderTime 22ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

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