diff --git a/core/field.js b/core/field.js index 54554129a..b87a0129c 100644 --- a/core/field.js +++ b/core/field.js @@ -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); }; diff --git a/core/field_registry.js b/core/field_registry.js index 114b89f9f..26a8aa643 100644 --- a/core/field_registry.js +++ b/core/field_registry.js @@ -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); }; diff --git a/tests/mocha/field_test.js b/tests/mocha/field_test.js index 6d5454434..13c983d91 100644 --- a/tests/mocha/field_test.js +++ b/tests/mocha/field_test.js @@ -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'); + }); + }); + }); + }); });