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.

232 lines
7.6 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. // setup an App namespace
  16. // This is done to prevent collisions in the global namespace
  17. Ext.ns('App');
  18. /**
  19. * App.BookStore
  20. * @extends Ext.data.Store
  21. * @cfg {String} url This will be a url of a location to load the BookStore
  22. * This is a specialized Store which maintains books.
  23. * It already knows about Amazon's XML definition and will expose the following
  24. * Record defintion:
  25. * - Author
  26. * - Manufacturer
  27. * - ProductGroup
  28. * - DetailPageURL
  29. */
  30. App.BookStore = function(config) {
  31. var config = config || {};
  32. Ext.applyIf(config, {
  33. reader: new Ext.data.XmlReader({
  34. // records will have an "Item" tag
  35. record: 'Item',
  36. id: 'ASIN',
  37. totalRecords: '@total'
  38. }, [
  39. // set up the fields mapping into the xml doc
  40. // The first needs mapping, the others are very basic
  41. {name: 'Author', mapping: 'ItemAttributes > Author'},
  42. 'Title',
  43. 'Manufacturer',
  44. 'ProductGroup',
  45. // Detail URL is not part of the column model of the grid
  46. 'DetailPageURL'
  47. ])
  48. });
  49. // call the superclass's constructor
  50. App.BookStore.superclass.constructor.call(this, config);
  51. };
  52. Ext.extend(App.BookStore, Ext.data.Store);
  53. /**
  54. * App.BookGrid
  55. * @extends Ext.grid.GridPanel
  56. * This is a custom grid which will display book information. It is tied to
  57. * a specific record definition by the dataIndex properties.
  58. *
  59. * It follows a very custom pattern used only when extending Ext.Components
  60. * in which you can omit the constructor.
  61. *
  62. * It also registers the class with the Component Manager with an xtype of
  63. * bookgrid. This allows the application to take care of the lazy-instatiation
  64. * facilities provided in Ext's Component Model.
  65. */
  66. App.BookGrid = Ext.extend(Ext.grid.GridPanel, {
  67. // override
  68. initComponent : function() {
  69. Ext.apply(this, {
  70. // Pass in a column model definition
  71. // Note that the DetailPageURL was defined in the record definition but is not used
  72. // here. That is okay.
  73. columns: [
  74. {header: "Author", width: 120, dataIndex: 'Author', sortable: true},
  75. {header: "Title", dataIndex: 'Title', sortable: true},
  76. {header: "Manufacturer", width: 115, dataIndex: 'Manufacturer', sortable: true},
  77. {header: "Product Group", dataIndex: 'ProductGroup', sortable: true}
  78. ],
  79. sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
  80. // Note the use of a storeId, this will register thisStore
  81. // with the StoreMgr and allow us to retrieve it very easily.
  82. store: new App.BookStore({
  83. storeId: 'gridBookStore',
  84. url: 'sheldon.xml'
  85. }),
  86. // force the grid to fit the space which is available
  87. viewConfig: {
  88. forceFit: true
  89. }
  90. });
  91. // finally call the superclasses implementation
  92. App.BookGrid.superclass.initComponent.call(this);
  93. }
  94. });
  95. // This will associate an string representation of a class
  96. // (called an xtype) with the Component Manager
  97. // It allows you to support lazy instantiation of your components
  98. Ext.reg('bookgrid', App.BookGrid);
  99. /**
  100. * App.BookDetail
  101. * @extends Ext.Panel
  102. * This is a specialized Panel which is used to show information about
  103. * a book.
  104. *
  105. * This demonstrates adding 2 custom properties (tplMarkup and
  106. * startingMarkup) to the class. It also overrides the initComponent
  107. * method and adds a new method called updateDetail.
  108. *
  109. * The class will be registered with an xtype of 'bookdetail'
  110. */
  111. App.BookDetail = Ext.extend(Ext.Panel, {
  112. // add tplMarkup as a new property
  113. tplMarkup: [
  114. 'Title: <a href="{DetailPageURL}" target="_blank">{Title}</a><br/>',
  115. 'Author: {Author}<br/>',
  116. 'Manufacturer: {Manufacturer}<br/>',
  117. 'Product Group: {ProductGroup}<br/>'
  118. ],
  119. // startingMarup as a new property
  120. startingMarkup: 'Please select a book to see additional details',
  121. // override initComponent to create and compile the template
  122. // apply styles to the body of the panel and initialize
  123. // html to startingMarkup
  124. initComponent: function() {
  125. this.tpl = new Ext.Template(this.tplMarkup);
  126. Ext.apply(this, {
  127. bodyStyle: {
  128. background: '#ffffff',
  129. padding: '7px'
  130. },
  131. html: this.startingMarkup
  132. });
  133. // call the superclass's initComponent implementation
  134. App.BookDetail.superclass.initComponent.call(this);
  135. },
  136. // add a method which updates the details
  137. updateDetail: function(data) {
  138. this.tpl.overwrite(this.body, data);
  139. }
  140. });
  141. // register the App.BookDetail class with an xtype of bookdetail
  142. Ext.reg('bookdetail', App.BookDetail);
  143. /**
  144. * App.BookMasterDetail
  145. * @extends Ext.Panel
  146. *
  147. * This is a specialized panel which is composed of both a bookgrid
  148. * and a bookdetail panel. It provides the glue between the two
  149. * components to allow them to communicate. You could consider this
  150. * the actual application.
  151. *
  152. */
  153. App.BookMasterDetail = Ext.extend(Ext.Panel, {
  154. // override initComponent
  155. initComponent: function() {
  156. // used applyIf rather than apply so user could
  157. // override the defaults
  158. Ext.applyIf(this, {
  159. frame: true,
  160. title: 'Book List',
  161. width: 540,
  162. height: 400,
  163. layout: 'border',
  164. items: [{
  165. xtype: 'bookgrid',
  166. itemId: 'gridPanel',
  167. region: 'north',
  168. height: 210,
  169. split: true
  170. },{
  171. xtype: 'bookdetail',
  172. itemId: 'detailPanel',
  173. region: 'center'
  174. }]
  175. })
  176. // call the superclass's initComponent implementation
  177. App.BookMasterDetail.superclass.initComponent.call(this);
  178. },
  179. // override initEvents
  180. initEvents: function() {
  181. // call the superclass's initEvents implementation
  182. App.BookMasterDetail.superclass.initEvents.call(this);
  183. // now add application specific events
  184. // notice we use the selectionmodel's rowselect event rather
  185. // than a click event from the grid to provide key navigation
  186. // as well as mouse navigation
  187. var bookGridSm = this.getComponent('gridPanel').getSelectionModel();
  188. bookGridSm.on('rowselect', this.onRowSelect, this);
  189. },
  190. // add a method called onRowSelect
  191. // This matches the method signature as defined by the 'rowselect'
  192. // event defined in Ext.grid.RowSelectionModel
  193. onRowSelect: function(sm, rowIdx, r) {
  194. // getComponent will retrieve itemId's or id's. Note that itemId's
  195. // are scoped locally to this instance of a component to avoid
  196. // conflicts with the ComponentMgr
  197. var detailPanel = this.getComponent('detailPanel');
  198. detailPanel.updateDetail(r.data);
  199. }
  200. });
  201. // register an xtype with this class
  202. Ext.reg('bookmasterdetail', App.BookMasterDetail);
  203. // Finally now that we've defined all of our classes we can instantiate
  204. // an instance of the app and renderTo an existing div called 'binding-example'
  205. // Note now that classes have encapsulated this behavior we can easily create
  206. // an instance of this app to be used in many different contexts, you could
  207. // easily place this application in an Ext.Window for example
  208. Ext.onReady(function() {
  209. // create an instance of the app
  210. var bookApp = new App.BookMasterDetail({
  211. renderTo: 'binding-example'
  212. });
  213. // We can retrieve a reference to the data store
  214. // via the StoreMgr by its storeId
  215. Ext.StoreMgr.get('gridBookStore').load();
  216. });