Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1159 lines
40 KiB

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