Web UI for ARGUS server
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.

219 lines
10 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js">/*!
  10. * Ext JS Library 3.3.1
  11. * Copyright(c) 2006-2010 Sencha Inc.
  12. * licensing@sencha.com
  13. * http://www.sencha.com/license
  14. */
  15. <div id="cls-Ext.data.DataWriter"></div>/**
  16. * @class Ext.data.DataWriter
  17. * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
  18. * an Ext.data.Store and a server-side framework. A Writer enabled Store will
  19. * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
  20. * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
  21. * and should not be created directly. For existing implementations, see
  22. * {@link Ext.data.JsonWriter}.</p>
  23. * <p>Creating a writer is simple:</p>
  24. * <pre><code>
  25. var writer = new Ext.data.JsonWriter({
  26. encode: false // &lt;--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust
  27. });
  28. * </code></pre>
  29. * * <p>Same old JsonReader as Ext-2.x:</p>
  30. * <pre><code>
  31. var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]);
  32. * </code></pre>
  33. *
  34. * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
  35. * <pre><code>
  36. // Create a standard HttpProxy instance.
  37. var proxy = new Ext.data.HttpProxy({
  38. url: 'app.php/users' // &lt;--- Supports "provides"-type urls, such as '/users.json', '/products.xml' (Hello Rails/Merb)
  39. });
  40. * </code></pre>
  41. * <p>For finer grained control, the proxy may also be configured with an <code>API</code>:</p>
  42. * <pre><code>
  43. // Maximum flexibility with the API-configuration
  44. var proxy = new Ext.data.HttpProxy({
  45. api: {
  46. read : 'app.php/users/read',
  47. create : 'app.php/users/create',
  48. update : 'app.php/users/update',
  49. destroy : { // &lt;--- Supports object-syntax as well
  50. url: 'app.php/users/destroy',
  51. method: "DELETE"
  52. }
  53. }
  54. });
  55. * </code></pre>
  56. * <p>Pulling it all together into a Writer-enabled Store:</p>
  57. * <pre><code>
  58. var store = new Ext.data.Store({
  59. proxy: proxy,
  60. reader: reader,
  61. writer: writer,
  62. autoLoad: true,
  63. autoSave: true // -- Cell-level updates.
  64. });
  65. * </code></pre>
  66. * <p>Initiating write-actions <b>automatically</b>, using the existing Ext2.0 Store/Record API:</p>
  67. * <pre><code>
  68. var rec = store.getAt(0);
  69. rec.set('email', 'foo@bar.com'); // &lt;--- Immediately initiates an UPDATE action through configured proxy.
  70. store.remove(rec); // &lt;---- Immediately initiates a DESTROY action through configured proxy.
  71. * </code></pre>
  72. * <p>For <b>record/batch</b> updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}</p>
  73. * <pre><code>
  74. var store = new Ext.data.Store({
  75. proxy: proxy,
  76. reader: reader,
  77. writer: writer,
  78. autoLoad: true,
  79. autoSave: false // -- disable cell-updates
  80. });
  81. var urec = store.getAt(0);
  82. urec.set('email', 'foo@bar.com');
  83. var drec = store.getAt(1);
  84. store.remove(drec);
  85. // Push the button!
  86. store.save();
  87. * </code></pre>
  88. * @constructor Create a new DataWriter
  89. * @param {Object} meta Metadata configuration options (implementation-specific)
  90. * @param {Object} recordType Either an Array of field definition objects as specified
  91. * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
  92. * using {@link Ext.data.Record#create}.
  93. */
  94. Ext.data.DataWriter = function(config){
  95. Ext.apply(this, config);
  96. };
  97. Ext.data.DataWriter.prototype = {
  98. <div id="cfg-Ext.data.DataWriter-writeAllFields"></div>/**
  99. * @cfg {Boolean} writeAllFields
  100. * <tt>false</tt> by default. Set <tt>true</tt> to have DataWriter return ALL fields of a modified
  101. * record -- not just those that changed.
  102. * <tt>false</tt> to have DataWriter only request modified fields from a record.
  103. */
  104. writeAllFields : false,
  105. <div id="cfg-Ext.data.DataWriter-listful"></div>/**
  106. * @cfg {Boolean} listful
  107. * <tt>false</tt> by default. Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
  108. * even when acting upon a single record.
  109. */
  110. listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
  111. <div id="method-Ext.data.DataWriter-apply"></div>/**
  112. * Compiles a Store recordset into a data-format defined by an extension such as {@link Ext.data.JsonWriter} or {@link Ext.data.XmlWriter} in preparation for a {@link Ext.data.Api#actions server-write action}. The first two params are similar similar in nature to {@link Ext#apply},
  113. * Where the first parameter is the <i>receiver</i> of paramaters and the second, baseParams, <i>the source</i>.
  114. * @param {Object} params The request-params receiver.
  115. * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
  116. * @param {String} action [{@link Ext.data.Api#actions create|update|destroy}]
  117. * @param {Record/Record[]} rs The recordset to write, the subject(s) of the write action.
  118. */
  119. apply : function(params, baseParams, action, rs) {
  120. var data = [],
  121. renderer = action + 'Record';
  122. // TODO implement @cfg listful here
  123. if (Ext.isArray(rs)) {
  124. Ext.each(rs, function(rec){
  125. data.push(this[renderer](rec));
  126. }, this);
  127. }
  128. else if (rs instanceof Ext.data.Record) {
  129. data = this[renderer](rs);
  130. }
  131. this.render(params, baseParams, data);
  132. },
  133. <div id="method-Ext.data.DataWriter-render"></div>/**
  134. * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
  135. * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
  136. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  137. * @param {Record[]} rs Store recordset
  138. * @param {Object} params Http params to be sent to server.
  139. * @param {Object} data object populated according to DataReader meta-data.
  140. */
  141. render : Ext.emptyFn,
  142. <div id="cfg-Ext.data.DataWriter-updateRecord"></div>/**
  143. * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
  144. * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
  145. */
  146. updateRecord : Ext.emptyFn,
  147. <div id="cfg-Ext.data.DataWriter-createRecord"></div>/**
  148. * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
  149. * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
  150. */
  151. createRecord : Ext.emptyFn,
  152. <div id="cfg-Ext.data.DataWriter-destroyRecord"></div>/**
  153. * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
  154. * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
  155. */
  156. destroyRecord : Ext.emptyFn,
  157. <div id="method-Ext.data.DataWriter-toHash"></div>/**
  158. * Converts a Record to a hash, taking into account the state of the Ext.data.Record along with configuration properties
  159. * related to its rendering, such as {@link #writeAllFields}, {@link Ext.data.Record#phantom phantom}, {@link Ext.data.Record#getChanges getChanges} and
  160. * {@link Ext.data.DataReader#idProperty idProperty}
  161. * @param {Ext.data.Record} rec The Record from which to create a hash.
  162. * @param {Object} config <b>NOT YET IMPLEMENTED</b>. Will implement an exlude/only configuration for fine-control over which fields do/don't get rendered.
  163. * @return {Object}
  164. * @protected
  165. * TODO Implement excludes/only configuration with 2nd param?
  166. */
  167. toHash : function(rec, config) {
  168. var map = rec.fields.map,
  169. data = {},
  170. raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
  171. m;
  172. Ext.iterate(raw, function(prop, value){
  173. if((m = map[prop])){
  174. data[m.mapping ? m.mapping : m.name] = value;
  175. }
  176. });
  177. // we don't want to write Ext auto-generated id to hash. Careful not to remove it on Models not having auto-increment pk though.
  178. // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
  179. // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
  180. if (rec.phantom) {
  181. if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
  182. delete data[this.meta.idProperty];
  183. }
  184. } else {
  185. data[this.meta.idProperty] = rec.id;
  186. }
  187. return data;
  188. },
  189. <div id="method-Ext.data.DataWriter-toArray"></div>/**
  190. * Converts a {@link Ext.data.DataWriter#toHash Hashed} {@link Ext.data.Record} to fields-array array suitable
  191. * for encoding to xml via XTemplate, eg:
  192. <code><pre>&lt;tpl for=".">&lt;{name}>{value}&lt;/{name}&lt;/tpl></pre></code>
  193. * eg, <b>non-phantom</b>:
  194. <code><pre>{id: 1, first: 'foo', last: 'bar'} --> [{name: 'id', value: 1}, {name: 'first', value: 'foo'}, {name: 'last', value: 'bar'}]</pre></code>
  195. * {@link Ext.data.Record#phantom Phantom} records will have had their idProperty omitted in {@link #toHash} if determined to be auto-generated.
  196. * Non AUTOINCREMENT pks should have been protected.
  197. * @param {Hash} data Hashed by Ext.data.DataWriter#toHash
  198. * @return {[Object]} Array of attribute-objects.
  199. * @protected
  200. */
  201. toArray : function(data) {
  202. var fields = [];
  203. Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this);
  204. return fields;
  205. }
  206. };</pre>
  207. </body>
  208. </html>