RangeBoundTextBox.js 4.7 KB

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