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.

368 lines
15 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.JsonReader"></div>/**
  16. * @class Ext.data.JsonReader
  17. * @extends Ext.data.DataReader
  18. * <p>Data reader class to create an Array of {@link Ext.data.Record} objects
  19. * from a JSON packet based on mappings in a provided {@link Ext.data.Record}
  20. * constructor.</p>
  21. * <p>Example code:</p>
  22. * <pre><code>
  23. var myReader = new Ext.data.JsonReader({
  24. // metadata configuration options:
  25. {@link #idProperty}: 'id'
  26. {@link #root}: 'rows',
  27. {@link #totalProperty}: 'results',
  28. {@link Ext.data.DataReader#messageProperty}: "msg" // The element within the response that provides a user-feedback message (optional)
  29. // the fields config option will internally create an {@link Ext.data.Record}
  30. // constructor that provides mapping for reading the record data objects
  31. {@link Ext.data.DataReader#fields fields}: [
  32. // map Record&#39;s 'firstname' field to data object&#39;s key of same name
  33. {name: 'name', mapping: 'firstname'},
  34. // map Record&#39;s 'job' field to data object&#39;s 'occupation' key
  35. {name: 'job', mapping: 'occupation'}
  36. ]
  37. });
  38. </code></pre>
  39. * <p>This would consume a JSON data object of the form:</p><pre><code>
  40. {
  41. results: 2000, // Reader&#39;s configured {@link #totalProperty}
  42. rows: [ // Reader&#39;s configured {@link #root}
  43. // record data objects:
  44. { {@link #idProperty id}: 1, firstname: 'Bill', occupation: 'Gardener' },
  45. { {@link #idProperty id}: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
  46. ...
  47. ]
  48. }
  49. </code></pre>
  50. * <p><b><u>Automatic configuration using metaData</u></b></p>
  51. * <p>It is possible to change a JsonReader's metadata at any time by including
  52. * a <b><tt>metaData</tt></b> property in the JSON data object. If the JSON data
  53. * object has a <b><tt>metaData</tt></b> property, a {@link Ext.data.Store Store}
  54. * object using this Reader will reconfigure itself to use the newly provided
  55. * field definition and fire its {@link Ext.data.Store#metachange metachange}
  56. * event. The metachange event handler may interrogate the <b><tt>metaData</tt></b>
  57. * property to perform any configuration required.</p>
  58. * <p>Note that reconfiguring a Store potentially invalidates objects which may
  59. * refer to Fields or Records which no longer exist.</p>
  60. * <p>To use this facility you would create the JsonReader like this:</p><pre><code>
  61. var myReader = new Ext.data.JsonReader();
  62. </code></pre>
  63. * <p>The first data packet from the server would configure the reader by
  64. * containing a <b><tt>metaData</tt></b> property <b>and</b> the data. For
  65. * example, the JSON data object might take the form:</p><pre><code>
  66. {
  67. metaData: {
  68. "{@link #idProperty}": "id",
  69. "{@link #root}": "rows",
  70. "{@link #totalProperty}": "results"
  71. "{@link #successProperty}": "success",
  72. "{@link Ext.data.DataReader#fields fields}": [
  73. {"name": "name"},
  74. {"name": "job", "mapping": "occupation"}
  75. ],
  76. // used by store to set its sortInfo
  77. "sortInfo":{
  78. "field": "name",
  79. "direction": "ASC"
  80. },
  81. // {@link Ext.PagingToolbar paging data} (if applicable)
  82. "start": 0,
  83. "limit": 2,
  84. // custom property
  85. "foo": "bar"
  86. },
  87. // Reader&#39;s configured {@link #successProperty}
  88. "success": true,
  89. // Reader&#39;s configured {@link #totalProperty}
  90. "results": 2000,
  91. // Reader&#39;s configured {@link #root}
  92. // (this data simulates 2 results {@link Ext.PagingToolbar per page})
  93. "rows": [ // <b>*Note:</b> this must be an Array
  94. { "id": 1, "name": "Bill", "occupation": "Gardener" },
  95. { "id": 2, "name": "Ben", "occupation": "Horticulturalist" }
  96. ]
  97. }
  98. * </code></pre>
  99. * <p>The <b><tt>metaData</tt></b> property in the JSON data object should contain:</p>
  100. * <div class="mdetail-params"><ul>
  101. * <li>any of the configuration options for this class</li>
  102. * <li>a <b><tt>{@link Ext.data.Record#fields fields}</tt></b> property which
  103. * the JsonReader will use as an argument to the
  104. * {@link Ext.data.Record#create data Record create method} in order to
  105. * configure the layout of the Records it will produce.</li>
  106. * <li>a <b><tt>{@link Ext.data.Store#sortInfo sortInfo}</tt></b> property
  107. * which the JsonReader will use to set the {@link Ext.data.Store}'s
  108. * {@link Ext.data.Store#sortInfo sortInfo} property</li>
  109. * <li>any custom properties needed</li>
  110. * </ul></div>
  111. *
  112. * @constructor
  113. * Create a new JsonReader
  114. * @param {Object} meta Metadata configuration options.
  115. * @param {Array/Object} recordType
  116. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  117. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  118. * constructor created from {@link Ext.data.Record#create}.</p>
  119. */
  120. Ext.data.JsonReader = function(meta, recordType){
  121. meta = meta || {};
  122. <div id="cfg-Ext.data.JsonReader-idProperty"></div>/**
  123. * @cfg {String} idProperty [id] Name of the property within a row object
  124. * that contains a record identifier value. Defaults to <tt>id</tt>
  125. */
  126. <div id="cfg-Ext.data.JsonReader-successProperty"></div>/**
  127. * @cfg {String} successProperty [success] Name of the property from which to
  128. * retrieve the success attribute. Defaults to <tt>success</tt>. See
  129. * {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
  130. * for additional information.
  131. */
  132. <div id="cfg-Ext.data.JsonReader-totalProperty"></div>/**
  133. * @cfg {String} totalProperty [total] Name of the property from which to
  134. * retrieve the total number of records in the dataset. This is only needed
  135. * if the whole dataset is not passed in one go, but is being paged from
  136. * the remote server. Defaults to <tt>total</tt>.
  137. */
  138. <div id="cfg-Ext.data.JsonReader-root"></div>/**
  139. * @cfg {String} root [undefined] <b>Required</b>. The name of the property
  140. * which contains the Array of row objects. Defaults to <tt>undefined</tt>.
  141. * An exception will be thrown if the root property is undefined. The data
  142. * packet value for this property should be an empty array to clear the data
  143. * or show no data.
  144. */
  145. Ext.applyIf(meta, {
  146. idProperty: 'id',
  147. successProperty: 'success',
  148. totalProperty: 'total'
  149. });
  150. Ext.data.JsonReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  151. };
  152. Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
  153. <div id="prop-Ext.data.JsonReader-meta"></div>/**
  154. * This JsonReader's metadata as passed to the constructor, or as passed in
  155. * the last data packet's <b><tt>metaData</tt></b> property.
  156. * @type Mixed
  157. * @property meta
  158. */
  159. <div id="method-Ext.data.JsonReader-read"></div>/**
  160. * This method is only used by a DataProxy which has retrieved data from a remote server.
  161. * @param {Object} response The XHR object which contains the JSON data in its responseText.
  162. * @return {Object} data A data block which is used by an Ext.data.Store object as
  163. * a cache of Ext.data.Records.
  164. */
  165. read : function(response){
  166. var json = response.responseText;
  167. var o = Ext.decode(json);
  168. if(!o) {
  169. throw {message: 'JsonReader.read: Json object not found'};
  170. }
  171. return this.readRecords(o);
  172. },
  173. /*
  174. * TODO: refactor code between JsonReader#readRecords, #readResponse into 1 method.
  175. * there's ugly duplication going on due to maintaining backwards compat. with 2.0. It's time to do this.
  176. */
  177. <div id="method-Ext.data.JsonReader-readResponse"></div>/**
  178. * Decode a JSON response from server.
  179. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  180. * @param {Object} response The XHR object returned through an Ajax server request.
  181. * @return {Response} A {@link Ext.data.Response Response} object containing the data response, and also status information.
  182. */
  183. readResponse : function(action, response) {
  184. var o = (response.responseText !== undefined) ? Ext.decode(response.responseText) : response;
  185. if(!o) {
  186. throw new Ext.data.JsonReader.Error('response');
  187. }
  188. var root = this.getRoot(o);
  189. if (action === Ext.data.Api.actions.create) {
  190. var def = Ext.isDefined(root);
  191. if (def && Ext.isEmpty(root)) {
  192. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  193. }
  194. else if (!def) {
  195. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  196. }
  197. }
  198. // instantiate response object
  199. var res = new Ext.data.Response({
  200. action: action,
  201. success: this.getSuccess(o),
  202. data: (root) ? this.extractData(root, false) : [],
  203. message: this.getMessage(o),
  204. raw: o
  205. });
  206. // blow up if no successProperty
  207. if (Ext.isEmpty(res.success)) {
  208. throw new Ext.data.JsonReader.Error('successProperty-response', this.meta.successProperty);
  209. }
  210. return res;
  211. },
  212. <div id="method-Ext.data.JsonReader-readRecords"></div>/**
  213. * Create a data block containing Ext.data.Records from a JSON object.
  214. * @param {Object} o An object which contains an Array of row objects in the property specified
  215. * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
  216. * which contains the total size of the dataset.
  217. * @return {Object} data A data block which is used by an Ext.data.Store object as
  218. * a cache of Ext.data.Records.
  219. */
  220. readRecords : function(o){
  221. <div id="prop-Ext.data.JsonReader-jsonData"></div>/**
  222. * After any data loads, the raw JSON data is available for further custom processing. If no data is
  223. * loaded or there is a load exception this property will be undefined.
  224. * @type Object
  225. */
  226. this.jsonData = o;
  227. if(o.metaData){
  228. this.onMetaChange(o.metaData);
  229. }
  230. var s = this.meta, Record = this.recordType,
  231. f = Record.prototype.fields, fi = f.items, fl = f.length, v;
  232. var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
  233. if(s.totalProperty){
  234. v = parseInt(this.getTotal(o), 10);
  235. if(!isNaN(v)){
  236. totalRecords = v;
  237. }
  238. }
  239. if(s.successProperty){
  240. v = this.getSuccess(o);
  241. if(v === false || v === 'false'){
  242. success = false;
  243. }
  244. }
  245. // TODO return Ext.data.Response instance instead. @see #readResponse
  246. return {
  247. success : success,
  248. records : this.extractData(root, true), // <-- true to return [Ext.data.Record]
  249. totalRecords : totalRecords
  250. };
  251. },
  252. // private
  253. buildExtractors : function() {
  254. if(this.ef){
  255. return;
  256. }
  257. var s = this.meta, Record = this.recordType,
  258. f = Record.prototype.fields, fi = f.items, fl = f.length;
  259. if(s.totalProperty) {
  260. this.getTotal = this.createAccessor(s.totalProperty);
  261. }
  262. if(s.successProperty) {
  263. this.getSuccess = this.createAccessor(s.successProperty);
  264. }
  265. if (s.messageProperty) {
  266. this.getMessage = this.createAccessor(s.messageProperty);
  267. }
  268. this.getRoot = s.root ? this.createAccessor(s.root) : function(p){return p;};
  269. if (s.id || s.idProperty) {
  270. var g = this.createAccessor(s.id || s.idProperty);
  271. this.getId = function(rec) {
  272. var r = g(rec);
  273. return (r === undefined || r === '') ? null : r;
  274. };
  275. } else {
  276. this.getId = function(){return null;};
  277. }
  278. var ef = [];
  279. for(var i = 0; i < fl; i++){
  280. f = fi[i];
  281. var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  282. ef.push(this.createAccessor(map));
  283. }
  284. this.ef = ef;
  285. },
  286. /**
  287. * @ignore
  288. * TODO This isn't used anywhere?? Don't we want to use this where possible instead of complex #createAccessor?
  289. */
  290. simpleAccess : function(obj, subsc) {
  291. return obj[subsc];
  292. },
  293. /**
  294. * @ignore
  295. */
  296. createAccessor : function(){
  297. var re = /[\[\.]/;
  298. return function(expr) {
  299. if(Ext.isEmpty(expr)){
  300. return Ext.emptyFn;
  301. }
  302. if(Ext.isFunction(expr)){
  303. return expr;
  304. }
  305. var i = String(expr).search(re);
  306. if(i >= 0){
  307. return new Function('obj', 'return obj' + (i > 0 ? '.' : '') + expr);
  308. }
  309. return function(obj){
  310. return obj[expr];
  311. };
  312. };
  313. }(),
  314. /**
  315. * type-casts a single row of raw-data from server
  316. * @param {Object} data
  317. * @param {Array} items
  318. * @param {Integer} len
  319. * @private
  320. */
  321. extractValues : function(data, items, len) {
  322. var f, values = {};
  323. for(var j = 0; j < len; j++){
  324. f = items[j];
  325. var v = this.ef[j](data);
  326. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  327. }
  328. return values;
  329. }
  330. });
  331. <div id="cls-Ext.data.JsonReader.Error"></div>/**
  332. * @class Ext.data.JsonReader.Error
  333. * Error class for JsonReader
  334. */
  335. Ext.data.JsonReader.Error = Ext.extend(Ext.Error, {
  336. constructor : function(message, arg) {
  337. this.arg = arg;
  338. Ext.Error.call(this, message);
  339. },
  340. name : 'Ext.data.JsonReader'
  341. });
  342. Ext.apply(Ext.data.JsonReader.Error.prototype, {
  343. lang: {
  344. 'response': 'An error occurred while json-decoding your server response',
  345. 'successProperty-response': 'Could not locate your "successProperty" in your server response. Please review your JsonReader config to ensure the config-property "successProperty" matches the property in your server-response. See the JsonReader docs.',
  346. 'root-undefined-config': 'Your JsonReader was configured without a "root" property. Please review your JsonReader config and make sure to define the root property. See the JsonReader docs.',
  347. 'idProperty-undefined' : 'Your JsonReader was configured without an "idProperty" Please review your JsonReader configuration and ensure the "idProperty" is set (e.g.: "id"). See the JsonReader docs.',
  348. 'root-empty': 'Data was expected to be returned by the server in the "root" property of the response. Please review your JsonReader configuration to ensure the "root" property matches that returned in the server-response. See JsonReader docs.'
  349. }
  350. });
  351. </pre>
  352. </body>
  353. </html>