Delete by id

This commit is contained in:
Rachel Fenichel
2017-11-30 17:21:12 -08:00
parent 08e065a75b
commit 0516310f66
2 changed files with 25 additions and 2 deletions

View File

@@ -355,6 +355,29 @@ Blockly.Workspace.prototype.getVariableUses = function(name, opt_type) {
return uses;
};
/**
* Find all the uses of a named variable.
* TODO (#1199): Possibly delete this function.
* @param {string} id ID of the variable to find.
* @return {!Array.<!Blockly.Block>} Array of block usages.
*/
Blockly.Workspace.prototype.getVariableUsesById = function(id) {
var uses = [];
var blocks = this.getAllBlocks();
// Iterate through every block and check the name.
for (var i = 0; i < blocks.length; i++) {
var blockVariables = blocks[i].getVarModels();
if (blockVariables) {
for (var j = 0; j < blockVariables.length; j++) {
if (blockVariables[j].getId() == id) {
uses.push(blocks[i]);
}
}
}
}
return uses;
};
/**
* Delete a variable by the passed in name and all of its uses from this
* workspace. May prompt the user for confirmation.
@@ -419,7 +442,7 @@ Blockly.Workspace.prototype.deleteVariableById = function(id) {
* @private
*/
Blockly.Workspace.prototype.deleteVariableInternal_ = function(variable) {
var uses = this.getVariableUses(variable.name);
var uses = this.getVariableUsesById(variable.getId());
Blockly.Events.setGroup(true);
for (var i = 0; i < uses.length; i++) {
uses[i].dispose(true, false);