From f6688d033923a2b53da401d6998e906b7d938ff2 Mon Sep 17 00:00:00 2001 From: Maribeth Bottorff Date: Wed, 16 Sep 2020 10:54:17 -0700 Subject: [PATCH] Convert toolbox position to enum (#4284) Convert toolbox position to enum --- core/mutator.js | 7 ++++--- core/options.js | 12 +++++++----- core/toolbox/toolbox.js | 11 +++++------ core/trashcan.js | 9 +++++---- core/utils/toolbox.js | 11 +++++++++++ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/core/mutator.js b/core/mutator.js index ec845b733..4cdda5e34 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -23,12 +23,12 @@ goog.require('Blockly.utils'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.global'); goog.require('Blockly.utils.object'); +goog.require('Blockly.utils.toolbox'); goog.require('Blockly.utils.xml'); goog.require('Blockly.WorkspaceSvg'); goog.require('Blockly.Xml'); goog.requireType('Blockly.utils.Metrics'); -goog.requireType('Blockly.utils.toolbox'); /** @@ -169,8 +169,9 @@ Blockly.Mutator.prototype.createEditor_ = function() { 'renderer': this.block_.workspace.options.renderer, 'rendererOverrides': this.block_.workspace.options.rendererOverrides })); - workspaceOptions.toolboxPosition = this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT : - Blockly.TOOLBOX_AT_LEFT; + workspaceOptions.toolboxPosition = this.block_.RTL ? + Blockly.utils.toolbox.Position.RIGHT : + Blockly.utils.toolbox.Position.LEFT; var hasFlyout = !!quarkXml; if (hasFlyout) { workspaceOptions.languageTree = diff --git a/core/options.js b/core/options.js index c515df354..7701b0b9e 100644 --- a/core/options.js +++ b/core/options.js @@ -82,12 +82,14 @@ Blockly.Options = function(options) { var toolboxAtStart = options['toolboxPosition']; toolboxAtStart = toolboxAtStart !== 'end'; + /** @type {!Blockly.utils.toolbox.Position} */ + var toolboxPosition; if (horizontalLayout) { - var toolboxPosition = toolboxAtStart ? - Blockly.TOOLBOX_AT_TOP : Blockly.TOOLBOX_AT_BOTTOM; + toolboxPosition = toolboxAtStart ? + Blockly.utils.toolbox.Position.TOP : Blockly.utils.toolbox.Position.BOTTOM; } else { - var toolboxPosition = (toolboxAtStart == rtl) ? - Blockly.TOOLBOX_AT_RIGHT : Blockly.TOOLBOX_AT_LEFT; + toolboxPosition = (toolboxAtStart == rtl) ? + Blockly.utils.toolbox.Position.RIGHT : Blockly.utils.toolbox.Position.LEFT; } var hasCss = options['css']; @@ -152,7 +154,7 @@ Blockly.Options = function(options) { this.gridOptions = Blockly.Options.parseGridOptions_(options); /** @type {!Blockly.Options.ZoomOptions} */ this.zoomOptions = Blockly.Options.parseZoomOptions_(options); - /** @type {number} */ + /** @type {!Blockly.utils.toolbox.Position} */ this.toolboxPosition = toolboxPosition; /** @type {!Blockly.Theme} */ this.theme = Blockly.Options.parseThemeOptions_(options); diff --git a/core/toolbox/toolbox.js b/core/toolbox/toolbox.js index 50117758a..b190bccfa 100644 --- a/core/toolbox/toolbox.js +++ b/core/toolbox/toolbox.js @@ -23,6 +23,7 @@ goog.require('Blockly.utils'); goog.require('Blockly.utils.aria'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.Rect'); +goog.require('Blockly.utils.toolbox'); goog.requireType('Blockly.Action'); goog.requireType('Blockly.IBlocklyActionable'); @@ -33,7 +34,6 @@ goog.requireType('Blockly.ISelectableToolboxItem'); goog.requireType('Blockly.IStyleable'); goog.requireType('Blockly.IToolbox'); goog.requireType('Blockly.IToolboxItem'); -goog.requireType('Blockly.utils.toolbox'); goog.requireType('Blockly.WorkspaceSvg'); @@ -126,8 +126,7 @@ Blockly.Toolbox = function(workspace) { /** * Position of the toolbox and flyout relative to the workspace. - * TODO (#4246): Add an enum for toolbox types. - * @type {number} + * @type {!Blockly.utils.toolbox.Position} */ this.toolboxPosition = workspace.options.toolboxPosition; @@ -484,11 +483,11 @@ Blockly.Toolbox.prototype.getClientRect = function() { // Assumes that the toolbox is on the SVG edge. If this changes // (e.g. toolboxes in mutators) then this code will need to be more complex. - if (this.toolboxPosition == Blockly.TOOLBOX_AT_TOP) { + if (this.toolboxPosition == Blockly.utils.toolbox.Position.TOP) { return new Blockly.utils.Rect(-BIG_NUM, bottom, -BIG_NUM, BIG_NUM); - } else if (this.toolboxPosition == Blockly.TOOLBOX_AT_BOTTOM) { + } else if (this.toolboxPosition == Blockly.utils.toolbox.Position.BOTTOM) { return new Blockly.utils.Rect(top, BIG_NUM, -BIG_NUM, BIG_NUM); - } else if (this.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) { + } else if (this.toolboxPosition == Blockly.utils.toolbox.Position.LEFT) { return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, -BIG_NUM, right); } else { // Right return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, left, BIG_NUM); diff --git a/core/trashcan.js b/core/trashcan.js index 7820512c8..d8686ae8c 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -15,6 +15,7 @@ goog.provide('Blockly.Trashcan'); goog.require('Blockly.Scrollbar'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.Rect'); +goog.require('Blockly.utils.toolbox'); goog.require('Blockly.Xml'); goog.requireType('Blockly.IDeleteArea'); @@ -66,16 +67,16 @@ Blockly.Trashcan = function(workspace) { // Create vertical or horizontal flyout. if (this.workspace_.horizontalLayout) { flyoutWorkspaceOptions.toolboxPosition = - this.workspace_.toolboxPosition == Blockly.TOOLBOX_AT_TOP ? - Blockly.TOOLBOX_AT_BOTTOM : Blockly.TOOLBOX_AT_TOP; + this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.TOP ? + Blockly.utils.toolbox.Position.BOTTOM : Blockly.utils.toolbox.Position.TOP; if (!Blockly.HorizontalFlyout) { throw Error('Missing require for Blockly.HorizontalFlyout'); } this.flyout = new Blockly.HorizontalFlyout(flyoutWorkspaceOptions); } else { flyoutWorkspaceOptions.toolboxPosition = - this.workspace_.toolboxPosition == Blockly.TOOLBOX_AT_RIGHT ? - Blockly.TOOLBOX_AT_LEFT : Blockly.TOOLBOX_AT_RIGHT; + this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT ? + Blockly.utils.toolbox.Position.LEFT : Blockly.utils.toolbox.Position.RIGHT; if (!Blockly.VerticalFlyout) { throw Error('Missing require for Blockly.VerticalFlyout'); } diff --git a/core/utils/toolbox.js b/core/utils/toolbox.js index d34339414..27b76b262 100644 --- a/core/utils/toolbox.js +++ b/core/utils/toolbox.js @@ -168,6 +168,17 @@ Blockly.utils.toolbox.CATEGORY_TOOLBOX_KIND = 'categoryToolbox'; */ Blockly.utils.toolbox.FLYOUT_TOOLBOX_KIND = 'flyoutToolbox'; +/** + * Position of the the toolbox relative to the flyout. + * @enum {number} + */ +Blockly.utils.toolbox.Position = { + TOP: Blockly.TOOLBOX_AT_TOP, + BOTTOM: Blockly.TOOLBOX_AT_BOTTOM, + LEFT: Blockly.TOOLBOX_AT_LEFT, + RIGHT: Blockly.TOOLBOX_AT_RIGHT +}; + /** * Converts the toolbox definition into toolbox JSON. * @param {?Blockly.utils.toolbox.ToolboxDefinition} toolboxDef The definition