From 4872a6ad7f39321c1d838f253e6df8ffb77c3f0d Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 23 Jan 2017 17:21:48 -0800 Subject: [PATCH] Enable custom flyout categories. --- core/constants.js | 16 ++++++++++++++++ core/flyout.js | 15 +++++++-------- core/procedures.js | 6 +----- core/variables.js | 6 +----- core/workspace_svg.js | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/core/constants.js b/core/constants.js index 5805229bf..ef828d11d 100644 --- a/core/constants.js +++ b/core/constants.js @@ -214,3 +214,19 @@ Blockly.DELETE_AREA_TRASH = 1; * @const */ Blockly.DELETE_AREA_TOOLBOX = 2; + +/** + * String for use in the "custom" attribute of a category in toolbox xml. + * This string indicates that the category should be dynamically populated with + * variable blocks. + * @const + */ +Blockly.VARIABLE_CATEGORY_NAME = 'VARIABLE'; + +/** + * String for use in the "custom" attribute of a category in toolbox xml. + * This string indicates that the category should be dynamically populated with + * procedure blocks. + * @const + */ +Blockly.PROCEDURE_CATEGORY_NAME = 'PROCEDURE'; diff --git a/core/flyout.js b/core/flyout.js index 0a17a02c2..496a4035d 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -726,14 +726,13 @@ Blockly.Flyout.prototype.show = function(xmlList) { this.hide(); this.clearOldBlocks_(); - if (xmlList == Blockly.Variables.NAME_TYPE) { - // Special category for variables. - xmlList = - Blockly.Variables.flyoutCategory(this.workspace_.targetWorkspace); - } else if (xmlList == Blockly.Procedures.NAME_TYPE) { - // Special category for procedures. - xmlList = - Blockly.Procedures.flyoutCategory(this.workspace_.targetWorkspace); + // Handle dynamic categories, represented by a name instead of a list of XML. + // Look up the correct category generation function and call that to get a + // valid XML list. + if (typeof xmlList == 'string') { + var fnToApply = this.workspace_.targetWorkspace.getToolboxCategoryCallback( + xmlList); + xmlList = fnToApply(this.workspace_.targetWorkspace); } this.setVisible(true); diff --git a/core/procedures.js b/core/procedures.js index 6b5fcf60b..39119dc12 100644 --- a/core/procedures.js +++ b/core/procedures.js @@ -27,16 +27,12 @@ goog.provide('Blockly.Procedures'); goog.require('Blockly.Blocks'); +goog.require('Blockly.constants'); goog.require('Blockly.Field'); goog.require('Blockly.Names'); goog.require('Blockly.Workspace'); -/** - * Category to separate procedure names from variables and generated functions. - */ -Blockly.Procedures.NAME_TYPE = 'PROCEDURE'; - /** * Find all user-created procedure definitions in a workspace. * @param {!Blockly.Workspace} root Root workspace. diff --git a/core/variables.js b/core/variables.js index 8eec06df4..45f488187 100644 --- a/core/variables.js +++ b/core/variables.js @@ -27,15 +27,11 @@ goog.provide('Blockly.Variables'); goog.require('Blockly.Blocks'); +goog.require('Blockly.constants'); goog.require('Blockly.Workspace'); goog.require('goog.string'); -/** - * Category to separate variable names from procedures and generated functions. - */ -Blockly.Variables.NAME_TYPE = 'VARIABLE'; - /** * Find all user-created variables that are in use in the workspace. * For use by generators. diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 38a7e747d..04158f680 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -89,6 +89,11 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface * @private */ this.highlightedBlocks_ = []; + + this.registerToolboxCategoryCallback(Blockly.VARIABLE_CATEGORY_NAME, + Blockly.Variables.flyoutCategory); + this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME, + Blockly.Procedures.flyoutCategory); }; goog.inherits(Blockly.WorkspaceSvg, Blockly.Workspace); @@ -242,6 +247,14 @@ Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; */ Blockly.WorkspaceSvg.prototype.flyoutButtonCallbacks_ = {}; +/** + * Map from function names to callbacks, for deciding what to do when a custom + * toolbox category is opened. + * @type {!Object>} + * @private + */ +Blockly.WorkspaceSvg.prototype.toolboxCategoryCallbacks_ = {}; + /** * Inverted screen CTM, for use in mouseToSvg. * @type {SVGMatrix} @@ -1666,6 +1679,30 @@ Blockly.WorkspaceSvg.prototype.getButtonCallback = function(key) { return this.flyoutButtonCallbacks_[key]; }; +/** + * Register a callback function associated with a given key, for populating + * custom toolbox categories in this workspace. See the variable and procedure + * categories as an example. + * @param {string} key The name to use to look up this function. + * @param {function(!Blockly.Workspace):!Array} func The function to + * call when the given toolbox category is opened. + */ +Blockly.WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key, + func) { + this.toolboxCategoryCallbacks_[key] = func; +}; + +/** + * Get the callback function associated with a given key, for populating + * custom toolbox categories in this workspace. + * @param {string} key The name to use to look up the function. + * @return {function(!Blockly.Workspace):!Array} The function + * corresponding to the given key for this workspace. + */ +Blockly.WorkspaceSvg.prototype.getToolboxCategoryCallback = function(key) { + return this.toolboxCategoryCallbacks_[key]; +}; + // Export symbols that would otherwise be renamed by Closure compiler. Blockly.WorkspaceSvg.prototype['setVisible'] = Blockly.WorkspaceSvg.prototype.setVisible;