Spinner.js
10.2 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* A field with a pair of up/down spinner buttons. This class is not normally instantiated directly,
* instead it is subclassed and the {@link #onSpinUp} and {@link #onSpinDown} methods are implemented
* to handle when the buttons are clicked. A good example of this is the {@link Ext.form.field.Number}
* field which uses the spinner to increment and decrement the field's value by its
* {@link Ext.form.field.Number#step step} config value.
*
* For example:
*
* @example
* Ext.define('Ext.ux.CustomSpinner', {
* extend: 'Ext.form.field.Spinner',
* alias: 'widget.customspinner',
*
* // override onSpinUp (using step isn't neccessary)
* onSpinUp: function() {
* var me = this;
* if (!me.readOnly) {
* var val = parseInt(me.getValue().split(' '), 10)||0; // gets rid of " Pack", defaults to zero on parse failure
* me.setValue((val + me.step) + ' Pack');
* }
* },
*
* // override onSpinDown
* onSpinDown: function() {
* var val, me = this;
* if (!me.readOnly) {
* var val = parseInt(me.getValue().split(' '), 10)||0; // gets rid of " Pack", defaults to zero on parse failure
* if (val <= me.step) {
* me.setValue('Dry!');
* } else {
* me.setValue((val - me.step) + ' Pack');
* }
* }
* }
* });
*
* Ext.create('Ext.form.FormPanel', {
* title: 'Form with SpinnerField',
* bodyPadding: 5,
* width: 350,
* renderTo: Ext.getBody(),
* items:[{
* xtype: 'customspinner',
* fieldLabel: 'How Much Beer?',
* step: 6
* }]
* });
*
* By default, pressing the up and down arrow keys will also trigger the onSpinUp and onSpinDown methods;
* to prevent this, set `{@link #keyNavEnabled} = false`.
*/
Ext.define('Ext.form.field.Spinner', {
extend: 'Ext.form.field.Trigger',
alias: 'widget.spinnerfield',
alternateClassName: 'Ext.form.Spinner',
requires: ['Ext.util.KeyNav'],
trigger1Cls: Ext.baseCSSPrefix + 'form-spinner-up',
trigger2Cls: Ext.baseCSSPrefix + 'form-spinner-down',
/**
* @cfg {Boolean} spinUpEnabled
* Specifies whether the up spinner button is enabled. Defaults to true. To change this after the component is
* created, use the {@link #setSpinUpEnabled} method.
*/
spinUpEnabled: true,
/**
* @cfg {Boolean} spinDownEnabled
* Specifies whether the down spinner button is enabled. Defaults to true. To change this after the component is
* created, use the {@link #setSpinDownEnabled} method.
*/
spinDownEnabled: true,
/**
* @cfg {Boolean} keyNavEnabled
* Specifies whether the up and down arrow keys should trigger spinning up and down. Defaults to true.
*/
keyNavEnabled: true,
/**
* @cfg {Boolean} mouseWheelEnabled
* Specifies whether the mouse wheel should trigger spinning up and down while the field has focus.
* Defaults to true.
*/
mouseWheelEnabled: true,
/**
* @cfg {Boolean} repeatTriggerClick
* Whether a {@link Ext.util.ClickRepeater click repeater} should be attached to the spinner buttons.
* Defaults to true.
*/
repeatTriggerClick: true,
/**
* @method
* @protected
* This method is called when the spinner up button is clicked, or when the up arrow key is pressed if
* {@link #keyNavEnabled} is true. Must be implemented by subclasses.
*/
onSpinUp: Ext.emptyFn,
/**
* @method
* @protected
* This method is called when the spinner down button is clicked, or when the down arrow key is pressed if
* {@link #keyNavEnabled} is true. Must be implemented by subclasses.
*/
onSpinDown: Ext.emptyFn,
triggerTpl: '<td style="{triggerStyle}">' +
'<div class="' + Ext.baseCSSPrefix + 'trigger-index-0 ' + Ext.baseCSSPrefix + 'form-trigger ' + Ext.baseCSSPrefix + 'form-spinner-up" role="button"></div>' +
'<div class="' + Ext.baseCSSPrefix + 'trigger-index-1 ' + Ext.baseCSSPrefix + 'form-trigger ' + Ext.baseCSSPrefix + 'form-spinner-down" role="button"></div>' +
'</td>' +
'</tr>',
initComponent: function() {
this.callParent();
this.addEvents(
/**
* @event spin
* Fires when the spinner is made to spin up or down.
* @param {Ext.form.field.Spinner} this
* @param {String} direction Either 'up' if spinning up, or 'down' if spinning down.
*/
'spin',
/**
* @event spinup
* Fires when the spinner is made to spin up.
* @param {Ext.form.field.Spinner} this
*/
'spinup',
/**
* @event spindown
* Fires when the spinner is made to spin down.
* @param {Ext.form.field.Spinner} this
*/
'spindown'
);
},
/**
* @private
* Override.
*/
onRender: function() {
var me = this,
triggers;
me.callParent(arguments);
triggers = me.triggerEl;
/**
* @property {Ext.Element} spinUpEl
* The spinner up button element
*/
me.spinUpEl = triggers.item(0);
/**
* @property {Ext.Element} spinDownEl
* The spinner down button element
*/
me.spinDownEl = triggers.item(1);
me.triggerCell = me.spinUpEl.parent();
// Set initial enabled/disabled states
me.setSpinUpEnabled(me.spinUpEnabled);
me.setSpinDownEnabled(me.spinDownEnabled);
// Init up/down arrow keys
if (me.keyNavEnabled) {
me.spinnerKeyNav = new Ext.util.KeyNav(me.inputEl, {
scope: me,
up: me.spinUp,
down: me.spinDown
});
}
// Init mouse wheel
if (me.mouseWheelEnabled) {
me.mon(me.bodyEl, 'mousewheel', me.onMouseWheel, me);
}
},
getSubTplMarkup: function() {
var me = this,
field = Ext.form.field.Base.prototype.getSubTplMarkup.apply(me, arguments);
return '<table id="' + me.id + '-triggerWrap" class="' + Ext.baseCSSPrefix + 'form-trigger-wrap" cellpadding="0" cellspacing="0">' +
'<tbody>' +
'<tr><td id="' + me.id + '-inputCell" class="' + Ext.baseCSSPrefix + 'form-trigger-input-cell">' + field + '</td>' +
me.getTriggerMarkup() +
'</tbody></table>';
},
getTriggerMarkup: function() {
var me = this,
hideTrigger = (me.readOnly || me.hideTrigger);
return me.getTpl('triggerTpl').apply({
triggerStyle: 'width:' + me.triggerWidth + (hideTrigger ? 'px;display:none' : 'px')
});
},
/**
* Get the total width of the spinner button area.
* @return {Number} The total spinner button width
*/
getTriggerWidth: function() {
var me = this,
totalTriggerWidth = 0;
if (me.triggerWrap && !me.hideTrigger && !me.readOnly) {
totalTriggerWidth = me.triggerWidth;
}
return totalTriggerWidth;
},
/**
* @private
* Handles the spinner up button clicks.
*/
onTrigger1Click: function() {
this.spinUp();
},
/**
* @private
* Handles the spinner down button clicks.
*/
onTrigger2Click: function() {
this.spinDown();
},
// private
// Handle trigger mouse up gesture; refocuses the input element upon end of spin.
onTriggerWrapMouseup: function() {
this.inputEl.focus();
},
/**
* Triggers the spinner to step up; fires the {@link #spin} and {@link #spinup} events and calls the
* {@link #onSpinUp} method. Does nothing if the field is {@link #disabled} or if {@link #spinUpEnabled}
* is false.
*/
spinUp: function() {
var me = this;
if (me.spinUpEnabled && !me.disabled) {
me.fireEvent('spin', me, 'up');
me.fireEvent('spinup', me);
me.onSpinUp();
}
},
/**
* Triggers the spinner to step down; fires the {@link #spin} and {@link #spindown} events and calls the
* {@link #onSpinDown} method. Does nothing if the field is {@link #disabled} or if {@link #spinDownEnabled}
* is false.
*/
spinDown: function() {
var me = this;
if (me.spinDownEnabled && !me.disabled) {
me.fireEvent('spin', me, 'down');
me.fireEvent('spindown', me);
me.onSpinDown();
}
},
/**
* Sets whether the spinner up button is enabled.
* @param {Boolean} enabled true to enable the button, false to disable it.
*/
setSpinUpEnabled: function(enabled) {
var me = this,
wasEnabled = me.spinUpEnabled;
me.spinUpEnabled = enabled;
if (wasEnabled !== enabled && me.rendered) {
me.spinUpEl[enabled ? 'removeCls' : 'addCls'](me.trigger1Cls + '-disabled');
}
},
/**
* Sets whether the spinner down button is enabled.
* @param {Boolean} enabled true to enable the button, false to disable it.
*/
setSpinDownEnabled: function(enabled) {
var me = this,
wasEnabled = me.spinDownEnabled;
me.spinDownEnabled = enabled;
if (wasEnabled !== enabled && me.rendered) {
me.spinDownEl[enabled ? 'removeCls' : 'addCls'](me.trigger2Cls + '-disabled');
}
},
/**
* @private
* Handles mousewheel events on the field
*/
onMouseWheel: function(e) {
var me = this,
delta;
if (me.hasFocus) {
delta = e.getWheelDelta();
if (delta > 0) {
me.spinUp();
}
else if (delta < 0) {
me.spinDown();
}
e.stopEvent();
}
},
onDestroy: function() {
Ext.destroyMembers(this, 'spinnerKeyNav', 'spinUpEl', 'spinDownEl');
this.callParent();
}
});