source: Dev/branches/rest-dojo-ui/client/dijit/form/RangeBoundTextBox.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: 4.6 KB
Line 
1define([
2        "dojo/_base/declare", // declare
3        "dojo/i18n", // i18n.getLocalization
4        "./MappedTextBox"
5], function(declare, i18n, MappedTextBox){
6
7/*=====
8        var MappedTextBox = dijit.form.MappedTextBox;
9=====*/
10
11        // module:
12        //              dijit/form/RangeBoundTextBox
13        // summary:
14        //              Base class for textbox form widgets which defines a range of valid values.
15
16        /*=====
17                dijit.form.RangeBoundTextBox.__Constraints = function(){
18                        // min: Number
19                        //              Minimum signed value.  Default is -Infinity
20                        // max: Number
21                        //              Maximum signed value.  Default is +Infinity
22                        this.min = min;
23                        this.max = max;
24                }
25        =====*/
26
27        return declare("dijit.form.RangeBoundTextBox", MappedTextBox, {
28                // summary:
29                //              Base class for textbox form widgets which defines a range of valid values.
30
31                // rangeMessage: String
32                //              The message to display if value is out-of-range
33                rangeMessage: "",
34
35                /*=====
36                // constraints: dijit.form.RangeBoundTextBox.__Constraints
37                constraints: {},
38                ======*/
39
40                rangeCheck: function(/*Number*/ primitive, /*dijit.form.RangeBoundTextBox.__Constraints*/ constraints){
41                        // summary:
42                        //              Overridable function used to validate the range of the numeric input value.
43                        // tags:
44                        //              protected
45                        return  ("min" in constraints? (this.compare(primitive,constraints.min) >= 0) : true) &&
46                                ("max" in constraints? (this.compare(primitive,constraints.max) <= 0) : true); // Boolean
47                },
48
49                isInRange: function(/*Boolean*/ /*===== isFocused =====*/){
50                        // summary:
51                        //              Tests if the value is in the min/max range specified in constraints
52                        // tags:
53                        //              protected
54                        return this.rangeCheck(this.get('value'), this.constraints);
55                },
56
57                _isDefinitelyOutOfRange: function(){
58                        // summary:
59                        //              Returns true if the value is out of range and will remain
60                        //              out of range even if the user types more characters
61                        var val = this.get('value');
62                        var isTooLittle = false;
63                        var isTooMuch = false;
64                        if("min" in this.constraints){
65                                var min = this.constraints.min;
66                                min = this.compare(val, ((typeof min == "number") && min >= 0 && val !=0) ? 0 : min);
67                                isTooLittle = (typeof min == "number") && min < 0;
68                        }
69                        if("max" in this.constraints){
70                                var max = this.constraints.max;
71                                max = this.compare(val, ((typeof max != "number") || max > 0) ? max : 0);
72                                isTooMuch = (typeof max == "number") && max > 0;
73                        }
74                        return isTooLittle || isTooMuch;
75                },
76
77                _isValidSubset: function(){
78                        // summary:
79                        //              Overrides `dijit.form.ValidationTextBox._isValidSubset`.
80                        //              Returns true if the input is syntactically valid, and either within
81                        //              range or could be made in range by more typing.
82                        return this.inherited(arguments) && !this._isDefinitelyOutOfRange();
83                },
84
85                isValid: function(/*Boolean*/ isFocused){
86                        // Overrides dijit.form.ValidationTextBox.isValid to check that the value is also in range.
87                        return this.inherited(arguments) &&
88                                ((this._isEmpty(this.textbox.value) && !this.required) || this.isInRange(isFocused)); // Boolean
89                },
90
91                getErrorMessage: function(/*Boolean*/ isFocused){
92                        // Overrides dijit.form.ValidationTextBox.getErrorMessage to print "out of range" message if appropriate
93                        var v = this.get('value');
94                        if(v !== null && v !== '' && v !== undefined && (typeof v != "number" || !isNaN(v)) && !this.isInRange(isFocused)){ // don't check isInRange w/o a real value
95                                return this.rangeMessage; // String
96                        }
97                        return this.inherited(arguments);
98                },
99
100                postMixInProperties: function(){
101                        this.inherited(arguments);
102                        if(!this.rangeMessage){
103                                this.messages = i18n.getLocalization("dijit.form", "validate", this.lang);
104                                this.rangeMessage = this.messages.rangeMessage;
105                        }
106                },
107
108                _setConstraintsAttr: function(/*Object*/ constraints){
109                        this.inherited(arguments);
110                        if(this.focusNode){ // not set when called from postMixInProperties
111                                if(this.constraints.min !== undefined){
112                                        this.focusNode.setAttribute("aria-valuemin", this.constraints.min);
113                                }else{
114                                        this.focusNode.removeAttribute("aria-valuemin");
115                                }
116                                if(this.constraints.max !== undefined){
117                                        this.focusNode.setAttribute("aria-valuemax", this.constraints.max);
118                                }else{
119                                        this.focusNode.removeAttribute("aria-valuemax");
120                                }
121                        }
122                },
123
124                _setValueAttr: function(/*Number*/ value, /*Boolean?*/ priorityChange){
125                        // summary:
126                        //              Hook so set('value', ...) works.
127
128                        this.focusNode.setAttribute("aria-valuenow", value);
129                        this.inherited(arguments);
130                },
131
132                applyTextDir: function(/*===== element, text =====*/){
133                        // summary:
134                        //              The function overridden in the _BidiSupport module,
135                        //              originally used for setting element.dir according to this.textDir.
136                        //              In this case does nothing.
137                        // element: Object
138                        // text: String
139                        // tags:
140                        //              protected.
141                }
142        });
143});
Note: See TracBrowser for help on using the repository browser.