From 703bd96f7e683ae607ff09bea3641aac3840e7f3 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 31 Oct 2016 14:24:00 -0700 Subject: [PATCH 1/2] Allow creation of buttons with developer-designated callbacks --- core/blockly.js | 26 ++++++++++++++++++++++++-- core/flyout.js | 3 ++- core/flyout_button.js | 28 +++++++++++++++++++++++----- core/toolbox.js | 1 + core/variables.js | 6 ++++++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/core/blockly.js b/core/blockly.js index 4b85b99bf..5f470cda7 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -117,6 +117,28 @@ 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 + * 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). @@ -342,7 +364,7 @@ Blockly.getMainWorkspace = function() { }; /** - * Wrapper to window.alert() that app developers may override to + * Wrapper to window.alert() that app developers may override to * provide alternatives to the modal browser window. * @param {string} message The message to display to the user. * @param {function()=} opt_callback The callback when the alert is dismissed. @@ -355,7 +377,7 @@ Blockly.alert = function(message, opt_callback) { }; /** - * Wrapper to window.confirm() that app developers may override to + * Wrapper to window.confirm() that app developers may override to * provide alternatives to the modal browser window. * @param {string} message The message to display to the user. * @param {!function(boolean)} callback The callback for handling user response. diff --git a/core/flyout.js b/core/flyout.js index 48e1c5a39..d2c35db5e 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -700,8 +700,9 @@ Blockly.Flyout.prototype.show = function(xmlList) { } } else if (tagName == 'BUTTON') { var label = xml.getAttribute('text'); + var callback_key = xml.getAttribute('callbackKey'); var curButton = new Blockly.FlyoutButton(this.workspace_, - this.targetWorkspace_, label); + this.targetWorkspace_, label, callback_key); contents.push({type: 'button', button: curButton}); gaps.push(default_gap); } diff --git a/core/flyout_button.js b/core/flyout_button.js index 9e68a754d..128072942 100644 --- a/core/flyout_button.js +++ b/core/flyout_button.js @@ -32,15 +32,15 @@ goog.require('goog.math.Coordinate'); /** * Class for a button in the flyout. - * @param {!Blockly.Workspace} workspace The workspace in which to place this + * @param {!Blockly.WorkspaceSvg} workspace The workspace in which to place this * button. - * @param {!Blockly.Workspace} targetWorkspace The flyout's target workspace. + * @param {!Blockly.WorkspaceSvg} targetWorkspace The flyout's target workspace. * @param {string} text The text to display on the button. * @constructor */ -Blockly.FlyoutButton = function(workspace, targetWorkspace, text) { +Blockly.FlyoutButton = function(workspace, targetWorkspace, text, callback_key) { /** - * @type {!Blockly.Workspace} + * @type {!Blockly.WorkspaceSvg} * @private */ this.workspace_ = workspace; @@ -62,6 +62,13 @@ Blockly.FlyoutButton = function(workspace, targetWorkspace, text) { * @private */ this.position_ = new goog.math.Coordinate(0, 0); + + /** + * Function to call when this button is clicked. + * @type {function(!Blockly.FlyoutButton)} + * @private + */ + this.callback_ = Blockly.flyoutButtonCallbacks_[callback_key]; }; /** @@ -148,6 +155,15 @@ Blockly.FlyoutButton.prototype.moveTo = function(x, y) { this.updateTransform_(); }; +/** + * Get the button's target workspace. + * @return {!Blockly.WorkspaceSvg} The target workspace of the flyout where this + * button resides. + */ +Blockly.FlyoutButton.prototype.getTargetWorkspace = function() { + return this.targetWorkspace_; +}; + /** * Dispose of this button. */ @@ -172,5 +188,7 @@ Blockly.FlyoutButton.prototype.onMouseUp = function(e) { // Stop binding to mouseup and mousemove events--flyout mouseup would normally // do this, but we're skipping that. Blockly.Flyout.terminateDrag_(); - Blockly.Variables.createVariable(this.targetWorkspace_); + + // Call the callback registered to this button. + this.callback_(this); }; diff --git a/core/toolbox.js b/core/toolbox.js index d534a4990..215ecfff5 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -362,6 +362,7 @@ Blockly.Toolbox.prototype.syncTrees_ = function(treeIn, treeOut, pathToMedia) { break; case 'BLOCK': case 'SHADOW': + case 'BUTTON': treeOut.blocks.push(childIn); lastElement = childIn; break; diff --git a/core/variables.js b/core/variables.js index 35fb3139b..b8439b239 100644 --- a/core/variables.js +++ b/core/variables.js @@ -103,6 +103,12 @@ Blockly.Variables.flyoutCategory = function(workspace) { var xmlList = []; var button = goog.dom.createDom('button'); button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE'); + + Blockly.registerButtonCallback('CREATE_VARIABLE', function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace()); + }); + xmlList.push(button); if (variableList.length > 0) { From 4d02e9bee6dee165af08834d8e01ac54087e2a19 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 31 Oct 2016 14:38:55 -0700 Subject: [PATCH 2/2] lint --- core/blockly.js | 2 +- core/flyout.js | 4 ++-- core/flyout_button.js | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/blockly.js b/core/blockly.js index 5f470cda7..3d33d8e8c 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -127,7 +127,7 @@ Blockly.flyoutButtonCallbacks_ = {}; /** * 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 + * For instance, a button specified by the XML * * should be matched by a call to * registerButtonCallback("CREATE_VARIABLE", yourCallbackFunction). diff --git a/core/flyout.js b/core/flyout.js index d2c35db5e..0fed89f80 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -700,9 +700,9 @@ Blockly.Flyout.prototype.show = function(xmlList) { } } else if (tagName == 'BUTTON') { var label = xml.getAttribute('text'); - var callback_key = xml.getAttribute('callbackKey'); + var callbackKey = xml.getAttribute('callbackKey'); var curButton = new Blockly.FlyoutButton(this.workspace_, - this.targetWorkspace_, label, callback_key); + this.targetWorkspace_, label, callbackKey); contents.push({type: 'button', button: curButton}); gaps.push(default_gap); } diff --git a/core/flyout_button.js b/core/flyout_button.js index 128072942..662d228c9 100644 --- a/core/flyout_button.js +++ b/core/flyout_button.js @@ -36,9 +36,11 @@ goog.require('goog.math.Coordinate'); * button. * @param {!Blockly.WorkspaceSvg} targetWorkspace The flyout's target workspace. * @param {string} text The text to display on the button. + * @param {string} callbackKey The key to use when looking up the callback for a + * click on this button. * @constructor */ -Blockly.FlyoutButton = function(workspace, targetWorkspace, text, callback_key) { +Blockly.FlyoutButton = function(workspace, targetWorkspace, text, callbackKey) { /** * @type {!Blockly.WorkspaceSvg} * @private @@ -68,7 +70,7 @@ Blockly.FlyoutButton = function(workspace, targetWorkspace, text, callback_key) * @type {function(!Blockly.FlyoutButton)} * @private */ - this.callback_ = Blockly.flyoutButtonCallbacks_[callback_key]; + this.callback_ = Blockly.flyoutButtonCallbacks_[callbackKey]; }; /**