Enable custom flyout categories.

This commit is contained in:
Rachel Fenichel
2017-01-23 17:21:48 -08:00
parent 9f564fba35
commit 4872a6ad7f
5 changed files with 62 additions and 18 deletions

View File

@@ -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';

View File

@@ -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);

View File

@@ -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.

View File

@@ -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.

View File

@@ -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<string, function(!Blockly.Workspace):!Array<!Element>>}
* @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<!Element>} 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<!Element>} 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;