Instrument blocks.js with getters for fields and inputs

This commit is contained in:
Tim Mickel 2016-06-09 13:26:07 -04:00
parent b1e913b99a
commit 3898fe1c45

View file

@ -22,6 +22,13 @@ function Blocks () {
this._stacks = [];
}
/**
* Blockly inputs that represent statements/substacks
* are prefixed with this string.
* @const{string}
*/
Blocks.SUBSTACK_INPUT_PREFIX = 'SUBSTACK';
/**
* Provide an object with metadata for the requested block ID.
* @param {!string} blockId ID of block we have stored.
@ -60,7 +67,7 @@ Blocks.prototype.getSubstack = function (id, substackNum) {
if (typeof block === 'undefined') return null;
if (!substackNum) substackNum = 1;
var inputName = 'SUBSTACK';
var inputName = Blocks.SUBSTACK_INPUT_PREFIX;
if (substackNum > 1) {
inputName += substackNum;
}
@ -80,6 +87,34 @@ Blocks.prototype.getOpcode = function (id) {
return this._blocks[id].opcode;
};
/**
* Get all fields and their values for a block.
* @param {?string} id ID of block to query.
* @return {!Object} All fields and their values.
*/
Blocks.prototype.getFields = function (id) {
if (typeof this._blocks[id] === 'undefined') return null;
return this._blocks[id].fields;
};
/**
* Get all non-substack inputs for a block.
* @param {?string} id ID of block to query.
* @return {!Object} All non-substack inputs and their associated blocks.
*/
Blocks.prototype.getInputs = function (id) {
if (typeof this._blocks[id] === 'undefined') return null;
var inputs = {};
for (var input in this._blocks[id].inputs) {
// Ignore blocks prefixed with substack prefix.
if (input.substring(0, Blocks.SUBSTACK_INPUT_PREFIX.length)
!= Blocks.SUBSTACK_INPUT_PREFIX) {
inputs[input] = this._blocks[id].inputs[input];
}
}
return inputs;
};
// ---------------------------------------------------------------------
/**