Click here to Skip to main content
12,634,888 members (28,847 online)
Click here to Skip to main content
Articles » Web Development » Ajax » Libraries » Downloads

Stats

35.6K views
363 downloads
35 bookmarked
Posted

AWUI - a simple web user interface library with AJAX support

, 26 Jul 2007 CPOL
An article on creating a custom JavaScript controls set and cross-browser support library
img
cb_circle
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_green
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_win98
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_winxp
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
js
doc
img
preview.psd
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
cb_off.gif
cb_off_gray.gif
cb_on.gif
cb_on_gray.gif
if (typeof(AWUI) == "undefined") alert ("AWUI is not available");

// aXmlHttpRequest
function aXmlHttpRequest (url_, method_, resultFormat_) {
	if ( AWUI.isEmpty(url_) )
		AWUI.processError ('Invalid URL', AWUI.ERROR_INVALID_VALUE);
	this.url = url_;
	// Request body (only for POST requests)
	this.body = '';
	this.xmlhttp = null;
	// Type of request (async / sync)
	this.type = aXmlHttpClient.prototype.TYPE_UNINITIALIZED;
	// Timeout in milliseconds for requests
	this.timeout = aXmlHttpClient.prototype.REQUEST_TIMEOUT_DEFAULT;
	this.method = (method_ ? method_ : "GET");
	this.resultFormat = ( resultFormat_ ? resultFormat_ : aXmlHttpClient.prototype.CONTENT_XML);
	this.addTimestamp = true;
}

aXmlHttpRequest.prototype = {
	prepare : function(xmlhttp_) {
		this.xmlhttp = xmlhttp_;
		var url = this.url;
		if (this.addTimestamp) 
			url += (url.indexOf("?") > -1 ? "&" : "?" ) + aXmlHttpClient.prototype.TIMESTAMP + "=" + (new Date()).getTime();
		
		switch (this.type) {
			case aXmlHttpClient.prototype.TYPE_ASYNC:
				this.xmlhttp.open (this.method, url, true);
				break;
			case aXmlHttpClient.prototype.TYPE_SYNC:
				this.xmlhttp.open (this.method, url, false);
				break;
			default:
				return AWUI.processError ('Invalid call type', AWUI.ERROR_INVALID_VALUE);
				break;
		}

		this.xmlhttp.setRequestHeader ('Accept-Charset', 'UTF-8');
	},
	
	send : function() {
		var data = this.body;
		if ( String(this.method).toUpperCase() == "GET")
			data = null;
		var length = 0;
		if ( !AWUI.isEmpty(data))
			length = data.length;
		else
			data = '';
		this.xmlhttp.setRequestHeader ('Content-Length', length);
		this.xmlhttp.send (data);
	}
};

//////////////////////////////////////////////////////////////////////////////
//
// aXmlHttpClient

function aXmlHttpClient () {
	this.xmlhttp = null;
	this.callback = null;
	this.request = null;

	// Id used for timing out async calls
	this.timeoutId = 0;
}

