Disable/enable function calls along with their definitions (#2019)

Fixes #1344

Extends the event listener on procedure caller blocks to also check
for their definition being enabled/disabled and update their own
state in response.
This commit is contained in:
RoboErikG
2018-08-23 11:18:29 -07:00
committed by GitHub
parent 57aa2e966b
commit 9eacd7d3b9
4 changed files with 410 additions and 0 deletions

View File

@@ -569,7 +569,9 @@ Blockly.Blocks['procedures_callnoreturn'] = {
this.argumentVarModels_ = [];
this.quarkConnections_ = {};
this.quarkIds_ = null;
this.previousDisabledState_ = false;
},
/**
* Returns the name of the procedure this block calls.
* @return {string} Procedure name.
@@ -789,6 +791,10 @@ Blockly.Blocks['procedures_callnoreturn'] = {
// Block is deleted or is in a flyout.
return;
}
if (!event.recordUndo) {
// Events not generated by user. Skip handling.
return;
}
if (event.type == Blockly.Events.BLOCK_CREATE &&
event.ids.indexOf(this.id) != -1) {
// Look for the case where a procedure call was created (usually through
@@ -843,6 +849,27 @@ Blockly.Blocks['procedures_callnoreturn'] = {
this.dispose(true, false);
Blockly.Events.setGroup(false);
}
} else if (event.type == Blockly.Events.CHANGE && event.element == 'disabled') {
var name = this.getProcedureCall();
var def = Blockly.Procedures.getDefinition(name, this.workspace);
if (def && def.id == event.blockId) {
// in most cases the old group should be ''
var oldGroup = Blockly.Events.getGroup();
if (oldGroup) {
// This should only be possible programatically and may indicate a problem
// with event grouping. If you see this message please investigate. If the
// use ends up being valid we may need to reorder events in the undo stack.
console.log('Saw an existing group while responding to a definition change');
}
Blockly.Events.setGroup(event.group);
if (event.newValue) {
this.previousDisabledState_ = this.disabled;
this.setDisabled(true);
} else {
this.setDisabled(this.previousDisabledState_);
}
Blockly.Events.setGroup(oldGroup);
}
}
},
/**
@@ -882,7 +909,9 @@ Blockly.Blocks['procedures_callreturn'] = {
this.arguments_ = [];
this.quarkConnections_ = {};
this.quarkIds_ = null;
this.previousDisabledState_ = false;
},
getProcedureCall: Blockly.Blocks['procedures_callnoreturn'].getProcedureCall,
renameProcedure: Blockly.Blocks['procedures_callnoreturn'].renameProcedure,
setProcedureParameters_: