[483] | 1 | define([ |
---|
| 2 | "dojo/_base/declare", // declare |
---|
| 3 | "dojo/i18n", // i18n.getLocalization |
---|
| 4 | "./MappedTextBox" |
---|
| 5 | ], function(declare, i18n, MappedTextBox){ |
---|
| 6 | |
---|
| 7 | // module: |
---|
| 8 | // dijit/form/RangeBoundTextBox |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | var RangeBoundTextBox = declare("dijit.form.RangeBoundTextBox", MappedTextBox, { |
---|
| 12 | // summary: |
---|
| 13 | // Base class for textbox form widgets which defines a range of valid values. |
---|
| 14 | |
---|
| 15 | // rangeMessage: String |
---|
| 16 | // The message to display if value is out-of-range |
---|
| 17 | rangeMessage: "", |
---|
| 18 | |
---|
| 19 | /*===== |
---|
| 20 | // constraints: RangeBoundTextBox.__Constraints |
---|
| 21 | constraints: {}, |
---|
| 22 | ======*/ |
---|
| 23 | |
---|
| 24 | rangeCheck: function(/*Number*/ primitive, /*dijit/form/RangeBoundTextBox.__Constraints*/ constraints){ |
---|
| 25 | // summary: |
---|
| 26 | // Overridable function used to validate the range of the numeric input value. |
---|
| 27 | // tags: |
---|
| 28 | // protected |
---|
| 29 | return ("min" in constraints? (this.compare(primitive,constraints.min) >= 0) : true) && |
---|
| 30 | ("max" in constraints? (this.compare(primitive,constraints.max) <= 0) : true); // Boolean |
---|
| 31 | }, |
---|
| 32 | |
---|
| 33 | isInRange: function(/*Boolean*/ /*===== isFocused =====*/){ |
---|
| 34 | // summary: |
---|
| 35 | // Tests if the value is in the min/max range specified in constraints |
---|
| 36 | // tags: |
---|
| 37 | // protected |
---|
| 38 | return this.rangeCheck(this.get('value'), this.constraints); |
---|
| 39 | }, |
---|
| 40 | |
---|
| 41 | _isDefinitelyOutOfRange: function(){ |
---|
| 42 | // summary: |
---|
| 43 | // Returns true if the value is out of range and will remain |
---|
| 44 | // out of range even if the user types more characters |
---|
| 45 | var val = this.get('value'); |
---|
| 46 | if(val == null){ return false; } // not yet valid enough to compare to |
---|
| 47 | var outOfRange = false; |
---|
| 48 | if("min" in this.constraints){ |
---|
| 49 | var min = this.constraints.min; |
---|
| 50 | outOfRange = this.compare(val, ((typeof min == "number") && min >= 0 && val != 0) ? 0 : min) < 0; |
---|
| 51 | } |
---|
| 52 | if(!outOfRange && ("max" in this.constraints)){ |
---|
| 53 | var max = this.constraints.max; |
---|
| 54 | outOfRange = this.compare(val, ((typeof max != "number") || max > 0) ? max : 0) > 0; |
---|
| 55 | } |
---|
| 56 | return outOfRange; |
---|
| 57 | }, |
---|
| 58 | |
---|
| 59 | _isValidSubset: function(){ |
---|
| 60 | // summary: |
---|
| 61 | // Overrides `dijit/form/ValidationTextBox._isValidSubset()`. |
---|
| 62 | // Returns true if the input is syntactically valid, and either within |
---|
| 63 | // range or could be made in range by more typing. |
---|
| 64 | return this.inherited(arguments) && !this._isDefinitelyOutOfRange(); |
---|
| 65 | }, |
---|
| 66 | |
---|
| 67 | isValid: function(/*Boolean*/ isFocused){ |
---|
| 68 | // Overrides dijit/form/ValidationTextBox.isValid() to check that the value is also in range. |
---|
| 69 | return this.inherited(arguments) && |
---|
| 70 | ((this._isEmpty(this.textbox.value) && !this.required) || this.isInRange(isFocused)); // Boolean |
---|
| 71 | }, |
---|
| 72 | |
---|
| 73 | getErrorMessage: function(/*Boolean*/ isFocused){ |
---|
| 74 | // Overrides dijit/form/ValidationTextBox.getErrorMessage() to print "out of range" message if appropriate |
---|
| 75 | var v = this.get('value'); |
---|
| 76 | if(v != null /* and !undefined */ && v !== '' && (typeof v != "number" || !isNaN(v)) && !this.isInRange(isFocused)){ // don't check isInRange w/o a real value |
---|
| 77 | return this.rangeMessage; // String |
---|
| 78 | } |
---|
| 79 | return this.inherited(arguments); |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | postMixInProperties: function(){ |
---|
| 83 | this.inherited(arguments); |
---|
| 84 | if(!this.rangeMessage){ |
---|
| 85 | this.messages = i18n.getLocalization("dijit.form", "validate", this.lang); |
---|
| 86 | this.rangeMessage = this.messages.rangeMessage; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | }); |
---|
| 90 | /*===== |
---|
| 91 | RangeBoundTextBox.__Constraints = declare(null, { |
---|
| 92 | // min: Number |
---|
| 93 | // Minimum signed value. Default is -Infinity |
---|
| 94 | // max: Number |
---|
| 95 | // Maximum signed value. Default is +Infinity |
---|
| 96 | }); |
---|
| 97 | =====*/ |
---|
| 98 | return RangeBoundTextBox; |
---|
| 99 | }); |
---|