mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
Added a test block for a dynamic dropdown.
This commit is contained in:
@@ -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.
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
@@ -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 {
|
||||
<block type="example_dropdown_long"></block>
|
||||
<block type="example_dropdown_images"></block>
|
||||
<block type="example_dropdown_images_and_text"></block>
|
||||
<button text="add option" callbackKey="addDynamicOption"></button>
|
||||
<button text="remove option" callbackKey="removeDynamicOption"></button>
|
||||
<block type="test_dropdown_dynamic"></block>
|
||||
<label text="Others"></label>
|
||||
<block type="example_angle"></block>
|
||||
<block type="example_date"></block>
|
||||
|
||||
Reference in New Issue
Block a user