|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Connection');
|
|
|
|
goog.require('Blockly.Events.BlockMove');
|
|
|
|
goog.require('goog.asserts');
|
|
goog.require('goog.dom');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection = function(source, type) {
|
|
|
|
|
|
|
|
|
|
this.sourceBlock_ = source;
|
|
|
|
this.type = type;
|
|
|
|
if (source.workspace.connectionDBList) {
|
|
this.db_ = source.workspace.connectionDBList[type];
|
|
this.dbOpposite_ =
|
|
source.workspace.connectionDBList[Blockly.OPPOSITE_TYPE[type]];
|
|
this.hidden_ = !this.db_;
|
|
}
|
|
this.generator_ = false
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.CAN_CONNECT = 0;
|
|
Blockly.Connection.REASON_SELF_CONNECTION = 1;
|
|
Blockly.Connection.REASON_WRONG_TYPE = 2;
|
|
Blockly.Connection.REASON_TARGET_NULL = 3;
|
|
Blockly.Connection.REASON_CHECKS_FAILED = 4;
|
|
Blockly.Connection.REASON_DIFFERENT_WORKSPACES = 5;
|
|
Blockly.Connection.REASON_SHADOW_PARENT = 6;
|
|
|
|
Blockly.Connection.REASON_ARGUMENT_GENERATOR = 7;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.targetConnection = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.check_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.shape_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.shadowDom_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.x_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.y_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.inDB_ = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.db_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.dbOpposite_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.hidden_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.connect_ = function(childConnection) {
|
|
var parentConnection = this;
|
|
var parentBlock = parentConnection.getSourceBlock();
|
|
var childBlock = childConnection.getSourceBlock();
|
|
var isSurroundingC = false;
|
|
if (parentConnection == parentBlock.getFirstStatementConnection()) {
|
|
isSurroundingC = true;
|
|
}
|
|
|
|
if (childConnection.isConnected()) {
|
|
|
|
|
|
|
|
if (isSurroundingC) {
|
|
var previousParentConnection = childConnection.targetConnection;
|
|
}
|
|
childConnection.disconnect();
|
|
}
|
|
if (parentConnection.isConnected()) {
|
|
|
|
|
|
var orphanBlock = parentConnection.targetBlock();
|
|
var shadowDom = parentConnection.getShadowDom();
|
|
|
|
parentConnection.setShadowDom(null);
|
|
|
|
if (orphanBlock.isShadow()) {
|
|
|
|
shadowDom = Blockly.Xml.blockToDom(orphanBlock);
|
|
orphanBlock.dispose();
|
|
orphanBlock = null;
|
|
} else if (parentConnection.type == Blockly.NEXT_STATEMENT) {
|
|
|
|
|
|
|
|
if (!orphanBlock.previousConnection) {
|
|
throw 'Orphan block does not have a previous connection.';
|
|
}
|
|
|
|
|
|
var newBlock = childBlock;
|
|
while (newBlock.nextConnection) {
|
|
var nextBlock = newBlock.getNextBlock();
|
|
if (nextBlock && !nextBlock.isShadow()) {
|
|
newBlock = nextBlock;
|
|
} else {
|
|
if (orphanBlock.previousConnection.checkType_(
|
|
newBlock.nextConnection)) {
|
|
newBlock.nextConnection.connect(orphanBlock.previousConnection);
|
|
orphanBlock = null;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (orphanBlock) {
|
|
|
|
parentConnection.disconnect();
|
|
if (Blockly.Events.recordUndo) {
|
|
|
|
var group = Blockly.Events.getGroup();
|
|
setTimeout(function() {
|
|
|
|
if (orphanBlock.workspace && !orphanBlock.getParent()) {
|
|
Blockly.Events.setGroup(group);
|
|
if (orphanBlock.outputConnection) {
|
|
orphanBlock.outputConnection.bumpAwayFrom_(parentConnection);
|
|
} else if (orphanBlock.previousConnection) {
|
|
orphanBlock.previousConnection.bumpAwayFrom_(parentConnection);
|
|
}
|
|
Blockly.Events.setGroup(false);
|
|
}
|
|
}, Blockly.BUMP_DELAY);
|
|
}
|
|
}
|
|
|
|
parentConnection.setShadowDom(shadowDom);
|
|
}
|
|
|
|
if (isSurroundingC && previousParentConnection) {
|
|
previousParentConnection.connect(parentBlock.previousConnection);
|
|
}
|
|
|
|
var event;
|
|
if (Blockly.Events.isEnabled()) {
|
|
event = new Blockly.Events.BlockMove(childBlock);
|
|
}
|
|
|
|
Blockly.Connection.connectReciprocally_(parentConnection, childConnection);
|
|
|
|
childBlock.setParent(parentBlock);
|
|
if (event) {
|
|
event.recordNew();
|
|
Blockly.Events.fire(event);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.dispose = function() {
|
|
if (this.isConnected()) {
|
|
this.disconnect()
|
|
}
|
|
if (this.inDB_) {
|
|
this.db_.removeConnection_(this);
|
|
}
|
|
this.db_ = null;
|
|
this.dbOpposite_ = null;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.isConnectedToNonInsertionMarker = function() {
|
|
return this.targetConnection && !this.targetBlock().isInsertionMarker();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.getSourceBlock = function() {
|
|
return this.sourceBlock_;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.isSuperior = function() {
|
|
return this.type == Blockly.INPUT_VALUE ||
|
|
this.type == Blockly.NEXT_STATEMENT;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.isConnected = function() {
|
|
return !!this.targetConnection;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.canConnectWithReason_ = function(target) {
|
|
if (!target) {
|
|
return Blockly.Connection.REASON_TARGET_NULL;
|
|
}
|
|
var blockA = this.sourceBlock_;
|
|
var blockB = target.getSourceBlock();
|
|
if (blockA && blockA == blockB) {
|
|
return Blockly.Connection.REASON_SELF_CONNECTION;
|
|
} else if (target.type != Blockly.OPPOSITE_TYPE[this.type]) {
|
|
return Blockly.Connection.REASON_WRONG_TYPE;
|
|
} else if (blockA && blockB && blockA.workspace !== blockB.workspace) {
|
|
return Blockly.Connection.REASON_DIFFERENT_WORKSPACES;
|
|
} else if (!this.checkType_(target)) {
|
|
return Blockly.Connection.REASON_CHECKS_FAILED;
|
|
} else if (blockA.isShadow() && !blockB.isShadow() && !blockA.type === 'polygon') {
|
|
return Blockly.Connection.REASON_SHADOW_PARENT;
|
|
} else if ((this.targetConnection &&
|
|
this.targetConnection.sourceBlock_ &&
|
|
(Blockly.scratchBlocksUtils.isShadowArgumentReporter(this.targetConnection.sourceBlock_) || this.targetConnection.sourceBlock_.canDragDuplicate())) ||
|
|
(target.targetConnection &&
|
|
target.targetConnection.sourceBlock_ &&
|
|
(Blockly.scratchBlocksUtils.isShadowArgumentReporter(target.targetConnection.sourceBlock_) || target.targetConnection.sourceBlock_.canDragDuplicate()))) {
|
|
return Blockly.Connection.REASON_ARGUMENT_GENERATOR
|
|
}
|
|
return Blockly.Connection.CAN_CONNECT;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.checkConnection_ = function(target) {
|
|
switch (this.canConnectWithReason_(target)) {
|
|
case Blockly.Connection.CAN_CONNECT:
|
|
break;
|
|
case Blockly.Connection.REASON_SELF_CONNECTION:
|
|
throw 'Attempted to connect a block to itself.';
|
|
case Blockly.Connection.REASON_DIFFERENT_WORKSPACES:
|
|
|
|
throw 'Blocks not on same workspace.';
|
|
case Blockly.Connection.REASON_WRONG_TYPE:
|
|
if (this.sourceBlock_.type.startsWith(Blockly.PROCEDURES_DEFINITION_BLOCK_TYPE)) {
|
|
console.warn('tried changing the type of a procedure, attempting to change define block type')
|
|
throw new Error('couldnt generate new procedure defintion block')
|
|
}
|
|
throw 'Attempt to connect incompatible types.';
|
|
case Blockly.Connection.REASON_TARGET_NULL:
|
|
throw 'Target connection is null.';
|
|
case Blockly.Connection.REASON_CHECKS_FAILED:
|
|
var msg = 'Connection checks failed. ';
|
|
msg += this + ' expected ' + this.check_ + ', found ' + target.check_;
|
|
throw msg;
|
|
case Blockly.Connection.REASON_SHADOW_PARENT:
|
|
throw 'Connecting non-shadow to shadow block.';
|
|
case Blockly.Connection.REASON_ARGUMENT_GENERATOR:
|
|
throw 'tried connecting a block to a generator.';
|
|
|
|
default:
|
|
throw 'Unknown connection failure: this should never happen!';
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.canConnectToPrevious_ = function(candidate) {
|
|
if (this.targetConnection) {
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
if (Blockly.draggingConnections_.indexOf(candidate) != -1) {
|
|
return false;
|
|
}
|
|
|
|
var firstStatementConnection =
|
|
this.sourceBlock_.getFirstStatementConnection();
|
|
|
|
var isComplexStatement = firstStatementConnection != null;
|
|
var isFirstStatementConnection = this == firstStatementConnection;
|
|
var isNextConnection = this == this.sourceBlock_.nextConnection;
|
|
|
|
|
|
|
|
|
|
if (isComplexStatement && !isFirstStatementConnection && !isNextConnection) {
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
var sourceHasPreviousConn = this.sourceBlock_.previousConnection != null;
|
|
|
|
|
|
if (isFirstStatementConnection &&
|
|
sourceHasPreviousConn &&
|
|
candidate.sourceBlock_.previousConnection.targetConnection &&
|
|
!this.sourceBlock_.previousConnection.checkType_(candidate.sourceBlock_.previousConnection.targetConnection)) {
|
|
return false
|
|
}
|
|
if (isFirstStatementConnection && sourceHasPreviousConn) {
|
|
return true;
|
|
}
|
|
|
|
if (isNextConnection ||
|
|
(isFirstStatementConnection && !sourceHasPreviousConn)) {
|
|
|
|
if (!candidate.targetConnection) {
|
|
return true;
|
|
}
|
|
|
|
var targetBlock = candidate.targetBlock();
|
|
|
|
if (!targetBlock.isInsertionMarker()) {
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
return !targetBlock.getPreviousBlock();
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.isConnectionAllowed = function(candidate) {
|
|
|
|
|
|
if (candidate.sourceBlock_.isInsertionMarker()) {
|
|
return false;
|
|
}
|
|
|
|
|
|
var canConnect = this.canConnectWithReason_(candidate);
|
|
if (canConnect != Blockly.Connection.CAN_CONNECT) {
|
|
return false;
|
|
}
|
|
|
|
var firstStatementConnection =
|
|
this.sourceBlock_.getFirstStatementConnection();
|
|
switch (candidate.type) {
|
|
case Blockly.PREVIOUS_STATEMENT:
|
|
return this.canConnectToPrevious_(candidate);
|
|
case Blockly.OUTPUT_VALUE: {
|
|
|
|
return false;
|
|
}
|
|
case Blockly.INPUT_VALUE: {
|
|
|
|
|
|
|
|
if (candidate.targetConnection &&
|
|
!candidate.targetBlock().isMovable() &&
|
|
!candidate.targetBlock().isShadow()) {
|
|
return false;
|
|
}
|
|
break;
|
|
}
|
|
case Blockly.NEXT_STATEMENT: {
|
|
|
|
|
|
|
|
|
|
if (firstStatementConnection &&
|
|
this == this.sourceBlock_.previousConnection &&
|
|
candidate.isConnectedToNonInsertionMarker() &&
|
|
!firstStatementConnection.targetConnection) {
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
if (candidate.isConnectedToNonInsertionMarker() &&
|
|
!this.sourceBlock_.nextConnection &&
|
|
!candidate.targetBlock().isShadow() &&
|
|
candidate.targetBlock().nextConnection) {
|
|
return false;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
throw 'Unknown connection type in isConnectionAllowed';
|
|
}
|
|
|
|
|
|
if (Blockly.draggingConnections_.indexOf(candidate) != -1) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.connect = function(otherConnection) {
|
|
if (this.targetConnection == otherConnection) {
|
|
|
|
return;
|
|
}
|
|
this.checkConnection_(otherConnection);
|
|
|
|
if (this.isSuperior()) {
|
|
|
|
if (!this.otherConnection && this.check_) {
|
|
|
|
const block = otherConnection.sourceBlock_;
|
|
const hasBranches = block.inputList.some(i => i.type === Blockly.NEXT_STATEMENT);
|
|
if (!hasBranches && block.type !== 'procedures_prototype') {
|
|
if (block.originalOutputShape_ === undefined) block.originalOutputShape_ = block.outputShape_;
|
|
const lastConnectShape = block.outputShape_;
|
|
block.outputShape_ = this.getOutputShape();
|
|
if (block.rendered && block.outputShape_ !== lastConnectShape) block.render(true);
|
|
}
|
|
}
|
|
this.connect_(otherConnection);
|
|
} else {
|
|
|
|
if (!this.check_ && otherConnection.check_) {
|
|
|
|
const block = this.sourceBlock_;
|
|
const hasBranches = block.inputList.some(i => i.type === Blockly.NEXT_STATEMENT);
|
|
|
|
if (!hasBranches) {
|
|
if (block.originalOutputShape_ === undefined) block.originalOutputShape_ = block.outputShape_;
|
|
const lastConnectShape = block.outputShape_;
|
|
block.outputShape_ = otherConnection.getOutputShape();
|
|
if (block.rendered && block.outputShape_ !== lastConnectShape) block.render(true);
|
|
}
|
|
}
|
|
otherConnection.connect_(this);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.connectReciprocally_ = function(first, second) {
|
|
goog.asserts.assert(first && second, 'Cannot connect null connections.');
|
|
first.targetConnection = second;
|
|
second.targetConnection = first;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.singleConnection_ = function(block, orphanBlock) {
|
|
var connection = false;
|
|
for (var i = 0; i < block.inputList.length; i++) {
|
|
var thisConnection = block.inputList[i].connection;
|
|
if (thisConnection && thisConnection.type == Blockly.INPUT_VALUE &&
|
|
orphanBlock.outputConnection.checkType_(thisConnection)) {
|
|
if (connection) {
|
|
return null;
|
|
}
|
|
connection = thisConnection;
|
|
}
|
|
}
|
|
return connection;
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.disconnect = function() {
|
|
var otherConnection = this.targetConnection;
|
|
goog.asserts.assert(otherConnection, 'Source connection not connected.');
|
|
goog.asserts.assert(otherConnection.targetConnection == this,
|
|
'Target connection not connected to source connection.');
|
|
|
|
var parentBlock, childBlock, parentConnection;
|
|
if (this.isSuperior()) {
|
|
|
|
parentBlock = this.sourceBlock_;
|
|
childBlock = otherConnection.getSourceBlock();
|
|
parentConnection = this;
|
|
} else {
|
|
|
|
parentBlock = otherConnection.getSourceBlock();
|
|
childBlock = this.sourceBlock_;
|
|
parentConnection = otherConnection;
|
|
}
|
|
if (childBlock.originalOutputShape_ !== undefined) childBlock.outputShape_ = childBlock.originalOutputShape_;
|
|
this.disconnectInternal_(parentBlock, childBlock);
|
|
parentConnection.respawnShadow_();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.disconnectInternal_ = function(parentBlock,
|
|
childBlock) {
|
|
var event;
|
|
if (Blockly.Events.isEnabled()) {
|
|
event = new Blockly.Events.BlockMove(childBlock);
|
|
}
|
|
var otherConnection = this.targetConnection;
|
|
otherConnection.targetConnection = null;
|
|
this.targetConnection = null;
|
|
childBlock.setParent(null);
|
|
if (event) {
|
|
event.recordNew();
|
|
Blockly.Events.fire(event);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.respawnShadow_ = function() {
|
|
var parentBlock = this.getSourceBlock();
|
|
var shadow = this.getShadowDom();
|
|
if (parentBlock.workspace && shadow && Blockly.Events.recordUndo) {
|
|
var blockShadow =
|
|
Blockly.Xml.domToBlock(shadow, parentBlock.workspace);
|
|
if (blockShadow.outputConnection) {
|
|
this.connect(blockShadow.outputConnection);
|
|
} else if (blockShadow.previousConnection) {
|
|
this.connect(blockShadow.previousConnection);
|
|
} else {
|
|
throw 'Child block does not have output or previous statement.';
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.targetBlock = function() {
|
|
if (this.isConnected()) {
|
|
return this.targetConnection.getSourceBlock();
|
|
}
|
|
return null;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.checkType_ = function(otherConnection) {
|
|
if (!this.check_ || !otherConnection.check_) {
|
|
|
|
|
|
if (otherConnection.check_ && otherConnection.check_[0] === 'procedure') return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
for (var i = 0; i < this.check_.length; i++) {
|
|
if (otherConnection.check_.indexOf(this.check_[i]) != -1) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.onCheckChanged_ = function() {
|
|
|
|
if (this.isConnected() && !this.checkType_(this.targetConnection)) {
|
|
var child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_;
|
|
child.unplug();
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.setCheck = function(check) {
|
|
if (check) {
|
|
|
|
if (!goog.isArray(check)) {
|
|
check = [check];
|
|
}
|
|
this.check_ = check;
|
|
this.onCheckChanged_();
|
|
} else {
|
|
this.check_ = null;
|
|
}
|
|
return this;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.setShape = function(shape) {
|
|
if (shape) {
|
|
this.shape_ = shape
|
|
} else {
|
|
this.shape_ = null;
|
|
}
|
|
return this;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.getOutputShape = function() {
|
|
if (this.shape_) return this.shape_
|
|
if (!this.check_) return Blockly.OUTPUT_SHAPE_ROUND;
|
|
if (this.check_.indexOf('Boolean') !== -1) {
|
|
return Blockly.OUTPUT_SHAPE_HEXAGONAL;
|
|
}
|
|
if (this.check_.indexOf('Number') !== -1) {
|
|
return Blockly.OUTPUT_SHAPE_ROUND;
|
|
}
|
|
if (this.check_.indexOf('String') !== -1) {
|
|
return Blockly.OUTPUT_SHAPE_SQUARE;
|
|
}
|
|
return Blockly.OUTPUT_SHAPE_ROUND;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.setShadowDom = function(shadow) {
|
|
this.shadowDom_ = shadow;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.getShadowDom = function() {
|
|
return this.shadowDom_;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.neighbours_ = function() {
|
|
return [];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Connection.prototype.toString = function() {
|
|
var msg;
|
|
var block = this.sourceBlock_;
|
|
if (!block) {
|
|
return 'Orphan Connection';
|
|
} else if (block.outputConnection == this) {
|
|
msg = 'Output Connection of ';
|
|
} else if (block.previousConnection == this) {
|
|
msg = 'Previous Connection of ';
|
|
} else if (block.nextConnection == this) {
|
|
msg = 'Next Connection of ';
|
|
} else {
|
|
var parentInput = goog.array.find(block.inputList, function(input) {
|
|
return input.connection == this;
|
|
}, this);
|
|
if (parentInput) {
|
|
msg = 'Input "' + parentInput.name + '" connection on ';
|
|
} else {
|
|
console.warn('Connection not actually connected to sourceBlock_');
|
|
return 'Orphan Connection';
|
|
}
|
|
}
|
|
return msg + block.toDevString();
|
|
};
|
|
|