TreePicker.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ux-TreePicker'>/**
  19. </span> * @class Ext.ux.TreePicker
  20. * @extends Ext.form.field.Picker
  21. *
  22. * A Picker field that contains a tree panel on its popup, enabling selection of tree nodes.
  23. */
  24. Ext.define('Ext.ux.TreePicker', {
  25. extend: 'Ext.form.field.Picker',
  26. xtype: 'treepicker',
  27. triggerCls: Ext.baseCSSPrefix + 'form-arrow-trigger',
  28. config: {
  29. <span id='Ext-ux-TreePicker-cfg-store'> /**
  30. </span> * @cfg {Ext.data.TreeStore} store
  31. * A tree store that the tree picker will be bound to
  32. */
  33. store: null,
  34. <span id='Ext-ux-TreePicker-cfg-displayField'> /**
  35. </span> * @cfg {String} displayField
  36. * The field inside the model that will be used as the node's text.
  37. * Defaults to the default value of {@link Ext.tree.Panel}'s `displayField` configuration.
  38. */
  39. displayField: null,
  40. <span id='Ext-ux-TreePicker-cfg-columns'> /**
  41. </span> * @cfg {Array} columns
  42. * An optional array of columns for multi-column trees
  43. */
  44. columns: null,
  45. <span id='Ext-ux-TreePicker-cfg-selectOnTab'> /**
  46. </span> * @cfg {Boolean} selectOnTab
  47. * Whether the Tab key should select the currently highlighted item. Defaults to `true`.
  48. */
  49. selectOnTab: true,
  50. <span id='Ext-ux-TreePicker-cfg-maxPickerHeight'> /**
  51. </span> * @cfg {Number} maxPickerHeight
  52. * The maximum height of the tree dropdown. Defaults to 300.
  53. */
  54. maxPickerHeight: 300,
  55. <span id='Ext-ux-TreePicker-cfg-minPickerHeight'> /**
  56. </span> * @cfg {Number} minPickerHeight
  57. * The minimum height of the tree dropdown. Defaults to 100.
  58. */
  59. minPickerHeight: 100
  60. },
  61. editable: false,
  62. initComponent: function() {
  63. var me = this;
  64. me.callParent(arguments);
  65. this.addEvents(
  66. <span id='Ext-ux-TreePicker-event-select'> /**
  67. </span> * @event select
  68. * Fires when a tree node is selected
  69. * @param {Ext.ux.TreePicker} picker This tree picker
  70. * @param {Ext.data.Model} record The selected record
  71. */
  72. 'select'
  73. );
  74. me.store.on('load', me.onLoad, me);
  75. },
  76. <span id='Ext-ux-TreePicker-method-createPicker'> /**
  77. </span> * Creates and returns the tree panel to be used as this field's picker.
  78. * @private
  79. */
  80. createPicker: function() {
  81. var me = this,
  82. picker = Ext.create('Ext.tree.Panel', {
  83. store: me.store,
  84. floating: true,
  85. hidden: true,
  86. displayField: me.displayField,
  87. columns: me.columns,
  88. maxHeight: me.maxTreeHeight,
  89. shadow: false,
  90. manageHeight: false,
  91. listeners: {
  92. itemclick: Ext.bind(me.onItemClick, me)
  93. },
  94. viewConfig: {
  95. listeners: {
  96. render: function(view) {
  97. view.getEl().on('keypress', me.onPickerKeypress, me);
  98. }
  99. }
  100. }
  101. }),
  102. view = picker.getView();
  103. view.on('render', me.setPickerViewStyles, me);
  104. if (Ext.isIE9 &amp;&amp; Ext.isStrict) {
  105. // In IE9 strict mode, the tree view grows by the height of the horizontal scroll bar when the items are highlighted or unhighlighted.
  106. // Also when items are collapsed or expanded the height of the view is off. Forcing a repaint fixes the problem.
  107. view.on('highlightitem', me.repaintPickerView, me);
  108. view.on('unhighlightitem', me.repaintPickerView, me);
  109. view.on('afteritemexpand', me.repaintPickerView, me);
  110. view.on('afteritemcollapse', me.repaintPickerView, me);
  111. }
  112. return picker;
  113. },
  114. <span id='Ext-ux-TreePicker-method-setPickerViewStyles'> /**
  115. </span> * Sets min/max height styles on the tree picker's view element after it is rendered.
  116. * @param {Ext.tree.View} view
  117. * @private
  118. */
  119. setPickerViewStyles: function(view) {
  120. view.getEl().setStyle({
  121. 'min-height': this.minPickerHeight + 'px',
  122. 'max-height': this.maxPickerHeight + 'px'
  123. });
  124. },
  125. <span id='Ext-ux-TreePicker-method-repaintPickerView'> /**
  126. </span> * repaints the tree view
  127. */
  128. repaintPickerView: function() {
  129. var style = this.picker.getView().getEl().dom.style;
  130. // can't use Element.repaint because it contains a setTimeout, which results in a flicker effect
  131. style.display = style.display;
  132. },
  133. <span id='Ext-ux-TreePicker-method-alignPicker'> /**
  134. </span> * Aligns the picker to the input element
  135. * @private
  136. */
  137. alignPicker: function() {
  138. var me = this,
  139. picker;
  140. if (me.isExpanded) {
  141. picker = me.getPicker();
  142. if (me.matchFieldWidth) {
  143. // Auto the height (it will be constrained by max height)
  144. picker.setWidth(me.bodyEl.getWidth());
  145. }
  146. if (picker.isFloating()) {
  147. me.doAlign();
  148. }
  149. }
  150. },
  151. <span id='Ext-ux-TreePicker-method-onItemClick'> /**
  152. </span> * Handles a click even on a tree node
  153. * @private
  154. * @param {Ext.tree.View} view
  155. * @param {Ext.data.Model} record
  156. * @param {HTMLElement} node
  157. * @param {Number} rowIndex
  158. * @param {Ext.EventObject} e
  159. */
  160. onItemClick: function(view, record, node, rowIndex, e) {
  161. this.selectItem(record);
  162. },
  163. <span id='Ext-ux-TreePicker-method-onPickerKeypress'> /**
  164. </span> * Handles a keypress event on the picker element
  165. * @private
  166. * @param {Ext.EventObject} e
  167. * @param {HTMLElement} el
  168. */
  169. onPickerKeypress: function(e, el) {
  170. var key = e.getKey();
  171. if(key === e.ENTER || (key === e.TAB &amp;&amp; this.selectOnTab)) {
  172. this.selectItem(this.picker.getSelectionModel().getSelection()[0]);
  173. }
  174. },
  175. <span id='Ext-ux-TreePicker-method-selectItem'> /**
  176. </span> * Changes the selection to a given record and closes the picker
  177. * @private
  178. * @param {Ext.data.Model} record
  179. */
  180. selectItem: function(record) {
  181. var me = this;
  182. me.setValue(record.get('id'));
  183. me.picker.hide();
  184. me.inputEl.focus();
  185. me.fireEvent('select', me, record)
  186. },
  187. <span id='Ext-ux-TreePicker-method-onExpand'> /**
  188. </span> * Runs when the picker is expanded. Selects the appropriate tree node based on the value of the input element,
  189. * and focuses the picker so that keyboard navigation will work.
  190. * @private
  191. */
  192. onExpand: function() {
  193. var me = this,
  194. picker = me.picker,
  195. store = picker.store,
  196. value = me.value;
  197. if(value) {
  198. picker.selectPath(store.getNodeById(value).getPath());
  199. } else {
  200. picker.getSelectionModel().select(store.getRootNode());
  201. }
  202. Ext.defer(function() {
  203. picker.getView().focus();
  204. }, 1);
  205. },
  206. <span id='Ext-ux-TreePicker-method-setValue'> /**
  207. </span> * Sets the specified value into the field
  208. * @param {Mixed} value
  209. * @return {Ext.ux.TreePicker} this
  210. */
  211. setValue: function(value) {
  212. var me = this,
  213. record;
  214. me.value = value;
  215. if (me.store.loading) {
  216. // Called while the Store is loading. Ensure it is processed by the onLoad method.
  217. return me;
  218. }
  219. // try to find a record in the store that matches the value
  220. record = value ? me.store.getNodeById(value) : me.store.getRootNode();
  221. // set the raw value to the record's display field if a record was found
  222. me.setRawValue(record ? record.get(this.displayField) : '');
  223. return me;
  224. },
  225. <span id='Ext-ux-TreePicker-method-getValue'> /**
  226. </span> * Returns the current data value of the field (the idProperty of the record)
  227. * @return {Number}
  228. */
  229. getValue: function() {
  230. return this.value;
  231. },
  232. <span id='Ext-ux-TreePicker-method-onLoad'> /**
  233. </span> * Handles the store's load event.
  234. * @private
  235. */
  236. onLoad: function() {
  237. var value = this.value;
  238. if (value) {
  239. this.setValue(value);
  240. }
  241. }
  242. });
  243. </pre>
  244. </body>
  245. </html>