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.

268 lines
9.8 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.XmlReader"></div>/**
  16. * @class Ext.data.XmlReader
  17. * @extends Ext.data.DataReader
  18. * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
  19. * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
  20. * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
  21. * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
  22. * <p>Example code:</p>
  23. * <pre><code>
  24. var Employee = Ext.data.Record.create([
  25. {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
  26. {name: 'occupation'} // This field will use "occupation" as the mapping.
  27. ]);
  28. var myReader = new Ext.data.XmlReader({
  29. totalProperty: "results", // The element which contains the total dataset size (optional)
  30. record: "row", // The repeated element which contains row information
  31. idProperty: "id" // The element within the row that provides an ID for the record (optional)
  32. messageProperty: "msg" // The element within the response that provides a user-feedback message (optional)
  33. }, Employee);
  34. </code></pre>
  35. * <p>
  36. * This would consume an XML file like this:
  37. * <pre><code>
  38. &lt;?xml version="1.0" encoding="UTF-8"?>
  39. &lt;dataset>
  40. &lt;results>2&lt;/results>
  41. &lt;row>
  42. &lt;id>1&lt;/id>
  43. &lt;name>Bill&lt;/name>
  44. &lt;occupation>Gardener&lt;/occupation>
  45. &lt;/row>
  46. &lt;row>
  47. &lt;id>2&lt;/id>
  48. &lt;name>Ben&lt;/name>
  49. &lt;occupation>Horticulturalist&lt;/occupation>
  50. &lt;/row>
  51. &lt;/dataset>
  52. </code></pre>
  53. * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
  54. * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
  55. * paged from the remote server.
  56. * @cfg {String} record The DomQuery path to the repeated element which contains record information.
  57. * @cfg {String} record The DomQuery path to the repeated element which contains record information.
  58. * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
  59. * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
  60. * a record identifier value.
  61. * @constructor
  62. * Create a new XmlReader.
  63. * @param {Object} meta Metadata configuration options
  64. * @param {Object} recordType Either an Array of field definition objects as passed to
  65. * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
  66. */
  67. Ext.data.XmlReader = function(meta, recordType){
  68. meta = meta || {};
  69. // backwards compat, convert idPath or id / success
  70. Ext.applyIf(meta, {
  71. idProperty: meta.idProperty || meta.idPath || meta.id,
  72. successProperty: meta.successProperty || meta.success
  73. });
  74. Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  75. };
  76. Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
  77. /**
  78. * This method is only used by a DataProxy which has retrieved data from a remote server.
  79. * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
  80. * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
  81. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  82. * a cache of Ext.data.Records.
  83. */
  84. read : function(response){
  85. var doc = response.responseXML;
  86. if(!doc) {
  87. throw {message: "XmlReader.read: XML Document not available"};
  88. }
  89. return this.readRecords(doc);
  90. },
  91. /**
  92. * Create a data block containing Ext.data.Records from an XML document.
  93. * @param {Object} doc A parsed XML document.
  94. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  95. * a cache of Ext.data.Records.
  96. */
  97. readRecords : function(doc){
  98. /**
  99. * After any data loads/reads, the raw XML Document is available for further custom processing.
  100. * @type XMLDocument
  101. */
  102. this.xmlData = doc;
  103. var root = doc.documentElement || doc,
  104. q = Ext.DomQuery,
  105. totalRecords = 0,
  106. success = true;
  107. if(this.meta.totalProperty){
  108. totalRecords = this.getTotal(root, 0);
  109. }
  110. if(this.meta.successProperty){
  111. success = this.getSuccess(root);
  112. }
  113. var records = this.extractData(q.select(this.meta.record, root), true); // <-- true to return Ext.data.Record[]
  114. // TODO return Ext.data.Response instance. @see #readResponse
  115. return {
  116. success : success,
  117. records : records,
  118. totalRecords : totalRecords || records.length
  119. };
  120. },
  121. /**
  122. * Decode an XML response from server.
  123. * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
  124. * @param {Object} response HTTP Response object from browser.
  125. * @return {Ext.data.Response} An instance of {@link Ext.data.Response}
  126. */
  127. readResponse : function(action, response) {
  128. var q = Ext.DomQuery,
  129. doc = response.responseXML,
  130. root = doc.documentElement || doc;
  131. // create general Response instance.
  132. var res = new Ext.data.Response({
  133. action: action,
  134. success : this.getSuccess(root),
  135. message: this.getMessage(root),
  136. data: this.extractData(q.select(this.meta.record, root) || q.select(this.meta.root, root), false),
  137. raw: doc
  138. });
  139. if (Ext.isEmpty(res.success)) {
  140. throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
  141. }
  142. // Create actions from a response having status 200 must return pk
  143. if (action === Ext.data.Api.actions.create) {
  144. var def = Ext.isDefined(res.data);
  145. if (def && Ext.isEmpty(res.data)) {
  146. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  147. }
  148. else if (!def) {
  149. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  150. }
  151. }
  152. return res;
  153. },
  154. getSuccess : function() {
  155. return true;
  156. },
  157. /**
  158. * build response-data extractor functions.
  159. * @private
  160. * @ignore
  161. */
  162. buildExtractors : function() {
  163. if(this.ef){
  164. return;
  165. }
  166. var s = this.meta,
  167. Record = this.recordType,
  168. f = Record.prototype.fields,
  169. fi = f.items,
  170. fl = f.length;
  171. if(s.totalProperty) {
  172. this.getTotal = this.createAccessor(s.totalProperty);
  173. }
  174. if(s.successProperty) {
  175. this.getSuccess = this.createAccessor(s.successProperty);
  176. }
  177. if (s.messageProperty) {
  178. this.getMessage = this.createAccessor(s.messageProperty);
  179. }
  180. this.getRoot = function(res) {
  181. return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
  182. };
  183. if (s.idPath || s.idProperty) {
  184. var g = this.createAccessor(s.idPath || s.idProperty);
  185. this.getId = function(rec) {
  186. var id = g(rec) || rec.id;
  187. return (id === undefined || id === '') ? null : id;
  188. };
  189. } else {
  190. this.getId = function(){return null;};
  191. }
  192. var ef = [];
  193. for(var i = 0; i < fl; i++){
  194. f = fi[i];
  195. var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  196. ef.push(this.createAccessor(map));
  197. }
  198. this.ef = ef;
  199. },
  200. /**
  201. * Creates a function to return some particular key of data from a response.
  202. * @param {String} key
  203. * @return {Function}
  204. * @private
  205. * @ignore
  206. */
  207. createAccessor : function(){
  208. var q = Ext.DomQuery;
  209. return function(key) {
  210. if (Ext.isFunction(key)) {
  211. return key;
  212. }
  213. switch(key) {
  214. case this.meta.totalProperty:
  215. return function(root, def){
  216. return q.selectNumber(key, root, def);
  217. };
  218. break;
  219. case this.meta.successProperty:
  220. return function(root, def) {
  221. var sv = q.selectValue(key, root, true);
  222. var success = sv !== false && sv !== 'false';
  223. return success;
  224. };
  225. break;
  226. default:
  227. return function(root, def) {
  228. return q.selectValue(key, root, def);
  229. };
  230. break;
  231. }
  232. };
  233. }(),
  234. /**
  235. * extracts values and type-casts a row of data from server, extracted by #extractData
  236. * @param {Hash} data
  237. * @param {Ext.data.Field[]} items
  238. * @param {Number} len
  239. * @private
  240. * @ignore
  241. */
  242. extractValues : function(data, items, len) {
  243. var f, values = {};
  244. for(var j = 0; j < len; j++){
  245. f = items[j];
  246. var v = this.ef[j](data);
  247. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  248. }
  249. return values;
  250. }
  251. });</pre>
  252. </body>
  253. </html>