From 2bb258165af6f571c4719a34bef191967531271f Mon Sep 17 00:00:00 2001 From: marisaleung Date: Fri, 5 May 2017 09:04:18 -0700 Subject: [PATCH] Names are correctly fetched from VariableModels! --- core/field_variable.js | 30 ++++++++++++++++++------------ core/variables.js | 18 +++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index d02dd2a7f..8f8b54284 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -134,30 +134,36 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) { * @this {Blockly.FieldVariable} */ Blockly.FieldVariable.dropdownCreate = function() { + var variableNameList = []; if (this.sourceBlock_ && this.sourceBlock_.workspace) { // Get a copy of the list, so that adding rename and new variable options // doesn't modify the workspace's list. - var variableList = this.sourceBlock_.workspace.getVariablesOfType(''); - } else { - var variableList = []; + + var variableModelList = this.sourceBlock_.workspace.getVariablesOfType(''); + for (var i = 0; i < variableModelList.length; i++) { + variableNameList.push(variableModelList[i].name); + } } // Ensure that the currently selected variable is an option. var name = this.getText(); - if (name && variableList.indexOf(name) == -1) { - variableList.push(name); + if (name && variableNameList.indexOf(name) == -1) { + variableNameList.push(name); } - variableList.sort(goog.string.caseInsensitiveCompare); + variableNameList.sort(goog.string.caseInsensitiveCompare); - this.renameVarItemIndex_ = variableList.length; - variableList.push(Blockly.Msg.RENAME_VARIABLE); + this.renameVarItemIndex_ = variableNameList.length; + variableNameList.push(Blockly.Msg.RENAME_VARIABLE); - this.deleteVarItemIndex_ = variableList.length; - variableList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name)); + this.deleteVarItemIndex_ = variableNameList.length; + variableNameList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name)); // Variables are not language-specific, use the name as both the user-facing // text and the internal representation. var options = []; - for (var i = 0; i < variableList.length; i++) { - options[i] = [variableList[i], variableList[i]]; + for (var i = 0; i < variableNameList.length; i++) { + // TODO(marisaleung): Set options[i] to [name, uuid]. This requires + // changes where the variable gets set since the initialized value would be + // id. + options[i] = [variableNameList[i], variableNameList[i]]; } return options; }; diff --git a/core/variables.js b/core/variables.js index 361e9c795..387349a66 100644 --- a/core/variables.js +++ b/core/variables.js @@ -105,8 +105,12 @@ Blockly.Variables.allVariables = function(root) { * @return {!Array.} Array of XML block elements. */ Blockly.Variables.flyoutCategory = function(workspace) { - var variableList = workspace.getVariablesOfType(''); - variableList.sort(goog.string.caseInsensitiveCompare); + var variableNameList = []; + var variableModelList = workspace.getVariablesOfType(''); + for (var i = 0; i < variableModelList.length; i++) { + variableNameList.push(variableModelList[i].name); + } + variableNameList.sort(goog.string.caseInsensitiveCompare); var xmlList = []; var button = goog.dom.createDom('button'); @@ -119,12 +123,12 @@ Blockly.Variables.flyoutCategory = function(workspace) { xmlList.push(button); - if (variableList.length > 0) { + if (variableNameList.length > 0) { if (Blockly.Blocks['variables_set']) { var gap = Blockly.Blocks['math_change'] ? 8 : 24; var blockText = '' + '' + - '' + variableList[0] + '' + + '' + variableNameList[0] + '' + '' + ''; var block = Blockly.Xml.textToDom(blockText).firstChild; @@ -134,7 +138,7 @@ Blockly.Variables.flyoutCategory = function(workspace) { var gap = Blockly.Blocks['variables_get'] ? 20 : 8; var blockText = '' + '' + - '' + variableList[0] + '' + + '' + variableNameList[0] + '' + '' + '' + '1' + @@ -146,11 +150,11 @@ Blockly.Variables.flyoutCategory = function(workspace) { xmlList.push(block); } - for (var i = 0; i < variableList.length; i++) { + for (var i = 0; i < variableNameList.length; i++) { if (Blockly.Blocks['variables_get']) { var blockText = '' + '' + - '' + variableList[i] + '' + + '' + variableNameList[i] + '' + '' + ''; var block = Blockly.Xml.textToDom(blockText).firstChild;