diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index d92193452..fd63dc0d2 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -19,6 +19,8 @@ */ 'use strict'; +goog.provide('Blockly.TestBlocks'); + Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT { "type": "empty_block", @@ -571,3 +573,61 @@ Blockly.Blocks['empty_block_with_mutator'] = { this.setMutator(new Blockly.Mutator(['math_number'])); } }; + +Blockly.Blocks['test_dropdown_dynamic'] = { + init: function() { + var dropdown = new Blockly.FieldDropdown(this.dynamicOptions); + this.appendDummyInput() + .appendField('dynamic') + .appendField(dropdown, 'OPTIONS'); + }, + + dynamicOptions: function() { + if (!Blockly.TestBlocks.dynamicDropdownOptions_.length) { + return [['', 'OPTION0']]; + } + return Blockly.TestBlocks.dynamicDropdownOptions_; + } +}; + +/** + * An array of options for the dynamic dropdown. + * @type {!Array} + * @package + */ +Blockly.TestBlocks.dynamicDropdownOptions_ = []; + +/** + * Handles "add option" button in the field test category. This will prompt + * the user for an option to add. + * @package + */ +Blockly.TestBlocks.addDynamicDropdownOption_ = function() { + Blockly.prompt('Add an option?', '', function(text) { + if (text) { + // Do not remove this log! Helps you know if it was added correctly. + console.log('Adding option: ' + text); + Blockly.TestBlocks.dynamicDropdownOptions_.push([text, + 'OPTION' + Blockly.TestBlocks.dynamicDropdownOptions_.length]); + } + }) +}; + +/** + * Handles "remove option" button in the field test category. This will prompt + * the user for an option to remove. + * @package + */ +Blockly.TestBlocks.removeDynamicDropdownOption_ = function() { + Blockly.prompt('Remove an option?', '', function(text) { + for (var i = 0, option; + option = Blockly.TestBlocks.dynamicDropdownOptions_[i]; + i++) { + if (option[0] == text) { + // Do not remove this log! Helps you know if it was removed correctly. + console.log('Removing option: ' + text); + Blockly.TestBlocks.dynamicDropdownOptions_.splice(i, 1); + } + } + }) +}; diff --git a/tests/playground.html b/tests/playground.html index bfca40eb4..62df37e3e 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -122,6 +122,7 @@ function start() { scaleSpeed: 1.1 } }); + addToolboxButtonCallbacks(); // Restore previously displayed text. if (sessionStorage) { var text = sessionStorage.getItem('textarea'); @@ -138,6 +139,13 @@ function start() { taChange(); } +function addToolboxButtonCallbacks() { + workspace.registerButtonCallback( + 'addDynamicOption', Blockly.TestBlocks.addDynamicDropdownOption_); + workspace.registerButtonCallback( + 'removeDynamicOption', Blockly.TestBlocks.removeDynamicDropdownOption_); +} + function changeTheme() { var theme = document.getElementById('themeChanger'); if (theme.value === "modern") { @@ -1182,6 +1190,9 @@ h1 { + + +