From 03250c9c5d664f7e71dcddd842b09aa5c0491443 Mon Sep 17 00:00:00 2001 From: Markus Bordihn Date: Wed, 29 Jun 2016 12:09:44 +0200 Subject: [PATCH 1/2] Rewrite anonymouse function "syncTree" for compiler and debugging reasons. See #448 for more details. --- core/toolbox.js | 163 +++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 78 deletions(-) diff --git a/core/toolbox.js b/core/toolbox.js index b5b27f869..1e8c84af8 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -265,85 +265,12 @@ Blockly.Toolbox.prototype.position = function() { * @private */ Blockly.Toolbox.prototype.populate_ = function(newTree) { - var rootOut = this.tree_; - var that = this; - rootOut.removeChildren(); // Delete any existing content. - rootOut.blocks = []; - var hasColours = false; - function syncTrees(treeIn, treeOut, pathToMedia) { - var lastElement = null; - for (var i = 0, childIn; childIn = treeIn.childNodes[i]; i++) { - if (!childIn.tagName) { - // Skip over text. - continue; - } - switch (childIn.tagName.toUpperCase()) { - case 'CATEGORY': - var childOut = rootOut.createNode(childIn.getAttribute('name')); - childOut.blocks = []; - treeOut.add(childOut); - var custom = childIn.getAttribute('custom'); - if (custom) { - // Variables and procedures are special dynamic categories. - childOut.blocks = custom; - } else { - syncTrees(childIn, childOut, pathToMedia); - } - var colour = childIn.getAttribute('colour'); - if (goog.isString(colour)) { - if (colour.match(/^#[0-9a-fA-F]{6}$/)) { - childOut.hexColour = colour; - } else { - childOut.hexColour = Blockly.hueToRgb(colour); - } - hasColours = true; - } else { - childOut.hexColour = ''; - } - if (childIn.getAttribute('expanded') == 'true') { - if (childOut.blocks.length) { - rootOut.setSelectedItem(childOut); - } - childOut.setExpanded(true); - } else { - childOut.setExpanded(false); - } - lastElement = childIn; - break; - case 'SEP': - if (lastElement) { - if (lastElement.tagName.toUpperCase() == 'CATEGORY') { - // Separator between two categories. - // - treeOut.add(new Blockly.Toolbox.TreeSeparator( - that.treeSeparatorConfig_)); - } else { - // Change the gap between two blocks. - // - // The default gap is 24, can be set larger or smaller. - // Note that a deprecated method is to add a gap to a block. - // - var newGap = parseFloat(childIn.getAttribute('gap')); - if (!isNaN(newGap)) { - var oldGap = parseFloat(lastElement.getAttribute('gap')); - var gap = isNaN(oldGap) ? newGap : oldGap + newGap; - lastElement.setAttribute('gap', gap); - } - } - } - break; - case 'BLOCK': - case 'SHADOW': - treeOut.blocks.push(childIn); - lastElement = childIn; - break; - } - } - } - syncTrees(newTree, this.tree_, this.workspace_.options.pathToMedia); - this.hasColours_ = hasColours; + this.tree_.removeChildren(); // Delete any existing content. + this.tree_.blocks = []; + this.hasColours_ = false; + this.syncTrees_(newTree, this.tree_, this.workspace_.options.pathToMedia); - if (rootOut.blocks.length) { + if (this.tree_.blocks.length) { throw 'Toolbox cannot have both blocks and categories in the root level.'; } @@ -351,6 +278,86 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { Blockly.resizeSvgContents(this.workspace_); }; + +/** + * Sync trees of the toolbox. + * @param {Node} treeIn DOM tree of blocks, or null. + * @param {Blockly.Toolbox.TreeControl} treeOut + * @param {string} pathToMedia + * @private + */ +Blockly.Toolbox.prototype.syncTrees_ = function(treeIn, treeOut, pathToMedia) { + var lastElement = null; + for (var i = 0, childIn; childIn = treeIn.childNodes[i]; i++) { + if (!childIn.tagName) { + // Skip over text. + continue; + } + switch (childIn.tagName.toUpperCase()) { + case 'CATEGORY': + var childOut = this.tree_.createNode(childIn.getAttribute('name')); + childOut.blocks = []; + treeOut.add(childOut); + var custom = childIn.getAttribute('custom'); + if (custom) { + // Variables and procedures are special dynamic categories. + childOut.blocks = custom; + } else { + this.syncTrees_(childIn, childOut, pathToMedia); + } + var colour = childIn.getAttribute('colour'); + if (goog.isString(colour)) { + if (colour.match(/^#[0-9a-fA-F]{6}$/)) { + childOut.hexColour = colour; + } else { + childOut.hexColour = Blockly.hueToRgb(colour); + } + this.hasColours_ = true; + } else { + childOut.hexColour = ''; + } + if (childIn.getAttribute('expanded') == 'true') { + if (childOut.blocks.length) { + this.tree_.setSelectedItem(childOut); + } + childOut.setExpanded(true); + } else { + childOut.setExpanded(false); + } + lastElement = childIn; + break; + case 'SEP': + if (lastElement) { + if (lastElement.tagName.toUpperCase() == 'CATEGORY') { + // Separator between two categories. + // + treeOut.add(new Blockly.Toolbox.TreeSeparator( + this.treeSeparatorConfig_)); + } else { + // Change the gap between two blocks. + // + // The default gap is 24, can be set larger or smaller. + // Note that a deprecated method is to add a gap to a block. + // + var newGap = parseFloat(childIn.getAttribute('gap')); + if (!isNaN(newGap)) { + var oldGap = parseFloat(lastElement.getAttribute('gap')); + var gap = isNaN(oldGap) ? newGap : oldGap + newGap; + lastElement.setAttribute('gap', gap); + } + } + } + break; + case 'BLOCK': + case 'SHADOW': + treeOut.blocks.push(childIn); + lastElement = childIn; + break; + } + } +}; + + /** * Recursively add colours to this toolbox. * @param {Blockly.Toolbox.TreeNode} opt_tree Starting point of tree. From c5246fc8e2aacd5bc666528db069cb36d09f10fa Mon Sep 17 00:00:00 2001 From: Markus Bordihn Date: Wed, 29 Jun 2016 23:38:32 +0200 Subject: [PATCH 2/2] Update toolbox.js --- core/toolbox.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/toolbox.js b/core/toolbox.js index 1e8c84af8..ac15ed128 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -278,7 +278,6 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { Blockly.resizeSvgContents(this.workspace_); }; - /** * Sync trees of the toolbox. * @param {Node} treeIn DOM tree of blocks, or null. @@ -357,7 +356,6 @@ Blockly.Toolbox.prototype.syncTrees_ = function(treeIn, treeOut, pathToMedia) { } }; - /** * Recursively add colours to this toolbox. * @param {Blockly.Toolbox.TreeNode} opt_tree Starting point of tree. @@ -457,6 +455,7 @@ Blockly.Toolbox.TreeControl.prototype.enterDocument = function() { this.handleTouchEvent_); } }; + /** * Handles touch events. * @param {!goog.events.BrowserEvent} e The browser event.