2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2014-01-28 03:00:09 -08:00
|
|
|
* @license
|
2013-10-30 14:46:03 -07:00
|
|
|
* Visual Blocks Editor
|
|
|
|
*
|
|
|
|
* Copyright 2012 Google Inc.
|
2014-10-07 13:09:55 -07:00
|
|
|
* https://developers.google.com/blockly/
|
2013-10-30 14:46:03 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-12-18 17:26:08 -08:00
|
|
|
* @fileoverview Utility functions for handling variables.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.Variables');
|
|
|
|
|
|
|
|
// TODO(scr): Fix circular dependencies
|
|
|
|
// goog.require('Blockly.Block');
|
|
|
|
goog.require('Blockly.Workspace');
|
2015-02-06 15:27:25 -08:00
|
|
|
goog.require('goog.string');
|
2013-10-30 14:46:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Category to separate variable names from procedures and generated functions.
|
|
|
|
*/
|
|
|
|
Blockly.Variables.NAME_TYPE = 'VARIABLE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all user-created variables.
|
2015-01-12 14:57:00 -08:00
|
|
|
* @param {!Blockly.Block|!Blockly.Workspace} root Root block or workspace.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @return {!Array.<string>} Array of variable names.
|
|
|
|
*/
|
2015-01-12 14:57:00 -08:00
|
|
|
Blockly.Variables.allVariables = function(root) {
|
2013-10-30 14:46:03 -07:00
|
|
|
var blocks;
|
2014-12-23 11:22:02 -08:00
|
|
|
if (root.getDescendants) {
|
|
|
|
// Root is Block.
|
|
|
|
blocks = root.getDescendants();
|
|
|
|
} else if (root.getAllBlocks) {
|
|
|
|
// Root is Workspace.
|
|
|
|
blocks = root.getAllBlocks();
|
2013-10-30 14:46:03 -07:00
|
|
|
} else {
|
2014-12-23 11:22:02 -08:00
|
|
|
throw 'Not Block or Workspace: ' + root;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
var variableHash = Object.create(null);
|
|
|
|
// Iterate through every block and add each variable to the hash.
|
|
|
|
for (var x = 0; x < blocks.length; x++) {
|
2016-02-29 15:02:05 -08:00
|
|
|
var blockVariables = blocks[x].getVars();
|
|
|
|
for (var y = 0; y < blockVariables.length; y++) {
|
|
|
|
var varName = blockVariables[y];
|
|
|
|
// Variable name may be null if the block is only half-built.
|
|
|
|
if (varName) {
|
|
|
|
variableHash[varName.toLowerCase()] = varName;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Flatten the hash into a list.
|
|
|
|
var variableList = [];
|
|
|
|
for (var name in variableHash) {
|
|
|
|
variableList.push(variableHash[name]);
|
|
|
|
}
|
|
|
|
return variableList;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all instances of the specified variable and rename them.
|
|
|
|
* @param {string} oldName Variable to rename.
|
|
|
|
* @param {string} newName New variable name.
|
2015-01-12 14:57:00 -08:00
|
|
|
* @param {!Blockly.Workspace} workspace Workspace rename variables in.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2015-01-12 14:57:00 -08:00
|
|
|
Blockly.Variables.renameVariable = function(oldName, newName, workspace) {
|
2016-03-06 14:51:03 -08:00
|
|
|
Blockly.Events.setGroup(true);
|
2014-12-23 11:22:02 -08:00
|
|
|
var blocks = workspace.getAllBlocks();
|
2013-10-30 14:46:03 -07:00
|
|
|
// Iterate through every block.
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
2016-02-29 15:02:05 -08:00
|
|
|
blocks[i].renameVar(oldName, newName);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2016-03-06 14:51:03 -08:00
|
|
|
Blockly.Events.setGroup(false);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the blocks required by the flyout for the variable category.
|
2015-10-25 22:20:08 -04:00
|
|
|
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
|
|
|
* @return {!Array.<!Element>} Array of XML block elements.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2015-10-25 22:20:08 -04:00
|
|
|
Blockly.Variables.flyoutCategory = function(workspace) {
|
|
|
|
var variableList = Blockly.Variables.allVariables(workspace);
|
2013-10-30 14:46:03 -07:00
|
|
|
variableList.sort(goog.string.caseInsensitiveCompare);
|
|
|
|
// In addition to the user's variables, we also want to display the default
|
|
|
|
// variable name at the top. We also don't want this duplicated if the
|
|
|
|
// user has created a variable of the same name.
|
2015-10-25 22:20:08 -04:00
|
|
|
goog.array.remove(variableList, Blockly.Msg.VARIABLES_DEFAULT_NAME);
|
|
|
|
variableList.unshift(Blockly.Msg.VARIABLES_DEFAULT_NAME);
|
|
|
|
|
|
|
|
var xmlList = [];
|
2013-10-30 14:46:03 -07:00
|
|
|
for (var i = 0; i < variableList.length; i++) {
|
2015-10-25 22:20:08 -04:00
|
|
|
if (Blockly.Blocks['variables_set']) {
|
|
|
|
// <block type="variables_set" gap="8">
|
|
|
|
// <field name="VAR">item</field>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'variables_set');
|
|
|
|
if (Blockly.Blocks['variables_get']) {
|
|
|
|
block.setAttribute('gap', 8);
|
|
|
|
}
|
|
|
|
var field = goog.dom.createDom('field', null, variableList[i]);
|
|
|
|
field.setAttribute('name', 'VAR');
|
|
|
|
block.appendChild(field);
|
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2015-10-25 22:20:08 -04:00
|
|
|
if (Blockly.Blocks['variables_get']) {
|
|
|
|
// <block type="variables_get" gap="24">
|
|
|
|
// <field name="VAR">item</field>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'variables_get');
|
|
|
|
if (Blockly.Blocks['variables_set']) {
|
|
|
|
block.setAttribute('gap', 24);
|
|
|
|
}
|
|
|
|
var field = goog.dom.createDom('field', null, variableList[i]);
|
|
|
|
field.setAttribute('name', 'VAR');
|
|
|
|
block.appendChild(field);
|
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
2015-10-25 22:20:08 -04:00
|
|
|
return xmlList;
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new variable name that is not yet being used. This will try to
|
|
|
|
* generate single letter variable names in the range 'i' to 'z' to start with.
|
2015-01-12 14:57:00 -08:00
|
|
|
* If no unique name is located it will try 'i' to 'z', 'a' to 'h',
|
|
|
|
* then 'i2' to 'z2' etc. Skip 'l'.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace to be unique in.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @return {string} New variable name.
|
|
|
|
*/
|
2015-01-12 14:57:00 -08:00
|
|
|
Blockly.Variables.generateUniqueName = function(workspace) {
|
|
|
|
var variableList = Blockly.Variables.allVariables(workspace);
|
2013-10-30 14:46:03 -07:00
|
|
|
var newName = '';
|
|
|
|
if (variableList.length) {
|
2015-01-12 14:57:00 -08:00
|
|
|
var nameSuffix = 1;
|
|
|
|
var letters = 'ijkmnopqrstuvwxyzabcdefgh'; // No 'l'.
|
|
|
|
var letterIndex = 0;
|
|
|
|
var potName = letters.charAt(letterIndex);
|
2013-10-30 14:46:03 -07:00
|
|
|
while (!newName) {
|
2015-01-12 14:57:00 -08:00
|
|
|
var inUse = false;
|
|
|
|
for (var i = 0; i < variableList.length; i++) {
|
2013-10-30 14:46:03 -07:00
|
|
|
if (variableList[i].toLowerCase() == potName) {
|
|
|
|
// This potential name is already used.
|
|
|
|
inUse = true;
|
2015-01-12 14:57:00 -08:00
|
|
|
break;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inUse) {
|
|
|
|
// Try the next potential name.
|
2015-01-12 14:57:00 -08:00
|
|
|
letterIndex++;
|
|
|
|
if (letterIndex == letters.length) {
|
|
|
|
// Reached the end of the character sequence so back to 'i'.
|
2013-10-30 14:46:03 -07:00
|
|
|
// a new suffix.
|
2015-01-12 14:57:00 -08:00
|
|
|
letterIndex = 0;
|
2013-10-30 14:46:03 -07:00
|
|
|
nameSuffix++;
|
|
|
|
}
|
2015-01-12 14:57:00 -08:00
|
|
|
potName = letters.charAt(letterIndex);
|
|
|
|
if (nameSuffix > 1) {
|
2013-10-30 14:46:03 -07:00
|
|
|
potName += nameSuffix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We can use the current potential name.
|
|
|
|
newName = potName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newName = 'i';
|
|
|
|
}
|
|
|
|
return newName;
|
|
|
|
};
|