First of all: Blockly Developers made an awesome job! Its just about understanding their concepts.
I think, that we are working on the same problem, but for different results. My target is very simple and I reached it (hopefully, maybe Im going into trouble at some other point). But so far I can say, that there is a simple solution for this problem! :)
RANGE_MIN = 2;
RANGE_MAX = 5;
Blockly.Blocks['foo'] = {
init: function() {
var inputsC = RANGE_MIN;
this.setOutput(true, 'FOO');
this.setColour(100);
this.setInputsInline(true);
this.appendValueInput('in1')
this.appendValueInput('in2')
.appendField('foo')
},
mutationToDom: function () {
var container = Blockly.utils.xml.createElement('mutation');
container.setAttribute('inputCount', this.inputsC);
return container;
},
domToMutation: function (xmlElement) {
this.inputsC = parseInt(xmlElement.getAttribute('inputCount'), 10) || RANGE_MIN;
this.updateShape_();
},
updateShape_: function() {
this.removeInput('in1');
if(this.inputsC == RANGE_MIN){
this.appendValueInput('in1')
.appendField(new Blockly.FieldImage(
15,
15,
"*", function() {
this.getSourceBlock().plus_();
} ));
}
else if(this.inputsC == RANGE_MAX){
this.appendValueInput('in1')
.appendField(new Blockly.FieldImage(
15,
15,
"*", function() {
this.getSourceBlock().minus_();
} ));
}
else{
this.appendValueInput('in1')
.appendField(new Blockly.FieldImage(
15,
15,
"*", function() {
this.getSourceBlock().minus_();
} ))
.appendField(new Blockly.FieldImage(
15,
15,
"*", function() {
this.getSourceBlock().plus_();
} ));
}
var i = 2;
while (this.getInput('in' + i)) {
this.removeInput('in' + i);
i++;
}
for (var x = 2; x <= this.inputsC; x++) {
this.appendValueInput('in' + x)
.appendField('foo')
}
},
plus_: function (){
this.inputsC = (this.inputsC + 1);
this.updateShape_();
},
minus_: function (){
this.inputsC = (this.inputsC - 1);
this.updateShape_();
}
};