1 //-------------------- ricoAjaxEngine.js
2 Rico.AjaxEngine = Class.create();
4 Rico.AjaxEngine.prototype = {
6 initialize: function() {
7 this.ajaxElements = new Array();
8 this.ajaxObjects = new Array();
9 this.requestURLS = new Array();
13 registerAjaxElement: function( anId, anElement ) {
16 this.ajaxElements[anId] = anElement;
19 registerAjaxObject: function( anId, anObject ) {
20 this.ajaxObjects[anId] = anObject;
23 registerRequest: function (requestLogicalName, requestURL) {
24 this.requestURLS[requestLogicalName] = requestURL;
27 sendRequest: function(requestName, options) {
28 // Allow for backwards Compatibility
29 if ( arguments.length >= 2 )
30 if (typeof arguments[1] == 'string')
31 options = {parameters: this._createQueryString(arguments, 1)};
32 this.sendRequestWithData(requestName, null, options);
35 sendRequestWithData: function(requestName, xmlDocument, options) {
36 var requestURL = this.requestURLS[requestName];
37 if ( requestURL == null )
40 // Allow for backwards Compatibility
41 if ( arguments.length >= 3 )
42 if (typeof arguments[2] == 'string')
43 options.parameters = this._createQueryString(arguments, 2);
45 new Ajax.Request(requestURL, this._requestOptions(options,xmlDocument));
48 sendRequestAndUpdate: function(requestName,container,options) {
49 // Allow for backwards Compatibility
50 if ( arguments.length >= 3 )
51 if (typeof arguments[2] == 'string')
52 options.parameters = this._createQueryString(arguments, 2);
54 this.sendRequestWithDataAndUpdate(requestName, null, container, options);
57 sendRequestWithDataAndUpdate: function(requestName,xmlDocument,container,options) {
58 var requestURL = this.requestURLS[requestName];
59 if ( requestURL == null )
62 // Allow for backwards Compatibility
63 if ( arguments.length >= 4 )
64 if (typeof arguments[3] == 'string')
65 options.parameters = this._createQueryString(arguments, 3);
67 var updaterOptions = this._requestOptions(options,xmlDocument);
69 new Ajax.Updater(container, requestURL, updaterOptions);
72 // Private -- not part of intended engine API --------------------------------------------------------------------
74 _requestOptions: function(options,xmlDoc) {
75 var requestHeaders = ['X-Rico-Version', Rico.Version ];
76 var sendMethod = 'post';
78 if (Rico.prototypeVersion < 1.4)
79 requestHeaders.push( 'Content-type', 'text/xml' );
82 (!options) ? options = {} : '';
84 if (!options._RicoOptionsProcessed){
85 // Check and keep any user onComplete functions
86 if (options.onComplete)
87 options.onRicoComplete = options.onComplete;
89 if (options.overrideOnComplete)
90 options.onComplete = options.overrideOnComplete;
92 options.onComplete = this._onRequestComplete.bind(this);
93 options._RicoOptionsProcessed = true;
96 // Set the default options and extend with any user options
98 requestHeaders: requestHeaders,
99 parameters: options.parameters,
102 onComplete: options.onComplete
104 // Set any user options:
105 Object.extend(this.options, options);
109 _createQueryString: function( theArgs, offset ) {
111 for ( var i = offset ; i < theArgs.length ; i++ ) {
115 var anArg = theArgs[i];
117 if ( anArg.name != undefined && anArg.value != undefined ) {
118 queryString += anArg.name + "=" + escape(anArg.value);
121 var ePos = anArg.indexOf('=');
122 var argName = anArg.substring( 0, ePos );
123 var argValue = anArg.substring( ePos + 1 );
124 queryString += argName + "=" + escape(argValue);
130 _onRequestComplete : function(request) {
133 // User can set an onFailure option - which will be called by prototype
134 if (request.status != 200)
137 var response = request.responseXML.getElementsByTagName("ajax-response");
138 if (response == null || response.length != 1)
140 this._processAjaxResponse( response[0].childNodes );
142 // Check if user has set a onComplete function
143 var onRicoComplete = this.options.onRicoComplete;
144 if (onRicoComplete != null)
148 _processAjaxResponse: function( xmlResponseElements ) {
149 for ( var i = 0 ; i < xmlResponseElements.length ; i++ ) {
150 var responseElement = xmlResponseElements[i];
152 // only process nodes of type element.....
153 if ( responseElement.nodeType != 1 )
156 var responseType = responseElement.getAttribute("type");
157 var responseId = responseElement.getAttribute("id");
159 if ( responseType == "object" )
160 this._processAjaxObjectUpdate( this.ajaxObjects[ responseId ], responseElement );
161 else if ( responseType == "element" )
162 this._processAjaxElementUpdate( this.ajaxElements[ responseId ], responseElement );
164 alert('unrecognized AjaxResponse type : ' + responseType );
168 _processAjaxObjectUpdate: function( ajaxObject, responseElement ) {
169 ajaxObject.ajaxUpdate( responseElement );
172 _processAjaxElementUpdate: function( ajaxElement, responseElement ) {
173 ajaxElement.innerHTML = RicoUtil.getContentAsString(responseElement);
178 Rico.includeLoaded('ricoAjaxEngine.js');