Add a getter; stop accessing private field sourceBlock_ from outside of a connection.

This commit is contained in:
rachel-fenichel
2016-03-15 16:03:49 -07:00
parent 06ff90bcaf
commit 2c70f7ec26
7 changed files with 49 additions and 29 deletions

View File

@@ -471,7 +471,7 @@ Blockly.Blocks['logic_ternary'] = {
if (block && !block.outputConnection.checkType_(parentConnection)) {
if (parentConnection === this.prevParentConnection_) {
this.unplug();
parentConnection.sourceBlock_.bumpNeighbours_();
parentConnection.getSourceBlock().bumpNeighbours_();
} else {
block.unplug();
block.bumpNeighbours_();

View File

@@ -221,7 +221,7 @@ Blockly.Blocks['procedures_defnoreturn'] = {
if (stackConnection.targetConnection ||
!this.statementConnection_ ||
this.statementConnection_.targetConnection ||
this.statementConnection_.sourceBlock_.workspace !=
this.statementConnection_.getSourceBlock().workspace !=
this.workspace) {
// Block no longer exists or has been attached elsewhere.
this.statementConnection_ = null;
@@ -552,7 +552,7 @@ Blockly.Blocks['procedures_callnoreturn'] = {
if (quarkName in this.quarkConnections_) {
var connection = this.quarkConnections_[quarkName];
if (!connection || connection.targetConnection ||
connection.sourceBlock_.workspace != this.workspace) {
connection.getSourceBlock().workspace != this.workspace) {
// Block no longer exists or has been attached elsewhere.
delete this.quarkConnections_[quarkName];
} else {

View File

@@ -308,7 +308,7 @@ Blockly.Block.prototype.bumpNeighbours_ = function() {
// either one of them is unconnected, then there could be confusion.
if (!connection.targetConnection || !otherConnection.targetConnection) {
// Only bump blocks if they are from different tree structures.
if (otherConnection.sourceBlock_.getRootBlock() != rootBlock) {
if (otherConnection.getSourceBlock().getRootBlock() != rootBlock) {
// Always bump the inferior block.
if (connection.isSuperior()) {
otherConnection.bumpAwayFrom_(connection);

View File

@@ -549,7 +549,7 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) {
// Determine which connection is inferior (lower in the source stack).
var inferiorConnection = Blockly.localConnection_.isSuperior() ?
Blockly.highlightedConnection_ : Blockly.localConnection_;
inferiorConnection.sourceBlock_.connectionUiEffect();
inferiorConnection.getSourceBlock().connectionUiEffect();
}
if (this.workspace.trashcan) {
// Don't throw an object in the trash can if it just got connected.

View File

@@ -66,8 +66,8 @@ Blockly.Connection.REASON_DIFFERENT_WORKSPACES = 5;
* @param {!Blockly.Connection} childConnection Connection on inferior block.
*/
Blockly.Connection.connect_ = function(parentConnection, childConnection) {
var parentBlock = parentConnection.sourceBlock_;
var childBlock = childConnection.sourceBlock_;
var parentBlock = parentConnection.getSourceBlock();
var childBlock = childConnection.getSourceBlock();
// Disconnect any existing parent on the child connection.
if (childConnection.targetConnection) {
childConnection.disconnect();
@@ -263,6 +263,14 @@ Blockly.Connection.prototype.dispose = function() {
this.dbOpposite_ = null;
};
/**
* Get the source block for this connection.
* @return {Blockly.Block} The source block, or null if there is none.
*/
Blockly.Connection.prototype.getSourceBlock = function() {
return this.sourceBlock_;
};
/**
* Does the connection belong to a superior block (higher in the source stack)?
* @return {boolean} True if connection faces down or right.
@@ -295,12 +303,13 @@ Blockly.Connection.prototype.distanceFrom = function(otherConnection) {
Blockly.Connection.prototype.canConnectWithReason_ = function(target) {
if (!target) {
return Blockly.Connection.REASON_TARGET_NULL;
} else if (this.sourceBlock_ && target.sourceBlock_ == this.sourceBlock_) {
} else if (this.sourceBlock_ &&
target.getSourceBlock() == this.sourceBlock_) {
return Blockly.Connection.REASON_SELF_CONNECTION;
} else if (target.type != Blockly.OPPOSITE_TYPE[this.type]) {
return Blockly.Connection.REASON_WRONG_TYPE;
} else if (this.sourceBlock_ && target.sourceBlock_ &&
this.sourceBlock_.workspace !== target.sourceBlock_.workspace) {
} else if (this.sourceBlock_ && target.getSourceBlock() &&
this.sourceBlock_.workspace !== target.getSourceBlock().workspace) {
return Blockly.Connection.REASON_DIFFERENT_WORKSPACES;
} else if (!this.checkType_(target)) {
return Blockly.Connection.REASON_CHECKS_FAILED;
@@ -374,7 +383,7 @@ Blockly.Connection.prototype.isConnectionAllowed = function(candidate,
}
// Don't let blocks try to connect to themselves or ones they nest.
var targetSourceBlock = candidate.sourceBlock_;
var targetSourceBlock = candidate.getSourceBlock();
var sourceBlock = this.sourceBlock_;
if (targetSourceBlock && sourceBlock) {
do {
@@ -484,11 +493,11 @@ Blockly.Connection.prototype.disconnect = function() {
if (this.isSuperior()) {
// Superior block.
parentBlock = this.sourceBlock_;
childBlock = otherConnection.sourceBlock_;
childBlock = otherConnection.getSourceBlock();
parentConnection = this;
} else {
// Inferior block.
parentBlock = otherConnection.sourceBlock_;
parentBlock = otherConnection.getSourceBlock();
childBlock = this.sourceBlock_;
parentConnection = otherConnection;
}
@@ -537,7 +546,7 @@ Blockly.Connection.prototype.disconnect = function() {
*/
Blockly.Connection.prototype.targetBlock = function() {
if (this.targetConnection) {
return this.targetConnection.sourceBlock_;
return this.targetConnection.getSourceBlock();
}
return null;
};
@@ -564,7 +573,7 @@ Blockly.Connection.prototype.bumpAwayFrom_ = function(staticConnection) {
if (!rootBlock.isMovable()) {
// Can't bump an uneditable block away.
// Check to see if the other block is movable.
rootBlock = staticConnection.sourceBlock_.getRootBlock();
rootBlock = staticConnection.getSourceBlock().getRootBlock();
if (!rootBlock.isMovable()) {
return;
}
@@ -664,7 +673,7 @@ Blockly.Connection.prototype.checkType_ = function(otherConnection) {
}
var otherTargetBlock = otherConnection.targetBlock();
if (otherTargetBlock && !otherTargetBlock.isMovable() &&
!otherConnection.sourceBlock_.isMovable()) {
!otherConnection.getSourceBlock().isMovable()) {
return false;
}
if (!this.check_ || !otherConnection.check_) {

View File

@@ -54,7 +54,7 @@ Blockly.ConnectionDB.prototype.addConnection = function(connection) {
if (connection.inDB_) {
throw 'Connection already in database.';
}
if (connection.sourceBlock_.isInFlyout) {
if (connection.getSourceBlock().isInFlyout) {
// Don't bother maintaining a database of connections in a flyout.
return;
}
@@ -177,7 +177,7 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) {
pointerMin = pointerMid;
pointerMax = pointerMid;
var neighbours = [];
var sourceBlock = connection.sourceBlock_;
var sourceBlock = connection.getSourceBlock();
if (db.length) {
while (pointerMin >= 0 && checkConnection_(pointerMin)) {
pointerMin--;

View File

@@ -38,35 +38,46 @@ function verify_DB_(msg, expected, db) {
function test_DB_addConnection() {
var db = new Blockly.ConnectionDB();
var o2 = {y_: 2, sourceBlock_: {}};
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o2);
verify_DB_('Adding connection #2', [o2], db);
var o4 = {y_: 4, sourceBlock_: {}};
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o4);
verify_DB_('Adding connection #4', [o2, o4], db);
var o1 = {y_: 1, sourceBlock_: {}};
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
verify_DB_('Adding connection #1', [o1, o2, o4], db);
var o3a = {y_: 3, sourceBlock_: {}};
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3a);
verify_DB_('Adding connection #3a', [o1, o2, o3a, o4], db);
var o3b = {y_: 3, sourceBlock_: {}};
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3b);
verify_DB_('Adding connection #3b', [o1, o2, o3b, o3a, o4], db);
}
function test_DB_removeConnection() {
var db = new Blockly.ConnectionDB();
var o1 = {y_: 1, sourceBlock_: {}};
var o2 = {y_: 2, sourceBlock_: {}};
var o3a = {y_: 3, sourceBlock_: {}};
var o3b = {y_: 3, sourceBlock_: {}};
var o3c = {y_: 3, sourceBlock_: {}};
var o4 = {y_: 4, sourceBlock_: {}};
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3c = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
db.addConnection(o2);
db.addConnection(o3c);