Reduce duplicated code in text and list blocks.

This commit is contained in:
Neil Fraser
2014-12-19 18:14:41 -08:00
parent 8c3f180669
commit 9a605e90e7
5 changed files with 151 additions and 146 deletions

View File

@@ -51,14 +51,11 @@ Blockly.Blocks['lists_create_with'] = {
*/
init: function() {
this.setColour(260);
this.appendValueInput('ADD0')
.appendField(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH);
this.appendValueInput('ADD1');
this.appendValueInput('ADD2');
this.itemCount_ = 3;
this.updateShape_();
this.setOutput(true, 'Array');
this.setMutator(new Blockly.Mutator(['lists_create_with_item']));
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_TOOLTIP);
this.itemCount_ = 3;
},
/**
* Create XML to represent list inputs.
@@ -76,20 +73,8 @@ Blockly.Blocks['lists_create_with'] = {
* @this Blockly.Block
*/
domToMutation: function(xmlElement) {
for (var x = 0; x < this.itemCount_; x++) {
this.removeInput('ADD' + x);
}
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
for (var x = 0; x < this.itemCount_; x++) {
var input = this.appendValueInput('ADD' + x);
if (x == 0) {
input.appendField(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH);
}
}
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(Blockly.Msg.LISTS_CREATE_EMPTY_TITLE);
}
this.updateShape_();
},
/**
* Populate the mutator's dialog with this block's components.
@@ -102,7 +87,7 @@ Blockly.Blocks['lists_create_with'] = {
Blockly.Block.obtain(workspace, 'lists_create_with_container');
containerBlock.initSvg();
var connection = containerBlock.getInput('STACK').connection;
for (var x = 0; x < this.itemCount_; x++) {
for (var i = 0; i < this.itemCount_; i++) {
var itemBlock = Blockly.Block.obtain(workspace, 'lists_create_with_item');
itemBlock.initSvg();
connection.connect(itemBlock.previousConnection);
@@ -116,33 +101,23 @@ Blockly.Blocks['lists_create_with'] = {
* @this Blockly.Block
*/
compose: function(containerBlock) {
// Disconnect all input blocks and remove all inputs.
if (this.itemCount_ == 0) {
this.removeInput('EMPTY');
} else {
for (var x = this.itemCount_ - 1; x >= 0; x--) {
this.removeInput('ADD' + x);
}
}
this.itemCount_ = 0;
// Rebuild the block's inputs.
var itemBlock = containerBlock.getInputTargetBlock('STACK');
// Count number of inputs.
var connections = [];
var i = 0;
while (itemBlock) {
var input = this.appendValueInput('ADD' + this.itemCount_);
if (this.itemCount_ == 0) {
input.appendField(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH);
}
// Reconnect any child blocks.
if (itemBlock.valueConnection_) {
input.connection.connect(itemBlock.valueConnection_);
}
this.itemCount_++;
connections[i] = itemBlock.valueConnection_;
itemBlock = itemBlock.nextConnection &&
itemBlock.nextConnection.targetBlock();
i++;
}
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(Blockly.Msg.LISTS_CREATE_EMPTY_TITLE);
this.itemCount_ = i;
this.updateShape_();
// Reconnect any child blocks.
for (var i = 0; i < this.itemCount_; i++) {
if (connections[i]) {
this.getInput('ADD' + i).connection.connect(connections[i]);
}
}
},
/**
@@ -152,14 +127,43 @@ Blockly.Blocks['lists_create_with'] = {
*/
saveConnections: function(containerBlock) {
var itemBlock = containerBlock.getInputTargetBlock('STACK');
var x = 0;
var i = 0;
while (itemBlock) {
var input = this.getInput('ADD' + x);
var input = this.getInput('ADD' + i);
itemBlock.valueConnection_ = input && input.connection.targetConnection;
x++;
i++;
itemBlock = itemBlock.nextConnection &&
itemBlock.nextConnection.targetBlock();
}
},
/**
* Modify this block to have the correct number of inputs.
* @private
* @this Blockly.Block
*/
updateShape_: function() {
// Delete everything.
if (this.getInput('EMPTY')) {
this.removeInput('EMPTY');
} else {
var i = 0;
while (this.getInput('ADD' + i)) {
this.removeInput('ADD' + i);
i++;
}
}
// Rebuild block.
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(Blockly.Msg.LISTS_CREATE_EMPTY_TITLE);
} else {
for (var i = 0; i < this.itemCount_; i++) {
var input = this.appendValueInput('ADD' + i);
if (i == 0) {
input.appendField(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH);
}
}
}
}
};

View File

@@ -89,11 +89,11 @@ Blockly.Blocks['controls_if'] = {
domToMutation: function(xmlElement) {
this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10);
this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10);
for (var x = 1; x <= this.elseifCount_; x++) {
this.appendValueInput('IF' + x)
for (var i = 1; i <= this.elseifCount_; i++) {
this.appendValueInput('IF' + i)
.setCheck('Boolean')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
this.appendStatementInput('DO' + x)
this.appendStatementInput('DO' + i)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
}
if (this.elseCount_) {
@@ -111,7 +111,7 @@ Blockly.Blocks['controls_if'] = {
var containerBlock = Blockly.Block.obtain(workspace, 'controls_if_if');
containerBlock.initSvg();
var connection = containerBlock.getInput('STACK').connection;
for (var x = 1; x <= this.elseifCount_; x++) {
for (var i = 1; i <= this.elseifCount_; i++) {
var elseifBlock = Blockly.Block.obtain(workspace, 'controls_if_elseif');
elseifBlock.initSvg();
connection.connect(elseifBlock.previousConnection);
@@ -136,9 +136,9 @@ Blockly.Blocks['controls_if'] = {
}
this.elseCount_ = 0;
// Disconnect all the elseif input blocks and remove the inputs.
for (var x = this.elseifCount_; x > 0; x--) {
this.removeInput('IF' + x);
this.removeInput('DO' + x);
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.
@@ -183,17 +183,17 @@ Blockly.Blocks['controls_if'] = {
*/
saveConnections: function(containerBlock) {
var clauseBlock = containerBlock.getInputTargetBlock('STACK');
var x = 1;
var i = 1;
while (clauseBlock) {
switch (clauseBlock.type) {
case 'controls_if_elseif':
var inputIf = this.getInput('IF' + x);
var inputDo = this.getInput('DO' + x);
var inputIf = this.getInput('IF' + i);
var inputDo = this.getInput('DO' + i);
clauseBlock.valueConnection_ =
inputIf && inputIf.connection.targetConnection;
clauseBlock.statementConnection_ =
inputDo && inputDo.connection.targetConnection;
x++;
i++;
break;
case 'controls_if_else':
var inputDo = this.getInput('ELSE');

View File

@@ -80,12 +80,12 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// Check for duplicated arguments.
var badArg = false;
var hash = {};
for (var x = 0; x < this.arguments_.length; x++) {
if (hash['arg_' + this.arguments_[x].toLowerCase()]) {
for (var i = 0; i < this.arguments_.length; i++) {
if (hash['arg_' + this.arguments_[i].toLowerCase()]) {
badArg = true;
break;
}
hash['arg_' + this.arguments_[x].toLowerCase()] = true;
hash['arg_' + this.arguments_[i].toLowerCase()] = true;
}
if (badArg) {
this.setWarningText(Blockly.Msg.PROCEDURES_DEF_DUPLICATE_WARNING);
@@ -107,9 +107,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
*/
mutationToDom: function() {
var container = document.createElement('mutation');
for (var x = 0; x < this.arguments_.length; x++) {
for (var i = 0; i < this.arguments_.length; i++) {
var parameter = document.createElement('arg');
parameter.setAttribute('name', this.arguments_[x]);
parameter.setAttribute('name', this.arguments_[i]);
container.appendChild(parameter);
}
@@ -126,7 +126,7 @@ Blockly.Blocks['procedures_defnoreturn'] = {
*/
domToMutation: function(xmlElement) {
this.arguments_ = [];
for (var x = 0, childNode; childNode = xmlElement.childNodes[x]; x++) {
for (var i = 0, childNode; childNode = xmlElement.childNodes[i]; i++) {
if (childNode.nodeName.toLowerCase() == 'arg') {
this.arguments_.push(childNode.getAttribute('name'));
}
@@ -157,12 +157,12 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// Parameter list.
var connection = containerBlock.getInput('STACK').connection;
for (var x = 0; x < this.arguments_.length; x++) {
for (var i = 0; i < this.arguments_.length; i++) {
var paramBlock = Blockly.Block.obtain(workspace, 'procedures_mutatorarg');
paramBlock.initSvg();
paramBlock.setFieldValue(this.arguments_[x], 'NAME');
paramBlock.setFieldValue(this.arguments_[i], 'NAME');
// Store the old location.
paramBlock.oldLocation = x;
paramBlock.oldLocation = i;
connection.connect(paramBlock.previousConnection);
connection = paramBlock.nextConnection;
}
@@ -262,9 +262,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
*/
renameVar: function(oldName, newName) {
var change = false;
for (var x = 0; x < this.arguments_.length; x++) {
if (Blockly.Names.equals(oldName, this.arguments_[x])) {
this.arguments_[x] = newName;
for (var i = 0; i < this.arguments_.length; i++) {
if (Blockly.Names.equals(oldName, this.arguments_[i])) {
this.arguments_[i] = newName;
change = true;
}
}
@@ -273,7 +273,7 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// Update the mutator's variables if the mutator is open.
if (this.mutator.isVisible_()) {
var blocks = this.mutator.workspace_.getAllBlocks();
for (var x = 0, block; block = blocks[x]; x++) {
for (var i = 0, block; block = blocks[i]; i++) {
if (block.type == 'procedures_mutatorarg' &&
Blockly.Names.equals(oldName, block.getFieldValue('NAME'))) {
block.setFieldValue(newName, 'NAME');
@@ -294,9 +294,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
option.text = Blockly.Msg.PROCEDURES_CREATE_DO.replace('%1', name);
var xmlMutation = goog.dom.createDom('mutation');
xmlMutation.setAttribute('name', name);
for (var x = 0; x < this.arguments_.length; x++) {
for (var i = 0; i < this.arguments_.length; i++) {
var xmlArg = goog.dom.createDom('arg');
xmlArg.setAttribute('name', this.arguments_[x]);
xmlArg.setAttribute('name', this.arguments_[i]);
xmlMutation.appendChild(xmlArg);
}
var xmlBlock = goog.dom.createDom('block', null, xmlMutation);
@@ -306,9 +306,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// Add options to create getters for each parameter.
if (!this.isCollapsed()) {
for (var x = 0; x < this.arguments_.length; x++) {
for (var i = 0; i < this.arguments_.length; i++) {
var option = {enabled: true};
var name = this.arguments_[x];
var name = this.arguments_[i];
option.text = Blockly.Msg.VARIABLES_SET_CREATE_GET.replace('%1', name);
var xmlField = goog.dom.createDom('field', null, name);
xmlField.setAttribute('name', 'VAR');
@@ -504,25 +504,25 @@ Blockly.Blocks['procedures_callnoreturn'] = {
var savedRendered = this.rendered;
this.rendered = false;
// Update the quarkConnections_ with existing connections.
for (var x = this.arguments_.length - 1; x >= 0; x--) {
var input = this.getInput('ARG' + x);
for (var i = this.arguments_.length - 1; i >= 0; i--) {
var input = this.getInput('ARG' + i);
if (input) {
var connection = input.connection.targetConnection;
this.quarkConnections_[this.quarkArguments_[x]] = connection;
this.quarkConnections_[this.quarkArguments_[i]] = connection;
// Disconnect all argument blocks and remove all inputs.
this.removeInput('ARG' + x);
this.removeInput('ARG' + i);
}
}
// Rebuild the block's arguments.
this.arguments_ = [].concat(paramNames);
this.quarkArguments_ = paramIds;
for (var x = 0; x < this.arguments_.length; x++) {
var input = this.appendValueInput('ARG' + x)
for (var i = 0; i < this.arguments_.length; i++) {
var input = this.appendValueInput('ARG' + i)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(this.arguments_[x]);
.appendField(this.arguments_[i]);
if (this.quarkArguments_) {
// Reconnect any child blocks.
var quarkName = this.quarkArguments_[x];
var quarkName = this.quarkArguments_[i];
if (quarkName in this.quarkConnections_) {
var connection = this.quarkConnections_[quarkName];
if (!connection || connection.targetConnection ||
@@ -551,9 +551,9 @@ Blockly.Blocks['procedures_callnoreturn'] = {
mutationToDom: function() {
var container = document.createElement('mutation');
container.setAttribute('name', this.getProcedureCall());
for (var x = 0; x < this.arguments_.length; x++) {
for (var i = 0; i < this.arguments_.length; i++) {
var parameter = document.createElement('arg');
parameter.setAttribute('name', this.arguments_[x]);
parameter.setAttribute('name', this.arguments_[i]);
container.appendChild(parameter);
}
return container;
@@ -575,7 +575,7 @@ Blockly.Blocks['procedures_callnoreturn'] = {
this.setProcedureParameters(def.arguments_, def.paramIds_);
} else {
this.arguments_ = [];
for (var x = 0, childNode; childNode = xmlElement.childNodes[x]; x++) {
for (var i = 0, childNode; childNode = xmlElement.childNodes[i]; i++) {
if (childNode.nodeName.toLowerCase() == 'arg') {
this.arguments_.push(childNode.getAttribute('name'));
}
@@ -593,10 +593,10 @@ Blockly.Blocks['procedures_callnoreturn'] = {
* @this Blockly.Block
*/
renameVar: function(oldName, newName) {
for (var x = 0; x < this.arguments_.length; x++) {
if (Blockly.Names.equals(oldName, this.arguments_[x])) {
this.arguments_[x] = newName;
this.getInput('ARG' + x).fieldRow[0].setText(newName);
for (var i = 0; i < this.arguments_.length; i++) {
if (Blockly.Names.equals(oldName, this.arguments_[i])) {
this.arguments_[i] = newName;
this.getInput('ARG' + i).fieldRow[0].setText(newName);
}
}
},

View File

@@ -68,13 +68,11 @@ Blockly.Blocks['text_join'] = {
init: function() {
this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL);
this.setColour(160);
this.appendValueInput('ADD0')
.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
this.appendValueInput('ADD1');
this.itemCount_ = 2;
this.updateShape_();
this.setOutput(true, 'String');
this.setMutator(new Blockly.Mutator(['text_create_join_item']));
this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP);
this.itemCount_ = 2;
},
/**
* Create XML to represent number of text inputs.
@@ -92,23 +90,8 @@ Blockly.Blocks['text_join'] = {
* @this Blockly.Block
*/
domToMutation: function(xmlElement) {
for (var x = 0; x < this.itemCount_; x++) {
this.removeInput('ADD' + x);
}
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
for (var x = 0; x < this.itemCount_; x++) {
var input = this.appendValueInput('ADD' + x);
if (x == 0) {
input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
}
}
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote0.png', 12, 12, '"'))
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote1.png', 12, 12, '"'));
}
this.updateShape_();
},
/**
* Populate the mutator's dialog with this block's components.
@@ -121,7 +104,7 @@ Blockly.Blocks['text_join'] = {
'text_create_join_container');
containerBlock.initSvg();
var connection = containerBlock.getInput('STACK').connection;
for (var x = 0; x < this.itemCount_; x++) {
for (var i = 0; i < this.itemCount_; i++) {
var itemBlock = Blockly.Block.obtain(workspace, 'text_create_join_item');
itemBlock.initSvg();
connection.connect(itemBlock.previousConnection);
@@ -135,36 +118,23 @@ Blockly.Blocks['text_join'] = {
* @this Blockly.Block
*/
compose: function(containerBlock) {
// Disconnect all input blocks and remove all inputs.
if (this.itemCount_ == 0) {
this.removeInput('EMPTY');
} else {
for (var x = this.itemCount_ - 1; x >= 0; x--) {
this.removeInput('ADD' + x);
}
}
this.itemCount_ = 0;
// Rebuild the block's inputs.
var itemBlock = containerBlock.getInputTargetBlock('STACK');
// Count number of inputs.
var connections = [];
var i = 0;
while (itemBlock) {
var input = this.appendValueInput('ADD' + this.itemCount_);
if (this.itemCount_ == 0) {
input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
}
// Reconnect any child blocks.
if (itemBlock.valueConnection_) {
input.connection.connect(itemBlock.valueConnection_);
}
this.itemCount_++;
connections[i] = itemBlock.valueConnection_;
itemBlock = itemBlock.nextConnection &&
itemBlock.nextConnection.targetBlock();
i++;
}
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote0.png', 12, 12, '"'))
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote1.png', 12, 12, '"'));
this.itemCount_ = i;
this.updateShape_();
// Reconnect any child blocks.
for (var i = 0; i < this.itemCount_; i++) {
if (connections[i]) {
this.getInput('ADD' + i).connection.connect(connections[i]);
}
}
},
/**
@@ -174,14 +144,46 @@ Blockly.Blocks['text_join'] = {
*/
saveConnections: function(containerBlock) {
var itemBlock = containerBlock.getInputTargetBlock('STACK');
var x = 0;
var i = 0;
while (itemBlock) {
var input = this.getInput('ADD' + x);
var input = this.getInput('ADD' + i);
itemBlock.valueConnection_ = input && input.connection.targetConnection;
x++;
i++;
itemBlock = itemBlock.nextConnection &&
itemBlock.nextConnection.targetBlock();
}
},
/**
* Modify this block to have the correct number of inputs.
* @private
* @this Blockly.Block
*/
updateShape_: function() {
// Delete everything.
if (this.getInput('EMPTY')) {
this.removeInput('EMPTY');
} else {
var i = 0;
while (this.getInput('ADD' + i)) {
this.removeInput('ADD' + i);
i++;
}
}
// Rebuild block.
if (this.itemCount_ == 0) {
this.appendDummyInput('EMPTY')
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote0.png', 12, 12, '"'))
.appendField(new Blockly.FieldImage(Blockly.pathToMedia +
'quote1.png', 12, 12, '"'));
} else {
for (var i = 0; i < this.itemCount_; i++) {
var input = this.appendValueInput('ADD' + i);
if (i == 0) {
input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
}
}
}
}
};