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.

238 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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.DataReader"></div>/**
  16. * @class Ext.data.DataReader
  17. * Abstract base class for reading structured data from a data source and converting
  18. * it into an object containing {@link Ext.data.Record} objects and metadata for use
  19. * by an {@link Ext.data.Store}. This class is intended to be extended and should not
  20. * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
  21. * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
  22. * @constructor Create a new DataReader
  23. * @param {Object} meta Metadata configuration options (implementation-specific).
  24. * @param {Array/Object} recordType
  25. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  26. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  27. * constructor created using {@link Ext.data.Record#create}.</p>
  28. */
  29. Ext.data.DataReader = function(meta, recordType){
  30. <div id="prop-Ext.data.DataReader-meta"></div>/**
  31. * This DataReader's configured metadata as passed to the constructor.
  32. * @type Mixed
  33. * @property meta
  34. */
  35. this.meta = meta;
  36. <div id="cfg-Ext.data.DataReader-fields"></div>/**
  37. * @cfg {Array/Object} fields
  38. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  39. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  40. * constructor created from {@link Ext.data.Record#create}.</p>
  41. */
  42. this.recordType = Ext.isArray(recordType) ?
  43. Ext.data.Record.create(recordType) : recordType;
  44. // if recordType defined make sure extraction functions are defined
  45. if (this.recordType){
  46. this.buildExtractors();
  47. }
  48. };
  49. Ext.data.DataReader.prototype = {
  50. <div id="cfg-Ext.data.DataReader-messageProperty"></div>/**
  51. * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
  52. */
  53. <div id="prop-Ext.data.DataReader-getTotal"></div>/**
  54. * Abstract method created in extension's buildExtractors impl.
  55. */
  56. getTotal: Ext.emptyFn,
  57. <div id="prop-Ext.data.DataReader-getRoot"></div>/**
  58. * Abstract method created in extension's buildExtractors impl.
  59. */
  60. getRoot: Ext.emptyFn,
  61. <div id="prop-Ext.data.DataReader-getMessage"></div>/**
  62. * Abstract method created in extension's buildExtractors impl.
  63. */
  64. getMessage: Ext.emptyFn,
  65. <div id="prop-Ext.data.DataReader-getSuccess"></div>/**
  66. * Abstract method created in extension's buildExtractors impl.
  67. */
  68. getSuccess: Ext.emptyFn,
  69. <div id="prop-Ext.data.DataReader-getId"></div>/**
  70. * Abstract method created in extension's buildExtractors impl.
  71. */
  72. getId: Ext.emptyFn,
  73. <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**
  74. * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
  75. */
  76. buildExtractors : Ext.emptyFn,
  77. <div id="prop-Ext.data.DataReader-extractValues"></div>/**
  78. * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
  79. */
  80. extractValues : Ext.emptyFn,
  81. <div id="method-Ext.data.DataReader-realize"></div>/**
  82. * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
  83. * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
  84. * data from server will be merged with the data in the local record.
  85. * In addition, you <b>must</b> return record-data from the server in the same order received.
  86. * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
  87. * @param {Record/Record[]} record The phantom record to be realized.
  88. * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
  89. */
  90. realize: function(rs, data){
  91. if (Ext.isArray(rs)) {
  92. for (var i = rs.length - 1; i >= 0; i--) {
  93. // recurse
  94. if (Ext.isArray(data)) {
  95. this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
  96. }
  97. else {
  98. // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
  99. // the else clause below will detect !this.isData and throw exception.
  100. this.realize(rs.splice(i,1).shift(), data);
  101. }
  102. }
  103. }
  104. else {
  105. // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
  106. if (Ext.isArray(data) && data.length == 1) {
  107. data = data.shift();
  108. }
  109. if (!this.isData(data)) {
  110. // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
  111. //rs.commit();
  112. throw new Ext.data.DataReader.Error('realize', rs);
  113. }
  114. rs.phantom = false; // <-- That's what it's all about
  115. rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
  116. rs.id = this.getId(data);
  117. rs.data = data;
  118. rs.commit();
  119. }
  120. },
  121. <div id="method-Ext.data.DataReader-update"></div>/**
  122. * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
  123. * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
  124. * the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be
  125. * suppressed as the record receives fresh new data-hash
  126. * @param {Record/Record[]} rs
  127. * @param {Object/Object[]} data
  128. */
  129. update : function(rs, data) {
  130. if (Ext.isArray(rs)) {
  131. for (var i=rs.length-1; i >= 0; i--) {
  132. if (Ext.isArray(data)) {
  133. this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
  134. }
  135. else {
  136. // weird...rs is an array but data isn't?? recurse but just send in the whole data object.
  137. // the else clause below will detect !this.isData and throw exception.
  138. this.update(rs.splice(i,1).shift(), data);
  139. }
  140. }
  141. }
  142. else {
  143. // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
  144. if (Ext.isArray(data) && data.length == 1) {
  145. data = data.shift();
  146. }
  147. if (this.isData(data)) {
  148. rs.data = Ext.apply(rs.data, data);
  149. }
  150. rs.commit();
  151. }
  152. },
  153. /**
  154. * returns extracted, type-cast rows of data. Iterates to call #extractValues for each row
  155. * @param {Object[]/Object} data-root from server response
  156. * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
  157. * @private
  158. */
  159. extractData : function(root, returnRecords) {
  160. // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
  161. var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
  162. var rs = [];
  163. // Had to add Check for XmlReader, #isData returns true if root is an Xml-object. Want to check in order to re-factor
  164. // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
  165. if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
  166. root = [root];
  167. }
  168. var f = this.recordType.prototype.fields,
  169. fi = f.items,
  170. fl = f.length,
  171. rs = [];
  172. if (returnRecords === true) {
  173. var Record = this.recordType;
  174. for (var i = 0; i < root.length; i++) {
  175. var n = root[i];
  176. var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
  177. record[rawName] = n; // <-- There's implementation of ugly bit, setting the raw record-data.
  178. rs.push(record);
  179. }
  180. }
  181. else {
  182. for (var i = 0; i < root.length; i++) {
  183. var data = this.extractValues(root[i], fi, fl);
  184. data[this.meta.idProperty] = this.getId(root[i]);
  185. rs.push(data);
  186. }
  187. }
  188. return rs;
  189. },
  190. <div id="method-Ext.data.DataReader-isData"></div>/**
  191. * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
  192. * corresponding to idProperty defined in your DataReader config containing non-empty pk.
  193. * @param {Object} data
  194. * @return {Boolean}
  195. */
  196. isData : function(data) {
  197. return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
  198. },
  199. // private function a store will createSequence upon
  200. onMetaChange : function(meta){
  201. delete this.ef;
  202. this.meta = meta;
  203. this.recordType = Ext.data.Record.create(meta.fields);
  204. this.buildExtractors();
  205. }
  206. };
  207. <div id="cls-Ext.data.DataReader.Error"></div>/**
  208. * @class Ext.data.DataReader.Error
  209. * @extends Ext.Error
  210. * General error class for Ext.data.DataReader
  211. */
  212. Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
  213. constructor : function(message, arg) {
  214. this.arg = arg;
  215. Ext.Error.call(this, message);
  216. },
  217. name: 'Ext.data.DataReader'
  218. });
  219. Ext.apply(Ext.data.DataReader.Error.prototype, {
  220. lang : {
  221. 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
  222. 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
  223. 'invalid-response': "#readResponse received an invalid response from the server."
  224. }
  225. });
  226. </pre>
  227. </body>
  228. </html>