From a47ddcb9762215c7a02a549b0f263903a6b3896c Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Sat, 2 Feb 2019 09:57:07 -0800 Subject: [PATCH 1/4] Added a test block for a dynamic dropdown. --- tests/blocks/test_blocks.js | 60 +++++++++++++++++++++++++++++++++++++ tests/playground.html | 11 +++++++ 2 files changed, 71 insertions(+) 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 { + + + From 463e42e3759d713c4815cbc634be7bfda5ca69e0 Mon Sep 17 00:00:00 2001 From: Andrew n marshall <9916202+AnmAtAnm@users.noreply.github.com> Date: Tue, 5 Feb 2019 09:13:53 -0800 Subject: [PATCH 2/4] New label and title case --- tests/playground.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/playground.html b/tests/playground.html index 62df37e3e..983af43ba 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -1190,9 +1190,10 @@ h1 { - - + + + From 74e5c49e0de9e4375dd1e68e23976ec815abaf0d Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 5 Feb 2019 14:32:40 -0800 Subject: [PATCH 3/4] Fixed comments and defaults. --- tests/blocks/test_blocks.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index fd63dc0d2..c3d9821c6 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -603,26 +603,35 @@ Blockly.TestBlocks.dynamicDropdownOptions_ = []; * @package */ Blockly.TestBlocks.addDynamicDropdownOption_ = function() { - Blockly.prompt('Add an option?', '', function(text) { + Blockly.prompt('Add an option?', + 'option ' + Blockly.TestBlocks.dynamicDropdownOptions_.length, + 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]); + // The option is an array containing human-readable text and + // language-neutral 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. + * the user for an option to remove. May remove multiple options with the + * same name. * @package */ Blockly.TestBlocks.removeDynamicDropdownOption_ = function() { - Blockly.prompt('Remove an option?', '', function(text) { + var defaultText = Blockly.TestBlocks.dynamicDropdownOptions_[0] ? + Blockly.TestBlocks.dynamicDropdownOptions_[0][0] : ''; + Blockly.prompt('Remove an option?', defaultText, function(text) { for (var i = 0, option; option = Blockly.TestBlocks.dynamicDropdownOptions_[i]; i++) { + // The option is an array containing human-readable text and + // language-neutral text, we'll compare against the human-readable text. if (option[0] == text) { // Do not remove this log! Helps you know if it was removed correctly. console.log('Removing option: ' + text); From 114171ada8a9ab57a9231d06d620cc3d083a607f Mon Sep 17 00:00:00 2001 From: Andrew n marshall <9916202+AnmAtAnm@users.noreply.github.com> Date: Thu, 7 Feb 2019 10:22:36 -0800 Subject: [PATCH 4/4] language-neutral text -> language-neutral id --- tests/blocks/test_blocks.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index c3d9821c6..267b87abe 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -609,8 +609,8 @@ Blockly.TestBlocks.addDynamicDropdownOption_ = function() { if (text) { // Do not remove this log! Helps you know if it was added correctly. console.log('Adding option: ' + text); - // The option is an array containing human-readable text and - // language-neutral text. + // The option is an array containing human-readable text and a + // language-neutral id. Blockly.TestBlocks.dynamicDropdownOptions_.push( [text, 'OPTION' + Blockly.TestBlocks.dynamicDropdownOptions_.length]); } @@ -630,8 +630,8 @@ Blockly.TestBlocks.removeDynamicDropdownOption_ = function() { for (var i = 0, option; option = Blockly.TestBlocks.dynamicDropdownOptions_[i]; i++) { - // The option is an array containing human-readable text and - // language-neutral text, we'll compare against the human-readable text. + // The option is an array containing human-readable text and a + // language-neutral id, we'll compare against the human-readable text. if (option[0] == text) { // Do not remove this log! Helps you know if it was removed correctly. console.log('Removing option: ' + text);