field_number.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2016 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Number input field
  22. * @author fenichel@google.com (Rachel Fenichel)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldNumber');
  26. goog.require('Blockly.FieldTextInput');
  27. goog.require('goog.math');
  28. /**
  29. * Class for an editable number field.
  30. * @param {number|string} value The initial content of the field.
  31. * @param {number|string|undefined} opt_min Minimum value.
  32. * @param {number|string|undefined} opt_max Maximum value.
  33. * @param {number|string|undefined} opt_precision Precision for value.
  34. * @param {Function=} opt_validator An optional function that is called
  35. * to validate any constraints on what the user entered. Takes the new
  36. * text as an argument and returns either the accepted text, a replacement
  37. * text, or null to abort the change.
  38. * @extends {Blockly.FieldTextInput}
  39. * @constructor
  40. */
  41. Blockly.FieldNumber =
  42. function(value, opt_min, opt_max, opt_precision, opt_validator) {
  43. value = String(value);
  44. Blockly.FieldNumber.superClass_.constructor.call(this, value, opt_validator);
  45. this.setConstraints(opt_min, opt_max, opt_precision);
  46. };
  47. goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);
  48. /**
  49. * Set the maximum, minimum and precision constraints on this field.
  50. * Any of these properties may be undefiend or NaN to be disabled.
  51. * Setting precision (usually a power of 10) enforces a minimum step between
  52. * values. That is, the user's value will rounded to the closest multiple of
  53. * precision. The least significant digit place is inferred from the precision.
  54. * Integers values can be enforces by choosing an integer precision.
  55. * @param {number|string|undefined} min Minimum value.
  56. * @param {number|string|undefined} max Maximum value.
  57. * @param {number|string|undefined} precision Precision for value.
  58. */
  59. Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
  60. precision = parseFloat(precision);
  61. this.precision_ = isNaN(precision) ? 0 : precision;
  62. min = parseFloat(min);
  63. this.min_ = isNaN(min) ? -Infinity : min;
  64. max = parseFloat(max);
  65. this.max_ = isNaN(max) ? Infinity : max;
  66. this.setValue(this.callValidator(this.getValue()));
  67. };
  68. /**
  69. * Ensure that only a number in the correct range may be entered.
  70. * @param {string} text The user's text.
  71. * @return {?string} A string representing a valid number, or null if invalid.
  72. */
  73. Blockly.FieldNumber.prototype.classValidator = function(text) {
  74. if (text === null) {
  75. return null;
  76. }
  77. text = String(text);
  78. // TODO: Handle cases like 'ten', '1.203,14', etc.
  79. // 'O' is sometimes mistaken for '0' by inexperienced users.
  80. text = text.replace(/O/ig, '0');
  81. // Strip out thousands separators.
  82. text = text.replace(/,/g, '');
  83. var n = parseFloat(text || 0);
  84. if (isNaN(n)) {
  85. // Invalid number.
  86. return null;
  87. }
  88. // Round to nearest multiple of precision.
  89. if (this.precision_ && isFinite(n)) {
  90. n = Math.round(n / this.precision_) * this.precision_;
  91. }
  92. // Get the value in range.
  93. n = goog.math.clamp(n, this.min_, this.max_);
  94. return String(n);
  95. };