1 | define([ |
---|
2 | "dojo/_base/declare", // declare |
---|
3 | "dojo/keys", // keys.END keys.HOME |
---|
4 | "./_Spinner", |
---|
5 | "./NumberTextBox" |
---|
6 | ], function(declare, keys, _Spinner, NumberTextBox){ |
---|
7 | |
---|
8 | // module: |
---|
9 | // dijit/form/NumberSpinner |
---|
10 | |
---|
11 | return declare("dijit.form.NumberSpinner", [_Spinner, NumberTextBox.Mixin], { |
---|
12 | // summary: |
---|
13 | // Extends NumberTextBox to add up/down arrows and pageup/pagedown for incremental change to the value |
---|
14 | // |
---|
15 | // description: |
---|
16 | // A `dijit/form/NumberTextBox` extension to provide keyboard accessible value selection |
---|
17 | // as well as icons for spinning direction. When using the keyboard, the typematic rules |
---|
18 | // apply, meaning holding the key will gradually increase or decrease the value and |
---|
19 | // accelerate. |
---|
20 | // |
---|
21 | // example: |
---|
22 | // | new NumberSpinner({ constraints:{ max:300, min:100 }}, "someInput"); |
---|
23 | |
---|
24 | baseClass: "dijitTextBox dijitSpinner dijitNumberTextBox", |
---|
25 | |
---|
26 | adjust: function(/*Object*/ val, /*Number*/ delta){ |
---|
27 | // summary: |
---|
28 | // Change Number val by the given amount |
---|
29 | // tags: |
---|
30 | // protected |
---|
31 | |
---|
32 | var tc = this.constraints, |
---|
33 | v = isNaN(val), |
---|
34 | gotMax = !isNaN(tc.max), |
---|
35 | gotMin = !isNaN(tc.min) |
---|
36 | ; |
---|
37 | if(v && delta != 0){ // blank or invalid value and they want to spin, so create defaults |
---|
38 | val = (delta > 0) ? |
---|
39 | gotMin ? tc.min : gotMax ? tc.max : 0 : |
---|
40 | gotMax ? this.constraints.max : gotMin ? tc.min : 0 |
---|
41 | ; |
---|
42 | } |
---|
43 | var newval = val + delta; |
---|
44 | if(v || isNaN(newval)){ |
---|
45 | return val; |
---|
46 | } |
---|
47 | if(gotMax && (newval > tc.max)){ |
---|
48 | newval = tc.max; |
---|
49 | } |
---|
50 | if(gotMin && (newval < tc.min)){ |
---|
51 | newval = tc.min; |
---|
52 | } |
---|
53 | return newval; |
---|
54 | }, |
---|
55 | |
---|
56 | _onKeyDown: function(e){ |
---|
57 | if(this.disabled || this.readOnly){ |
---|
58 | return; |
---|
59 | } |
---|
60 | if((e.keyCode == keys.HOME || e.keyCode == keys.END) && !(e.ctrlKey || e.altKey || e.metaKey) |
---|
61 | && typeof this.get('value') != 'undefined' /* gibberish, so HOME and END are default editing keys*/){ |
---|
62 | var value = this.constraints[(e.keyCode == keys.HOME ? "min" : "max")]; |
---|
63 | if(typeof value == "number"){ |
---|
64 | this._setValueAttr(value, false); |
---|
65 | } |
---|
66 | // eat home or end key whether we change the value or not |
---|
67 | e.stopPropagation(); |
---|
68 | e.preventDefault(); |
---|
69 | } |
---|
70 | } |
---|
71 | }); |
---|
72 | }); |
---|