OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@1020 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_TrackBack / branches / DOM-branch / trackback / js / rico / ricoAjaxEngine.js
1 //-------------------- ricoAjaxEngine.js
2 Rico.AjaxEngine = Class.create();
3
4 Rico.AjaxEngine.prototype = {
5
6    initialize: function() {
7       this.ajaxElements = new Array();
8       this.ajaxObjects  = new Array();
9       this.requestURLS  = new Array();
10       this.options = {};
11    },
12
13    registerAjaxElement: function( anId, anElement ) {
14       if ( !anElement )
15          anElement = $(anId);
16       this.ajaxElements[anId] = anElement;
17    },
18
19    registerAjaxObject: function( anId, anObject ) {
20       this.ajaxObjects[anId] = anObject;
21    },
22
23    registerRequest: function (requestLogicalName, requestURL) {
24       this.requestURLS[requestLogicalName] = requestURL;
25    },
26
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);
33    },
34
35    sendRequestWithData: function(requestName, xmlDocument, options) {
36       var requestURL = this.requestURLS[requestName];
37       if ( requestURL == null )
38          return;
39
40       // Allow for backwards Compatibility
41       if ( arguments.length >= 3 )
42         if (typeof arguments[2] == 'string')
43           options.parameters = this._createQueryString(arguments, 2);
44
45       new Ajax.Request(requestURL, this._requestOptions(options,xmlDocument));
46    },
47
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);
53
54       this.sendRequestWithDataAndUpdate(requestName, null, container, options);
55    },
56
57    sendRequestWithDataAndUpdate: function(requestName,xmlDocument,container,options) {
58       var requestURL = this.requestURLS[requestName];
59       if ( requestURL == null )
60          return;
61
62       // Allow for backwards Compatibility
63       if ( arguments.length >= 4 )
64         if (typeof arguments[3] == 'string')
65           options.parameters = this._createQueryString(arguments, 3);
66
67       var updaterOptions = this._requestOptions(options,xmlDocument);
68
69       new Ajax.Updater(container, requestURL, updaterOptions);
70    },
71
72    // Private -- not part of intended engine API --------------------------------------------------------------------
73
74    _requestOptions: function(options,xmlDoc) {
75       var requestHeaders = ['X-Rico-Version', Rico.Version ];
76       var sendMethod = 'post';
77       if ( xmlDoc == null )
78         if (Rico.prototypeVersion < 1.4)
79         requestHeaders.push( 'Content-type', 'text/xml' );
80       else
81           sendMethod = 'get';
82       (!options) ? options = {} : '';
83
84       if (!options._RicoOptionsProcessed){
85       // Check and keep any user onComplete functions
86         if (options.onComplete)
87              options.onRicoComplete = options.onComplete;
88         // Fix onComplete
89         if (options.overrideOnComplete)
90           options.onComplete = options.overrideOnComplete;
91         else
92           options.onComplete = this._onRequestComplete.bind(this);
93         options._RicoOptionsProcessed = true;
94       }
95
96      // Set the default options and extend with any user options
97      this.options = {
98                      requestHeaders: requestHeaders,
99                      parameters:     options.parameters,
100                      postBody:       xmlDoc,
101                      method:         sendMethod,
102                      onComplete:     options.onComplete
103                     };
104      // Set any user options:
105      Object.extend(this.options, options);
106      return this.options;
107    },
108
109    _createQueryString: function( theArgs, offset ) {
110       var queryString = ""
111       for ( var i = offset ; i < theArgs.length ; i++ ) {
112           if ( i != offset )
113             queryString += "&";
114
115           var anArg = theArgs[i];
116
117           if ( anArg.name != undefined && anArg.value != undefined ) {
118             queryString += anArg.name +  "=" + escape(anArg.value);
119           }
120           else {
121              var ePos  = anArg.indexOf('=');
122              var argName  = anArg.substring( 0, ePos );
123              var argValue = anArg.substring( ePos + 1 );
124              queryString += argName + "=" + escape(argValue);
125           }
126       }
127       return queryString;
128    },
129
130    _onRequestComplete : function(request) {
131       if(!request)
132           return;
133       // User can set an onFailure option - which will be called by prototype
134       if (request.status != 200)
135         return;
136
137       var response = request.responseXML.getElementsByTagName("ajax-response");
138       if (response == null || response.length != 1)
139          return;
140       this._processAjaxResponse( response[0].childNodes );
141       
142       // Check if user has set a onComplete function
143       var onRicoComplete = this.options.onRicoComplete;
144       if (onRicoComplete != null)
145           onRicoComplete();
146    },
147
148    _processAjaxResponse: function( xmlResponseElements ) {
149       for ( var i = 0 ; i < xmlResponseElements.length ; i++ ) {
150          var responseElement = xmlResponseElements[i];
151
152          // only process nodes of type element.....
153          if ( responseElement.nodeType != 1 )
154             continue;
155
156          var responseType = responseElement.getAttribute("type");
157          var responseId   = responseElement.getAttribute("id");
158
159          if ( responseType == "object" )
160             this._processAjaxObjectUpdate( this.ajaxObjects[ responseId ], responseElement );
161          else if ( responseType == "element" )
162             this._processAjaxElementUpdate( this.ajaxElements[ responseId ], responseElement );
163          else
164             alert('unrecognized AjaxResponse type : ' + responseType );
165       }
166    },
167
168    _processAjaxObjectUpdate: function( ajaxObject, responseElement ) {
169       ajaxObject.ajaxUpdate( responseElement );
170    },
171
172    _processAjaxElementUpdate: function( ajaxElement, responseElement ) {
173       ajaxElement.innerHTML = RicoUtil.getContentAsString(responseElement);
174    }
175
176 }
177
178 Rico.includeLoaded('ricoAjaxEngine.js');