Merge pull request #800 from groklearning/add-allInputsFilled-methods

Adds `allInputsFilled` methods to Block and Workspace.
This commit is contained in:
Rachel Fenichel
2017-01-16 15:33:21 -08:00
committed by GitHub
2 changed files with 53 additions and 0 deletions

View File

@@ -1342,3 +1342,39 @@ Blockly.Block.prototype.moveBy = function(dx, dy) {
Blockly.Block.prototype.makeConnection_ = function(type) {
return new Blockly.Connection(this, type);
};
/**
* Recursively checks whether all statement and value inputs are filled with
* blocks. Also checks all following statement blocks in this stack.
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
* whether shadow blocks are counted as filled. Defaults to true.
* @return {boolean} True if all inputs are filled, false otherwise.
*/
Blockly.Block.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
// Account for the shadow block filledness toggle.
if (opt_shadowBlocksAreFilled === undefined) {
opt_shadowBlocksAreFilled = true;
}
if (!opt_shadowBlocksAreFilled && this.isShadow()) {
return false;
}
// Recursively check each input block of the current block.
for (var i = 0, input; input = this.inputList[i]; i++) {
if (!input.connection) {
continue;
}
var target = input.connection.targetBlock();
if (!target || !target.allInputsFilled(opt_shadowBlocksAreFilled)) {
return false;
}
}
// Recursively check the next block after the current block.
var next = this.getNextBlock();
if (next) {
return next.allInputsFilled(opt_shadowBlocksAreFilled);
}
return true;
};

View File

@@ -478,6 +478,23 @@ Blockly.Workspace.prototype.getBlockById = function(id) {
return this.blockDB_[id] || null;
};
/**
* Checks whether all value and statement inputs in the workspace are filled
* with blocks.
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
* whether shadow blocks are counted as filled. Defaults to true.
* @return {boolean} True if all inputs are filled, false otherwise.
*/
Blockly.Workspace.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
var blocks = this.getTopBlocks(false);
for (var i = 0, block; block = blocks[i]; i++) {
if (!block.allInputsFilled(opt_shadowBlocksAreFilled)) {
return false;
}
}
return true;
};
/**
* Database of all workspaces.
* @private