Associate flyout button callbacks directly with workspaces

This commit is contained in:
Rachel Fenichel
2016-12-08 13:05:19 -08:00
parent 09980308ec
commit 086fd720b2
4 changed files with 37 additions and 25 deletions

View File

@@ -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<string, function(!Blockly.FlyoutButton)>}
*/
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
* <button text="create variable" callbackKey="CREATE_VARIABLE"></button>
* 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).

View File

@@ -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);
}
/**

View File

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

View File

@@ -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<string, function(!Blockly.FlyoutButton)>}
* @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
* <button text="create variable" callbackKey="CREATE_VARIABLE"></button>
* 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;