diff --git a/core/variables.js b/core/variables.js index 717e9f705..0e12b99d9 100644 --- a/core/variables.js +++ b/core/variables.js @@ -275,19 +275,30 @@ Blockly.Variables.generateUniqueName = function(workspace) { */ Blockly.Variables.createVariableButtonHandler = function( workspace, opt_callback, opt_type) { + var type = opt_type || ''; // This function needs to be named so it can be called recursively. var promptAndCheckWithAlert = function(defaultName) { Blockly.Variables.promptName(Blockly.Msg.NEW_VARIABLE_TITLE, defaultName, function(text) { if (text) { - if (workspace.getVariable(text, opt_type)) { - Blockly.alert(Blockly.Msg.VARIABLE_ALREADY_EXISTS.replace('%1', - text.toLowerCase()), + var existing = Blockly.Variables.nameUsedWithAnyType_(text, + workspace); + if (existing) { + var lowerCase = text.toLowerCase(); + if (existing.type == type) { + var msg = Blockly.Msg.VARIABLE_ALREADY_EXISTS.replace('%1', + lowerCase); + } else { + var msg = Blockly.Msg.VARIABLE_ALREADY_EXISTS_FOR_ANOTHER_TYPE; + msg = msg.replace('%1', lowerCase).replace('%2', existing.type); + } + Blockly.alert(msg, function() { promptAndCheckWithAlert(text); // Recurse }); } else { - workspace.createVariable(text, opt_type); + // No conflict + workspace.createVariable(text, type); if (opt_callback) { opt_callback(text); } @@ -340,9 +351,21 @@ Blockly.Variables.renameVariable = function(workspace, variable, Blockly.Variables.promptName(promptText, defaultName, function(newName) { if (newName) { - workspace.renameVariableById(variable.getId(), newName); - if (opt_callback) { - opt_callback(newName); + var existing = Blockly.Variables.nameUsedWithOtherType_(newName, + variable.type, workspace); + if (existing) { + var msg = + Blockly.Msg.VARIABLE_ALREADY_EXISTS_FOR_ANOTHER_TYPE.replace( + '%1', newName.toLowerCase()).replace('%2', existing.type); + Blockly.alert(msg, + function() { + promptAndCheckWithAlert(newName); // Recurse + }); + } else { + workspace.renameVariableById(variable.getId(), newName); + if (opt_callback) { + opt_callback(newName); + } } } else { // User canceled prompt without a value. @@ -378,6 +401,30 @@ Blockly.Variables.promptName = function(promptText, defaultText, callback) { }); }; +Blockly.Variables.nameUsedWithOtherType_ = function(name, type, workspace) { + var allVariables = workspace.getVariableMap().getAllVariables(); + + name = name.toLowerCase(); + for (var i = 0, variable; variable = allVariables[i]; i++) { + if (variable.name.toLowerCase() == name && variable.type != type) { + return variable; + } + } + return null; +}; + +Blockly.Variables.nameUsedWithAnyType_ = function(name, workspace) { + var allVariables = workspace.getVariableMap().getAllVariables(); + + name = name.toLowerCase(); + for (var i = 0, variable; variable = allVariables[i]; i++) { + if (variable.name.toLowerCase() == name) { + return variable; + } + } + return null; +}; + /** * Generate XML string for variable field. * @param {!Blockly.VariableModel} variableModel The variable model to generate diff --git a/msg/messages.js b/msg/messages.js index 21f869bb2..ea9d8f91e 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -136,7 +136,7 @@ Blockly.Msg.NEW_VARIABLE_TITLE = 'New variable name:'; /// alert - Tells the user that the name they entered is already in use. Blockly.Msg.VARIABLE_ALREADY_EXISTS = 'A variable named "%1" already exists.'; /// alert - Tells the user that the name they entered is already in use for another type. -Blockly.Msg.VARIABLE_ALREADY_EXISTS_FOR_ANOTHER_TYPE = 'A variable named "%1" already exists for another variable of type "%2".'; +Blockly.Msg.VARIABLE_ALREADY_EXISTS_FOR_ANOTHER_TYPE = 'A variable named "%1" already exists for another type: "%2".'; // Variable deletion. /// confirm - Ask the user to confirm their deletion of multiple uses of a variable.