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