From 390b2373e9efae553d2337ad1d356e7ca626b7fb Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Wed, 15 Feb 2017 19:16:37 -0500 Subject: [PATCH] Refactor filter methods into util --- src/util/filter-toolbox.js | 49 ++++++++++++++++++++++++++++++++++++++ src/virtual-machine.js | 29 ++++++++++------------ 2 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 src/util/filter-toolbox.js diff --git a/src/util/filter-toolbox.js b/src/util/filter-toolbox.js new file mode 100644 index 000000000..c4b18b41c --- /dev/null +++ b/src/util/filter-toolbox.js @@ -0,0 +1,49 @@ +/** + * Filter Blockly toolbox XML node containing blocks to only those with + * valid opcodes. Return a copy of the node with valid blocks. + * @param {HTMLElement} node Blockly toolbox XML node + * @param {Array.} opcodes Valid opcodes. Blocks producing other opcodes + * will be filtered. + * @returns {HTMLElement} filtered toolbox XML node + */ +var filterToolboxNode = function (node, opcodes) { + var filteredCategory = node.cloneNode(); + for (var block = node.firstElementChild; block; block = block.nextElementSibling) { + if (block.nodeName.toLowerCase() !== 'block') continue; + var opcode = block.getAttribute('type').toLowerCase(); + if (opcodes.indexOf(opcode) !== -1) { + filteredCategory.appendChild(block.cloneNode(true)); + } + } + return filteredCategory; +}; + +/** + * Filter Blockly toolbox XML and return a copy which only contains blocks with + * existent opcodes. Categories with no valid children will be removed. + * @param {HTMLElement} toolbox Blockly toolbox XML node + * @param {Array.} opcodes Valid opcodes. Blocks producing other opcodes + * will be filtered. + * @returns {HTMLElement} filtered toolbox XML node + */ +var filterToolbox = function (toolbox, opcodes) { + if (!toolbox.hasChildNodes()) return toolbox; + var filteredToolbox; + if (toolbox.firstElementChild.nodeName.toLowerCase() === 'category') { + filteredToolbox = toolbox.cloneNode(); + for ( + var category = toolbox.firstElementChild; + category; + category = category.nextElementSibling + ) { + if (category.nodeName.toLowerCase() !== 'category') continue; + var filteredCategory = filterToolboxNode(category, opcodes); + if (filteredCategory.hasChildNodes()) filteredToolbox.appendChild(filteredCategory); + } + } else { + filteredToolbox = filterToolboxNode(toolbox, opcodes); + } + return filteredToolbox; +}; + +module.exports = filterToolbox; diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 52616e7c2..1f3bdfa1f 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1,6 +1,7 @@ var EventEmitter = require('events'); var util = require('util'); +var filterToolbox = require('./util/filter-toolbox'); var Runtime = require('./engine/runtime'); var sb2import = require('./import/sb2import'); @@ -336,23 +337,17 @@ VirtualMachine.prototype.postSpriteInfo = function (data) { this.editingTarget.postSpriteInfo(data); }; -VirtualMachine.prototype.filterToolbox = function (toolboxDOM) { - var filteredToolbox = toolboxDOM.cloneNode(); - var category = toolboxDOM.firstElementChild; - while (category) { - var filteredCategory = category.cloneNode(); - var block = category.firstElementChild; - while (block) { - var opcode = block.getAttribute('type'); - if (opcode in this.runtime._primitives || opcode in this.runtime._hats) { - filteredCategory.appendChild(block.cloneNode(true)); - } - block = block.nextElementSibling; - } - if (filteredCategory.hasChildNodes()) filteredToolbox.appendChild(filteredCategory); - category = category.nextElementSibling; - } - return filteredToolbox; + +/** + * Filter Blockly toolbox XML and return a copy which only contains blocks with + * existent opcodes. Categories with no valid children will be removed. + * @param {HTMLElement} toolbox Blockly toolbox XML node + * @returns {HTMLElement} filtered toolbox XML node + */ +VirtualMachine.prototype.filterToolbox = function (toolbox) { + var opcodes = Object.keys(this.runtime._primitives) + .concat(Object.keys(this.runtime._hats)); + return filterToolbox(toolbox, opcodes); }; module.exports = VirtualMachine;