source: Dev/branches/rest-dojo-ui/client/dijit/form/NumberSpinner.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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