mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
See issue #1486 - adds fromJson_ fn to every Blockly.Field, removing Block.newField*FromJson_ helpers
This commit is contained in:
@@ -1224,37 +1224,35 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
||||
input = this.appendDummyInput(element['name']);
|
||||
break;
|
||||
case 'field_label':
|
||||
field = Blockly.Block.newFieldLabelFromJson_(element);
|
||||
field = Blockly.FieldLabel.fromJson_(element);
|
||||
break;
|
||||
case 'field_input':
|
||||
field = Blockly.Block.newFieldTextInputFromJson_(element);
|
||||
field = Blockly.FieldTextInput.fromJson_(element);
|
||||
break;
|
||||
case 'field_angle':
|
||||
field = new Blockly.FieldAngle(element['angle']);
|
||||
field = Blockly.FieldAngle.fromJson_(element);
|
||||
break;
|
||||
case 'field_checkbox':
|
||||
field = new Blockly.FieldCheckbox(
|
||||
element['checked'] ? 'TRUE' : 'FALSE');
|
||||
field = Blockly.FieldCheckbox.fromJson_(element);
|
||||
break;
|
||||
case 'field_colour':
|
||||
field = new Blockly.FieldColour(element['colour']);
|
||||
field = Blockly.FieldColour.fromJson_(element);
|
||||
break;
|
||||
case 'field_variable':
|
||||
field = Blockly.Block.newFieldVariableFromJson_(element);
|
||||
field = Blockly.FieldVariable.fromJson_(element);
|
||||
break;
|
||||
case 'field_dropdown':
|
||||
field = new Blockly.FieldDropdown(element['options']);
|
||||
field = Blockly.FieldDropdown.fromJson_(element);
|
||||
break;
|
||||
case 'field_image':
|
||||
field = Blockly.Block.newFieldImageFromJson_(element);
|
||||
field = Blockly.FieldImage.fromJson_(element);
|
||||
break;
|
||||
case 'field_number':
|
||||
field = new Blockly.FieldNumber(element['value'],
|
||||
element['min'], element['max'], element['precision']);
|
||||
field = Blockly.FieldNumber.fromJson_(element);
|
||||
break;
|
||||
case 'field_date':
|
||||
if (Blockly.FieldDate) {
|
||||
field = new Blockly.FieldDate(element['date']);
|
||||
field = Blockly.FieldDate.fromJson_(element);
|
||||
break;
|
||||
}
|
||||
// Fall through if FieldDate is not compiled in.
|
||||
@@ -1285,66 +1283,6 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldImage from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (src, width, height, and alt).
|
||||
* @returns {!Blockly.FieldImage} The new image.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldImageFromJson_ = function(options) {
|
||||
var src = Blockly.utils.replaceMessageReferences(options['src']);
|
||||
var width = Number(Blockly.utils.replaceMessageReferences(options['width']));
|
||||
var height =
|
||||
Number(Blockly.utils.replaceMessageReferences(options['height']));
|
||||
var alt = Blockly.utils.replaceMessageReferences(options['alt']);
|
||||
return new Blockly.FieldImage(src, width, height, alt);
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldLabel from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, and class).
|
||||
* @returns {!Blockly.FieldLabel} The new label.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldLabelFromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
return new Blockly.FieldLabel(text, options['class']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldTextInput from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, class, and
|
||||
* spellcheck).
|
||||
* @returns {!Blockly.FieldTextInput} The new text input.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldTextInputFromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
var field = new Blockly.FieldTextInput(text, options['class']);
|
||||
if (typeof options['spellcheck'] == 'boolean') {
|
||||
field.setSpellcheck(options['spellcheck']);
|
||||
}
|
||||
return field;
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldVariable from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (variable).
|
||||
* @returns {!Blockly.FieldVariable} The variable field.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldVariableFromJson_ = function(options) {
|
||||
var varname = Blockly.utils.replaceMessageReferences(options['variable']);
|
||||
var variableTypes = options['variableTypes'];
|
||||
var defaultType = options['defaultType'];
|
||||
return new Blockly.FieldVariable(varname, null, variableTypes, defaultType);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add a value input, statement input or local variable to this block.
|
||||
* @param {number} type Either Blockly.INPUT_VALUE or Blockly.NEXT_STATEMENT or
|
||||
|
||||
@@ -53,6 +53,16 @@ Blockly.FieldAngle = function(opt_value, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput);
|
||||
|
||||
/**
|
||||
* Construct a FieldAngle from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (angle).
|
||||
* @returns {!Blockly.FieldAngle} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldAngle.fromJson_ = function(options) {
|
||||
return new Blockly.FieldAngle(options['angle'])
|
||||
};
|
||||
|
||||
/**
|
||||
* Round angles to the nearest 15 degrees when using mouse.
|
||||
* Set to 0 to disable rounding.
|
||||
|
||||
@@ -46,6 +46,16 @@ Blockly.FieldCheckbox = function(state, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldCheckbox, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldCheckbox from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (checked).
|
||||
* @returns {!Blockly.FieldCheckbox} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldCheckbox.fromJson_ = function(options) {
|
||||
return new Blockly.FieldCheckbox(options['checked'] ? 'TRUE' : 'FALSE');
|
||||
};
|
||||
|
||||
/**
|
||||
* Character for the checkmark.
|
||||
*/
|
||||
|
||||
@@ -52,6 +52,16 @@ Blockly.FieldColour = function(colour, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldColour, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldColour from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (colour).
|
||||
* @returns {!Blockly.FieldColour} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldColour.fromJson_ = function(options) {
|
||||
return new Blockly.FieldColour(options['colour']);
|
||||
};
|
||||
|
||||
/**
|
||||
* By default use the global constants for colours.
|
||||
* @type {Array.<string>}
|
||||
|
||||
@@ -59,6 +59,16 @@ Blockly.FieldDate = function(date, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldDate, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldDate from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (date).
|
||||
* @returns {!Blockly.FieldDate} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldDate.fromJson_ = function(options) {
|
||||
return new Blockly.FieldDate(options['date']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Mouse cursor style when over the hotspot that initiates the editor.
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,16 @@ Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldDropdown, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldDropdown from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (options).
|
||||
* @returns {!Blockly.FieldDropdown} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldDropdown.fromJson_ = function(options) {
|
||||
return new Blockly.FieldDropdown(options['options']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Horizontal distance that a checkmark overhangs the dropdown.
|
||||
*/
|
||||
|
||||
@@ -60,6 +60,23 @@ Blockly.FieldImage = function(src, width, height, opt_alt, opt_onClick) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldImage, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldImage from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (src, width, height, and
|
||||
* alt).
|
||||
* @returns {!Blockly.FieldImage} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldImage.fromJson_ = function(options) {
|
||||
var src = Blockly.utils.replaceMessageReferences(options['src']);
|
||||
var width = Number(Blockly.utils.replaceMessageReferences(options['width']));
|
||||
var height =
|
||||
Number(Blockly.utils.replaceMessageReferences(options['height']));
|
||||
var alt = Blockly.utils.replaceMessageReferences(options['alt']);
|
||||
return new Blockly.FieldImage(src, width, height, alt);
|
||||
};
|
||||
|
||||
/**
|
||||
* Editable fields are saved by the XML renderer, non-editable fields are not.
|
||||
*/
|
||||
|
||||
@@ -46,6 +46,18 @@ Blockly.FieldLabel = function(text, opt_class) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldLabel, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldLabel from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, and class).
|
||||
* @returns {!Blockly.FieldLabel} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldLabel.fromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
return new Blockly.FieldLabel(text, options['class']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Editable fields are saved by the XML renderer, non-editable fields are not.
|
||||
*/
|
||||
|
||||
@@ -53,6 +53,18 @@ Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision,
|
||||
};
|
||||
goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);
|
||||
|
||||
/**
|
||||
* Construct a FieldNumber from a JSON arg object.
|
||||
* @param {!Object} options A JSON object with options (value, min, max, and
|
||||
* precision).
|
||||
* @returns {!Blockly.FieldNumber} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldNumber.fromJson_ = function(options) {
|
||||
return new Blockly.FieldNumber(options['value'],
|
||||
options['min'], options['max'], options['precision']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the maximum, minimum and precision constraints on this field.
|
||||
* Any of these properties may be undefiend or NaN to be disabled.
|
||||
|
||||
@@ -50,6 +50,23 @@ Blockly.FieldTextInput = function(text, opt_validator) {
|
||||
};
|
||||
goog.inherits(Blockly.FieldTextInput, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Construct a FieldTextInput from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, class, and
|
||||
* spellcheck).
|
||||
* @returns {!Blockly.FieldTextInput} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldTextInput.fromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
var field = new Blockly.FieldTextInput(text, options['class']);
|
||||
if (typeof options['spellcheck'] === 'boolean') {
|
||||
field.setSpellcheck(options['spellcheck']);
|
||||
}
|
||||
return field;
|
||||
};
|
||||
|
||||
/**
|
||||
* Point size of text. Should match blocklyText's font-size in CSS.
|
||||
*/
|
||||
|
||||
@@ -61,6 +61,21 @@ Blockly.FieldVariable = function(varname, opt_validator, opt_variableTypes,
|
||||
};
|
||||
goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);
|
||||
|
||||
/**
|
||||
* Construct a FieldVariable from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (variable,
|
||||
* variableTypes, and defaultType).
|
||||
* @returns {!Blockly.FieldVariable} The new field instance.
|
||||
* @package
|
||||
*/
|
||||
Blockly.FieldVariable.fromJson_ = function(options) {
|
||||
var varname = Blockly.utils.replaceMessageReferences(options['variable']);
|
||||
var variableTypes = options['variableTypes'];
|
||||
var defaultType = options['defaultType'];
|
||||
return new Blockly.FieldVariable(varname, null, variableTypes, defaultType);
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize everything needed to render this field. This includes making sure
|
||||
* that the field's value is valid.
|
||||
|
||||
@@ -37,3 +37,8 @@ function test_fieldangle_constructor() {
|
||||
assertEquals(new Blockly.FieldAngle('bad').getValue(), '0');
|
||||
assertEquals(new Blockly.FieldAngle(NaN).getValue(), '0');
|
||||
}
|
||||
|
||||
function test_fieldangle_fromJson() {
|
||||
assertEquals(Blockly.FieldAngle.fromJson_({}).getValue(), '0');
|
||||
assertEquals(Blockly.FieldAngle.fromJson_({ angle: 1 }).getValue(), '1');
|
||||
}
|
||||
|
||||
@@ -61,3 +61,20 @@ function test_fieldnumber_constructor() {
|
||||
field = new Blockly.FieldNumber(NaN);
|
||||
assertEquals(field.getValue(), '0');
|
||||
}
|
||||
|
||||
function test_fieldnumber_fromJson() {
|
||||
assertEquals(Blockly.FieldNumber.fromJson_({}).getValue(), '0');
|
||||
assertEquals(Blockly.FieldNumber.fromJson_({ value: 1 }).getValue(), '1');
|
||||
|
||||
// All options
|
||||
var field = Blockly.FieldNumber.fromJson_({
|
||||
value: 0,
|
||||
min: -128,
|
||||
max: 127,
|
||||
precision: 1
|
||||
});
|
||||
assertEquals(field.getValue(), '0');
|
||||
assertEquals(field.min_, -128);
|
||||
assertEquals(field.max_, 127);
|
||||
assertEquals(field.precision_, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user