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.

478 lines
20 KiB

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