mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Refactor filter methods into util
This commit is contained in:
parent
a9b495fced
commit
390b2373e9
2 changed files with 61 additions and 17 deletions
49
src/util/filter-toolbox.js
Normal file
49
src/util/filter-toolbox.js
Normal file
|
@ -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.<string>} 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.<string>} 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;
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue