diff --git a/blocks/logic.js b/blocks/logic.js index 01f4e55b6..f115224bd 100644 --- a/blocks/logic.js +++ b/blocks/logic.js @@ -331,7 +331,7 @@ Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN = { domToMutation: function(xmlElement) { this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10) || 0; this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10) || 0; - this.updateShape_(); + this.rebuildShape_(); }, /** * Populate the mutator's dialog with this block's components. @@ -388,11 +388,8 @@ Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN = { } this.updateShape_(); // Reconnect any child blocks. - for (var i = 1; i <= this.elseifCount_; i++) { - Blockly.Mutator.reconnect(valueConnections[i], this, 'IF' + i); - Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i); - } - Blockly.Mutator.reconnect(elseStatementConnection, this, 'ELSE'); + this.reconnectChildBlocks_(valueConnections, statementConnections, + elseStatementConnection); }, /** * Store pointers to any connected child blocks. @@ -425,6 +422,30 @@ Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN = { clauseBlock.nextConnection.targetBlock(); } }, + /** + * Reconstructs the block with all child blocks attached. + * Fixes issue #2037. + */ + rebuildShape_: function() { + var valueConnections = [null]; + var statementConnections = [null]; + var elseStatementConnection = null; + + if (this.getInput('ELSE')) { + elseStatementConnection = this.getInput('ELSE').connection.targetConnection; + } + var i = 1; + while (this.getInput('IF' + i)) { + var inputIf = this.getInput('IF' + i); + var inputDo = this.getInput('DO' + i); + valueConnections.push(inputIf.connection.targetConnection); + statementConnections.push(inputDo.connection.targetConnection); + i++; + } + this.updateShape_(); + this.reconnectChildBlocks_(valueConnections, statementConnections, + elseStatementConnection); + }, /** * Modify this block to have the correct number of inputs. * @this Blockly.Block @@ -453,6 +474,23 @@ Blockly.Constants.Logic.CONTROLS_IF_MUTATOR_MIXIN = { this.appendStatementInput('ELSE') .appendField(Blockly.Msg['CONTROLS_IF_MSG_ELSE']); } + }, + /** + * Reconnects child blocks. + * @param {!Array} valueConnections List of value + * connectsions for if input. + * @param {!Array} statementConnections List of + * statement connections for do input. + * @param {?Blockly.RenderedConnection} elseStatementConnection Statement + * connection for else input. + */ + reconnectChildBlocks_: function(valueConnections, statementConnections, + elseStatementConnection) { + for (var i = 1; i <= this.elseifCount_; i++) { + Blockly.Mutator.reconnect(valueConnections[i], this, 'IF' + i); + Blockly.Mutator.reconnect(statementConnections[i], this, 'DO' + i); + } + Blockly.Mutator.reconnect(elseStatementConnection, this, 'ELSE'); } };