Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions core/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,19 @@ Blockly.Variables.allDeveloperVariables = function(workspace) {
*/
Blockly.Variables.flyoutCategory = function(workspace) {
var xmlList = [];
var button = document.createElement('button');
button.setAttribute('text', '%{BKY_NEW_VARIABLE}');
button.setAttribute('callbackKey', 'CREATE_VARIABLE');
xmlList.push(Blockly.Variables.createButton_(
'%{BKY_NEW_VARIABLE}', 'CREATE_VARIABLE'));

xmlList.push(Blockly.Variables.createButton_(
'%{BKY_DELETE_VARIABLE_BUTTON_TITLE}', 'DELETE_VARIABLE'));

workspace.registerButtonCallback('DELETE_VARIABLE', function(deleteButton) {
Blockly.Variables.deleteVariableButtonHandler(deleteButton.getTargetWorkspace());
});
workspace.registerButtonCallback('CREATE_VARIABLE', function(button) {
Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace());
});

xmlList.push(button);

var blockList = Blockly.Variables.flyoutCategoryBlocks(workspace);
xmlList = xmlList.concat(blockList);
Expand Down Expand Up @@ -324,9 +328,44 @@ Blockly.Variables.createVariableButtonHandler = function(
};
promptAndCheckWithAlert('');
};

/**
* Handles "Delete Variable" button in the default variables toolbox category.
* It will prompt the user for a varibale name, including re-prompts if a name
* does not exist.
*
* @param {!Blockly.Workspace} workspace The workspace on which to delete the
* variable.
*/
Blockly.Variables.deleteVariableButtonHandler = function(workspace) {
var promptAndCheckWithAlert = function(defaultName) {
Blockly.Variables.promptName(Blockly.Msg['DELETE_VARIABLE_PROMPT'], defaultName,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using the existing text field prompt can you make one with a dropdown with all existing variables?

function(text) {
if (text) {
var existing =
Blockly.Variables.nameUsedWithAnyType_(text, workspace);
if (existing){
workspace.deleteVariableById(existing.getId());
}
else {
var lowerCase = text.toLowerCase();
var msg = Blockly.Msg['VARIABLE_DOES_NOT_EXIST'];
msg = msg.replace('%1', lowerCase);
Blockly.alert(msg, function() {promptAndCheckWithAlert(text);});
}
}
}
);
};
promptAndCheckWithAlert('');
};

goog.exportSymbol('Blockly.Variables.createVariableButtonHandler',
Blockly.Variables.createVariableButtonHandler);

goog.exportSymbol('Blockly.Variables.deleteVariableButtonHandler',
Blockly.Variables.deleteVariableButtonHandler);

/**
* Original name of Blockly.Variables.createVariableButtonHandler(..).
* @deprecated Use Blockly.Variables.createVariableButtonHandler(..).
Expand Down Expand Up @@ -581,6 +620,19 @@ Blockly.Variables.createVariable_ = function(workspace, id, opt_name,
return variable;
};

/**
* Helper function to create a button for the flyout window.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that this creates the XML for the button, not the button itself. That's because the XML then gets passed into the flyout code, which creates the button onscreen.

* @param {string} msg The text displayed on the button.
* @param {string} callbackKey The callback key.
* @return {Element} button The created button.
*/
Blockly.Variables.createButton_ = function(msg, callbackKey) {
var button = document.createElement('button');
button.setAttribute('text', msg);
button.setAttribute('callbackKey', callbackKey);
return button;
};

/**
* Helper function to get the list of variables that have been added to the
* workspace after adding a new block, using the given list of variables that
Expand Down
31 changes: 19 additions & 12 deletions core/variables_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour = function(button) {
Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace(),
null, 'Colour');
};
Blockly.VariablesDynamic.onDeleteVariableButtonClick = function(button) {
Blockly.Variables.deleteVariableButtonHandler(button.getTargetWorkspace());
};

/**
* Construct the elements (blocks and button) required by the flyout for the
* variable category.
Expand All @@ -56,25 +60,28 @@ Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour = function(button) {
*/
Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
var xmlList = [];
var button = document.createElement('button');
button.setAttribute('text', Blockly.Msg['NEW_STRING_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_STRING');
xmlList.push(button);
button = document.createElement('button');
button.setAttribute('text', Blockly.Msg['NEW_NUMBER_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_NUMBER');
xmlList.push(button);
button = document.createElement('button');
button.setAttribute('text', Blockly.Msg['NEW_COLOUR_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_COLOUR');
xmlList.push(button);

xmlList.push(Blockly.Variables.createButton_(
'%{BKY_NEW_STRING_VARIABLE}', 'CREATE_VARIABLE_STRING'));

xmlList.push(Blockly.Variables.createButton_(
'%{BKY_NEW_NUMBER_VARIABLE}', 'CREATE_VARIABLE_NUMBER'));

xmlList.push(Blockly.Variables.createButton_(
'%{BKY_NEW_COLOUR_VARIABLE}', 'CREATE_VARIABLE_COLOUR'));

xmlList.push(Blockly.Variables.createButton_(
'%{BKY_DELETE_VARIABLE_BUTTON_TITLE}', 'DELETE_VARIABLE'));

workspace.registerButtonCallback('CREATE_VARIABLE_STRING',
Blockly.VariablesDynamic.onCreateVariableButtonClick_String);
workspace.registerButtonCallback('CREATE_VARIABLE_NUMBER',
Blockly.VariablesDynamic.onCreateVariableButtonClick_Number);
workspace.registerButtonCallback('CREATE_VARIABLE_COLOUR',
Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour);
workspace.registerButtonCallback('DELETE_VARIABLE',
Blockly.VariablesDynamic.onDeleteVariableButtonClick);



var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
Expand Down
7 changes: 7 additions & 0 deletions msg/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ Blockly.Msg.DELETE_VARIABLE_CONFIRMATION = 'Delete %1 uses of the "%2" variable?
Blockly.Msg.CANNOT_DELETE_VARIABLE_PROCEDURE = 'Can\'t delete the variable "%1" because it\'s part of the definition of the function "%2"';
/// dropdown choice - Delete the currently selected variable.
Blockly.Msg.DELETE_VARIABLE = 'Delete the "%1" variable';
/// alert - Tells the user that the variable name does not exist.
Blockly.Msg.VARIABLE_DOES_NOT_EXIST = 'A variable named "%1" does not exist.';
/// alert - Text on the button used to launch the delete variable dialogue.
Blockly.Msg.DELETE_VARIABLE_BUTTON_TITLE = 'Delete variable...';
/// alert - Prompts the user to enter the name of an exisiting variable in order to delete it. See [https://github.com/google/blockly/wiki/Variables#dropdown-menu https://github.com/google/blockly/wiki/Variables#dropdown-menu]
Blockly.Msg.DELETE_VARIABLE_PROMPT = 'Delete variable with name:';


// Colour Blocks.
/// {{Optional}} url - Information about colour.
Expand Down