2016-05-04 13:14:05 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Visual Blocks Editor
|
|
|
|
*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
* https://developers.google.com/blockly/
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Number input field
|
|
|
|
* @author fenichel@google.com (Rachel Fenichel)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.FieldNumber');
|
|
|
|
|
|
|
|
goog.require('Blockly.FieldTextInput');
|
2016-06-21 04:10:32 -07:00
|
|
|
goog.require('goog.math');
|
2016-05-04 13:14:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for an editable number field.
|
2016-06-21 04:10:32 -07:00
|
|
|
* @param {string} value The initial content of the field.
|
2016-06-22 15:07:07 -07:00
|
|
|
* @param {number|string|undefined} opt_min Minimum value.
|
|
|
|
* @param {number|string|undefined} opt_max Maximum value.
|
|
|
|
* @param {number|string|undefined} opt_precision Precision for value.
|
2016-05-04 13:14:05 -07:00
|
|
|
* @param {Function=} opt_validator An optional function that is called
|
|
|
|
* to validate any constraints on what the user entered. Takes the new
|
|
|
|
* text as an argument and returns either the accepted text, a replacement
|
|
|
|
* text, or null to abort the change.
|
|
|
|
* @extends {Blockly.FieldTextInput}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-06-22 15:07:07 -07:00
|
|
|
Blockly.FieldNumber =
|
|
|
|
function(value, opt_min, opt_max, opt_precision, opt_validator) {
|
2016-07-10 22:54:57 -07:00
|
|
|
value = String(value);
|
2016-06-21 04:10:32 -07:00
|
|
|
Blockly.FieldNumber.superClass_.constructor.call(this, value, opt_validator);
|
2016-06-22 15:07:07 -07:00
|
|
|
this.setConstraints(opt_min, opt_max, opt_precision);
|
2016-05-04 13:14:05 -07:00
|
|
|
};
|
|
|
|
goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);
|
2016-06-21 04:10:32 -07:00
|
|
|
|
|
|
|
/**
|
2016-06-22 15:07:07 -07:00
|
|
|
* Set the maximum, minimum and precision constraints on this field.
|
|
|
|
* Any of these properties may be undefiend or NaN to be disabled.
|
2016-06-21 04:10:32 -07:00
|
|
|
* Setting precision (usually a power of 10) enforces a minimum step between
|
|
|
|
* values. That is, the user's value will rounded to the closest multiple of
|
|
|
|
* precision. The least significant digit place is inferred from the precision.
|
|
|
|
* Integers values can be enforces by choosing an integer precision.
|
2016-06-22 15:07:07 -07:00
|
|
|
* @param {number|string|undefined} min Minimum value.
|
|
|
|
* @param {number|string|undefined} max Maximum value.
|
2016-06-21 04:10:32 -07:00
|
|
|
* @param {number|string|undefined} precision Precision for value.
|
|
|
|
*/
|
2016-06-22 15:07:07 -07:00
|
|
|
Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
|
2016-06-21 04:10:32 -07:00
|
|
|
precision = parseFloat(precision);
|
2016-06-22 15:07:07 -07:00
|
|
|
this.precision_ = isNaN(precision) ? 0 : precision;
|
2016-06-21 04:10:32 -07:00
|
|
|
min = parseFloat(min);
|
2016-06-22 15:07:07 -07:00
|
|
|
this.min_ = isNaN(min) ? -Infinity : min;
|
2016-06-21 04:10:32 -07:00
|
|
|
max = parseFloat(max);
|
2016-06-22 15:07:07 -07:00
|
|
|
this.max_ = isNaN(max) ? Infinity : max;
|
2016-06-29 17:44:12 -07:00
|
|
|
this.setValue(this.callValidator(this.getValue));
|
2016-06-21 04:10:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that only a number in the correct range may be entered.
|
|
|
|
* @param {string} text The user's text.
|
|
|
|
* @return {?string} A string representing a valid number, or null if invalid.
|
2016-06-29 17:44:12 -07:00
|
|
|
* @this {!Blockly.FieldNumber}
|
2016-06-21 04:10:32 -07:00
|
|
|
*/
|
2016-06-29 17:44:12 -07:00
|
|
|
Blockly.FieldNumber.classValidator = function(text) {
|
2016-06-21 04:10:32 -07:00
|
|
|
if (text === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
text = String(text);
|
|
|
|
// TODO: Handle cases like 'ten', '1.203,14', etc.
|
|
|
|
// 'O' is sometimes mistaken for '0' by inexperienced users.
|
|
|
|
text = text.replace(/O/ig, '0');
|
|
|
|
// Strip out thousands separators.
|
|
|
|
text = text.replace(/,/g, '');
|
|
|
|
var n = parseFloat(text || 0);
|
|
|
|
if (isNaN(n)) {
|
|
|
|
// Invalid number.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Round to nearest multiple of precision.
|
|
|
|
if (this.precision_ && Number.isFinite(n)) {
|
|
|
|
n = Math.round(n / this.precision_) * this.precision_;
|
|
|
|
}
|
|
|
|
// Get the value in range.
|
|
|
|
n = goog.math.clamp(n, this.min_, this.max_);
|
|
|
|
return String(n);
|
|
|
|
};
|