Changed field and colour constructors to use better organization. (#2900)

This commit is contained in:
Beka Westberg
2019-08-23 10:54:44 -07:00
committed by Sam El-Husseini
parent 7434bae9d0
commit 23e0733f21
2 changed files with 46 additions and 25 deletions

View File

@@ -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.

View File

@@ -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