From d89d8a211333311e684f15f3c2e8b60ea0b70ee6 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 4 May 2016 13:14:05 -0700 Subject: [PATCH] Add lightweight field_number --- blocks/math.js | 2 +- core/block.js | 3 +++ core/blockly.js | 1 + core/field_number.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 core/field_number.js diff --git a/blocks/math.js b/blocks/math.js index 0350048c8..c19c693c6 100644 --- a/blocks/math.js +++ b/blocks/math.js @@ -43,7 +43,7 @@ Blockly.Blocks['math_number'] = { this.setHelpUrl(Blockly.Msg.MATH_NUMBER_HELPURL); this.setColour(Blockly.Blocks.math.HUE); this.appendDummyInput() - .appendField(new Blockly.FieldTextInput('0', + .appendField(new Blockly.FieldNumber('0', Blockly.FieldTextInput.numberValidator), 'NUM'); this.setOutput(true, 'Number'); // Assign 'this' to a variable for use in the tooltip closure below. diff --git a/core/block.js b/core/block.js index 8cdcdf7df..6524e98f9 100644 --- a/core/block.js +++ b/core/block.js @@ -1082,6 +1082,9 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) { field = new Blockly.FieldImage(element['src'], element['width'], element['height'], element['alt']); break; + case 'field_number': + field = new Blockly.FieldNumber(element['text']); + break; case 'field_date': if (Blockly.FieldDate) { field = new Blockly.FieldDate(element['date']); diff --git a/core/blockly.js b/core/blockly.js index aee1a20ca..07e51e8a2 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -38,6 +38,7 @@ goog.require('Blockly.FieldColour'); goog.require('Blockly.FieldDropdown'); goog.require('Blockly.FieldImage'); goog.require('Blockly.FieldTextInput'); +goog.require('Blockly.FieldNumber'); goog.require('Blockly.FieldVariable'); goog.require('Blockly.Generator'); goog.require('Blockly.Msg'); diff --git a/core/field_number.js b/core/field_number.js new file mode 100644 index 000000000..600464173 --- /dev/null +++ b/core/field_number.js @@ -0,0 +1,53 @@ +/** + * @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'); + +/** + * Class for an editable number field. + * @param {string} text The initial content of the field. + * @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 + */ +Blockly.FieldNumber = function(text, opt_validator) { + Blockly.FieldNumber.superClass_.constructor.call(this, text, + opt_validator); +}; +goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput); + +/** + * Return the current value. + * @return {number} Current value as a number + */ +Blockly.FieldColour.prototype.getValue = function() { + return parseFloat(this.text_ || 0); +};