Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Test Block for a Dynamic Dropdown #2252

Merged
merged 4 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions tests/blocks/test_blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
'use strict';

goog.provide('Blockly.TestBlocks');

Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
{
"type": "empty_block",
Expand Down Expand Up @@ -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<!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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for default: "option " + Blockly.TestBlocks.dynamicDropdownOptions_.length

Or something more complicated to prevent duplicates.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "May remove multiple options with the same name."

* @package
*/
Blockly.TestBlocks.removeDynamicDropdownOption_ = function() {
Blockly.prompt('Remove an option?', '', function(text) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for default: Blockly.TestBlocks.dynamicDropdownOptions_[0][0]

Combined with the above suggestion, this should put the cursor just after the unique number; easy to edit.

for (var i = 0, option;
option = Blockly.TestBlocks.dynamicDropdownOptions_[i];
i++) {
if (option[0] == text) {
AnmAtAnm marked this conversation as resolved.
Show resolved Hide resolved
// Do not remove this log! Helps you know if it was removed correctly.
console.log('Removing option: ' + text);
Blockly.TestBlocks.dynamicDropdownOptions_.splice(i, 1);
}
}
})
};
12 changes: 12 additions & 0 deletions tests/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
scaleSpeed: 1.1
}
});
addToolboxButtonCallbacks();
// Restore previously displayed text.
if (sessionStorage) {
var text = sessionStorage.getItem('textarea');
Expand All @@ -138,6 +139,13 @@
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") {
Expand Down Expand Up @@ -1182,6 +1190,10 @@ <h1>Blockly Playground</h1>
<block type="example_dropdown_long"></block>
<block type="example_dropdown_images"></block>
<block type="example_dropdown_images_and_text"></block>
<label text="Dynamic Drop-downs"></label>
<block type="test_dropdown_dynamic"></block>
<button text="Add option" callbackKey="addDynamicOption"></button>
<button text="Remove option" callbackKey="removeDynamicOption"></button>
<label text="Others"></label>
<block type="example_angle"></block>
<block type="example_date"></block>
Expand Down