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.

1175 lines
40 KiB

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