Window.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @class Ext.chooser.Window
  3. * @extends Ext.window.Window
  4. * @author Ed Spencer
  5. *
  6. * This is a simple subclass of the built-in Ext.window.Window class. Although it weighs in at 100+ lines, most of this
  7. * is just configuration. This Window class uses a border layout and creates a DataView in the central region and an
  8. * information panel in the east. It also sets up a toolbar to enable sorting and filtering of the items in the
  9. * DataView. We add a few simple methods to the class at the bottom, see the comments inline for details.
  10. */
  11. Ext.define('Ext.chooser.Window', {
  12. extend: 'Ext.window.Window',
  13. uses: [
  14. 'Ext.layout.container.Border',
  15. 'Ext.form.field.Text',
  16. 'Ext.form.field.ComboBox',
  17. 'Ext.toolbar.TextItem'
  18. ],
  19. height: 400,
  20. width : 600,
  21. title : 'Choose an Image',
  22. closeAction: 'hide',
  23. layout: 'border',
  24. // modal: true,
  25. border: false,
  26. bodyBorder: false,
  27. /**
  28. * initComponent is a great place to put any code that needs to be run when a new instance of a component is
  29. * created. Here we just specify the items that will go into our Window, plus the Buttons that we want to appear
  30. * at the bottom. Finally we call the superclass initComponent.
  31. */
  32. initComponent: function() {
  33. this.items = [
  34. {
  35. xtype: 'panel',
  36. region: 'center',
  37. autoScroll: true,
  38. items: {
  39. xtype: 'iconbrowser',
  40. id: 'img-chooser-view',
  41. listeners: {
  42. scope: this,
  43. selectionchange: this.onIconSelect,
  44. itemdblclick: this.fireImageSelected
  45. }
  46. },
  47. tbar: [
  48. {
  49. xtype: 'textfield',
  50. name : 'filter',
  51. fieldLabel: 'Filter',
  52. labelAlign: 'right',
  53. labelWidth: 35,
  54. listeners: {
  55. scope : this,
  56. buffer: 50,
  57. change: this.filter
  58. }
  59. },
  60. ' ',
  61. {
  62. xtype: 'combo',
  63. fieldLabel: 'Sort By',
  64. labelAlign: 'right',
  65. labelWidth: 45,
  66. valueField: 'field',
  67. displayField: 'label',
  68. value: 'Type',
  69. editable: false,
  70. store: Ext.create('Ext.data.Store', {
  71. fields: ['field', 'label'],
  72. sorters: 'type',
  73. proxy : {
  74. type: 'memory',
  75. data : [{label: 'Name', field: 'name'}, {label: 'Type', field: 'type'}]
  76. }
  77. }),
  78. listeners: {
  79. scope : this,
  80. select: this.sort
  81. }
  82. }
  83. ]
  84. },
  85. {
  86. xtype: 'infopanel',
  87. region: 'east',
  88. split: true
  89. }
  90. ];
  91. this.buttons = [
  92. {
  93. text: 'OK',
  94. scope: this,
  95. handler: this.fireImageSelected
  96. },
  97. {
  98. text: 'Cancel',
  99. scope: this,
  100. handler: function() {
  101. this.hide();
  102. }
  103. }
  104. ];
  105. this.callParent(arguments);
  106. /**
  107. * Specifies a new event that this component will fire when the user selects an item. The event is fired by the
  108. * fireImageSelected function below. Other components can listen to this event and take action when it is fired
  109. */
  110. this.addEvents(
  111. /**
  112. * @event selected
  113. * Fired whenever the user selects an image by double clicked it or clicking the window's OK button
  114. * @param {Ext.data.Model} image The image that was selected
  115. */
  116. 'selected'
  117. );
  118. },
  119. /**
  120. * @private
  121. * Called whenever the user types in the Filter textfield. Filters the DataView's store
  122. */
  123. filter: function(field, newValue) {
  124. var store = this.down('iconbrowser').store,
  125. dataview = this.down('dataview');
  126. store.suspendEvents();
  127. store.clearFilter();
  128. dataview.getSelectionModel().clearSelections();
  129. store.resumeEvents();
  130. store.filter({
  131. property: 'name',
  132. anyMatch: true,
  133. value : newValue
  134. });
  135. },
  136. /**
  137. * @private
  138. * Called whenever the user changes the sort field using the top toolbar's combobox
  139. */
  140. sort: function() {
  141. var field = this.down('combobox').getValue();
  142. this.down('dataview').store.sort(field);
  143. },
  144. /**
  145. * Called whenever the user clicks on an item in the DataView. This tells the info panel in the east region to
  146. * display the details of the image that was clicked on
  147. */
  148. onIconSelect: function(dataview, selections) {
  149. var selected = selections[0];
  150. if (selected) {
  151. this.down('infopanel').loadRecord(selected);
  152. }
  153. },
  154. /**
  155. * Fires the 'selected' event, informing other components that an image has been selected
  156. */
  157. fireImageSelected: function() {
  158. var selectedImage = this.down('iconbrowser').selModel.getSelection()[0];
  159. if (selectedImage) {
  160. this.fireEvent('selected', selectedImage);
  161. this.hide();
  162. }
  163. }
  164. });