3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js"><div id="cls-Ext.ux.Spinner"></div>/**
\r
9 * @class Ext.ux.Spinner
\r
10 * @extends Ext.util.Observable
\r
11 * Creates a Spinner control utilized by Ext.ux.form.SpinnerField
\r
13 Ext.ux.Spinner = Ext.extend(Ext.util.Observable, {
\r
15 alternateIncrementValue: 5,
\r
16 triggerClass: 'x-form-spinner-trigger',
\r
17 splitterClass: 'x-form-spinner-splitter',
\r
18 alternateKey: Ext.EventObject.shiftKey,
\r
22 constructor: function(config){
\r
23 Ext.ux.Spinner.superclass.constructor.call(this, config);
\r
24 Ext.apply(this, config);
\r
25 this.mimicing = false;
\r
28 init: function(field){
\r
31 field.afterMethod('onRender', this.doRender, this);
\r
32 field.afterMethod('onEnable', this.doEnable, this);
\r
33 field.afterMethod('onDisable', this.doDisable, this);
\r
34 field.afterMethod('afterRender', this.doAfterRender, this);
\r
35 field.afterMethod('onResize', this.doResize, this);
\r
36 field.afterMethod('onFocus', this.doFocus, this);
\r
37 field.beforeMethod('onDestroy', this.doDestroy, this);
\r
40 doRender: function(ct, position){
\r
41 var el = this.el = this.field.getEl();
\r
45 f.wrap = this.wrap = el.wrap({
\r
46 cls: "x-form-field-wrap"
\r
50 this.wrap = f.wrap.addClass('x-form-field-wrap');
\r
53 this.trigger = this.wrap.createChild({
\r
55 src: Ext.BLANK_IMAGE_URL,
\r
56 cls: "x-form-trigger " + this.triggerClass
\r
60 this.wrap.setWidth(el.getWidth() + this.trigger.getWidth());
\r
63 this.splitter = this.wrap.createChild({
\r
65 cls: this.splitterClass,
\r
66 style: 'width:13px; height:2px;'
\r
68 this.splitter.setRight((Ext.isIE) ? 1 : 2).setTop(10).show();
\r
70 this.proxy = this.trigger.createProxy('', this.splitter, true);
\r
71 this.proxy.addClass("x-form-spinner-proxy");
\r
72 this.proxy.setStyle('left', '0px');
\r
73 this.proxy.setSize(14, 1);
\r
75 this.dd = new Ext.dd.DDProxy(this.splitter.dom.id, "SpinnerDrag", {
\r
76 dragElId: this.proxy.id
\r
83 doAfterRender: function(){
\r
85 if (Ext.isIE && this.el.getY() != (y = this.trigger.getY())) {
\r
91 doEnable: function(){
\r
93 this.wrap.removeClass(this.field.disabledClass);
\r
97 doDisable: function(){
\r
99 this.wrap.addClass(this.field.disabledClass);
\r
100 this.el.removeClass(this.field.disabledClass);
\r
104 doResize: function(w, h){
\r
105 if (typeof w == 'number') {
\r
106 this.el.setWidth(w - this.trigger.getWidth());
\r
108 this.wrap.setWidth(this.el.getWidth() + this.trigger.getWidth());
\r
111 doFocus: function(){
\r
112 if (!this.mimicing) {
\r
113 this.wrap.addClass('x-trigger-wrap-focus');
\r
114 this.mimicing = true;
\r
115 Ext.get(Ext.isIE ? document.body : document).on("mousedown", this.mimicBlur, this, {
\r
118 this.el.on('keydown', this.checkTab, this);
\r
123 checkTab: function(e){
\r
124 if (e.getKey() == e.TAB) {
\r
125 this.triggerBlur();
\r
130 mimicBlur: function(e){
\r
131 if (!this.wrap.contains(e.target) && this.field.validateBlur(e)) {
\r
132 this.triggerBlur();
\r
137 triggerBlur: function(){
\r
138 this.mimicing = false;
\r
139 Ext.get(Ext.isIE ? document.body : document).un("mousedown", this.mimicBlur, this);
\r
140 this.el.un("keydown", this.checkTab, this);
\r
141 this.field.beforeBlur();
\r
142 this.wrap.removeClass('x-trigger-wrap-focus');
\r
143 this.field.onBlur.call(this.field);
\r
146 initTrigger: function(){
\r
147 this.trigger.addClassOnOver('x-form-trigger-over');
\r
148 this.trigger.addClassOnClick('x-form-trigger-click');
\r
151 initSpinner: function(){
\r
152 this.field.addEvents({
\r
158 this.keyNav = new Ext.KeyNav(this.el, {
\r
160 e.preventDefault();
\r
164 "down": function(e){
\r
165 e.preventDefault();
\r
169 "pageUp": function(e){
\r
170 e.preventDefault();
\r
171 this.onSpinUpAlternate();
\r
174 "pageDown": function(e){
\r
175 e.preventDefault();
\r
176 this.onSpinDownAlternate();
\r
182 this.repeater = new Ext.util.ClickRepeater(this.trigger, {
\r
183 accelerate: this.accelerate
\r
185 this.field.mon(this.repeater, "click", this.onTriggerClick, this, {
\r
186 preventDefault: true
\r
189 this.field.mon(this.trigger, {
\r
190 mouseover: this.onMouseOver,
\r
191 mouseout: this.onMouseOut,
\r
192 mousemove: this.onMouseMove,
\r
193 mousedown: this.onMouseDown,
\r
194 mouseup: this.onMouseUp,
\r
196 preventDefault: true
\r
199 this.field.mon(this.wrap, "mousewheel", this.handleMouseWheel, this);
\r
201 this.dd.setXConstraint(0, 0, 10)
\r
202 this.dd.setYConstraint(1500, 1500, 10);
\r
203 this.dd.endDrag = this.endDrag.createDelegate(this);
\r
204 this.dd.startDrag = this.startDrag.createDelegate(this);
\r
205 this.dd.onDrag = this.onDrag.createDelegate(this);
\r
208 onMouseOver: function(){
\r
209 if (this.disabled) {
\r
212 var middle = this.getMiddle();
\r
213 this.tmpHoverClass = (Ext.EventObject.getPageY() < middle) ? 'x-form-spinner-overup' : 'x-form-spinner-overdown';
\r
214 this.trigger.addClass(this.tmpHoverClass);
\r
218 onMouseOut: function(){
\r
219 this.trigger.removeClass(this.tmpHoverClass);
\r
223 onMouseMove: function(){
\r
224 if (this.disabled) {
\r
227 var middle = this.getMiddle();
\r
228 if (((Ext.EventObject.getPageY() > middle) && this.tmpHoverClass == "x-form-spinner-overup") ||
\r
229 ((Ext.EventObject.getPageY() < middle) && this.tmpHoverClass == "x-form-spinner-overdown")) {
\r
234 onMouseDown: function(){
\r
235 if (this.disabled) {
\r
238 var middle = this.getMiddle();
\r
239 this.tmpClickClass = (Ext.EventObject.getPageY() < middle) ? 'x-form-spinner-clickup' : 'x-form-spinner-clickdown';
\r
240 this.trigger.addClass(this.tmpClickClass);
\r
244 onMouseUp: function(){
\r
245 this.trigger.removeClass(this.tmpClickClass);
\r
249 onTriggerClick: function(){
\r
250 if (this.disabled || this.el.dom.readOnly) {
\r
253 var middle = this.getMiddle();
\r
254 var ud = (Ext.EventObject.getPageY() < middle) ? 'Up' : 'Down';
\r
255 this['onSpin' + ud]();
\r
259 getMiddle: function(){
\r
260 var t = this.trigger.getTop();
\r
261 var h = this.trigger.getHeight();
\r
262 var middle = t + (h / 2);
\r
267 //checks if control is allowed to spin
\r
268 isSpinnable: function(){
\r
269 if (this.disabled || this.el.dom.readOnly) {
\r
270 Ext.EventObject.preventDefault(); //prevent scrolling when disabled/readonly
\r
276 handleMouseWheel: function(e){
\r
277 //disable scrolling when not focused
\r
278 if (this.wrap.hasClass('x-trigger-wrap-focus') == false) {
\r
282 var delta = e.getWheelDelta();
\r
295 startDrag: function(){
\r
297 this._previousY = Ext.fly(this.dd.getDragEl()).getTop();
\r
301 endDrag: function(){
\r
306 onDrag: function(){
\r
307 if (this.disabled) {
\r
310 var y = Ext.fly(this.dd.getDragEl()).getTop();
\r
313 if (this._previousY > y) {
\r
316 if (this._previousY < y) {
\r
320 this['onSpin' + ud]();
\r
323 this._previousY = y;
\r
327 onSpinUp: function(){
\r
328 if (this.isSpinnable() == false) {
\r
331 if (Ext.EventObject.shiftKey == true) {
\r
332 this.onSpinUpAlternate();
\r
336 this.spin(false, false);
\r
338 this.field.fireEvent("spin", this);
\r
339 this.field.fireEvent("spinup", this);
\r
343 onSpinDown: function(){
\r
344 if (this.isSpinnable() == false) {
\r
347 if (Ext.EventObject.shiftKey == true) {
\r
348 this.onSpinDownAlternate();
\r
352 this.spin(true, false);
\r
354 this.field.fireEvent("spin", this);
\r
355 this.field.fireEvent("spindown", this);
\r
359 onSpinUpAlternate: function(){
\r
360 if (this.isSpinnable() == false) {
\r
363 this.spin(false, true);
\r
364 this.field.fireEvent("spin", this);
\r
365 this.field.fireEvent("spinup", this);
\r
369 onSpinDownAlternate: function(){
\r
370 if (this.isSpinnable() == false) {
\r
373 this.spin(true, true);
\r
374 this.field.fireEvent("spin", this);
\r
375 this.field.fireEvent("spindown", this);
\r
378 spin: function(down, alternate){
\r
379 var v = parseFloat(this.field.getValue());
\r
380 var incr = (alternate == true) ? this.alternateIncrementValue : this.incrementValue;
\r
381 (down == true) ? v -= incr : v += incr;
\r
383 v = (isNaN(v)) ? this.defaultValue : v;
\r
384 v = this.fixBoundries(v);
\r
385 this.field.setRawValue(v);
\r
388 fixBoundries: function(value){
\r
391 if (this.field.minValue != undefined && v < this.field.minValue) {
\r
392 v = this.field.minValue;
\r
394 if (this.field.maxValue != undefined && v > this.field.maxValue) {
\r
395 v = this.field.maxValue;
\r
398 return this.fixPrecision(v);
\r
402 fixPrecision: function(value){
\r
403 var nan = isNaN(value);
\r
404 if (!this.field.allowDecimals || this.field.decimalPrecision == -1 || nan || !value) {
\r
405 return nan ? '' : value;
\r
407 return parseFloat(parseFloat(value).toFixed(this.field.decimalPrecision));
\r
410 doDestroy: function(){
\r
411 if (this.trigger) {
\r
412 this.trigger.remove();
\r
415 this.wrap.remove();
\r
416 delete this.field.wrap;
\r
419 if (this.splitter) {
\r
420 this.splitter.remove();
\r
429 this.proxy.remove();
\r
432 if (this.repeater) {
\r
433 this.repeater.purgeListeners();
\r
439 Ext.form.Spinner = Ext.ux.Spinner;</pre>