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.

491 lines
21 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.data.XmlWriter
  17. * @extends Ext.data.DataWriter
  18. * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
  19. * XmlWriter uses an instance of {@link Ext.XTemplate} for maximum flexibility in defining your own custom XML schema if the default schema is not appropriate for your needs.
  20. * See the {@link #tpl} configuration-property.
  21. */
  22. Ext.data.XmlWriter = function(params) {
  23. Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
  24. // compile the XTemplate for rendering XML documents.
  25. this.tpl = (typeof(this.tpl) === 'string') ? new Ext.XTemplate(this.tpl).compile() : this.tpl.compile();
  26. };
  27. Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
  28. /**
  29. * @cfg {String} documentRoot [xrequest] (Optional) The name of the XML document root-node. <b>Note:</b>
  30. * this parameter is required </b>only when</b> sending extra {@link Ext.data.Store#baseParams baseParams} to the server
  31. * during a write-request -- if no baseParams are set, the {@link Ext.data.XmlReader#record} meta-property can
  32. * suffice as the XML document root-node for write-actions involving just a <b>single record</b>. For requests
  33. * involving <b>multiple</b> records and <b>NO</b> baseParams, the {@link Ext.data.XmlWriter#root} property can
  34. * act as the XML document root.
  35. */
  36. documentRoot: 'xrequest',
  37. /**
  38. * @cfg {Boolean} forceDocumentRoot [false] Set to <tt>true</tt> to force XML documents having a root-node as defined
  39. * by {@link #documentRoot}, even with no baseParams defined.
  40. */
  41. forceDocumentRoot: false,
  42. /**
  43. * @cfg {String} root [records] The name of the containing element which will contain the nodes of an write-action involving <b>multiple</b> records. Each
  44. * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
  45. * eg:
  46. <code><pre>
  47. &lt;?xml version="1.0" encoding="UTF-8"?>
  48. &lt;user>&lt;first>Barney&lt;/first>&lt;/user>
  49. </code></pre>
  50. * However, when <b>multiple</b> records are written in a batch-operation, these records must be wrapped in a containing
  51. * Element.
  52. * eg:
  53. <code><pre>
  54. &lt;?xml version="1.0" encoding="UTF-8"?>
  55. &lt;records>
  56. &lt;first>Barney&lt;/first>&lt;/user>
  57. &lt;records>&lt;first>Barney&lt;/first>&lt;/user>
  58. &lt;/records>
  59. </code></pre>
  60. * Defaults to <tt>records</tt>. Do not confuse the nature of this property with that of {@link #documentRoot}
  61. */
  62. root: 'records',
  63. /**
  64. * @cfg {String} xmlVersion [1.0] The <tt>version</tt> written to header of xml documents.
  65. <code><pre>&lt;?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
  66. */
  67. xmlVersion : '1.0',
  68. /**
  69. * @cfg {String} xmlEncoding [ISO-8859-15] The <tt>encoding</tt> written to header of xml documents.
  70. <code><pre>&lt;?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
  71. */
  72. xmlEncoding: 'ISO-8859-15',
  73. /**
  74. * @cfg {String/Ext.XTemplate} tpl The XML template used to render {@link Ext.data.Api#actions write-actions} to your server.
  75. * <p>One can easily provide his/her own custom {@link Ext.XTemplate#constructor template-definition} if the default does not suffice.</p>
  76. * <p>Defaults to:</p>
  77. <code><pre>
  78. &lt;?xml version="{version}" encoding="{encoding}"?>
  79. &lt;tpl if="documentRoot">&lt;{documentRoot}>
  80. &lt;tpl for="baseParams">
  81. &lt;tpl for=".">
  82. &lt;{name}>{value}&lt;/{name}>
  83. &lt;/tpl>
  84. &lt;/tpl>
  85. &lt;tpl if="records.length &gt; 1">&lt;{root}>',
  86. &lt;tpl for="records">
  87. &lt;{parent.record}>
  88. &lt;tpl for=".">
  89. &lt;{name}>{value}&lt;/{name}>
  90. &lt;/tpl>
  91. &lt;/{parent.record}>
  92. &lt;/tpl>
  93. &lt;tpl if="records.length &gt; 1">&lt;/{root}>&lt;/tpl>
  94. &lt;tpl if="documentRoot">&lt;/{documentRoot}>&lt;/tpl>
  95. </pre></code>
  96. * <p>Templates will be called with the following API</p>
  97. * <ul>
  98. * <li>{String} version [1.0] The xml version.</li>
  99. * <li>{String} encoding [ISO-8859-15] The xml encoding.</li>
  100. * <li>{String/false} documentRoot The XML document root-node name or <tt>false</tt> if not required. See {@link #documentRoot} and {@link #forceDocumentRoot}.</li>
  101. * <li>{String} record The meta-data parameter defined on your {@link Ext.data.XmlReader#record} configuration represents the name of the xml-tag containing each record.</li>
  102. * <li>{String} root The meta-data parameter defined by {@link Ext.data.XmlWriter#root} configuration-parameter. Represents the name of the xml root-tag when sending <b>multiple</b> records to the server.</li>
  103. * <li>{Array} records The records being sent to the server, ie: the subject of the write-action being performed. The records parameter will be always be an array, even when only a single record is being acted upon.
  104. * Each item within the records array will contain an array of field objects having the following properties:
  105. * <ul>
  106. * <li>{String} name The field-name of the record as defined by your {@link Ext.data.Record#create Ext.data.Record definition}. The "mapping" property will be used, otherwise it will match the "name" property. Use this parameter to define the XML tag-name of the property.</li>
  107. * <li>{Mixed} value The record value of the field enclosed within XML tags specified by name property above.</li>
  108. * </ul></li>
  109. * <li>{Array} baseParams. The baseParams as defined upon {@link Ext.data.Store#baseParams}. Note that the baseParams have been converted into an array of [{name : "foo", value: "bar"}, ...] pairs in the same manner as the <b>records</b> parameter above. See {@link #documentRoot} and {@link #forceDocumentRoot}.</li>
  110. * </ul>
  111. */
  112. // Encoding the ? here in case it's being included by some kind of page that will parse it (eg. PHP)
  113. tpl: '<tpl for="."><\u003fxml version="{version}" encoding="{encoding}"\u003f><tpl if="documentRoot"><{documentRoot}><tpl for="baseParams"><tpl for="."><{name}>{value}</{name}></tpl></tpl></tpl><tpl if="records.length&gt;1"><{root}></tpl><tpl for="records"><{parent.record}><tpl for="."><{name}>{value}</{name}></tpl></{parent.record}></tpl><tpl if="records.length&gt;1"></{root}></tpl><tpl if="documentRoot"></{documentRoot}></tpl></tpl>',
  114. /**
  115. * XmlWriter implementation of the final stage of a write action.
  116. * @param {Object} params Transport-proxy's (eg: {@link Ext.Ajax#request}) params-object to write-to.
  117. * @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}.
  118. * @param {Object/Object[]} data Data-object representing the compiled Store-recordset.
  119. */
  120. render : function(params, baseParams, data) {
  121. baseParams = this.toArray(baseParams);
  122. params.xmlData = this.tpl.applyTemplate({
  123. version: this.xmlVersion,
  124. encoding: this.xmlEncoding,
  125. documentRoot: (baseParams.length > 0 || this.forceDocumentRoot === true) ? this.documentRoot : false,
  126. record: this.meta.record,
  127. root: this.root,
  128. baseParams: baseParams,
  129. records: (Ext.isArray(data[0])) ? data : [data]
  130. });
  131. },
  132. /**
  133. * createRecord
  134. * @protected
  135. * @param {Ext.data.Record} rec
  136. * @return {Array} Array of <tt>name:value</tt> pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
  137. */
  138. createRecord : function(rec) {
  139. return this.toArray(this.toHash(rec));
  140. },
  141. /**
  142. * updateRecord
  143. * @protected
  144. * @param {Ext.data.Record} rec
  145. * @return {Array} Array of {name:value} pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
  146. */
  147. updateRecord : function(rec) {
  148. return this.toArray(this.toHash(rec));
  149. },
  150. /**
  151. * destroyRecord
  152. * @protected
  153. * @param {Ext.data.Record} rec
  154. * @return {Array} Array containing a attribute-object (name/value pair) representing the {@link Ext.data.DataReader#idProperty idProperty}.
  155. */
  156. destroyRecord : function(rec) {
  157. var data = {};
  158. data[this.meta.idProperty] = rec.id;
  159. return this.toArray(data);
  160. }
  161. });
  162. /**
  163. * @class Ext.data.XmlReader
  164. * @extends Ext.data.DataReader
  165. * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
  166. * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
  167. * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
  168. * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
  169. * <p>Example code:</p>
  170. * <pre><code>
  171. var Employee = Ext.data.Record.create([
  172. {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
  173. {name: 'occupation'} // This field will use "occupation" as the mapping.
  174. ]);
  175. var myReader = new Ext.data.XmlReader({
  176. totalProperty: "results", // The element which contains the total dataset size (optional)
  177. record: "row", // The repeated element which contains row information
  178. idProperty: "id" // The element within the row that provides an ID for the record (optional)
  179. messageProperty: "msg" // The element within the response that provides a user-feedback message (optional)
  180. }, Employee);
  181. </code></pre>
  182. * <p>
  183. * This would consume an XML file like this:
  184. * <pre><code>
  185. &lt;?xml version="1.0" encoding="UTF-8"?>
  186. &lt;dataset>
  187. &lt;results>2&lt;/results>
  188. &lt;row>
  189. &lt;id>1&lt;/id>
  190. &lt;name>Bill&lt;/name>
  191. &lt;occupation>Gardener&lt;/occupation>
  192. &lt;/row>
  193. &lt;row>
  194. &lt;id>2&lt;/id>
  195. &lt;name>Ben&lt;/name>
  196. &lt;occupation>Horticulturalist&lt;/occupation>
  197. &lt;/row>
  198. &lt;/dataset>
  199. </code></pre>
  200. * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
  201. * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
  202. * paged from the remote server.
  203. * @cfg {String} record The DomQuery path to the repeated element which contains record information.
  204. * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
  205. * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
  206. * a record identifier value.
  207. * @constructor
  208. * Create a new XmlReader.
  209. * @param {Object} meta Metadata configuration options
  210. * @param {Object} recordType Either an Array of field definition objects as passed to
  211. * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
  212. */
  213. Ext.data.XmlReader = function(meta, recordType){
  214. meta = meta || {};
  215. // backwards compat, convert idPath or id / success
  216. Ext.applyIf(meta, {
  217. idProperty: meta.idProperty || meta.idPath || meta.id,
  218. successProperty: meta.successProperty || meta.success
  219. });
  220. Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  221. };
  222. Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
  223. /**
  224. * This method is only used by a DataProxy which has retrieved data from a remote server.
  225. * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
  226. * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
  227. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  228. * a cache of Ext.data.Records.
  229. */
  230. read : function(response){
  231. var doc = response.responseXML;
  232. if(!doc) {
  233. throw {message: "XmlReader.read: XML Document not available"};
  234. }
  235. return this.readRecords(doc);
  236. },
  237. /**
  238. * Create a data block containing Ext.data.Records from an XML document.
  239. * @param {Object} doc A parsed XML document.
  240. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  241. * a cache of Ext.data.Records.
  242. */
  243. readRecords : function(doc){
  244. /**
  245. * After any data loads/reads, the raw XML Document is available for further custom processing.
  246. * @type XMLDocument
  247. */
  248. this.xmlData = doc;
  249. var root = doc.documentElement || doc,
  250. q = Ext.DomQuery,
  251. totalRecords = 0,
  252. success = true;
  253. if(this.meta.totalProperty){
  254. totalRecords = this.getTotal(root, 0);
  255. }
  256. if(this.meta.successProperty){
  257. success = this.getSuccess(root);
  258. }
  259. var records = this.extractData(q.select(this.meta.record, root), true); // <-- true to return Ext.data.Record[]
  260. // TODO return Ext.data.Response instance. @see #readResponse
  261. return {
  262. success : success,
  263. records : records,
  264. totalRecords : totalRecords || records.length
  265. };
  266. },
  267. /**
  268. * Decode an XML response from server.
  269. * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
  270. * @param {Object} response HTTP Response object from browser.
  271. * @return {Ext.data.Response} An instance of {@link Ext.data.Response}
  272. */
  273. readResponse : function(action, response) {
  274. var q = Ext.DomQuery,
  275. doc = response.responseXML,
  276. root = doc.documentElement || doc;
  277. // create general Response instance.
  278. var res = new Ext.data.Response({
  279. action: action,
  280. success : this.getSuccess(root),
  281. message: this.getMessage(root),
  282. data: this.extractData(q.select(this.meta.record, root) || q.select(this.meta.root, root), false),
  283. raw: doc
  284. });
  285. if (Ext.isEmpty(res.success)) {
  286. throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
  287. }
  288. // Create actions from a response having status 200 must return pk
  289. if (action === Ext.data.Api.actions.create) {
  290. var def = Ext.isDefined(res.data);
  291. if (def && Ext.isEmpty(res.data)) {
  292. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  293. }
  294. else if (!def) {
  295. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  296. }
  297. }
  298. return res;
  299. },
  300. getSuccess : function() {
  301. return true;
  302. },
  303. /**
  304. * build response-data extractor functions.
  305. * @private
  306. * @ignore
  307. */
  308. buildExtractors : function() {
  309. if(this.ef){
  310. return;
  311. }
  312. var s = this.meta,
  313. Record = this.recordType,
  314. f = Record.prototype.fields,
  315. fi = f.items,
  316. fl = f.length;
  317. if(s.totalProperty) {
  318. this.getTotal = this.createAccessor(s.totalProperty);
  319. }
  320. if(s.successProperty) {
  321. this.getSuccess = this.createAccessor(s.successProperty);
  322. }
  323. if (s.messageProperty) {
  324. this.getMessage = this.createAccessor(s.messageProperty);
  325. }
  326. this.getRoot = function(res) {
  327. return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
  328. };
  329. if (s.idPath || s.idProperty) {
  330. var g = this.createAccessor(s.idPath || s.idProperty);
  331. this.getId = function(rec) {
  332. var id = g(rec) || rec.id;
  333. return (id === undefined || id === '') ? null : id;
  334. };
  335. } else {
  336. this.getId = function(){return null;};
  337. }
  338. var ef = [];
  339. for(var i = 0; i < fl; i++){
  340. f = fi[i];
  341. var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  342. ef.push(this.createAccessor(map));
  343. }
  344. this.ef = ef;
  345. },
  346. /**
  347. * Creates a function to return some particular key of data from a response.
  348. * @param {String} key
  349. * @return {Function}
  350. * @private
  351. * @ignore
  352. */
  353. createAccessor : function(){
  354. var q = Ext.DomQuery;
  355. return function(key) {
  356. if (Ext.isFunction(key)) {
  357. return key;
  358. }
  359. switch(key) {
  360. case this.meta.totalProperty:
  361. return function(root, def){
  362. return q.selectNumber(key, root, def);
  363. };
  364. break;
  365. case this.meta.successProperty:
  366. return function(root, def) {
  367. var sv = q.selectValue(key, root, true);
  368. var success = sv !== false && sv !== 'false';
  369. return success;
  370. };
  371. break;
  372. default:
  373. return function(root, def) {
  374. return q.selectValue(key, root, def);
  375. };
  376. break;
  377. }
  378. };
  379. }(),
  380. /**
  381. * extracts values and type-casts a row of data from server, extracted by #extractData
  382. * @param {Hash} data
  383. * @param {Ext.data.Field[]} items
  384. * @param {Number} len
  385. * @private
  386. * @ignore
  387. */
  388. extractValues : function(data, items, len) {
  389. var f, values = {};
  390. for(var j = 0; j < len; j++){
  391. f = items[j];
  392. var v = this.ef[j](data);
  393. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  394. }
  395. return values;
  396. }
  397. });/**
  398. * @class Ext.data.XmlStore
  399. * @extends Ext.data.Store
  400. * <p>Small helper class to make creating {@link Ext.data.Store}s from XML data easier.
  401. * A XmlStore will be automatically configured with a {@link Ext.data.XmlReader}.</p>
  402. * <p>A store configuration would be something like:<pre><code>
  403. var store = new Ext.data.XmlStore({
  404. // store configs
  405. autoDestroy: true,
  406. storeId: 'myStore',
  407. url: 'sheldon.xml', // automatically configures a HttpProxy
  408. // reader configs
  409. record: 'Item', // records will have an "Item" tag
  410. idPath: 'ASIN',
  411. totalRecords: '@TotalResults'
  412. fields: [
  413. // set up the fields mapping into the xml doc
  414. // The first needs mapping, the others are very basic
  415. {name: 'Author', mapping: 'ItemAttributes > Author'},
  416. 'Title', 'Manufacturer', 'ProductGroup'
  417. ]
  418. });
  419. * </code></pre></p>
  420. * <p>This store is configured to consume a returned object of the form:<pre><code>
  421. &#60?xml version="1.0" encoding="UTF-8"?>
  422. &#60ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-05-15">
  423. &#60Items>
  424. &#60Request>
  425. &#60IsValid>True&#60/IsValid>
  426. &#60ItemSearchRequest>
  427. &#60Author>Sidney Sheldon&#60/Author>
  428. &#60SearchIndex>Books&#60/SearchIndex>
  429. &#60/ItemSearchRequest>
  430. &#60/Request>
  431. &#60TotalResults>203&#60/TotalResults>
  432. &#60TotalPages>21&#60/TotalPages>
  433. &#60Item>
  434. &#60ASIN>0446355453&#60/ASIN>
  435. &#60DetailPageURL>
  436. http://www.amazon.com/
  437. &#60/DetailPageURL>
  438. &#60ItemAttributes>
  439. &#60Author>Sidney Sheldon&#60/Author>
  440. &#60Manufacturer>Warner Books&#60/Manufacturer>
  441. &#60ProductGroup>Book&#60/ProductGroup>
  442. &#60Title>Master of the Game&#60/Title>
  443. &#60/ItemAttributes>
  444. &#60/Item>
  445. &#60/Items>
  446. &#60/ItemSearchResponse>
  447. * </code></pre>
  448. * An object literal of this form could also be used as the {@link #data} config option.</p>
  449. * <p><b>Note:</b> Although not listed here, this class accepts all of the configuration options of
  450. * <b>{@link Ext.data.XmlReader XmlReader}</b>.</p>
  451. * @constructor
  452. * @param {Object} config
  453. * @xtype xmlstore
  454. */
  455. Ext.data.XmlStore = Ext.extend(Ext.data.Store, {
  456. /**
  457. * @cfg {Ext.data.DataReader} reader @hide
  458. */
  459. constructor: function(config){
  460. Ext.data.XmlStore.superclass.constructor.call(this, Ext.apply(config, {
  461. reader: new Ext.data.XmlReader(config)
  462. }));
  463. }
  464. });
  465. Ext.reg('xmlstore', Ext.data.XmlStore);