Reduce code duplication in 'if' block.

This commit is contained in:
Neil Fraser
2016-03-01 18:21:02 -08:00
parent fee94ae405
commit 5097963cb1
5 changed files with 55 additions and 56 deletions

View File

@@ -94,17 +94,7 @@ Blockly.Blocks['controls_if'] = {
domToMutation: function(xmlElement) {
this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10) || 0;
this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10) || 0;
for (var i = 1; i <= this.elseifCount_; i++) {
this.appendValueInput('IF' + i)
.setCheck('Boolean')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
this.appendStatementInput('DO' + i)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
}
if (this.elseCount_) {
this.appendStatementInput('ELSE')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE);
}
this.updateShape_();
},
/**
* Populate the mutator's dialog with this block's components.
@@ -135,44 +125,23 @@ Blockly.Blocks['controls_if'] = {
* @this Blockly.Block
*/
compose: function(containerBlock) {
// Disconnect the else input blocks and remove the inputs.
if (this.elseCount_) {
this.removeInput('ELSE');
}
this.elseCount_ = 0;
// Disconnect all the elseif input blocks and remove the inputs.
for (var i = this.elseifCount_; i > 0; i--) {
this.removeInput('IF' + i);
this.removeInput('DO' + i);
}
this.elseifCount_ = 0;
// Rebuild the block's optional inputs.
var clauseBlock = containerBlock.nextConnection.targetBlock();
// Count number of inputs.
this.elseifCount_ = 0;
this.elseCount_ = 0;
var valueConnections = [null];
var statementConnections = [null];
var elseStatementConnection = null;
while (clauseBlock) {
switch (clauseBlock.type) {
case 'controls_if_elseif':
this.elseifCount_++;
var ifInput = this.appendValueInput('IF' + this.elseifCount_)
.setCheck('Boolean')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
var doInput = this.appendStatementInput('DO' + this.elseifCount_);
doInput.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
// Reconnect any child blocks.
if (clauseBlock.valueConnection_) {
ifInput.connection.connect(clauseBlock.valueConnection_);
}
if (clauseBlock.statementConnection_) {
doInput.connection.connect(clauseBlock.statementConnection_);
}
valueConnections.push(clauseBlock.valueConnection_);
statementConnections.push(clauseBlock.statementConnection_);
break;
case 'controls_if_else':
this.elseCount_++;
var elseInput = this.appendStatementInput('ELSE');
elseInput.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE);
// Reconnect any child blocks.
if (clauseBlock.statementConnection_) {
elseInput.connection.connect(clauseBlock.statementConnection_);
}
elseStatementConnection = clauseBlock.statementConnection_;
break;
default:
throw 'Unknown block type.';
@@ -180,6 +149,19 @@ Blockly.Blocks['controls_if'] = {
clauseBlock = clauseBlock.nextConnection &&
clauseBlock.nextConnection.targetBlock();
}
this.updateShape_();
// Reconnect any child blocks.
for (var i = 1; i <= this.elseifCount_; i++) {
if (valueConnections[i]) {
this.getInput('IF' + i).connection.connect(valueConnections[i]);
}
if (statementConnections[i]) {
this.getInput('DO' + i).connection.connect(statementConnections[i]);
}
}
if (elseStatementConnection) {
this.getInput('ELSE').connection.connect(elseStatementConnection);
}
},
/**
* Store pointers to any connected child blocks.
@@ -211,6 +193,35 @@ Blockly.Blocks['controls_if'] = {
clauseBlock = clauseBlock.nextConnection &&
clauseBlock.nextConnection.targetBlock();
}
},
/**
* Modify this block to have the correct number of inputs.
* @private
* @this Blockly.Block
*/
updateShape_: function() {
// Delete everything.
if (this.getInput('ELSE')) {
this.removeInput('ELSE');
}
var i = 1;
while (this.getInput('IF' + i)) {
this.removeInput('IF' + i);
this.removeInput('DO' + i);
i++;
}
// Rebuild block.
for (var i = 1; i <= this.elseifCount_; i++) {
this.appendValueInput('IF' + i)
.setCheck('Boolean')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
this.appendStatementInput('DO' + i)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
}
if (this.elseCount_) {
this.appendStatementInput('ELSE')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE);
}
}
};

View File

@@ -1312,6 +1312,7 @@ Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset.
*/
Blockly.Block.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
var event = new Blockly.Events.Move(this);
this.xy_.translate(dx, dy);
event.recordNew();

View File

@@ -286,6 +286,7 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset.
*/
Blockly.BlockSvg.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
var event = new Blockly.Events.Move(this);
var xy = this.getRelativeToSurfaceXY();
this.getSvgRoot().setAttribute('transform',

View File

@@ -43,7 +43,7 @@ goog.require('goog.userAgent');
Blockly.inject = function(container, opt_options) {
if (goog.isString(container)) {
container = document.getElementById(container) ||
document.querySelector(selector);
document.querySelector(container);
}
// Verify that the container is in document.
if (!goog.dom.contains(document, container)) {

View File

@@ -224,18 +224,6 @@ h1 {
height: 95%;
width: 70%;
}
#collaborators {
float: right;
width: 30px;
margin-left: 10px;
}
#collaborators > img {
margin-right: 5px;
height: 30px;
padding-bottom: 5px;
width: 30px;
border-radius: 3px;
}
#importExport {
font-family: monospace;
}
@@ -243,8 +231,6 @@ h1 {
</head>
<body onload="start()">
<div id="collaborators"></div>
<div id="blocklyDiv"></div>
<xml id="toolbox" style="display: none">