From 086fd720b2f48a3d6f9125050ffd72ce181deb16 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 8 Dec 2016 13:05:19 -0800 Subject: [PATCH] Associate flyout button callbacks directly with workspaces --- core/blockly.js | 22 ---------------------- core/flyout_button.js | 4 ++-- core/variables.js | 2 +- core/workspace_svg.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/core/blockly.js b/core/blockly.js index 4428bca57..7d71259c4 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -117,13 +117,6 @@ Blockly.clipboardSource_ = null; */ Blockly.dragMode_ = Blockly.DRAG_NONE; -/** - * Map from function names to callbacks, for deciding what to do when a button - * is clicked. - * @type {!Object} - */ -Blockly.flyoutButtonCallbacks_ = {}; - /** * Cached value for whether 3D is supported. * @type {!boolean} @@ -131,21 +124,6 @@ Blockly.flyoutButtonCallbacks_ = {}; */ Blockly.cache3dSupported_ = null; -/** - * Register a callback function associated with a given key, for clicks on - * buttons and labels in the flyout. - * For instance, a button specified by the XML - * - * should be matched by a call to - * registerButtonCallback("CREATE_VARIABLE", yourCallbackFunction). - * @param {string} key The name to use to look up this function. - * @param {function(!Blockly.FlyoutButton)} func The function to call when the - * given button is clicked. - */ -Blockly.registerButtonCallback = function(key, func) { - Blockly.flyoutButtonCallbacks_[key] = func; -}; - /** * Convert a hue (HSV model) into an RGB hex triplet. * @param {number} hue Hue on a colour wheel (0-360). diff --git a/core/flyout_button.js b/core/flyout_button.js index 4e3fd7ec7..334cb3e6a 100644 --- a/core/flyout_button.js +++ b/core/flyout_button.js @@ -84,10 +84,10 @@ Blockly.FlyoutButton = function(workspace, targetWorkspace, xml, isLabel) { if (this.isLabel_ && callbackKey) { console.warn('Labels should not have callbacks. Label text: ' + this.text_); } else if (!this.isLabel_ && - !(callbackKey && Blockly.flyoutButtonCallbacks_[callbackKey])) { + !(callbackKey && targetWorkspace.getButtonCallback(callbackKey))) { console.warn('Buttons should have callbacks. Button text: ' + this.text_); } else { - this.callback_ = Blockly.flyoutButtonCallbacks_[callbackKey]; + this.callback_ = targetWorkspace.getButtonCallback(callbackKey); } /** diff --git a/core/variables.js b/core/variables.js index b8439b239..8eec06df4 100644 --- a/core/variables.js +++ b/core/variables.js @@ -105,7 +105,7 @@ Blockly.Variables.flyoutCategory = function(workspace) { button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); button.setAttribute('callbackKey', 'CREATE_VARIABLE'); - Blockly.registerButtonCallback('CREATE_VARIABLE', function(button) { + workspace.registerButtonCallback('CREATE_VARIABLE', function(button) { Blockly.Variables.createVariable(button.getTargetWorkspace()); }); diff --git a/core/workspace_svg.js b/core/workspace_svg.js index bd1a75a0e..7915bc217 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -199,6 +199,14 @@ Blockly.WorkspaceSvg.prototype.lastSound_ = null; */ Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; +/** + * Map from function names to callbacks, for deciding what to do when a button + * is clicked. + * @type {!Object} + * @private + */ +Blockly.WorkspaceSvg.prototype.flyoutButtonCallbacks_ = {}; + /** * Inverted screen CTM, for use in mouseToSvg. * @type {SVGMatrix} @@ -1512,6 +1520,32 @@ Blockly.WorkspaceSvg.prototype.clear = function() { this.setResizesEnabled(true); }; +/** + * Register a callback function associated with a given key, for clicks on + * buttons and labels in the flyout. + * For instance, a button specified by the XML + * + * should be matched by a call to + * registerButtonCallback("CREATE_VARIABLE", yourCallbackFunction). + * @param {string} key The name to use to look up this function. + * @param {function(!Blockly.FlyoutButton)} func The function to call when the + * given button is clicked. + */ +Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) { + this.flyoutButtonCallbacks_[key] = func; +}; + +/** + * Get the callback function associated with a given key, for clicks on buttons + * and labels in the flyout. + * @param {string} key The name to use to look up the function. + * @return {function(!Blockly.FlyoutButton)} The function corresponding to the + * given key for this workspace. + */ +Blockly.WorkspaceSvg.prototype.getButtonCallback = function(key) { + return this.flyoutButtonCallbacks_[key]; +}; + // Export symbols that would otherwise be renamed by Closure compiler. Blockly.WorkspaceSvg.prototype['setVisible'] = Blockly.WorkspaceSvg.prototype.setVisible;