Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / pkgs / direct-debug.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.data.DirectProxy\r
9  * @extends Ext.data.DataProxy\r
10  */\r
11 Ext.data.DirectProxy = function(config){\r
12     Ext.apply(this, config);\r
13     if(typeof this.paramOrder == 'string'){\r
14         this.paramOrder = this.paramOrder.split(/[\s,|]/);\r
15     }\r
16     Ext.data.DirectProxy.superclass.constructor.call(this, config);\r
17 };\r
18 \r
19 Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {\r
20     /**\r
21      * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed\r
22      * server side.  Specify the params in the order in which they must be executed on the server-side\r
23      * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,\r
24      * comma, or pipe. For example,\r
25      * any of the following would be acceptable:<pre><code>\r
26 paramOrder: ['param1','param2','param3']\r
27 paramOrder: 'param1 param2 param3'\r
28 paramOrder: 'param1,param2,param3'\r
29 paramOrder: 'param1|param2|param'\r
30      </code></pre>\r
31      */\r
32     paramOrder: undefined,\r
33 \r
34     /**\r
35      * @cfg {Boolean} paramsAsHash\r
36      * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a\r
37      * <tt>{@link #paramOrder}</tt> nullifies this configuration.\r
38      */\r
39     paramsAsHash: true,\r
40 \r
41     /**\r
42      * @cfg {Function} directFn\r
43      * Function to call when executing a request.  directFn is a simple alternative to defining the api configuration-parameter\r
44      * for Store's which will not implement a full CRUD api.\r
45      */\r
46     directFn : undefined,\r
47 \r
48     // protected\r
49     doRequest : function(action, rs, params, reader, callback, scope, options) {\r
50         var args = [];\r
51         var directFn = this.api[action] || this.directFn;\r
52 \r
53         switch (action) {\r
54             case Ext.data.Api.actions.create:\r
55                 args.push(params.jsonData[reader.meta.root]);           // <-- create(Hash)\r
56                 break;\r
57             case Ext.data.Api.actions.read:\r
58                 // If the method has no parameters, ignore the paramOrder/paramsAsHash.\r
59                 if(directFn.directCfg.method.len > 0){\r
60                     if(this.paramOrder){\r
61                         for(var i = 0, len = this.paramOrder.length; i < len; i++){\r
62                             args.push(params[this.paramOrder[i]]);\r
63                         }\r
64                     }else if(this.paramsAsHash){\r
65                         args.push(params);\r
66                     }\r
67                 }\r
68                 break;\r
69             case Ext.data.Api.actions.update:\r
70                 args.push(params.jsonData[reader.meta.root]);        // <-- update(Hash/Hash[])\r
71                 break;\r
72             case Ext.data.Api.actions.destroy:\r
73                 args.push(params.jsonData[reader.meta.root]);        // <-- destroy(Int/Int[])\r
74                 break;\r
75         }\r
76 \r
77         var trans = {\r
78             params : params || {},\r
79             request: {\r
80                 callback : callback,\r
81                 scope : scope,\r
82                 arg : options\r
83             },\r
84             reader: reader\r
85         };\r
86 \r
87         args.push(this.createCallback(action, rs, trans), this);\r
88         directFn.apply(window, args);\r
89     },\r
90 \r
91     // private\r
92     createCallback : function(action, rs, trans) {\r
93         return function(result, res) {\r
94             if (!res.status) {\r
95                 // @deprecated fire loadexception\r
96                 if (action === Ext.data.Api.actions.read) {\r
97                     this.fireEvent("loadexception", this, trans, res, null);\r
98                 }\r
99                 this.fireEvent('exception', this, 'remote', action, trans, res, null);\r
100                 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
101                 return;\r
102             }\r
103             if (action === Ext.data.Api.actions.read) {\r
104                 this.onRead(action, trans, result, res);\r
105             } else {\r
106                 this.onWrite(action, trans, result, res, rs);\r
107             }\r
108         };\r
109     },\r
110     /**\r
111      * Callback for read actions\r
112      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
113      * @param {Object} trans The request transaction object\r
114      * @param {Object} res The server response\r
115      * @private\r
116      */\r
117     onRead : function(action, trans, result, res) {\r
118         var records;\r
119         try {\r
120             records = trans.reader.readRecords(result);\r
121         }\r
122         catch (ex) {\r
123             // @deprecated: Fire old loadexception for backwards-compat.\r
124             this.fireEvent("loadexception", this, trans, res, ex);\r
125 \r
126             this.fireEvent('exception', this, 'response', action, trans, res, ex);\r
127             trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
128             return;\r
129         }\r
130         this.fireEvent("load", this, res, trans.request.arg);\r
131         trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);\r
132     },\r
133     /**\r
134      * Callback for write actions\r
135      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
136      * @param {Object} trans The request transaction object\r
137      * @param {Object} res The server response\r
138      * @private\r
139      */\r
140     onWrite : function(action, trans, result, res, rs) {\r
141         var data = trans.reader.extractData(result);\r
142         this.fireEvent("write", this, action, data, res, rs, trans.request.arg);\r
143         trans.request.callback.call(trans.request.scope, data, res, true);\r
144     }\r
145 });\r
146 \r
147 /**\r
148  * @class Ext.data.DirectStore\r
149  * @extends Ext.data.Store\r
150  * <p>Small helper class to create an {@link Ext.data.Store} configured with an\r
151  * {@link Ext.data.DirectProxy} and {@link Ext.data.JsonReader} to make interacting\r
152  * with an {@link Ext.Direct} Server-side {@link Ext.direct.Provider Provider} easier.\r
153  * To create a different proxy/reader combination create a basic {@link Ext.data.Store}\r
154  * configured as needed.</p>\r
155  *\r
156  * <p><b>*Note:</b> Although they are not listed, this class inherits all of the config options of:</p>\r
157  * <div><ul class="mdetail-params">\r
158  * <li><b>{@link Ext.data.Store Store}</b></li>\r
159  * <div class="sub-desc"><ul class="mdetail-params">\r
160  *\r
161  * </ul></div>\r
162  * <li><b>{@link Ext.data.JsonReader JsonReader}</b></li>\r
163  * <div class="sub-desc"><ul class="mdetail-params">\r
164  * <li><tt><b>{@link Ext.data.JsonReader#root root}</b></tt></li>\r
165  * <li><tt><b>{@link Ext.data.JsonReader#idProperty idProperty}</b></tt></li>\r
166  * <li><tt><b>{@link Ext.data.JsonReader#totalProperty totalProperty}</b></tt></li>\r
167  * </ul></div>\r
168  *\r
169  * <li><b>{@link Ext.data.DirectProxy DirectProxy}</b></li>\r
170  * <div class="sub-desc"><ul class="mdetail-params">\r
171  * <li><tt><b>{@link Ext.data.DirectProxy#directFn directFn}</b></tt></li>\r
172  * <li><tt><b>{@link Ext.data.DirectProxy#paramOrder paramOrder}</b></tt></li>\r
173  * <li><tt><b>{@link Ext.data.DirectProxy#paramsAsHash paramsAsHash}</b></tt></li>\r
174  * </ul></div>\r
175  * </ul></div>\r
176  *\r
177  * @xtype directstore\r
178  *\r
179  * @constructor\r
180  * @param {Object} config\r
181  */\r
182 Ext.data.DirectStore = function(c){\r
183     // each transaction upon a singe record will generatie a distinct Direct transaction since Direct queues them into one Ajax request.\r
184     c.batchTransactions = false;\r
185 \r
186     Ext.data.DirectStore.superclass.constructor.call(this, Ext.apply(c, {\r
187         proxy: (typeof(c.proxy) == 'undefined') ? new Ext.data.DirectProxy(Ext.copyTo({}, c, 'paramOrder,paramsAsHash,directFn,api')) : c.proxy,\r
188         reader: (typeof(c.reader) == 'undefined' && typeof(c.fields) == 'object') ? new Ext.data.JsonReader(Ext.copyTo({}, c, 'totalProperty,root,idProperty'), c.fields) : c.reader\r
189     }));\r
190 };\r
191 Ext.extend(Ext.data.DirectStore, Ext.data.Store, {});\r
192 Ext.reg('directstore', Ext.data.DirectStore);
193 /**\r
194  * @class Ext.Direct\r
195  * @extends Ext.util.Observable\r
196  * <p><b><u>Overview</u></b></p>\r
197  * \r
198  * <p>Ext.Direct aims to streamline communication between the client and server\r
199  * by providing a single interface that reduces the amount of common code\r
200  * typically required to validate data and handle returned data packets\r
201  * (reading data, error conditions, etc).</p>\r
202  *  \r
203  * <p>The Ext.direct namespace includes several classes for a closer integration\r
204  * with the server-side. The Ext.data namespace also includes classes for working\r
205  * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>\r
206  * \r
207  * <p><b><u>Specification</u></b></p>\r
208  * \r
209  * <p>For additional information consult the \r
210  * <a href="http://extjs.com/products/extjs/direct.php">Ext.Direct Specification</a>.</p>\r
211  *   \r
212  * <p><b><u>Providers</u></b></p>\r
213  * \r
214  * <p>Ext.Direct uses a provider architecture, where one or more providers are\r
215  * used to transport data to and from the server. There are several providers\r
216  * that exist in the core at the moment:</p><div class="mdetail-params"><ul>\r
217  * \r
218  * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>\r
219  * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>\r
220  * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side\r
221  * on the client.</li>\r
222  * </ul></div>\r
223  * \r
224  * <p>A provider does not need to be invoked directly, providers are added via\r
225  * {@link Ext.Direct}.{@link Ext.Direct#add add}.</p>\r
226  * \r
227  * <p><b><u>Router</u></b></p>\r
228  * \r
229  * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client\r
230  * to the appropriate server-side method. Because the Ext.Direct API is completely\r
231  * platform-agnostic, you could completely swap out a Java based server solution\r
232  * and replace it with one that uses C# without changing the client side JavaScript\r
233  * at all.</p>\r
234  * \r
235  * <p><b><u>Server side events</u></b></p>\r
236  * \r
237  * <p>Custom events from the server may be handled by the client by adding\r
238  * listeners, for example:</p>\r
239  * <pre><code>\r
240 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}\r
241 \r
242 // add a handler for a 'message' event sent by the server \r
243 Ext.Direct.on('message', function(e){\r
244     out.append(String.format('&lt;p>&lt;i>{0}&lt;/i>&lt;/p>', e.data));\r
245             out.el.scrollTo('t', 100000, true);\r
246 });\r
247  * </code></pre>\r
248  * @singleton\r
249  */\r
250 Ext.Direct = Ext.extend(Ext.util.Observable, {\r
251     /**\r
252      * Each event type implements a getData() method. The default event types are:\r
253      * <div class="mdetail-params"><ul>\r
254      * <li><b><tt>event</tt></b> : Ext.Direct.Event</li>\r
255      * <li><b><tt>exception</tt></b> : Ext.Direct.ExceptionEvent</li>\r
256      * <li><b><tt>rpc</tt></b> : Ext.Direct.RemotingEvent</li>\r
257      * </ul></div>\r
258      * @property eventTypes\r
259      * @type Object\r
260      */\r
261 \r
262     /**\r
263      * Four types of possible exceptions which can occur:\r
264      * <div class="mdetail-params"><ul>\r
265      * <li><b><tt>Ext.Direct.exceptions.TRANSPORT</tt></b> : 'xhr'</li>\r
266      * <li><b><tt>Ext.Direct.exceptions.PARSE</tt></b> : 'parse'</li>\r
267      * <li><b><tt>Ext.Direct.exceptions.LOGIN</tt></b> : 'login'</li>\r
268      * <li><b><tt>Ext.Direct.exceptions.SERVER</tt></b> : 'exception'</li>\r
269      * </ul></div>\r
270      * @property exceptions\r
271      * @type Object\r
272      */\r
273     exceptions: {\r
274         TRANSPORT: 'xhr',\r
275         PARSE: 'parse',\r
276         LOGIN: 'login',\r
277         SERVER: 'exception'\r
278     },\r
279     \r
280     // private\r
281     constructor: function(){\r
282         this.addEvents(\r
283             /**\r
284              * @event event\r
285              * Fires after an event.\r
286              * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.\r
287              * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.\r
288              */\r
289             'event',\r
290             /**\r
291              * @event exception\r
292              * Fires after an event exception.\r
293              * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.\r
294              */\r
295             'exception'\r
296         );\r
297         this.transactions = {};\r
298         this.providers = {};\r
299     },\r
300 \r
301     /**\r
302      * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.\r
303      * If the provider is not already connected, it will auto-connect.\r
304      * <pre><code>\r
305 var pollProv = new Ext.direct.PollingProvider({\r
306     url: 'php/poll2.php'\r
307 }); \r
308 \r
309 Ext.Direct.addProvider(\r
310     {\r
311         "type":"remoting",       // create a {@link Ext.direct.RemotingProvider} \r
312         "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.\r
313         "actions":{              // each property within the actions object represents a Class \r
314             "TestAction":[       // array of methods within each server side Class   \r
315             {\r
316                 "name":"doEcho", // name of method\r
317                 "len":1\r
318             },{\r
319                 "name":"multiply",\r
320                 "len":1\r
321             },{\r
322                 "name":"doForm",\r
323                 "formHandler":true, // handle form on server with Ext.Direct.Transaction \r
324                 "len":1\r
325             }]\r
326         },\r
327         "namespace":"myApplication",// namespace to create the Remoting Provider in\r
328     },{\r
329         type: 'polling', // create a {@link Ext.direct.PollingProvider} \r
330         url:  'php/poll.php'\r
331     },\r
332     pollProv // reference to previously created instance\r
333 );\r
334      * </code></pre>\r
335      * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance\r
336      * or config object for a Provider) or any number of Provider descriptions as arguments.  Each\r
337      * Provider description instructs Ext.Direct how to create client-side stub methods.\r
338      */\r
339     addProvider : function(provider){        \r
340         var a = arguments;\r
341         if(a.length > 1){\r
342             for(var i = 0, len = a.length; i < len; i++){\r
343                 this.addProvider(a[i]);\r
344             }\r
345             return;\r
346         }\r
347         \r
348         // if provider has not already been instantiated\r
349         if(!provider.events){\r
350             provider = new Ext.Direct.PROVIDERS[provider.type](provider);\r
351         }\r
352         provider.id = provider.id || Ext.id();\r
353         this.providers[provider.id] = provider;\r
354 \r
355         provider.on('data', this.onProviderData, this);\r
356         provider.on('exception', this.onProviderException, this);\r
357 \r
358 \r
359         if(!provider.isConnected()){\r
360             provider.connect();\r
361         }\r
362 \r
363         return provider;\r
364     },\r
365 \r
366     /**\r
367      * Retrieve a {@link Ext.direct.Provider provider} by the\r
368      * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is\r
369      * {@link #addProvider added}.\r
370      * @param {String} id Unique identifier assigned to the provider when calling {@link #addProvider} \r
371      */\r
372     getProvider : function(id){\r
373         return this.providers[id];\r
374     },\r
375 \r
376     removeProvider : function(id){\r
377         var provider = id.id ? id : this.providers[id.id];\r
378         provider.un('data', this.onProviderData, this);\r
379         provider.un('exception', this.onProviderException, this);\r
380         delete this.providers[provider.id];\r
381         return provider;\r
382     },\r
383 \r
384     addTransaction: function(t){\r
385         this.transactions[t.tid] = t;\r
386         return t;\r
387     },\r
388 \r
389     removeTransaction: function(t){\r
390         delete this.transactions[t.tid || t];\r
391         return t;\r
392     },\r
393 \r
394     getTransaction: function(tid){\r
395         return this.transactions[tid.tid || tid];\r
396     },\r
397 \r
398     onProviderData : function(provider, e){\r
399         if(Ext.isArray(e)){\r
400             for(var i = 0, len = e.length; i < len; i++){\r
401                 this.onProviderData(provider, e[i]);\r
402             }\r
403             return;\r
404         }\r
405         if(e.name && e.name != 'event' && e.name != 'exception'){\r
406             this.fireEvent(e.name, e);\r
407         }else if(e.type == 'exception'){\r
408             this.fireEvent('exception', e);\r
409         }\r
410         this.fireEvent('event', e, provider);\r
411     },\r
412 \r
413     createEvent : function(response, extraProps){\r
414         return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));\r
415     }\r
416 });\r
417 // overwrite impl. with static instance\r
418 Ext.Direct = new Ext.Direct();\r
419 \r
420 Ext.Direct.TID = 1;\r
421 Ext.Direct.PROVIDERS = {};/**\r
422  * @class Ext.Direct.Transaction\r
423  * @extends Object\r
424  * <p>Supporting Class for Ext.Direct (not intended to be used directly).</p>\r
425  * @constructor\r
426  * @param {Object} config\r
427  */\r
428 Ext.Direct.Transaction = function(config){\r
429     Ext.apply(this, config);\r
430     this.tid = ++Ext.Direct.TID;\r
431     this.retryCount = 0;\r
432 };\r
433 Ext.Direct.Transaction.prototype = {\r
434     send: function(){\r
435         this.provider.queueTransaction(this);\r
436     },\r
437 \r
438     retry: function(){\r
439         this.retryCount++;\r
440         this.send();\r
441     },\r
442 \r
443     getProvider: function(){\r
444         return this.provider;\r
445     }\r
446 };Ext.Direct.Event = function(config){\r
447     Ext.apply(this, config);\r
448 }\r
449 Ext.Direct.Event.prototype = {\r
450     status: true,\r
451     getData: function(){\r
452         return this.data;\r
453     }\r
454 };\r
455 \r
456 Ext.Direct.RemotingEvent = Ext.extend(Ext.Direct.Event, {\r
457     type: 'rpc',\r
458     getTransaction: function(){\r
459         return this.transaction || Ext.Direct.getTransaction(this.tid);\r
460     }\r
461 });\r
462 \r
463 Ext.Direct.ExceptionEvent = Ext.extend(Ext.Direct.RemotingEvent, {\r
464     status: false,\r
465     type: 'exception'\r
466 });\r
467 \r
468 Ext.Direct.eventTypes = {\r
469     'rpc':  Ext.Direct.RemotingEvent,\r
470     'event':  Ext.Direct.Event,\r
471     'exception':  Ext.Direct.ExceptionEvent\r
472 };\r
473 \r
474 /**\r
475  * @class Ext.direct.Provider\r
476  * @extends Ext.util.Observable\r
477  * <p>Ext.direct.Provider is an abstract class meant to be extended.</p>\r
478  * \r
479  * <p>For example ExtJs implements the following subclasses:</p>\r
480  * <pre><code>\r
481 Provider\r
482 |\r
483 +---{@link Ext.direct.JsonProvider JsonProvider} \r
484     |\r
485     +---{@link Ext.direct.PollingProvider PollingProvider}   \r
486     |\r
487     +---{@link Ext.direct.RemotingProvider RemotingProvider}   \r
488  * </code></pre>\r
489  * @abstract\r
490  */\r
491 Ext.direct.Provider = Ext.extend(Ext.util.Observable, {    \r
492     /**\r
493      * @cfg {String} id\r
494      * The unique id of the provider (defaults to an {@link Ext#id auto-assigned id}).\r
495      * You should assign an id if you need to be able to access the provider later and you do\r
496      * not have an object reference available, for example:\r
497      * <pre><code>\r
498 Ext.Direct.addProvider(\r
499     {\r
500         type: 'polling',\r
501         url:  'php/poll.php',\r
502         id:   'poll-provider'\r
503     }\r
504 );\r
505      \r
506 var p = {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#getProvider getProvider}('poll-provider');\r
507 p.disconnect();\r
508      * </code></pre>\r
509      */\r
510         \r
511     /**\r
512      * @cfg {Number} priority\r
513      * Priority of the request. Lower is higher priority, <tt>0</tt> means "duplex" (always on).\r
514      * All Providers default to <tt>1</tt> except for PollingProvider which defaults to <tt>3</tt>.\r
515      */    \r
516     priority: 1,\r
517 \r
518     /**\r
519      * @cfg {String} type\r
520      * <b>Required</b>, <tt>undefined</tt> by default.  The <tt>type</tt> of provider specified\r
521      * to {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#addProvider addProvider} to create a\r
522      * new Provider. Acceptable values by default are:<div class="mdetail-params"><ul>\r
523      * <li><b><tt>polling</tt></b> : {@link Ext.direct.PollingProvider PollingProvider}</li>\r
524      * <li><b><tt>remoting</tt></b> : {@link Ext.direct.RemotingProvider RemotingProvider}</li>\r
525      * </ul></div>\r
526      */    \r
527  \r
528     // private\r
529     constructor : function(config){\r
530         Ext.apply(this, config);\r
531         this.addEvents(\r
532             /**\r
533              * @event connect\r
534              * Fires when the Provider connects to the server-side\r
535              * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.\r
536              */            \r
537             'connect',\r
538             /**\r
539              * @event disconnect\r
540              * Fires when the Provider disconnects from the server-side\r
541              * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.\r
542              */            \r
543             'disconnect',\r
544             /**\r
545              * @event data\r
546              * Fires when the Provider receives data from the server-side\r
547              * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.\r
548              * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.\r
549              */            \r
550             'data',\r
551             /**\r
552              * @event exception\r
553              * Fires when the Provider receives an exception from the server-side\r
554              */                        \r
555             'exception'\r
556         );\r
557         Ext.direct.Provider.superclass.constructor.call(this, config);\r
558     },\r
559 \r
560     /**\r
561      * Returns whether or not the server-side is currently connected.\r
562      * Abstract method for subclasses to implement.\r
563      */\r
564     isConnected: function(){\r
565         return false;\r
566     },\r
567 \r
568     /**\r
569      * Abstract methods for subclasses to implement.\r
570      */\r
571     connect: Ext.emptyFn,\r
572     \r
573     /**\r
574      * Abstract methods for subclasses to implement.\r
575      */\r
576     disconnect: Ext.emptyFn\r
577 });\r
578 /**\r
579  * @class Ext.direct.JsonProvider\r
580  * @extends Ext.direct.Provider\r
581  */\r
582 Ext.direct.JsonProvider = Ext.extend(Ext.direct.Provider, {\r
583     parseResponse: function(xhr){\r
584         if(!Ext.isEmpty(xhr.responseText)){\r
585             if(typeof xhr.responseText == 'object'){\r
586                 return xhr.responseText;\r
587             }\r
588             return Ext.decode(xhr.responseText);\r
589         }\r
590         return null;\r
591     },\r
592 \r
593     getEvents: function(xhr){\r
594         var data = null;\r
595         try{\r
596             data = this.parseResponse(xhr);\r
597         }catch(e){\r
598             var event = new Ext.Direct.ExceptionEvent({\r
599                 data: e,\r
600                 xhr: xhr,\r
601                 code: Ext.Direct.exceptions.PARSE,\r
602                 message: 'Error parsing json response: \n\n ' + data\r
603             })\r
604             return [event];\r
605         }\r
606         var events = [];\r
607         if(Ext.isArray(data)){\r
608             for(var i = 0, len = data.length; i < len; i++){\r
609                 events.push(Ext.Direct.createEvent(data[i]));\r
610             }\r
611         }else{\r
612             events.push(Ext.Direct.createEvent(data));\r
613         }\r
614         return events;\r
615     }\r
616 });/**\r
617  * @class Ext.direct.PollingProvider\r
618  * @extends Ext.direct.JsonProvider\r
619  *\r
620  * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.\r
621  * The initial request for data originates from the client, and then is responded to by the\r
622  * server.</p>\r
623  * \r
624  * <p>All configurations for the PollingProvider should be generated by the server-side\r
625  * API portion of the Ext.Direct stack.</p>\r
626  *\r
627  * <p>An instance of PollingProvider may be created directly via the new keyword or by simply\r
628  * specifying <tt>type = 'polling'</tt>.  For example:</p>\r
629  * <pre><code>\r
630 var pollA = new Ext.direct.PollingProvider({\r
631     type:'polling',\r
632     url: 'php/pollA.php',\r
633 });\r
634 Ext.Direct.addProvider(pollA);\r
635 pollA.disconnect();\r
636 \r
637 Ext.Direct.addProvider(\r
638     {\r
639         type:'polling',\r
640         url: 'php/pollB.php',\r
641         id: 'pollB-provider'\r
642     }\r
643 );\r
644 var pollB = Ext.Direct.getProvider('pollB-provider');\r
645  * </code></pre>\r
646  */\r
647 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {\r
648     /**\r
649      * @cfg {Number} priority\r
650      * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.\r
651      */\r
652     // override default priority\r
653     priority: 3,\r
654     \r
655     /**\r
656      * @cfg {Number} interval\r
657      * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every\r
658      * 3 seconds).\r
659      */\r
660     interval: 3000,\r
661 \r
662     /**\r
663      * @cfg {Object} baseParams An object containing properties which are to be sent as parameters\r
664      * on every polling request\r
665      */\r
666     \r
667     /**\r
668      * @cfg {String/Function} url\r
669      * The url which the PollingProvider should contact with each request. This can also be\r
670      * an imported Ext.Direct method which will accept the baseParams as its only argument.\r
671      */\r
672 \r
673     // private\r
674     constructor : function(config){\r
675         Ext.direct.PollingProvider.superclass.constructor.call(this, config);\r
676         this.addEvents(\r
677             /**\r
678              * @event beforepoll\r
679              * Fired immediately before a poll takes place, an event handler can return false\r
680              * in order to cancel the poll.\r
681              * @param {Ext.direct.PollingProvider}\r
682              */\r
683             'beforepoll',            \r
684             /**\r
685              * @event poll\r
686              * This event has not yet been implemented.\r
687              * @param {Ext.direct.PollingProvider}\r
688              */\r
689             'poll'\r
690         );\r
691     },\r
692 \r
693     // inherited\r
694     isConnected: function(){\r
695         return !!this.pollTask;\r
696     },\r
697 \r
698     /**\r
699      * Connect to the server-side and begin the polling process. To handle each\r
700      * response subscribe to the data event.\r
701      */\r
702     connect: function(){\r
703         if(this.url && !this.pollTask){\r
704             this.pollTask = Ext.TaskMgr.start({\r
705                 run: function(){\r
706                     if(this.fireEvent('beforepoll', this) !== false){\r
707                         if(typeof this.url == 'function'){\r
708                             this.url(this.baseParams);\r
709                         }else{\r
710                             Ext.Ajax.request({\r
711                                 url: this.url,\r
712                                 callback: this.onData,\r
713                                 scope: this,\r
714                                 params: this.baseParams\r
715                             });\r
716                         }\r
717                     }\r
718                 },\r
719                 interval: this.interval,\r
720                 scope: this\r
721             });\r
722             this.fireEvent('connect', this);\r
723         }else if(!this.url){\r
724             throw 'Error initializing PollingProvider, no url configured.';\r
725         }\r
726     },\r
727 \r
728     /**\r
729      * Disconnect from the server-side and stop the polling process. The disconnect\r
730      * event will be fired on a successful disconnect.\r
731      */\r
732     disconnect: function(){\r
733         if(this.pollTask){\r
734             Ext.TaskMgr.stop(this.pollTask);\r
735             delete this.pollTask;\r
736             this.fireEvent('disconnect', this);\r
737         }\r
738     },\r
739 \r
740     // private\r
741     onData: function(opt, success, xhr){\r
742         if(success){\r
743             var events = this.getEvents(xhr);\r
744             for(var i = 0, len = events.length; i < len; i++){\r
745                 var e = events[i];\r
746                 this.fireEvent('data', this, e);\r
747             }\r
748         }else{\r
749             var e = new Ext.Direct.ExceptionEvent({\r
750                 data: e,\r
751                 code: Ext.Direct.exceptions.TRANSPORT,\r
752                 message: 'Unable to connect to the server.',\r
753                 xhr: xhr\r
754             });\r
755             this.fireEvent('data', this, e);\r
756         }\r
757     }\r
758 });\r
759 \r
760 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;/**\r
761  * @class Ext.direct.RemotingProvider\r
762  * @extends Ext.direct.JsonProvider\r
763  * \r
764  * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to\r
765  * server side methods on the client (a remote procedure call (RPC) type of\r
766  * connection where the client can initiate a procedure on the server).</p>\r
767  * \r
768  * <p>This allows for code to be organized in a fashion that is maintainable,\r
769  * while providing a clear path between client and server, something that is\r
770  * not always apparent when using URLs.</p>\r
771  * \r
772  * <p>To accomplish this the server-side needs to describe what classes and methods\r
773  * are available on the client-side. This configuration will typically be\r
774  * outputted by the server-side Ext.Direct stack when the API description is built.</p>\r
775  */\r
776 Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, {       \r
777     /**\r
778      * @cfg {Object} actions\r
779      * Object literal defining the server side actions and methods. For example, if\r
780      * the Provider is configured with:\r
781      * <pre><code>\r
782 "actions":{ // each property within the 'actions' object represents a server side Class \r
783     "TestAction":[ // array of methods within each server side Class to be   \r
784     {              // stubbed out on client\r
785         "name":"doEcho", \r
786         "len":1            \r
787     },{\r
788         "name":"multiply",// name of method\r
789         "len":2           // The number of parameters that will be used to create an\r
790                           // array of data to send to the server side function.\r
791                           // Ensure the server sends back a Number, not a String. \r
792     },{\r
793         "name":"doForm",\r
794         "formHandler":true, // direct the client to use specialized form handling method \r
795         "len":1\r
796     }]\r
797 }\r
798      * </code></pre>\r
799      * <p>Note that a Store is not required, a server method can be called at any time.\r
800      * In the following example a <b>client side</b> handler is used to call the\r
801      * server side method "multiply" in the server-side "TestAction" Class:</p>\r
802      * <pre><code>\r
803 TestAction.multiply(\r
804     2, 4, // pass two arguments to server, so specify len=2\r
805     // callback function after the server is called\r
806     // result: the result returned by the server\r
807     //      e: Ext.Direct.RemotingEvent object\r
808     function(result, e){\r
809         var t = e.getTransaction();\r
810         var action = t.action; // server side Class called\r
811         var method = t.method; // server side method called\r
812         if(e.status){\r
813             var answer = Ext.encode(result); // 8\r
814     \r
815         }else{\r
816             var msg = e.message; // failure message\r
817         }\r
818     }\r
819 );\r
820      * </code></pre>\r
821      * In the example above, the server side "multiply" function will be passed two\r
822      * arguments (2 and 4).  The "multiply" method should return the value 8 which will be\r
823      * available as the <tt>result</tt> in the example above. \r
824      */\r
825     \r
826     /**\r
827      * @cfg {String/Object} namespace\r
828      * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).\r
829      * Explicitly specify the namespace Object, or specify a String to have a\r
830      * {@link Ext#namespace namespace created} implicitly.\r
831      */\r
832     \r
833     /**\r
834      * @cfg {String} url\r
835      * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router. \r
836      */\r
837     \r
838     /**\r
839      * @cfg {String} enableUrlEncode\r
840      * Specify which param will hold the arguments for the method.\r
841      * Defaults to <tt>'data'</tt>.\r
842      */\r
843     \r
844     /**\r
845      * @cfg {Number/Boolean} enableBuffer\r
846      * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method\r
847      * calls. If a number is specified this is the amount of time in milliseconds\r
848      * to wait before sending a batched request (defaults to <tt>10</tt>).</p>\r
849      * <br><p>Calls which are received within the specified timeframe will be\r
850      * concatenated together and sent in a single request, optimizing the\r
851      * application by reducing the amount of round trips that have to be made\r
852      * to the server.</p>\r
853      */\r
854     enableBuffer: 10,\r
855     \r
856     /**\r
857      * @cfg {Number} maxRetries\r
858      * Number of times to re-attempt delivery on failure of a call. Defaults to <tt>1</tt>.\r
859      */\r
860     maxRetries: 1,\r
861     \r
862     /**\r
863      * @cfg {Number} timeout\r
864      * The timeout to use for each request. Defaults to <tt>undefined</tt>.\r
865      */\r
866     timeout: undefined,\r
867 \r
868     constructor : function(config){\r
869         Ext.direct.RemotingProvider.superclass.constructor.call(this, config);\r
870         this.addEvents(\r
871             /**\r
872              * @event beforecall\r
873              * Fires immediately before the client-side sends off the RPC call.\r
874              * By returning false from an event handler you can prevent the call from\r
875              * executing.\r
876              * @param {Ext.direct.RemotingProvider} provider\r
877              * @param {Ext.Direct.Transaction} transaction\r
878              */            \r
879             'beforecall',            \r
880             /**\r
881              * @event call\r
882              * Fires immediately after the request to the server-side is sent. This does\r
883              * NOT fire after the response has come back from the call.\r
884              * @param {Ext.direct.RemotingProvider} provider\r
885              * @param {Ext.Direct.Transaction} transaction\r
886              */            \r
887             'call'\r
888         );\r
889         this.namespace = (Ext.isString(this.namespace)) ? Ext.ns(this.namespace) : this.namespace || window;\r
890         this.transactions = {};\r
891         this.callBuffer = [];\r
892     },\r
893 \r
894     // private\r
895     initAPI : function(){\r
896         var o = this.actions;\r
897         for(var c in o){\r
898             var cls = this.namespace[c] || (this.namespace[c] = {}),\r
899                 ms = o[c];\r
900             for(var i = 0, len = ms.length; i < len; i++){\r
901                 var m = ms[i];\r
902                 cls[m.name] = this.createMethod(c, m);\r
903             }\r
904         }\r
905     },\r
906 \r
907     // inherited\r
908     isConnected: function(){\r
909         return !!this.connected;\r
910     },\r
911 \r
912     connect: function(){\r
913         if(this.url){\r
914             this.initAPI();\r
915             this.connected = true;\r
916             this.fireEvent('connect', this);\r
917         }else if(!this.url){\r
918             throw 'Error initializing RemotingProvider, no url configured.';\r
919         }\r
920     },\r
921 \r
922     disconnect: function(){\r
923         if(this.connected){\r
924             this.connected = false;\r
925             this.fireEvent('disconnect', this);\r
926         }\r
927     },\r
928 \r
929     onData: function(opt, success, xhr){\r
930         if(success){\r
931             var events = this.getEvents(xhr);\r
932             for(var i = 0, len = events.length; i < len; i++){\r
933                 var e = events[i],\r
934                     t = this.getTransaction(e);\r
935                 this.fireEvent('data', this, e);\r
936                 if(t){\r
937                     this.doCallback(t, e, true);\r
938                     Ext.Direct.removeTransaction(t);\r
939                 }\r
940             }\r
941         }else{\r
942             var ts = [].concat(opt.ts);\r
943             for(var i = 0, len = ts.length; i < len; i++){\r
944                 var t = this.getTransaction(ts[i]);\r
945                 if(t && t.retryCount < this.maxRetries){\r
946                     t.retry();\r
947                 }else{\r
948                     var e = new Ext.Direct.ExceptionEvent({\r
949                         data: e,\r
950                         transaction: t,\r
951                         code: Ext.Direct.exceptions.TRANSPORT,\r
952                         message: 'Unable to connect to the server.',\r
953                         xhr: xhr\r
954                     });\r
955                     this.fireEvent('data', this, e);\r
956                     if(t){\r
957                         this.doCallback(t, e, false);\r
958                         Ext.Direct.removeTransaction(t);\r
959                     }\r
960                 }\r
961             }\r
962         }\r
963     },\r
964 \r
965     getCallData: function(t){\r
966         return {\r
967             action: t.action,\r
968             method: t.method,\r
969             data: t.data,\r
970             type: 'rpc',\r
971             tid: t.tid\r
972         };\r
973     },\r
974 \r
975     doSend : function(data){\r
976         var o = {\r
977             url: this.url,\r
978             callback: this.onData,\r
979             scope: this,\r
980             ts: data,\r
981             timeout: this.timeout\r
982         }, callData;\r
983 \r
984         if(Ext.isArray(data)){\r
985             callData = [];\r
986             for(var i = 0, len = data.length; i < len; i++){\r
987                 callData.push(this.getCallData(data[i]));\r
988             }\r
989         }else{\r
990             callData = this.getCallData(data);\r
991         }\r
992 \r
993         if(this.enableUrlEncode){\r
994             var params = {};\r
995             params[Ext.isString(this.enableUrlEncode) ? this.enableUrlEncode : 'data'] = Ext.encode(callData);\r
996             o.params = params;\r
997         }else{\r
998             o.jsonData = callData;\r
999         }\r
1000         Ext.Ajax.request(o);\r
1001     },\r
1002 \r
1003     combineAndSend : function(){\r
1004         var len = this.callBuffer.length;\r
1005         if(len > 0){\r
1006             this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer);\r
1007             this.callBuffer = [];\r
1008         }\r
1009     },\r
1010 \r
1011     queueTransaction: function(t){\r
1012         if(t.form){\r
1013             this.processForm(t);\r
1014             return;\r
1015         }\r
1016         this.callBuffer.push(t);\r
1017         if(this.enableBuffer){\r
1018             if(!this.callTask){\r
1019                 this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this);\r
1020             }\r
1021             this.callTask.delay(Ext.isNumber(this.enableBuffer) ? this.enableBuffer : 10);\r
1022         }else{\r
1023             this.combineAndSend();\r
1024         }\r
1025     },\r
1026 \r
1027     doCall : function(c, m, args){\r
1028         var data = null, hs = args[m.len], scope = args[m.len+1];\r
1029 \r
1030         if(m.len !== 0){\r
1031             data = args.slice(0, m.len);\r
1032         }\r
1033 \r
1034         var t = new Ext.Direct.Transaction({\r
1035             provider: this,\r
1036             args: args,\r
1037             action: c,\r
1038             method: m.name,\r
1039             data: data,\r
1040             cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs\r
1041         });\r
1042 \r
1043         if(this.fireEvent('beforecall', this, t) !== false){\r
1044             Ext.Direct.addTransaction(t);\r
1045             this.queueTransaction(t);\r
1046             this.fireEvent('call', this, t);\r
1047         }\r
1048     },\r
1049 \r
1050     doForm : function(c, m, form, callback, scope){\r
1051         var t = new Ext.Direct.Transaction({\r
1052             provider: this,\r
1053             action: c,\r
1054             method: m.name,\r
1055             args:[form, callback, scope],\r
1056             cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback,\r
1057             isForm: true\r
1058         });\r
1059 \r
1060         if(this.fireEvent('beforecall', this, t) !== false){\r
1061             Ext.Direct.addTransaction(t);\r
1062             var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data',\r
1063                 params = {\r
1064                     extTID: t.tid,\r
1065                     extAction: c,\r
1066                     extMethod: m.name,\r
1067                     extType: 'rpc',\r
1068                     extUpload: String(isUpload)\r
1069                 };\r
1070             \r
1071             // change made from typeof callback check to callback.params\r
1072             // to support addl param passing in DirectSubmit EAC 6/2\r
1073             Ext.apply(t, {\r
1074                 form: Ext.getDom(form),\r
1075                 isUpload: isUpload,\r
1076                 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params\r
1077             });\r
1078             this.fireEvent('call', this, t);\r
1079             this.processForm(t);\r
1080         }\r
1081     },\r
1082     \r
1083     processForm: function(t){\r
1084         Ext.Ajax.request({\r
1085             url: this.url,\r
1086             params: t.params,\r
1087             callback: this.onData,\r
1088             scope: this,\r
1089             form: t.form,\r
1090             isUpload: t.isUpload,\r
1091             ts: t\r
1092         });\r
1093     },\r
1094 \r
1095     createMethod : function(c, m){\r
1096         var f;\r
1097         if(!m.formHandler){\r
1098             f = function(){\r
1099                 this.doCall(c, m, Array.prototype.slice.call(arguments, 0));\r
1100             }.createDelegate(this);\r
1101         }else{\r
1102             f = function(form, callback, scope){\r
1103                 this.doForm(c, m, form, callback, scope);\r
1104             }.createDelegate(this);\r
1105         }\r
1106         f.directCfg = {\r
1107             action: c,\r
1108             method: m\r
1109         };\r
1110         return f;\r
1111     },\r
1112 \r
1113     getTransaction: function(opt){\r
1114         return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null;\r
1115     },\r
1116 \r
1117     doCallback: function(t, e){\r
1118         var fn = e.status ? 'success' : 'failure';\r
1119         if(t && t.cb){\r
1120             var hs = t.cb,\r
1121                 result = Ext.isDefined(e.result) ? e.result : e.data;\r
1122             if(Ext.isFunction(hs)){\r
1123                 hs(result, e);\r
1124             } else{\r
1125                 Ext.callback(hs[fn], hs.scope, [result, e]);\r
1126                 Ext.callback(hs.callback, hs.scope, [result, e]);\r
1127             }\r
1128         }\r
1129     }\r
1130 });\r
1131 Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider;