From ae1a0aab8ac02bd38fb22491633568b39e771c18 Mon Sep 17 00:00:00 2001 From: rachel-fenichel Date: Wed, 30 Mar 2016 15:09:42 -0700 Subject: [PATCH] add isConnected() to connections. --- core/block.js | 22 +++++++++++----------- core/block_render_svg.js | 10 +++++----- core/connection.js | 28 ++++++++++++++++++---------- core/mutator.js | 2 +- core/xml.js | 2 +- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/core/block.js b/core/block.js index 5abefe053..8b0ed2b1f 100644 --- a/core/block.js +++ b/core/block.js @@ -201,7 +201,7 @@ Blockly.Block.prototype.dispose = function(healStack) { var connections = this.getConnections_(true); for (var i = 0; i < connections.length; i++) { var connection = connections[i]; - if (connection.targetConnection) { + if (connection.isConnected()) { connection.disconnect(); } connections[i].dispose(); @@ -219,13 +219,13 @@ Blockly.Block.prototype.dispose = function(healStack) { */ Blockly.Block.prototype.unplug = function(opt_healStack) { if (this.outputConnection) { - if (this.outputConnection.targetConnection) { + if (this.outputConnection.isConnected()) { // Disconnect from any superior block. this.outputConnection.disconnect(); } } else if (this.previousConnection) { var previousTarget = null; - if (this.previousConnection.targetConnection) { + if (this.previousConnection.isConnected()) { // Remember the connection that any next statements need to connect to. previousTarget = this.previousConnection.targetConnection; // Detach this block from the parent's tree. @@ -313,7 +313,7 @@ Blockly.Block.prototype.bumpNeighbours_ = function() { var myConnections = this.getConnections_(false); for (var i = 0, connection; connection = myConnections[i]; i++) { // Spider down from this block bumping all sub-blocks. - if (connection.targetConnection && connection.isSuperior()) { + if (connection.isConnected() && connection.isSuperior()) { connection.targetBlock().bumpNeighbours_(); } @@ -321,7 +321,7 @@ Blockly.Block.prototype.bumpNeighbours_ = function() { for (var j = 0, otherConnection; otherConnection = neighbours[j]; j++) { // If both connections are connected, that's probably fine. But if // either one of them is unconnected, then there could be confusion. - if (!connection.targetConnection || !otherConnection.targetConnection) { + if (!connection.isConnected() || !otherConnection.isConnected()) { // Only bump blocks if they are from different tree structures. if (otherConnection.getSourceBlock().getRootBlock() != rootBlock) { // Always bump the inferior block. @@ -431,10 +431,10 @@ Blockly.Block.prototype.setParent = function(newParent) { } // Disconnect from superior blocks. - if (this.previousConnection && this.previousConnection.targetConnection) { + if (this.previousConnection && this.previousConnection.isConnected()) { throw 'Still connected to previous block.'; } - if (this.outputConnection && this.outputConnection.targetConnection) { + if (this.outputConnection && this.outputConnection.isConnected()) { throw 'Still connected to parent block.'; } this.parentBlock_ = null; @@ -721,7 +721,7 @@ Blockly.Block.prototype.setTitleValue = function(newValue, name) { */ Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) { if (this.previousConnection) { - goog.asserts.assert(!this.previousConnection.targetConnection, + goog.asserts.assert(!this.previousConnection.isConnected(), 'Must disconnect previous statement before removing connection.'); this.previousConnection.dispose(); this.previousConnection = null; @@ -750,7 +750,7 @@ Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) { */ Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) { if (this.nextConnection) { - goog.asserts.assert(!this.nextConnection.targetConnection, + goog.asserts.assert(!this.nextConnection.isConnected(), 'Must disconnect next statement before removing connection.'); this.nextConnection.dispose(); this.nextConnection = null; @@ -778,7 +778,7 @@ Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) { */ Blockly.Block.prototype.setOutput = function(newBoolean, opt_check) { if (this.outputConnection) { - goog.asserts.assert(!this.outputConnection.targetConnection, + goog.asserts.assert(!this.outputConnection.isConnected(), 'Must disconnect output value before removing connection.'); this.outputConnection.dispose(); this.outputConnection = null; @@ -1227,7 +1227,7 @@ Blockly.Block.prototype.moveNumberedInputBefore = function( Blockly.Block.prototype.removeInput = function(name, opt_quiet) { for (var i = 0, input; input = this.inputList[i]; i++) { if (input.name == name) { - if (input.connection && input.connection.targetConnection) { + if (input.connection && input.connection.isConnected()) { input.connection.setShadowDom(null); var block = input.connection.targetBlock(); if (block.isShadow()) { diff --git a/core/block_render_svg.js b/core/block_render_svg.js index c4d503370..92ba5270f 100644 --- a/core/block_render_svg.js +++ b/core/block_render_svg.js @@ -379,7 +379,7 @@ Blockly.BlockSvg.prototype.renderCompute_ = function(iconWidth) { input.renderWidth = 0; } // Expand input size if there is a connection. - if (input.connection && input.connection.targetConnection) { + if (input.connection && input.connection.isConnected()) { var linkedBlock = input.connection.targetBlock(); var bBox = linkedBlock.getHeightWidth(); input.renderHeight = Math.max(input.renderHeight, bBox.height); @@ -692,7 +692,7 @@ Blockly.BlockSvg.prototype.renderDrawRight_ = function(steps, highlightSteps, connectionY = connectionsXY.y + cursorY + Blockly.BlockSvg.INLINE_PADDING_Y + 1; input.connection.moveTo(connectionX, connectionY); - if (input.connection.targetConnection) { + if (input.connection.isConnected()) { input.connection.tighten_(); } } @@ -740,7 +740,7 @@ Blockly.BlockSvg.prototype.renderDrawRight_ = function(steps, highlightSteps, (this.RTL ? -inputRows.rightEdge - 1 : inputRows.rightEdge + 1); connectionY = connectionsXY.y + cursorY; input.connection.moveTo(connectionX, connectionY); - if (input.connection.targetConnection) { + if (input.connection.isConnected()) { input.connection.tighten_(); this.width = Math.max(this.width, inputRows.rightEdge + input.connection.targetBlock().getHeightWidth().width - @@ -822,7 +822,7 @@ Blockly.BlockSvg.prototype.renderDrawRight_ = function(steps, highlightSteps, connectionX = connectionsXY.x + (this.RTL ? -cursorX : cursorX + 1); connectionY = connectionsXY.y + cursorY + 1; input.connection.moveTo(connectionX, connectionY); - if (input.connection.targetConnection) { + if (input.connection.isConnected()) { input.connection.tighten_(); this.width = Math.max(this.width, inputRows.statementEdge + input.connection.targetBlock().getHeightWidth().width); @@ -873,7 +873,7 @@ Blockly.BlockSvg.prototype.renderDrawBottom_ = } var connectionY = connectionsXY.y + cursorY + 1; this.nextConnection.moveTo(connectionX, connectionY); - if (this.nextConnection.targetConnection) { + if (this.nextConnection.isConnected()) { this.nextConnection.tighten_(); } this.height += 4; // Height of tab. diff --git a/core/connection.js b/core/connection.js index 70ce2ba05..beef7699a 100644 --- a/core/connection.js +++ b/core/connection.js @@ -69,10 +69,10 @@ Blockly.Connection.connect_ = function(parentConnection, childConnection) { var parentBlock = parentConnection.getSourceBlock(); var childBlock = childConnection.getSourceBlock(); // Disconnect any existing parent on the child connection. - if (childConnection.targetConnection) { + if (childConnection.isConnected()) { childConnection.disconnect(); } - if (parentConnection.targetConnection) { + if (parentConnection.isConnected()) { // Other connection is already connected to something. // Disconnect it and reattach it or bump it as needed. var orphanBlock = parentConnection.targetBlock(); @@ -111,7 +111,7 @@ Blockly.Connection.connect_ = function(parentConnection, childConnection) { // block. Since this block may be a stack, walk down to the end. var newBlock = childBlock; while (newBlock.nextConnection) { - if (newBlock.nextConnection.targetConnection) { + if (newBlock.nextConnection.isConnected()) { newBlock = newBlock.getNextBlock(); } else { if (orphanBlock.previousConnection.checkType_( @@ -247,7 +247,7 @@ Blockly.Connection.prototype.hidden_ = null; * Sever all links to this connection (not including from the source object). */ Blockly.Connection.prototype.dispose = function() { - if (this.targetConnection) { + if (this.isConnected()) { throw 'Disconnect connection before disposing of it.'; } if (this.inDB_) { @@ -280,6 +280,14 @@ Blockly.Connection.prototype.isSuperior = function() { this.type == Blockly.NEXT_STATEMENT; }; +/** + * Is the connection connected? + * @return {boolean} True if connection is connected to another connection. + */ +Blockly.Connection.prototype.isConnected = function() { + return !!this.targetConnection; +}; + /** * Returns the distance between this connection and another connection. * @param {!Blockly.Connection} otherConnection The other connection to measure @@ -368,7 +376,7 @@ Blockly.Connection.prototype.isConnectionAllowed = function(candidate, // bottom of a statement block to one that's already connected. if (candidate.type == Blockly.OUTPUT_VALUE || candidate.type == Blockly.PREVIOUS_STATEMENT) { - if (candidate.targetConnection || this.targetConnection) { + if (candidate.isConnected() || this.isConnected()) { return false; } } @@ -376,7 +384,7 @@ Blockly.Connection.prototype.isConnectionAllowed = function(candidate, // Offering to connect the left (male) of a value block to an already // connected value pair is ok, we'll splice it in. // However, don't offer to splice into an immovable block. - if (candidate.type == Blockly.INPUT_VALUE && candidate.targetConnection && + if (candidate.type == Blockly.INPUT_VALUE && candidate.isConnected() && !candidate.targetBlock().isMovable() && !candidate.targetBlock().isShadow()) { return false; @@ -385,7 +393,7 @@ Blockly.Connection.prototype.isConnectionAllowed = function(candidate, // Don't let a block with no next connection bump other blocks out of the // stack. if (this.type == Blockly.PREVIOUS_STATEMENT && - candidate.targetConnection && + candidate.isConnected() && !this.sourceBlock_.nextConnection) { return false; } @@ -553,7 +561,7 @@ Blockly.Connection.prototype.disconnect = function() { * @return {Blockly.Block} The connected block or null if none is connected. */ Blockly.Connection.prototype.targetBlock = function() { - if (this.targetConnection) { + if (this.isConnected()) { return this.targetConnection.getSourceBlock(); } return null; @@ -702,7 +710,7 @@ Blockly.Connection.prototype.setCheck = function(check) { } this.check_ = check; // The new value type may not be compatible with the existing connection. - if (this.targetConnection && !this.checkType_(this.targetConnection)) { + if (this.isConnected() && !this.checkType_(this.targetConnection)) { var child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_; child.unplug(); // Bump away. @@ -763,7 +771,7 @@ Blockly.Connection.prototype.setHidden = function(hidden) { */ Blockly.Connection.prototype.hideAll = function() { this.setHidden(true); - if (this.targetConnection) { + if (this.isConnected()) { var blocks = this.targetBlock().getDescendants(); for (var b = 0; b < blocks.length; b++) { var block = blocks[b]; diff --git a/core/mutator.js b/core/mutator.js index 095f957f5..ba7c5c27a 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -359,7 +359,7 @@ Blockly.Mutator.reconnect = function(connectionChild, block, inputName) { var currentParent = connectionChild.targetBlock(); if ((!currentParent || currentParent == block) && connectionParent.targetConnection != connectionChild) { - if (connectionParent.targetConnection) { + if (connectionParent.isConnected()) { // There's already something connected here. Get rid of it. connectionParent.disconnect(); } diff --git a/core/xml.js b/core/xml.js index e61a22280..3cfe3663c 100644 --- a/core/xml.js +++ b/core/xml.js @@ -465,7 +465,7 @@ Blockly.Xml.domToBlockHeadless_ = function(workspace, xmlBlock) { if (childBlockNode) { if (!block.nextConnection) { throw 'Next statement does not exist.'; - } else if (block.nextConnection.targetConnection) { + } else if (block.nextConnection.isConnected()) { // This could happen if there is more than one XML 'next' tag. throw 'Next statement is already connected.'; }