From 3bce368115f329ba2864b2217eeaa5a75f06fbfb Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Thu, 7 Jan 2016 17:01:01 -0800 Subject: [PATCH] Simplify workspace options by removing svg property. --- core/block_svg.js | 4 ++-- core/blockly.js | 4 ++-- core/inject.js | 3 +-- core/mutator.js | 1 - core/scrollbar.js | 2 +- core/toolbox.js | 7 +++---- core/utils.js | 3 +-- core/workspace_svg.js | 37 ++++++++++++++++++++++++++++--------- 8 files changed, 38 insertions(+), 23 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 63a302d59..d3272a090 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -1154,7 +1154,7 @@ Blockly.BlockSvg.prototype.disposeUiEffect = function() { clone.translateY_ = xy.y; clone.setAttribute('transform', 'translate(' + clone.translateX_ + ',' + clone.translateY_ + ')'); - this.workspace.options.svg.appendChild(clone); + this.workspace.getParentSvg().appendChild(clone); clone.bBox_ = clone.getBBox(); // Start the animation. Blockly.BlockSvg.disposeUiStep_(clone, this.RTL, new Date(), @@ -1212,7 +1212,7 @@ Blockly.BlockSvg.prototype.connectionUiEffect = function() { var ripple = Blockly.createSvgElement('circle', {'cx': xy.x, 'cy': xy.y, 'r': 0, 'fill': 'none', 'stroke': '#888', 'stroke-width': 10}, - this.workspace.options.svg); + this.workspace.getParentSvg()); // Start the animation. Blockly.BlockSvg.connectionUiStep_(ripple, new Date(), this.workspace.scale); }; diff --git a/core/blockly.js b/core/blockly.js index 9a8701e70..32dfc8bdc 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -254,7 +254,7 @@ Blockly.svgResize = function(workspace) { while (mainWorkspace.options.parentWorkspace) { mainWorkspace = mainWorkspace.options.parentWorkspace; } - var svg = mainWorkspace.options.svg; + var svg = mainWorkspace.getParentSvg(); var div = svg.parentNode; if (!div) { // Workspace deteted, or something. @@ -517,7 +517,7 @@ Blockly.hideChaff = function(opt_allowToolbox) { * @this Blockly.WorkspaceSvg */ Blockly.getMainWorkspaceMetrics_ = function() { - var svgSize = Blockly.svgSize(this.options.svg); + var svgSize = Blockly.svgSize(this.getParentSvg()); if (this.toolbox_) { svgSize.width -= this.toolbox_.width; } diff --git a/core/inject.js b/core/inject.js index fc2467511..ea0caaa8f 100644 --- a/core/inject.js +++ b/core/inject.js @@ -328,7 +328,6 @@ Blockly.createDom_ = function(container, options) { // x1, y1, x1, x2 properties will be set later in updateGridPattern_. } options.gridPattern = gridPattern; - options.svg = svg; return svg; }; @@ -411,7 +410,7 @@ Blockly.createMainWorkspace_ = function(svg, options) { */ Blockly.init_ = function(mainWorkspace) { var options = mainWorkspace.options; - var svg = mainWorkspace.options.svg; + var svg = mainWorkspace.getParentSvg(); // Supress the browser's context menu. Blockly.bindEvent_(svg, 'contextmenu', null, function(e) { diff --git a/core/mutator.js b/core/mutator.js index e21b1033a..80a472489 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -102,7 +102,6 @@ Blockly.Mutator.prototype.iconClick_ = function(e) { Blockly.Mutator.prototype.createEditor_ = function() { /* Create the editor. Here's the markup that will be generated: - [Workspace] */ diff --git a/core/scrollbar.js b/core/scrollbar.js index b833ebe14..e872900f0 100644 --- a/core/scrollbar.js +++ b/core/scrollbar.js @@ -369,7 +369,7 @@ Blockly.Scrollbar.prototype.onMouseDownBar_ = function(e) { e.stopPropagation(); return; } - var mouseXY = Blockly.mouseToSvg(e, this.workspace_.options.svg); + var mouseXY = Blockly.mouseToSvg(e, this.workspace_.getParentSvg()); var mouseLocation = this.horizontal_ ? mouseXY.x : mouseXY.y; var knobXY = Blockly.getSvgXY_(this.svgKnob_, this.workspace_); diff --git a/core/toolbox.js b/core/toolbox.js index 3b681811a..227e8e0c4 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -116,8 +116,7 @@ Blockly.Toolbox.prototype.init = function() { var workspaceOptions = { disabledPatternId: workspace.options.disabledPatternId, parentWorkspace: workspace, - RTL: workspace.RTL, - svg: workspace.options.svg + RTL: workspace.RTL }; /** * @type {!Blockly.Flyout} @@ -162,7 +161,7 @@ Blockly.Toolbox.prototype.position = function() { // Not initialized yet. return; } - var svg = this.workspace_.options.svg; + var svg = this.workspace_.getParentSvg(); var svgPosition = goog.style.getPageOffset(svg); var svgSize = Blockly.svgSize(svg); if (this.workspace_.RTL) { @@ -296,7 +295,7 @@ Blockly.Toolbox.prototype.getRect = 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.workspace_.RTL) { - var svgSize = Blockly.svgSize(this.workspace_.options.svg); + var svgSize = Blockly.svgSize(this.workspace_.getParentSvg()); var x = svgSize.width - this.width; } else { var x = -BIG_NUM; diff --git a/core/utils.js b/core/utils.js index 9d19a9fc2..c8fc97c0f 100644 --- a/core/utils.js +++ b/core/utils.js @@ -318,7 +318,7 @@ Blockly.getSvgXY_ = function(element, workspace) { x += xy.x * scale; y += xy.y * scale; element = element.parentNode; - } while (element && element != workspace.options.svg); + } while (element && element != workspace.getParentSvg()); return new goog.math.Coordinate(x, y); }; @@ -591,4 +591,3 @@ Blockly.genUid.crypto_ = this.crypto; */ Blockly.genUid.soup_ = '!#$%()*+,-./:;=?@[]^_`{|}~' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 906e35df2..8f4bff4ed 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -165,8 +165,8 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { if (opt_backgroundClass) { /** @type {SVGElement} */ this.svgBackground_ = Blockly.createSvgElement('rect', - {'height': '100%', 'width': '100%', - 'class': opt_backgroundClass}, this.svgGroup_); + {'height': '100%', 'width': '100%', 'class': opt_backgroundClass}, + this.svgGroup_); if (opt_backgroundClass == 'blocklyMainBackground') { this.svgBackground_.style.fill = 'url(#' + this.options.gridPattern.id + ')'; @@ -242,7 +242,7 @@ Blockly.WorkspaceSvg.prototype.dispose = function() { } if (!this.options.parentWorkspace) { // Top-most workspace. Dispose of the SVG too. - goog.dom.removeNode(this.options.svg); + goog.dom.removeNode(this.getParentSvg()); } }; @@ -340,6 +340,25 @@ Blockly.WorkspaceSvg.prototype.getBubbleCanvas = function() { return this.svgBubbleCanvas_; }; +/** + * Get the SVG element that forms the bubble surface. + * @return {!Element} SVG element. + */ +Blockly.WorkspaceSvg.prototype.getParentSvg = function() { + if (this.getParentSvg.cachedParentSvg_) { + return this.getParentSvg.cachedParentSvg_; + } + var element = this.svgGroup_; + while (element) { + if (element.tagName == 'svg') { + this.getParentSvg.cachedParentSvg_ = element; + return element; + } + element = element.parentNode; + } + return null; +}; + /** * Translate this workspace to new coordinates. * @param {number} x Horizontal translation. @@ -389,7 +408,7 @@ Blockly.WorkspaceSvg.prototype.getWidth = function() { * @param {boolean} isVisible True if workspace should be visible. */ Blockly.WorkspaceSvg.prototype.setVisible = function(isVisible) { - this.options.svg.style.display = isVisible ? 'block' : 'none'; + this.getParentSvg().style.display = isVisible ? 'block' : 'none'; if (this.toolbox_) { // Currently does not support toolboxes in mutators. this.toolbox_.HtmlDiv.style.display = isVisible ? 'block' : 'none'; @@ -562,7 +581,7 @@ Blockly.WorkspaceSvg.prototype.recordDeleteAreas = function() { */ Blockly.WorkspaceSvg.prototype.isDeleteArea = function(e) { var isDelete = false; - var mouseXY = Blockly.mouseToSvg(e, Blockly.mainWorkspace.options.svg); + var mouseXY = Blockly.mouseToSvg(e, Blockly.mainWorkspace.getParentSvg()); var xy = new goog.math.Coordinate(mouseXY.x, mouseXY.y); if (this.deleteAreaTrash_) { if (this.deleteAreaTrash_.contains(xy)) { @@ -640,7 +659,7 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { */ Blockly.WorkspaceSvg.prototype.startDrag = function(e, x, y) { // Record the starting offset between the bubble's location and the mouse. - var point = Blockly.mouseToSvg(e, this.options.svg); + var point = Blockly.mouseToSvg(e, this.getParentSvg()); // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; @@ -654,7 +673,7 @@ Blockly.WorkspaceSvg.prototype.startDrag = function(e, x, y) { * @return {!goog.math.Coordinate} New location of object. */ Blockly.WorkspaceSvg.prototype.moveDrag = function(e) { - var point = Blockly.mouseToSvg(e, this.options.svg); + var point = Blockly.mouseToSvg(e, this.getParentSvg()); // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; @@ -672,7 +691,7 @@ Blockly.WorkspaceSvg.prototype.onMouseWheel_ = function(e) { // TODO: Remove terminateDrag and compensate for coordinate skew during zoom. Blockly.terminateDrag_(); var delta = e.deltaY > 0 ? -1 : 1; - var position = Blockly.mouseToSvg(e, this.options.svg); + var position = Blockly.mouseToSvg(e, this.getParentSvg()); this.zoom(position.x, position.y, delta); e.preventDefault(); }; @@ -966,7 +985,7 @@ Blockly.WorkspaceSvg.prototype.markFocused = function() { Blockly.WorkspaceSvg.prototype.zoom = function(x, y, type) { var speed = this.options.zoomOptions.scaleSpeed; var metrics = this.getMetrics(); - var center = this.options.svg.createSVGPoint(); + var center = this.getParentSvg().createSVGPoint(); center.x = x; center.y = y; center = center.matrixTransform(this.getCanvas().getCTM().inverse());