Tuesday, December 22, 2009

AJAX API

function AjaxProcess() { //start of class AjaxProcess


/*

* @Type : Class/Function

* @Name : AjaxProcess

* @Param : Mandatory -url - non empty url string

* @Param : Optional - typeOfOperation - boolean, true for synchronous process,and false otherwise - Defaults to false

* @Param : Optional - params - string, parameters to be sent via post request. Presence of this param will force a post request

* @Setter : setSource(newSourceURL)- To set the source for the ajax request

* @Setter : setAsynchronous(typeOfOperation)- To set the type of ajax request

* @Setter : setXMLResponse(xmlResponseFlag)- required to obtain xml response

* @Setter : setDebugFlag(debugFlag)- To help developers to check with

custom alerts with out affecting the flow

* @Property : initiate() - To initate the ajax request using an object of this class

* @Property : getResponse() - To get the response from the server using an object of this class

* @Property : getResponseText()- To get the text response from the server using an object of this class

* @Property : getHtmlFilteredResponseText - To get the html filtered text response from the server using an object of this class

* @Property : getResponseTable()- To get the table formated response from the server using an object of this class (special case will be rarely used when the server prints a html table as a response)

* @Property : rebuildRequest() - To re-initate the ajax request using an object of this class

* @Property : loadXMLString(textXml) - To load the given the xml string and convert the same into a xml document by parsing the same

* @Created : 13th, Oct'09

* @Author : Karthikeyan A

**/



var urlToProcess= arguments[0];

var opType=arguments[1];

var params=arguments[2];



var xmlHttpReq=null;

var ajaxResponse=null;



var isXMLResponse=false;

var isDebug=false;



/************Getters and Setters************************/

this.setSource= function(newSourceURL) {

if (typeof newSourceURL!="string") {

if (isDebug) alert("The param newSourceURL is not a string in function setSource(newSourceURL)");

newSourceURL=""

}

urlToProcess=newSourceURL;

}



this.setAsynchronous=function(typeOfOperation) {

if (typeof typeOfOperation!="boolean") {

if (isDebug) alert("The param typeOfOperation is not a boolean in function setAsynchronous(typeOfOperation)");

typeOfOperation=false;

}

opType=typeOfOperation;

}



this.setXMLResponse=function(xmlResponseFlag) {

if (typeof xmlResponseFlag!="boolean") {

if (isDebug) alert("The param typeOfOperation is not a boolean in function setXMLResponse(xmlResponseFlag)");

xmlResponseFlag=false;

}

isXMLResponse=xmlResponseFlag;

}



this.setDebugFlag=function(debugFlag) {

if (typeof xmlResponseFlag!="boolean") {

alert("The param typeOfOperation is not a boolean in function setDebugFlag(debugFlag)");

xmlResponseFlag=false;

}

isDebug=debugFlag;

}



/********* properties to be used by users***************/

//function to initate the ajax request using an object of this class

this.initiate = function() {

try {

//check for validity and re-initialize (if necessary) the parameters required for the class

if (typeof urlToProcess!="string") {

if (isDebug) alert("The param urlToProcess is not a string in function AjaxProcess(url,typeOfOperation)");

throw new SyntaxError("The param urlToProcess is not a string in function AjaxProcess(url,typeOfOperation)");

} else if (this.trim(urlToProcess)=="") {

if (isDebug) alert("The param urlToProcess is cannot be an empty string in function AjaxProcess(url,typeOfOperation)");

throw new SyntaxError("The param urlToProcess cannot be an empty string in function AjaxProcess(url,typeOfOperation)");

}



if (typeof typeOfOperation!="boolean") {

if (isDebug) alert("The param typeOfOperation is not a boolean in function AjaxProcess(url,typeOfOperation)");

typeOfOperation=false;

}



//create an xml http request

xmlHttpReq=this.xmlHttpRequest();



//if parameters- the thrid argument has been provided, force a post request

if (typeof params=="string") {

//mark the source and async type of the request

xmlHttpReq.open("POST",urlToProcess,opType);

//send request to server

xmlHttpReq.send(params);

} else {

//mark the source and async type of the request

xmlHttpReq.open("GET",urlToProcess,opType);

//send request to server

xmlHttpReq.send(null);

}



//get the response in text/xml format as desired (governed by the usXMLResponse flag)

if (!isXMLResponse) {

ajaxResponse=xmlHttpReq.responseText;

} else {

ajaxResponse=xmlHttpReq.responseXML;

}

} catch(error) {

//throw formated error messages in case of errors

error.message="API Error in method AjaxProcess(url,typeOfOperation).initiate(): -"+error.message;

throw error

}

}



this.getResponseXML=function() {

return xmlHttpReq.responseXML;

}



this.getResponseText=function() {

return xmlHttpReq.responseText;

}



if (!isXMLResponse) { //start of isXMLResponse if

this.getHtmlFilteredResponseText=function() {

var htmlFilteredajaxResponse=ajaxResponse.replace(/(<([^>]+)>)/ig,"");

htmlFilteredajaxResponse=this.trim(htmlFilteredajaxResponse, "\n");

return htmlFilteredajaxResponse;

}



this.getResponseTable=function() {

//To get the table formated response from the server using an object of this class

//(special case will be rarely used when the server prints a html table as a response)

var ajaxTableResponse=this.trim(ajaxResponse, "\n")

return ajaxTableResponse;

}

} //end of isXMLResponse if



this.rebuildRequest=function() {

xmlHttpReq=null;

ajaxResponse=null;

this.initiate();

}



this.recycle=function() {

xmlHttpReq=null;

ajaxResponse=null;

urlToProcess= null;

opType=null;

isXMLResponse=null;

}



/*****************Back end usage only****************************/

//function to generate a xmlhttprequest

this.xmlHttpRequest= function() {

var xmlhttp;

try {

if (window.XMLHttpRequest) {

// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

} else if (window.ActiveXObject) {

// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

} else {

throw new Error("Your browser does not support XMLHTTP! - in function xmlHttpRequest()");

return;

}

} catch(error) {

error.message="API Error in method AjaxProcess(url,typeOfOperation).xmlHttpRequest(): -"+error.message;

xmlhttp=null;

throw error

} finally {

return xmlhttp;

}

}



this.loadXMLString= function(textXml) {

var xmlDoc=null;

try {

if (window.DOMParser) { //firefox, Chrome, Opera, Safari

parser=new DOMParser();

xmlDoc=parser.parseFromString(textXml,"text/xml");

} else { // Internet Explorer

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async="false";

xmlDoc.loadXML(textXml);

}

} catch(error) {

//throw formated error messages in case of errors

error.message="API Error in method AjaxProcess(url,typeOfOperation).loadXMLString(textXml): -"+error.message;

//mark the return value

xmlDoc=null;

throw error;

} finally {

return xmlDoc;

}

}



//function to trim of leading and trailing spaces

this.trim=function(str, chars) {

return this.ltrim(this.rtrim(str, chars), chars);

}



//function to trim of white spaces on the left

this.ltrim=function(str, chars) {

chars = chars

"\\s";

return str.replace(new RegExp("^[" + chars + "]+", "g"), "");

}



//function to trim of white spaces on the right

this.rtrim=function(str, chars) {

chars = chars

"\\s";

return str.replace(new RegExp("[" + chars + "]+$", "g"), "");

}

} //end of class AjaxProcess

No comments:

Post a Comment