From 3898fe1c450cf634c7c64de496f236248fcdb1d0 Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Thu, 9 Jun 2016 13:26:07 -0400 Subject: [PATCH] Instrument blocks.js with getters for fields and inputs --- src/engine/blocks.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 752cbb4d3..5ee0a7e10 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -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; +}; + // --------------------------------------------------------------------- /**