From 23e0733f217b6991c939cc7b7ef8d3cadf359c6d Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 23 Aug 2019 10:54:44 -0700 Subject: [PATCH] Changed field and colour constructors to use better organization. (#2900) --- core/field.js | 38 ++++++++++++++++++++++++-------------- core/field_colour.js | 33 ++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/core/field.js b/core/field.js index e4336819b..d984bae4c 100644 --- a/core/field.js +++ b/core/field.js @@ -56,21 +56,9 @@ Blockly.Field = function(value, opt_validator, opt_config) { * @type {Blockly.utils.Size} */ this.size_ = new Blockly.utils.Size(0, 0); - - if (opt_config) { - var tooltip = opt_config['tooltip']; - if (typeof tooltip == 'string') { - tooltip = Blockly.utils.replaceMessageReferences( - opt_config['tooltip']); - } - tooltip && this.setTooltip(tooltip); - - // TODO (#2884): Possibly add CSS class config option. - // TODO (#2885): Possibly add cursor config option. - } - this.setValue(value); - opt_validator && this.setValidator(opt_validator); + this.setValidator(opt_validator); + this.configure_(opt_config); }; /** @@ -206,6 +194,28 @@ Blockly.Field.prototype.EDITABLE = true; */ Blockly.Field.prototype.SERIALIZABLE = false; +/** + * Configure the field based on the given map of options. + * @param {Object} opt_config The map of options to configure the field + * based on. + * @private + */ +Blockly.Field.prototype.configure_ = function(opt_config) { + if (!opt_config) { + return; + } + + var tooltip = opt_config['tooltip']; + if (typeof tooltip == 'string') { + tooltip = Blockly.utils.replaceMessageReferences( + opt_config['tooltip']); + } + tooltip && this.setTooltip(tooltip); + + // TODO (#2884): Possibly add CSS class config option. + // TODO (#2885): Possibly add cursor config option. +}; + /** * Attach this field to a block. * @param {!Blockly.Block} block The block containing this field. diff --git a/core/field_colour.js b/core/field_colour.js index 4388b212e..f9d5431b7 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -46,27 +46,20 @@ goog.require('Blockly.utils.Size'); * changes to the field's value. Takes in a colour string & returns a * validated colour string ('#rrggbb' format), or null to abort the change. * @param {Object=} opt_config A map of options used to configure the field. - * See the documentation for a list of properties this parameter supports. - * https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/colour + * See the [field creation documentation]{@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/colour} + * for a list of properties this parameter supports. * @extends {Blockly.Field} * @constructor */ Blockly.FieldColour = function(opt_value, opt_validator, opt_config) { - if (opt_config) { - if (opt_config['colourOptions']) { - this.setColours(opt_config['colourOptions'], opt_config['colourTitles']); - } - if (opt_config['columns']) { - this.setColumns(opt_config['columns']); - } - } - opt_value = this.doClassValidation_(opt_value); if (opt_value === null) { opt_value = Blockly.FieldColour.COLOURS[0]; } Blockly.FieldColour.superClass_.constructor.call( this, opt_value, opt_validator, opt_config); + + this.configure_(opt_config); }; goog.inherits(Blockly.FieldColour, Blockly.Field); @@ -157,6 +150,24 @@ Blockly.FieldColour.prototype.DROPDOWN_BORDER_COLOUR = '#dadce0'; */ Blockly.FieldColour.prototype.DROPDOWN_BACKGROUND_COLOUR = 'white'; +/** + * Configure the field based on the given map of options. + * @param {Object} opt_config A map of options to configure the field based on. + * @private + */ +Blockly.FieldColour.prototype.configure_ = function(opt_config) { + if (!opt_config) { + return; + } + + if (opt_config['colourOptions']) { + this.setColours(opt_config['colourOptions'], opt_config['colourTitles']); + } + if (opt_config['columns']) { + this.setColumns(opt_config['columns']); + } +}; + /** * Create the block UI for this colour field. * @package