aXmlHttpClient.prototype = {
	STATE_UNINITIALIZED : 0,
	STATE_LOADING : 1,
	STATE_LOADED : 2,
	STATE_INTERACTIVE : 3,
	STATE_COMPLETE : 4,
	
	TYPE_UNINITIALIZED : 0,
	TYPE_SYNC : 1,
	TYPE_ASYNC : 2,

	REQUEST_TIMEOUT_DEFAULT : 120000,

	HTTP_STATUS_OK : 200,
	
	CONTENT_XML  : "text/xml",
	CONTENT_TEXT  : "text/plain",
	TIMESTAMP : "ts",
	

	init : function() {
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var MSXML_XMLHTTP_PROGIDS = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
			var success = false;
			for ( var i = 0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++ ) {
				try {
					this.xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
					success = true;
				} catch (e) {
				}
			}
			if ( !success )
				return AWUI.processError ('Unable to create XMLHttpRequest.', AWUI.ERROR_DOM);
		}
	},
	
	// process result
	_getResponse : function (request_)
	{
		var needXml = true;
		if (request_)
			needXml = (request_.resultFormat == aXmlHttpClient.prototype.CONTENT_XML);
			
		var res = (needXml ? this.xmlhttp.responseXML : this.xmlhttp.responseText);
		
		return res;
	},
	
	// Perform an synchronous call (results returned directly)
	call : function (request_ /*url*/, method_, resultFormat_) {
		if ( this.callInProgress () )
			return AWUI.processError ('Call in progress', AWUI.ERROR_RUNTIME);
		if ( !this.xmlhttp )
			this.init ();
		
		if (!request_)
			return AWUI.processError ('Invalid HTTP call parameters', AWUI.ERROR_OBJECT);

		if (typeof(request_) != "object") 
			request_ = new aXmlHttpRequest (request_, method_, resultFormat_);
			
		this.callback = null;
		this.request = request_;

		this.request.type = this.TYPE_SYNC;
		this.request.prepare (this.xmlhttp);
		this.request.send ();

		if ( this.xmlhttp.status == this.HTTP_STATUS_OK ) {
			return this._getResponse();

		} else {
			var errorMsg = 'HTTP request failed, status: ' + this.xmlhttp.status + 
				(this.xmlhttp.statusText ? ' ' + this.xmlhttp.statusText : "");
			// .headers = this.xmlhttp.getAllResponseHeaders ();
			AWUI.processError (errorMsg, AWUI.ERROR_RUNTIME);
		}
	},
	
	// Perform an asynchronous call (results sent to handler - callback function)
	callAsync : function (request_/*url*/, handler_, method_, resultFormat_) {
		
		if ( this.callInProgress () )
			return AWUI.processError ('Call in progress', AWUI.ERROR_RUNTIME);
		if ( !this.xmlhttp )
			this.init ();

		if (!request_)
			return AWUI.processError ('Invalid HTTP call parameters', AWUI.ERROR_OBJECT);
			
		if (typeof(request_) != "object") 
			request_ = new aXmlHttpRequest (request_, method_, resultFormat_);
				
		if ( !AWUI.isEmpty(handler_) )
			this.callback = handler_;
		else
			this.callback = null;

		request_.type = this.TYPE_ASYNC;
		var self = this;
		var req = request_;
		
		request_.prepare (this.xmlhttp);
		
		this.xmlhttp.onreadystatechange = function() {
			aXmlHttpOnStateChange (self, req);
		}
		request_.send ();
		this.timeoutId = window.setTimeout(
			function() { self.abort (self); },
			request_.timeout 
		);
		
	},
	// Checks to see if XmlHttpRequest is busy
	callInProgress : function () {
		if ( !this.xmlhttp )
			return false;
		if ( this.xmlhttp.readyState == this.STATE_UNINITIALIZED || this.xmlhttp.readyState == this.STATE_COMPLETE )
			return false;
		return true;
	},
	
	// Aborts the request
	abort : function (client_)	{
		if (!client_) 
			client_ = this;
		if ( client_.callInProgress () ) {
			client_.xmlhttp.abort ();
			if (typeof(client_.callback) == "function")	
				client_.callback (null);
		}
	}
}


// Callback for asyncCalls
function aXmlHttpOnStateChange (client_, request_) {
	if ( AWUI.isEmpty(client_) || AWUI.isEmpty(request_))
		AWUI.processError ("Invalid XmlHttp object", AWUI.ERROR_OBJECT);
		
	switch (client_.xmlhttp.readyState) {
		case aXmlHttpClient.prototype.STATE_LOADING:
		case aXmlHttpClient.prototype.STATE_LOADED:
		case aXmlHttpClient.prototype.STATE_INTERACTIVE:
			break;
		// Download complete
		case aXmlHttpClient.prototype.STATE_COMPLETE:
			window.clearTimeout (client_.timeoutId);

			switch (client_.xmlhttp.status) {
				
				case aXmlHttpClient.prototype.HTTP_STATUS_OK:
					if (typeof(client_.callback) == 'function')	{
						client_.callback ( client_._getResponse(request_) );
					}
					break;

				// Special case for IE on aborted requests
				case 0:
					break;

				default:
					var errorMsg = 'HTTP request failed, status: ' + client_.xmlhttp.status + 
						(client_.xmlhttp.statusText ? ' ' + client_.xmlhttp.statusText : "");
					AWUI.processError (errorMsg, AWUI.ERROR_RUNTIME);
					//e.headers = client_.xmlhttp.getAllResponseHeaders ();
					break;
			}
			break;
	}
}













By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Artem Kustikov
Software Developer (Senior)
Belarus Belarus
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 26 Jul 2007
Article Copyright 2007 by Artem Kustikov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid