3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.ux.Spinner
9 * @extends Ext.util.Observable
10 * Creates a Spinner control utilized by Ext.ux.form.SpinnerField
12 Ext.ux.Spinner = Ext.extend(Ext.util.Observable, {
14 alternateIncrementValue: 5,
15 triggerClass: 'x-form-spinner-trigger',
16 splitterClass: 'x-form-spinner-splitter',
17 alternateKey: Ext.EventObject.shiftKey,
21 constructor: function(config){
22 Ext.ux.Spinner.superclass.constructor.call(this, config);
23 Ext.apply(this, config);
24 this.mimicing = false;
27 init: function(field){
30 field.afterMethod('onRender', this.doRender, this);
31 field.afterMethod('onEnable', this.doEnable, this);
32 field.afterMethod('onDisable', this.doDisable, this);
33 field.afterMethod('afterRender', this.doAfterRender, this);
34 field.afterMethod('onResize', this.doResize, this);
35 field.afterMethod('onFocus', this.doFocus, this);
36 field.beforeMethod('onDestroy', this.doDestroy, this);
39 doRender: function(ct, position){
40 var el = this.el = this.field.getEl();
44 f.wrap = this.wrap = el.wrap({
45 cls: "x-form-field-wrap"
49 this.wrap = f.wrap.addClass('x-form-field-wrap');
52 this.trigger = this.wrap.createChild({
54 src: Ext.BLANK_IMAGE_URL,
55 cls: "x-form-trigger " + this.triggerClass
59 this.wrap.setWidth(el.getWidth() + this.trigger.getWidth());
62 this.splitter = this.wrap.createChild({
64 cls: this.splitterClass,
65 style: 'width:13px; height:2px;'
67 this.splitter.setRight((Ext.isIE) ? 1 : 2).setTop(10).show();
69 this.proxy = this.trigger.createProxy('', this.splitter, true);
70 this.proxy.addClass("x-form-spinner-proxy");
71 this.proxy.setStyle('left', '0px');
72 this.proxy.setSize(14, 1);
74 this.dd = new Ext.dd.DDProxy(this.splitter.dom.id, "SpinnerDrag", {
75 dragElId: this.proxy.id
82 doAfterRender: function(){
84 if (Ext.isIE && this.el.getY() != (y = this.trigger.getY())) {
92 this.wrap.removeClass(this.field.disabledClass);
96 doDisable: function(){
98 this.wrap.addClass(this.field.disabledClass);
99 this.el.removeClass(this.field.disabledClass);
103 doResize: function(w, h){
104 if (typeof w == 'number') {
105 this.el.setWidth(w - this.trigger.getWidth());
107 this.wrap.setWidth(this.el.getWidth() + this.trigger.getWidth());
111 if (!this.mimicing) {
112 this.wrap.addClass('x-trigger-wrap-focus');
113 this.mimicing = true;
114 Ext.get(Ext.isIE ? document.body : document).on("mousedown", this.mimicBlur, this, {
117 this.el.on('keydown', this.checkTab, this);
122 checkTab: function(e){
123 if (e.getKey() == e.TAB) {
129 mimicBlur: function(e){
130 if (!this.wrap.contains(e.target) && this.field.validateBlur(e)) {
136 triggerBlur: function(){
137 this.mimicing = false;
138 Ext.get(Ext.isIE ? document.body : document).un("mousedown", this.mimicBlur, this);
139 this.el.un("keydown", this.checkTab, this);
140 this.field.beforeBlur();
141 this.wrap.removeClass('x-trigger-wrap-focus');
142 this.field.onBlur.call(this.field);
145 initTrigger: function(){
146 this.trigger.addClassOnOver('x-form-trigger-over');
147 this.trigger.addClassOnClick('x-form-trigger-click');
150 initSpinner: function(){
151 this.field.addEvents({
157 this.keyNav = new Ext.KeyNav(this.el, {
168 "pageUp": function(e){
170 this.onSpinUpAlternate();
173 "pageDown": function(e){
175 this.onSpinDownAlternate();
181 this.repeater = new Ext.util.ClickRepeater(this.trigger, {
182 accelerate: this.accelerate
184 this.field.mon(this.repeater, "click", this.onTriggerClick, this, {
188 this.field.mon(this.trigger, {
189 mouseover: this.onMouseOver,
190 mouseout: this.onMouseOut,
191 mousemove: this.onMouseMove,
192 mousedown: this.onMouseDown,
193 mouseup: this.onMouseUp,
198 this.field.mon(this.wrap, "mousewheel", this.handleMouseWheel, this);
200 this.dd.setXConstraint(0, 0, 10)
201 this.dd.setYConstraint(1500, 1500, 10);
202 this.dd.endDrag = this.endDrag.createDelegate(this);
203 this.dd.startDrag = this.startDrag.createDelegate(this);
204 this.dd.onDrag = this.onDrag.createDelegate(this);
207 onMouseOver: function(){
211 var middle = this.getMiddle();
212 this.tmpHoverClass = (Ext.EventObject.getPageY() < middle) ? 'x-form-spinner-overup' : 'x-form-spinner-overdown';
213 this.trigger.addClass(this.tmpHoverClass);
217 onMouseOut: function(){
218 this.trigger.removeClass(this.tmpHoverClass);
222 onMouseMove: function(){
226 var middle = this.getMiddle();
227 if (((Ext.EventObject.getPageY() > middle) && this.tmpHoverClass == "x-form-spinner-overup") ||
228 ((Ext.EventObject.getPageY() < middle) && this.tmpHoverClass == "x-form-spinner-overdown")) {
233 onMouseDown: function(){
237 var middle = this.getMiddle();
238 this.tmpClickClass = (Ext.EventObject.getPageY() < middle) ? 'x-form-spinner-clickup' : 'x-form-spinner-clickdown';
239 this.trigger.addClass(this.tmpClickClass);
243 onMouseUp: function(){
244 this.trigger.removeClass(this.tmpClickClass);
248 onTriggerClick: function(){
249 if (this.disabled || this.el.dom.readOnly) {
252 var middle = this.getMiddle();
253 var ud = (Ext.EventObject.getPageY() < middle) ? 'Up' : 'Down';
254 this['onSpin' + ud]();
258 getMiddle: function(){
259 var t = this.trigger.getTop();
260 var h = this.trigger.getHeight();
261 var middle = t + (h / 2);
266 //checks if control is allowed to spin
267 isSpinnable: function(){
268 if (this.disabled || this.el.dom.readOnly) {
269 Ext.EventObject.preventDefault(); //prevent scrolling when disabled/readonly
275 handleMouseWheel: function(e){
276 //disable scrolling when not focused
277 if (this.wrap.hasClass('x-trigger-wrap-focus') == false) {
281 var delta = e.getWheelDelta();
294 startDrag: function(){
296 this._previousY = Ext.fly(this.dd.getDragEl()).getTop();
309 var y = Ext.fly(this.dd.getDragEl()).getTop();
312 if (this._previousY > y) {
315 if (this._previousY < y) {
319 this['onSpin' + ud]();
326 onSpinUp: function(){
327 if (this.isSpinnable() == false) {
330 if (Ext.EventObject.shiftKey == true) {
331 this.onSpinUpAlternate();
335 this.spin(false, false);
337 this.field.fireEvent("spin", this);
338 this.field.fireEvent("spinup", this);
342 onSpinDown: function(){
343 if (this.isSpinnable() == false) {
346 if (Ext.EventObject.shiftKey == true) {
347 this.onSpinDownAlternate();
351 this.spin(true, false);
353 this.field.fireEvent("spin", this);
354 this.field.fireEvent("spindown", this);
358 onSpinUpAlternate: function(){
359 if (this.isSpinnable() == false) {
362 this.spin(false, true);
363 this.field.fireEvent("spin", this);
364 this.field.fireEvent("spinup", this);
368 onSpinDownAlternate: function(){
369 if (this.isSpinnable() == false) {
372 this.spin(true, true);
373 this.field.fireEvent("spin", this);
374 this.field.fireEvent("spindown", this);
377 spin: function(down, alternate){
378 var v = parseFloat(this.field.getValue());
379 var incr = (alternate == true) ? this.alternateIncrementValue : this.incrementValue;
380 (down == true) ? v -= incr : v += incr;
382 v = (isNaN(v)) ? this.defaultValue : v;
383 v = this.fixBoundries(v);
384 this.field.setRawValue(v);
387 fixBoundries: function(value){
390 if (this.field.minValue != undefined && v < this.field.minValue) {
391 v = this.field.minValue;
393 if (this.field.maxValue != undefined && v > this.field.maxValue) {
394 v = this.field.maxValue;
397 return this.fixPrecision(v);
401 fixPrecision: function(value){
402 var nan = isNaN(value);
403 if (!this.field.allowDecimals || this.field.decimalPrecision == -1 || nan || !value) {
404 return nan ? '' : value;
406 return parseFloat(parseFloat(value).toFixed(this.field.decimalPrecision));
409 doDestroy: function(){
411 this.trigger.remove();
415 delete this.field.wrap;
419 this.splitter.remove();
432 this.repeater.purgeListeners();
438 Ext.form.Spinner = Ext.ux.Spinner;