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');
|
|
|
|
|
2016-06-03 16:12:59 -07:00
|
|
|
goog.require('Blockly.Blocks');
|
2013-10-30 14:46:03 -07:00
|
|
|
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';
|
|
|
|
|
|
|
|
/**
|
2016-07-07 15:23:20 -07:00
|
|
|
* Find all user-created variables that are in use in the workspace.
|
|
|
|
* For use by generators.
|
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.
|
|
|
|
*/
|
2016-07-07 15:23:20 -07:00
|
|
|
Blockly.Variables.allUsedVariables = function(root) {
|
2013-10-30 14:46:03 -07:00
|
|
|
var blocks;
|
2016-07-13 12:50:44 -07:00
|
|
|
if (root instanceof Blockly.Block) {
|
2014-12-23 11:22:02 -08:00
|
|
|
// Root is Block.
|
|
|
|
blocks = root.getDescendants();
|
2016-07-13 14:36:35 -07:00
|
|
|
} else if (root instanceof Blockly.Workspace ||
|
|
|
|
root instanceof Blockly.WorkspaceSvg) {
|
2014-12-23 11:22:02 -08:00
|
|
|
// 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();
|
2016-06-22 16:16:31 -07:00
|
|
|
if (blockVariables) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-07-07 15:23:20 -07:00
|
|
|
/**
|
|
|
|
* Find all variables that the user has created through the workspace or
|
|
|
|
* toolbox. For use by generators.
|
|
|
|
* @param {!Blockly.Workspace} root The workspace to inspect.
|
|
|
|
* @return {!Array.<string>} Array of variable names.
|
|
|
|
*/
|
|
|
|
Blockly.Variables.allVariables = function(root) {
|
2016-07-13 12:50:44 -07:00
|
|
|
if (root instanceof Blockly.Block) {
|
2016-07-07 15:23:20 -07:00
|
|
|
// Root is Block.
|
|
|
|
console.warn('Deprecated call to Blockly.Variables.allVariables ' +
|
|
|
|
'with a block instead of a workspace. You may want ' +
|
|
|
|
'Blockly.Variables.allUsedVariables');
|
|
|
|
}
|
|
|
|
return root.variableList;
|
|
|
|
};
|
|
|
|
|
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) {
|
2016-06-22 16:16:31 -07:00
|
|
|
var variableList = workspace.variableList;
|
2013-10-30 14:46:03 -07:00
|
|
|
variableList.sort(goog.string.caseInsensitiveCompare);
|
2015-10-25 22:20:08 -04:00
|
|
|
|
|
|
|
var xmlList = [];
|
2016-06-21 13:42:03 -07:00
|
|
|
var button = goog.dom.createDom('button');
|
2016-08-16 16:53:53 -07:00
|
|
|
button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
|
2016-10-31 14:24:00 -07:00
|
|
|
button.setAttribute('callbackKey', 'CREATE_VARIABLE');
|
|
|
|
|
2017-02-02 14:17:43 -05:00
|
|
|
workspace.registerButtonCallback('CREATE_VARIABLE', function(button) {
|
2016-10-31 14:24:00 -07:00
|
|
|
Blockly.Variables.createVariable(button.getTargetWorkspace());
|
|
|
|
});
|
|
|
|
|
2016-06-21 13:42:03 -07:00
|
|
|
xmlList.push(button);
|
2016-07-01 15:51:59 -07:00
|
|
|
|
2016-07-13 14:36:35 -07:00
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
for (var i = 0; i < variableList.length; i++) {
|
2016-07-13 14:36:35 -07:00
|
|
|
if (Blockly.Blocks['data_variable']) {
|
|
|
|
// <block type="data_variable">
|
|
|
|
// <value name="VARIABLE">
|
|
|
|
// <shadow type="data_variablemenu"></shadow>
|
|
|
|
// </value>
|
2015-10-25 22:20:08 -04:00
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
2016-07-13 14:36:35 -07:00
|
|
|
block.setAttribute('type', 'data_variable');
|
2016-07-01 16:17:30 -07:00
|
|
|
block.setAttribute('gap', 8);
|
2016-07-13 16:28:41 -07:00
|
|
|
block.appendChild(Blockly.Variables.createVariableDom_(variableList[i]));
|
2015-10-25 22:20:08 -04:00
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2016-07-01 15:51:59 -07:00
|
|
|
}
|
2016-07-01 16:17:30 -07:00
|
|
|
|
2016-07-13 14:36:35 -07:00
|
|
|
if (xmlList.length > 1) { // The button is always there.
|
|
|
|
xmlList[xmlList.length - 1].setAttribute('gap', 24);
|
2016-07-01 16:17:30 -07:00
|
|
|
|
2016-07-13 14:36:35 -07:00
|
|
|
if (Blockly.Blocks['data_setvariableto']) {
|
|
|
|
// <block type="data_setvariableto" gap="20">
|
|
|
|
// <value name="VARIABLE">
|
|
|
|
// <shadow type="data_variablemenu"></shadow>
|
|
|
|
// </value>
|
|
|
|
// <value name="VALUE">
|
2016-09-27 14:22:46 -04:00
|
|
|
// <shadow type="text">
|
|
|
|
// <field name="TEXT">0</field>
|
2016-07-13 14:36:35 -07:00
|
|
|
// </shadow>
|
|
|
|
// </value>
|
2015-10-25 22:20:08 -04:00
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
2016-07-13 14:36:35 -07:00
|
|
|
block.setAttribute('type', 'data_setvariableto');
|
|
|
|
block.setAttribute('gap', 8);
|
2016-07-13 16:28:41 -07:00
|
|
|
block.appendChild(Blockly.Variables.createVariableDom_(variableList[0]));
|
2016-09-27 14:22:46 -04:00
|
|
|
block.appendChild(Blockly.Variables.createTextDom_());
|
2016-07-13 14:36:35 -07:00
|
|
|
xmlList.push(block);
|
|
|
|
}
|
|
|
|
if (Blockly.Blocks['data_changevariableby']) {
|
|
|
|
// <block type="data_changevariableby">
|
|
|
|
// <value name="VARIABLE">
|
|
|
|
// <shadow type="data_variablemenu"></shadow>
|
|
|
|
// </value>
|
|
|
|
// <value name="VALUE">
|
|
|
|
// <shadow type="math_number">
|
|
|
|
// <field name="NUM">0</field>
|
|
|
|
// </shadow>
|
|
|
|
// </value>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'data_changevariableby');
|
|
|
|
block.setAttribute('gap', 8);
|
2016-07-13 16:28:41 -07:00
|
|
|
block.appendChild(Blockly.Variables.createVariableDom_(variableList[0]));
|
2016-07-13 14:36:35 -07:00
|
|
|
block.appendChild(Blockly.Variables.createMathNumberDom_());
|
|
|
|
xmlList.push(block);
|
|
|
|
}
|
|
|
|
if (Blockly.Blocks['data_showvariable']) {
|
|
|
|
// <block type="data_showvariable">
|
|
|
|
// <value name="VARIABLE">
|
|
|
|
// <shadow type="data_variablemenu"></shadow>
|
|
|
|
// </value>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'data_showvariable');
|
|
|
|
block.setAttribute('gap', 8);
|
2016-07-13 16:28:41 -07:00
|
|
|
block.appendChild(Blockly.Variables.createVariableDom_(variableList[0]));
|
2016-07-13 14:36:35 -07:00
|
|
|
xmlList.push(block);
|
|
|
|
}
|
|
|
|
if (Blockly.Blocks['data_hidevariable']) {
|
|
|
|
// <block type="data_showvariable">
|
|
|
|
// <value name="VARIABLE">
|
|
|
|
// <shadow type="data_variablemenu"></shadow>
|
|
|
|
// </value>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
2016-07-13 16:28:41 -07:00
|
|
|
block.setAttribute('type', 'data_hidevariable');
|
|
|
|
block.appendChild(Blockly.Variables.createVariableDom_(variableList[0]));
|
2015-10-25 22:20:08 -04:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-07-13 14:36:35 -07:00
|
|
|
/**
|
|
|
|
* Create a dom element for a value tag with the given name attribute.
|
|
|
|
* @param {string} name The value to use for the name attribute.
|
|
|
|
* @return {!Element} An XML element: <value name="name"></value>
|
|
|
|
*/
|
|
|
|
Blockly.Variables.createValueDom_ = function(name) {
|
|
|
|
var value = goog.dom.createDom('value');
|
|
|
|
value.setAttribute('name', name);
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a dom element for a shadow tag with the given tupe attribute.
|
|
|
|
* @param {string} type The value to use for the type attribute.
|
2016-07-13 16:28:41 -07:00
|
|
|
* @param {string} value The value to have inside the tag.
|
|
|
|
* @return {!Element} An XML element: <shadow type="type">value</shadow>
|
2016-07-13 14:36:35 -07:00
|
|
|
*/
|
|
|
|
Blockly.Variables.createShadowDom_ = function(type) {
|
|
|
|
var shadow = goog.dom.createDom('shadow');
|
|
|
|
shadow.setAttribute('type', type);
|
|
|
|
return shadow;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a dom element for value tag with a shadow variable inside.
|
2016-07-13 16:28:41 -07:00
|
|
|
* @param {string} name The name of the variable to select.
|
2016-07-13 14:36:35 -07:00
|
|
|
* @return {!Element} An XML element.
|
|
|
|
*/
|
2016-07-13 16:28:41 -07:00
|
|
|
Blockly.Variables.createVariableDom_ = function(name) {
|
2016-07-13 14:36:35 -07:00
|
|
|
// <value name="VARIABLE">
|
2016-07-13 16:28:41 -07:00
|
|
|
// <shadow type="data_variablemenu">
|
|
|
|
// <field name="VARIABLE">variablename
|
|
|
|
// </field>
|
|
|
|
// </shadow>
|
2016-07-13 14:36:35 -07:00
|
|
|
// </value>
|
|
|
|
var value = Blockly.Variables.createValueDom_('VARIABLE');
|
2016-07-13 16:28:41 -07:00
|
|
|
var shadow = Blockly.Variables.createShadowDom_('data_variablemenu');
|
|
|
|
var field = goog.dom.createDom('field', null, name);
|
|
|
|
field.setAttribute('name', 'VARIABLE');
|
|
|
|
shadow.appendChild(field);
|
|
|
|
value.appendChild(shadow);
|
2016-07-13 14:36:35 -07:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2016-09-27 14:22:46 -04:00
|
|
|
/**
|
|
|
|
* Create a dom element for value tag with a shadow text inside.
|
|
|
|
* @return {!Element} An XML element.
|
|
|
|
*/
|
|
|
|
Blockly.Variables.createTextDom_ = function() {
|
|
|
|
// <value name="VALUE">
|
|
|
|
// <shadow type="text">
|
|
|
|
// <field name="TEXT">0</field>
|
|
|
|
// </shadow>
|
|
|
|
// </value>
|
|
|
|
var value = Blockly.Variables.createValueDom_('VALUE');
|
|
|
|
var shadow = Blockly.Variables.createShadowDom_('text');
|
|
|
|
var field = goog.dom.createDom('field', null, '0');
|
|
|
|
field.setAttribute('name', 'TEXT');
|
|
|
|
shadow.appendChild(field);
|
|
|
|
value.appendChild(shadow);
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2016-07-13 14:36:35 -07:00
|
|
|
/**
|
|
|
|
* Create a dom element for value tag with a shadow number inside.
|
|
|
|
* @return {!Element} An XML element.
|
|
|
|
*/
|
|
|
|
Blockly.Variables.createMathNumberDom_ = function() {
|
|
|
|
// <value name="VALUE">
|
|
|
|
// <shadow type="math_number">
|
|
|
|
// <field name="NUM">0</field>
|
|
|
|
// </shadow>
|
|
|
|
// </value>
|
|
|
|
var value = Blockly.Variables.createValueDom_('VALUE');
|
|
|
|
var shadow = Blockly.Variables.createShadowDom_('math_number');
|
2016-09-27 14:22:46 -04:00
|
|
|
var field = goog.dom.createDom('field', null, '1');
|
2016-07-13 14:36:35 -07:00
|
|
|
field.setAttribute('name', 'NUM');
|
|
|
|
shadow.appendChild(field);
|
|
|
|
value.appendChild(shadow);
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
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) {
|
2016-06-22 16:16:31 -07:00
|
|
|
var variableList = workspace.variableList;
|
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;
|
|
|
|
};
|
2016-06-22 17:19:49 -07:00
|
|
|
|
2016-07-01 15:51:59 -07:00
|
|
|
/**
|
|
|
|
* Create a new variable on the given workspace.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace on which to create the
|
|
|
|
* variable.
|
2016-11-02 13:56:27 -07:00
|
|
|
* @param {function(?string)=} opt_callback A callback. It
|
|
|
|
* will be passed a new variable name, or null if the change is to be
|
|
|
|
* aborted (cancel button).
|
2016-07-01 15:51:59 -07:00
|
|
|
*/
|
2016-10-20 09:30:45 -07:00
|
|
|
Blockly.Variables.createVariable = function(workspace, opt_callback) {
|
|
|
|
var promptAndCheckWithAlert = function(defaultName) {
|
|
|
|
Blockly.Variables.promptName(Blockly.Msg.NEW_VARIABLE_TITLE, defaultName,
|
|
|
|
function(text) {
|
|
|
|
if (text) {
|
|
|
|
if (workspace.variableIndexOf(text) != -1) {
|
|
|
|
Blockly.alert(Blockly.Msg.VARIABLE_ALREADY_EXISTS.replace('%1',
|
|
|
|
text.toLowerCase()),
|
|
|
|
function() {
|
|
|
|
promptAndCheckWithAlert(text); // Recurse
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
workspace.createVariable(text);
|
|
|
|
if (opt_callback) {
|
|
|
|
opt_callback(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// User canceled prompt without a value.
|
|
|
|
if (opt_callback) {
|
|
|
|
opt_callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
promptAndCheckWithAlert('');
|
2016-07-01 15:51:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the user for a new variable name.
|
|
|
|
* @param {string} promptText The string of the prompt.
|
|
|
|
* @param {string} defaultText The default value to show in the prompt's field.
|
2016-11-02 13:56:27 -07:00
|
|
|
* @param {function(?string)} callback A callback. It will be passed the new
|
2016-10-20 09:30:45 -07:00
|
|
|
* variable name, or null if the user picked something illegal.
|
2016-07-01 15:51:59 -07:00
|
|
|
*/
|
2016-10-20 09:30:45 -07:00
|
|
|
Blockly.Variables.promptName = function(promptText, defaultText, callback) {
|
|
|
|
Blockly.prompt(promptText, defaultText, function(newVar) {
|
|
|
|
// Merge runs of whitespace. Strip leading and trailing whitespace.
|
|
|
|
// Beyond this, all names are legal.
|
|
|
|
if (newVar) {
|
|
|
|
newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
|
|
|
|
if (newVar == Blockly.Msg.RENAME_VARIABLE ||
|
|
|
|
newVar == Blockly.Msg.NEW_VARIABLE) {
|
|
|
|
// Ok, not ALL names are legal...
|
|
|
|
newVar = null;
|
|
|
|
}
|
2016-07-01 15:51:59 -07:00
|
|
|
}
|
2016-10-20 09:30:45 -07:00
|
|
|
callback(newVar);
|
|
|
|
});
|
2016-07-01 15:51:59 -07:00
|
|
|
};
|