Web Interface For Lite Argus Control modules
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.

260 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * @class Ext.data.HttpProxy
  9. * @extends Ext.data.DataProxy
  10. * <p>An implementation of {@link Ext.data.DataProxy} that processes data requests within the same
  11. * domain of the originating page.</p>
  12. * <p><b>Note</b>: this class cannot be used to retrieve data from a domain other
  13. * than the domain from which the running page was served. For cross-domain requests, use a
  14. * {@link Ext.data.ScriptTagProxy ScriptTagProxy}.</p>
  15. * <p>Be aware that to enable the browser to parse an XML document, the server must set
  16. * the Content-Type header in the HTTP response to "<tt>text/xml</tt>".</p>
  17. * @constructor
  18. * @param {Object} conn
  19. * An {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}.
  20. * <p>Note that if this HttpProxy is being used by a {@link Ext.data.Store Store}, then the
  21. * Store's call to {@link #load} will override any specified <tt>callback</tt> and <tt>params</tt>
  22. * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters,
  23. * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be
  24. * used to pass parameters known at instantiation time.</p>
  25. * <p>If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make
  26. * the request.</p>
  27. */
  28. Ext.data.HttpProxy = function(conn){
  29. Ext.data.HttpProxy.superclass.constructor.call(this, conn);
  30. /**
  31. * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy
  32. * uses to make requests to the server. Properties of this object may be changed dynamically to
  33. * change the way data is requested.
  34. * @property
  35. */
  36. this.conn = conn;
  37. // nullify the connection url. The url param has been copied to 'this' above. The connection
  38. // url will be set during each execution of doRequest when buildUrl is called. This makes it easier for users to override the
  39. // connection url during beforeaction events (ie: beforeload, beforewrite, etc).
  40. // Url is always re-defined during doRequest.
  41. this.conn.url = null;
  42. this.useAjax = !conn || !conn.events;
  43. // A hash containing active requests, keyed on action [Ext.data.Api.actions.create|read|update|destroy]
  44. var actions = Ext.data.Api.actions;
  45. this.activeRequest = {};
  46. for (var verb in actions) {
  47. this.activeRequest[actions[verb]] = undefined;
  48. }
  49. };
  50. Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, {
  51. /**
  52. * Return the {@link Ext.data.Connection} object being used by this Proxy.
  53. * @return {Connection} The Connection object. This object may be used to subscribe to events on
  54. * a finer-grained basis than the DataProxy events.
  55. */
  56. getConnection : function() {
  57. return this.useAjax ? Ext.Ajax : this.conn;
  58. },
  59. /**
  60. * Used for overriding the url used for a single request. Designed to be called during a beforeaction event. Calling setUrl
  61. * will override any urls set via the api configuration parameter. Set the optional parameter makePermanent to set the url for
  62. * all subsequent requests. If not set to makePermanent, the next request will use the same url or api configuration defined
  63. * in the initial proxy configuration.
  64. * @param {String} url
  65. * @param {Boolean} makePermanent (Optional) [false]
  66. *
  67. * (e.g.: beforeload, beforesave, etc).
  68. */
  69. setUrl : function(url, makePermanent) {
  70. this.conn.url = url;
  71. if (makePermanent === true) {
  72. this.url = url;
  73. this.api = null;
  74. Ext.data.Api.prepare(this);
  75. }
  76. },
  77. /**
  78. * HttpProxy implementation of DataProxy#doRequest
  79. * @param {String} action The crud action type (create, read, update, destroy)
  80. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
  81. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  82. * for the request to the remote server.
  83. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  84. * object into a block of Ext.data.Records.
  85. * @param {Function} callback
  86. * <div class="sub-desc"><p>A function to be called after the request.
  87. * The <tt>callback</tt> is passed the following arguments:<ul>
  88. * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>
  89. * <li><tt>options</tt>: Options object from the action request</li>
  90. * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>
  91. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  92. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  93. * @protected
  94. */
  95. doRequest : function(action, rs, params, reader, cb, scope, arg) {
  96. var o = {
  97. method: (this.api[action]) ? this.api[action]['method'] : undefined,
  98. request: {
  99. callback : cb,
  100. scope : scope,
  101. arg : arg
  102. },
  103. reader: reader,
  104. callback : this.createCallback(action, rs),
  105. scope: this
  106. };
  107. // If possible, transmit data using jsonData || xmlData on Ext.Ajax.request (An installed DataWriter would have written it there.).
  108. // Use std HTTP params otherwise.
  109. if (params.jsonData) {
  110. o.jsonData = params.jsonData;
  111. } else if (params.xmlData) {
  112. o.xmlData = params.xmlData;
  113. } else {
  114. o.params = params || {};
  115. }
  116. // Set the connection url. If this.conn.url is not null here,
  117. // the user must have overridden the url during a beforewrite/beforeload event-handler.
  118. // this.conn.url is nullified after each request.
  119. this.conn.url = this.buildUrl(action, rs);
  120. if(this.useAjax){
  121. Ext.applyIf(o, this.conn);
  122. // If a currently running request is found for this action, abort it.
  123. if (this.activeRequest[action]) {
  124. ////
  125. // Disabled aborting activeRequest while implementing REST. activeRequest[action] will have to become an array
  126. // TODO ideas anyone?
  127. //
  128. //Ext.Ajax.abort(this.activeRequest[action]);
  129. }
  130. this.activeRequest[action] = Ext.Ajax.request(o);
  131. }else{
  132. this.conn.request(o);
  133. }
  134. // request is sent, nullify the connection url in preparation for the next request
  135. this.conn.url = null;
  136. },
  137. /**
  138. * Returns a callback function for a request. Note a special case is made for the
  139. * read action vs all the others.
  140. * @param {String} action [create|update|delete|load]
  141. * @param {Ext.data.Record[]} rs The Store-recordset being acted upon
  142. * @private
  143. */
  144. createCallback : function(action, rs) {
  145. return function(o, success, response) {
  146. this.activeRequest[action] = undefined;
  147. if (!success) {
  148. if (action === Ext.data.Api.actions.read) {
  149. // @deprecated: fire loadexception for backwards compat.
  150. // TODO remove
  151. this.fireEvent('loadexception', this, o, response);
  152. }
  153. this.fireEvent('exception', this, 'response', action, o, response);
  154. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  155. return;
  156. }
  157. if (action === Ext.data.Api.actions.read) {
  158. this.onRead(action, o, response);
  159. } else {
  160. this.onWrite(action, o, response, rs);
  161. }
  162. };
  163. },
  164. /**
  165. * Callback for read action
  166. * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
  167. * @param {Object} o The request transaction object
  168. * @param {Object} res The server response
  169. * @fires loadexception (deprecated)
  170. * @fires exception
  171. * @fires load
  172. * @protected
  173. */
  174. onRead : function(action, o, response) {
  175. var result;
  176. try {
  177. result = o.reader.read(response);
  178. }catch(e){
  179. // @deprecated: fire old loadexception for backwards-compat.
  180. // TODO remove
  181. this.fireEvent('loadexception', this, o, response, e);
  182. this.fireEvent('exception', this, 'response', action, o, response, e);
  183. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  184. return;
  185. }
  186. if (result.success === false) {
  187. // @deprecated: fire old loadexception for backwards-compat.
  188. // TODO remove
  189. this.fireEvent('loadexception', this, o, response);
  190. // Get DataReader read-back a response-object to pass along to exception event
  191. var res = o.reader.readResponse(action, response);
  192. this.fireEvent('exception', this, 'remote', action, o, res, null);
  193. }
  194. else {
  195. this.fireEvent('load', this, o, o.request.arg);
  196. }
  197. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  198. // the calls to request.callback(...) in each will have to be made identical.
  199. // NOTE reader.readResponse does not currently return Ext.data.Response
  200. o.request.callback.call(o.request.scope, result, o.request.arg, result.success);
  201. },
  202. /**
  203. * Callback for write actions
  204. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  205. * @param {Object} trans The request transaction object
  206. * @param {Object} res The server response
  207. * @fires exception
  208. * @fires write
  209. * @protected
  210. */
  211. onWrite : function(action, o, response, rs) {
  212. var reader = o.reader;
  213. var res;
  214. try {
  215. res = reader.readResponse(action, response);
  216. } catch (e) {
  217. this.fireEvent('exception', this, 'response', action, o, response, e);
  218. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  219. return;
  220. }
  221. if (res.success === true) {
  222. this.fireEvent('write', this, action, res.data, res, rs, o.request.arg);
  223. } else {
  224. this.fireEvent('exception', this, 'remote', action, o, res, rs);
  225. }
  226. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  227. // the calls to request.callback(...) in each will have to be made similar.
  228. // NOTE reader.readResponse does not currently return Ext.data.Response
  229. o.request.callback.call(o.request.scope, res.data, res, res.success);
  230. },
  231. // inherit docs
  232. destroy: function(){
  233. if(!this.useAjax){
  234. this.conn.abort();
  235. }else if(this.activeRequest){
  236. var actions = Ext.data.Api.actions;
  237. for (var verb in actions) {
  238. if(this.activeRequest[actions[verb]]){
  239. Ext.Ajax.abort(this.activeRequest[actions[verb]]);
  240. }
  241. }
  242. }
  243. Ext.data.HttpProxy.superclass.destroy.call(this);
  244. }
  245. });