Changed abstract field to new configuration paradigm.

This commit is contained in:
Beka Westberg
2019-08-08 10:45:01 -07:00
parent a72950cc83
commit 1e73f69cea
3 changed files with 59 additions and 11 deletions

View File

@@ -31,6 +31,7 @@ goog.provide('Blockly.Field');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockChange');
goog.require('Blockly.Gesture');
goog.require('Blockly.utils');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.Size');
goog.require('Blockly.utils.userAgent');
@@ -44,15 +45,25 @@ goog.require('Blockly.utils.style');
* @param {Function=} opt_validator A function that is called to validate
* changes to the field's value. Takes in a value & returns a validated
* value, or null to abort the change.
* @param {Object=} opt_config A map of options used to configure the field. See
* the individual field's documentation for a list of properties this
* parameter supports.
* @constructor
*/
Blockly.Field = function(value, opt_validator) {
Blockly.Field = function(value, opt_validator, opt_config) {
/**
* The size of the area rendered by the field.
* @type {Blockly.utils.Size}
*/
this.size_ = new Blockly.utils.Size(0, 0);
if (opt_config) {
var tooltip = Blockly.utils.replaceMessageReferences(opt_config['tooltip']);
tooltip && this.setTooltip(tooltip);
// TODO: Possibly eventually add configurations like cursor and css class.
}
this.setValue(value);
opt_validator && this.setValidator(opt_validator);
};

View File

@@ -28,8 +28,6 @@
goog.provide('Blockly.fieldRegistry');
goog.require('Blockly.utils');
/**
* The set of all registered fields, keyed by field type as used in the JSON
@@ -82,12 +80,5 @@ Blockly.fieldRegistry.fromJson = function(options) {
' being reached.');
return null;
}
var field = fieldClass.fromJson(options);
if (options['tooltip'] !== undefined) {
var rawValue = options['tooltip'];
var localizedText = Blockly.utils.replaceMessageReferences(rawValue);
field.setTooltip(localizedText);
}
return field;
return fieldClass.fromJson(options);
};

View File

@@ -482,4 +482,50 @@ suite('Abstract Fields', function() {
delete Blockly.Blocks['tooltip'];
});
});
suite('Customization', function() {
// All this field does is wrap the abstract field.
function CustomField(opt_config) {
CustomField.superClass_.constructor.call(
this, 'value', null, opt_config);
}
goog.inherits(CustomField, Blockly.Field);
CustomField.fromJson = function(options) {
return new CustomField(options);
};
suite('Tooltip', function() {
test('JS Constructor', function() {
var field = new Blockly.Field('value', null, {
tooltip: 'test tooltip',
});
chai.assert.equal(field.tooltip_, 'test tooltip');
});
test('JSON Definition', function() {
var field = CustomField.fromJson({
tooltip: "test tooltip"
});
chai.assert.equal(field.tooltip_, 'test tooltip');
});
suite('W/ Msg References', function() {
setup(function() {
Blockly.Msg['TOOLTIP'] = 'test tooltip';
});
teardown(function() {
delete Blockly.Msg['TOOLTIP'];
});
test('JS Constructor', function() {
var field = new Blockly.Field('value', null, {
tooltip: '%{BKY_TOOLTIP}',
});
chai.assert.equal(field.tooltip_, 'test tooltip');
});
test('JSON Definition', function() {
var field = CustomField.fromJson({
tooltip: "%{BKY_TOOLTIP}"
});
chai.assert.equal(field.tooltip_, 'test tooltip');
});
});
});
});
});