/**
* Reconfigure this block based on the mutator dialog's components.
* @param {!Blockly.Block} containerBlock Root block in mutator.
* @this {Blockly.Block}
*/
compose: function(containerBlock) {
//Interprets the configuration of the sub-blocks and uses them to modify the main block.
//This function should accept the "containerBlock" which was returned by decompose as a parameter.
var clauseBlock = containerBlock.nextConnection.targetBlock();
var clauseBlockStatement = this.workspace.newBlock('settings_superior_submenu').getInput('SUBMENU_STATEMENT');
//var clauseBlockStatement = containerBlock.getInput('SUBMENU_STATEMENT').getSourceBlock();
// Count number of inputs.
this.mainMenuCount_ = 0;
this.submenuCount_ = [null];
while (clauseBlock && !clauseBlock.isInsertionMarker()) {
switch (clauseBlock.type) {
case 'settings_superior_main_menu':
this.mainMenuCount_++; // Actual counter, how many Main Menu Mutator Blocks are connected
break;
/*case 'settings_superior_submenu': // OLD...
this.submenuCount_++;
break;
case 'settings_superior_option':
this.optionCount_++;
break;*/
default:
throw TypeError('Unknown block type: ' + clauseBlock.type);
}
clauseBlock = clauseBlock.nextConnection &&
clauseBlock.nextConnection.targetBlock();
}
HERE the submenuCount will be done, but it is not working as it should atm. This is where I need help!! Also one Line here gives an Error as stated in the code comment.
for ( let i = 1; i <= this.mainMenuCount_; i++) { // TO DO: add to the loop above
this.submenuCount_.push([0]); // push the array to the size of the mainMenuCounter_
while (clauseBlock && !clauseBlock.isInsertionMarker()) {
switch (clauseBlock.type) {
case 'settings_superior_submenu':
this.submenuCount_[i-1]++; // count the Submenus of each Main Menu
break;
default:
throw TypeError('Unknown block type: ' + clauseBlock.type);
}
clauseBlock = clauseBlock.nextConnection &&
clauseBlock.nextConnection.targetBlock();
}
clauseBlockStatement = clauseBlockStatement.nextConnection && // ERROR: Cannot read properties of undefined (reading 'nextConnection')
clauseBlockStatement.nextConnection.targetBlock();
}
this.updateShape_();
},
The way my Main Block shall behave. That works fine (if I set the submenuCount_ array manuelly:
/**
* Modify this block to have the correct number of inputs.
* @this {Blockly.Block}
* @private
*/
updateShape_: function() {
// Delete everything.
var i = 1;
if(this.getField('MAIN_MENU_DROPDOWN_FUNCTION')) {
this.getInput('DUMMY_INPUT_SETTINGS_BLOCK')
.removeField('MAIN_MENU_DROPDOWN_FUNCTION');
}
// Rebuild block.
var dropdownMainMenus = [];
for (i = 1; i <= this.mainMenuCount_; i++) { //
if (i == 1) {
dropdownMainMenus.push(["Main Menu " + i.toString(), "MAIN_MENU_" + i.toString()]);
this.getInput('DUMMY_INPUT_SETTINGS_BLOCK')
.appendField(new Blockly.FieldDropdown(dropdownMainMenus, this.validateMainMenu), "MAIN_MENU_DROPDOWN_FUNCTION");
} else if (i > 1) {
dropdownMainMenus.push(["Main Menu " + i.toString(), "MAIN_MENU_" + i.toString()])
}
}
},
//EXTRA FOR DROPDOWN MENU
validateMainMenu: function(mainMenuNumber) {
// validates which Main Menu is selected in the Dropdown Menu
this.getSourceBlock().updateBlockBasedOnMainMenuSelectedInDropdown(mainMenuNumber);
return mainMenuNumber;
},
updateBlockBasedOnMainMenuSelectedInDropdown: function(mainMenuNumber) {
// Adding Submenus / Options to specific Main Menu
var i = 1;
var p = 1;
for (let k = 1; k <= this.mainMenuCount_; k++) {
while (this.getInput('SUBMENU_' + k.toString() + "_" + p.toString())) {
this.removeInput('SUBMENU_' + k.toString() + "_" + p.toString())
p++;
}
}
// TO DO: remove Options /
while (i <= this.mainMenuCount_) {
if(mainMenuNumber == 'MAIN_MENU_' + i.toString()){
for (let k = 1; k <= this.submenuCount_[i-1]; k++) {
this.appendDummyInput('SUBMENU_' + i.toString() + "_" + k.toString())
.appendField('Submenu ' + i.toString() + "." + k.toString())
.appendField(new Blockly.FieldCheckbox(true), "CHECKBOX_SUBMENU_" + i.toString() + "_" + k.toString())
.setAlign(Blockly.ALIGN_RIGHT);
}
for (let j = 1; j <= this.optionCount_; j++) {
this.appendDummyInput('OPTION_' + i.toString() + "_" + j.toString())
.appendField('Option ' + i.toString() + "." + j.toString())
.setAlign(Blockly.ALIGN_RIGHT);
}
}
i++;
}
},
Blockly.Extensions.register('add_lines_to_dummy_input',
function() {
this.getInput('DUMMY_INPUT_SETTINGS_BLOCK')
.appendField(new Blockly.FieldTextInput("any"), "SETTINGS_FIELD_INPUT")
.appendField(new Blockly.FieldLabelSerializable("Settings:"), "SETTINGS_NAME")
});
Blockly.Extensions.registerMutator('ba_hw_settings_superior_block_mutator',
Blockly.Constants.Logic.BA_HW_SETTINGS_SUPERIOR_BLOCK_MUTATOR_MIXIN, null,
['settings_superior_main_menu', 'settings_superior_submenu', 'settings_superior_option', 'add_image_search_to_settings_block']);