-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<!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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion for default: 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); | ||
} | ||
} | ||
}) | ||
}; |
There was a problem hiding this comment.
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.