Handle mutations with both mixins and functions

This commit is contained in:
Rachel Fenichel
2017-02-23 17:07:06 -08:00
parent 4b0c32e91f
commit aa8e996799
3 changed files with 23 additions and 11 deletions

View File

@@ -459,7 +459,7 @@ Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN = {
};
Blockly.Extensions.registerMutator('controls_if_mutator',
Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN,
Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN, null,
['controls_if_elseif', 'controls_if_else']);
/**
* "controls_if" extension function. Adds mutator, shape updating methods, and

View File

@@ -207,7 +207,7 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
"output": "Boolean",
"colour": "%{BKY_MATH_HUE}",
"tooltip": "%{BKY_MATH_IS_TOOLTIP}",
"extensions": ["math_is_divisibleby_mutator"]
"mutator": "math_is_divisibleby_mutator"
},
// Block for adding to a variable in place.
@@ -288,7 +288,8 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
"output": "Number",
"colour": "%{BKY_MATH_HUE}",
"helpUrl": "%{BKY_MATH_ONLIST_HELPURL}",
"extensions": ["math_op_tooltip", "math_modes_of_list_mutator"]
"mutator": "math_modes_of_list_mutator",
"extensions": ["math_op_tooltip"]
},
// Block for remainder of a division.
@@ -480,14 +481,14 @@ Blockly.Constants.Math.IS_DIVISIBLEBY_MUTATOR_MIXIN = {
* @package
*/
Blockly.Constants.Math.IS_DIVISIBLE_MUTATOR_EXTENSION = function() {
this.mixin(Blockly.Constants.Math.IS_DIVISIBLEBY_MUTATOR_MIXIN);
this.getField('PROPERTY').setValidator(function(option) {
var divisorInput = (option == 'DIVISIBLE_BY');
this.sourceBlock_.updateShape_(divisorInput);
});
};
Blockly.Extensions.register('math_is_divisibleby_mutator',
Blockly.Extensions.registerMutator('math_is_divisibleby_mutator',
Blockly.Constants.Math.IS_DIVISIBLEBY_MUTATOR_MIXIN,
Blockly.Constants.Math.IS_DIVISIBLE_MUTATOR_EXTENSION);
/**
@@ -555,11 +556,11 @@ Blockly.Constants.Math.LIST_MODES_MUTATOR_MIXIN = {
* @package
*/
Blockly.Constants.Math.LIST_MODES_MUTATOR_EXTENSION = function() {
this.mixin(Blockly.Constants.Math.LIST_MODES_MUTATOR_MIXIN);
this.getField('OP').setValidator(function(newOp) {
this.updateType_(newOp);
}.bind(this));
};
Blockly.Extensions.register('math_modes_of_list_mutator',
Blockly.Extensions.registerMutator('math_modes_of_list_mutator',
Blockly.Constants.Math.LIST_MODES_MUTATOR_MIXIN,
Blockly.Constants.Math.LIST_MODES_MUTATOR_EXTENSION);

View File

@@ -92,11 +92,14 @@ Blockly.Extensions.registerMixin = function(name, mixinObj) {
* decompose are defined on the mixin.
* @param {string} name The name of this mutator extension.
* @param {!Object} mixinObj The values to mix in.
* @param {Array.<string>=} opt_blockList A list of blocks to appear in the flyout
* of the mutator dialog.
* @param {function()=} opt_helperFn An optional function to apply after mixing
* in the object.
* @param {Array.<string>=} opt_blockList A list of blocks to appear in the
* flyout of the mutator dialog.
* @throws {Error} if the mutation is invalid or can't be applied to the block.
*/
Blockly.Extensions.registerMutator = function(name, mixinObj, opt_blockList) {
Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
opt_blockList) {
var errorPrefix = 'Error when registering mutator "' + name + '": ';
// Sanity check the mixin object before registering it.
@@ -106,13 +109,21 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_blockList) {
var hasMutatorDialog = Blockly.Extensions.checkMutatorDialog(mixinObj,
errorPrefix);
if (opt_helperFn && !goog.isFunction(opt_helperFn)) {
throw new Error('Extension "' + name + '" is not a function');
}
// Sanity checks passed.
Blockly.Extensions.register(name, function() {
if (hasMutatorDialog) {
this.setMutator(new Blockly.Mutator(opt_blockList));
}
// Finally, mixin the object.
// Mixin the object.
this.mixin(mixinObj);
if (opt_helperFn) {
opt_helperFn.apply(this);
}
});
};