123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 'use strict';
- goog.provide('Blockly.Variables');
- goog.require('Blockly.Blocks');
- goog.require('Blockly.Workspace');
- goog.require('goog.string');
- Blockly.Variables.NAME_TYPE = 'VARIABLE';
- Blockly.Variables.allUsedVariables = function(root) {
- var blocks;
- if (root instanceof Blockly.Block) {
-
- blocks = root.getDescendants();
- } else if (root.getAllBlocks) {
-
- blocks = root.getAllBlocks();
- } else {
- throw 'Not Block or Workspace: ' + root;
- }
- var variableHash = Object.create(null);
-
- for (var x = 0; x < blocks.length; x++) {
- var blockVariables = blocks[x].getVars();
- if (blockVariables) {
- for (var y = 0; y < blockVariables.length; y++) {
- var varName = blockVariables[y];
-
- if (varName) {
- variableHash[varName.toLowerCase()] = varName;
- }
- }
- }
- }
-
- var variableList = [];
- for (var name in variableHash) {
- variableList.push(variableHash[name]);
- }
- return variableList;
- };
- Blockly.Variables.allVariables = function(root) {
- if (root instanceof Blockly.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;
- };
- Blockly.Variables.flyoutCategory = function(workspace) {
- var variableList = workspace.variableList;
- variableList.sort(goog.string.caseInsensitiveCompare);
- var xmlList = [];
- var button = goog.dom.createDom('button');
- button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
- button.setAttribute('callbackKey', 'CREATE_VARIABLE');
- Blockly.registerButtonCallback('CREATE_VARIABLE', function(button) {
- Blockly.Variables.createVariable(button.getTargetWorkspace());
- });
- xmlList.push(button);
- if (variableList.length > 0) {
-
- for (var i = 0; i < variableList.length; i++) {
- if (Blockly.Blocks['variables_set']) {
-
-
-
- var block = goog.dom.createDom('block');
- block.setAttribute('type', 'variables_set');
- if (Blockly.Blocks['math_change']) {
- block.setAttribute('gap', 8);
- } else {
-
- }
- var field = goog.dom.createDom('field', null, variableList[i]);
- field.setAttribute('name', 'VAR');
- block.appendChild(field);
- xmlList.push(block);
- }
- }
- for (var i = 0; i < variableList.length; i++) {
- if (Blockly.Blocks['variables_get']) {
-
-
-
- var block = goog.dom.createDom('block');
- block.setAttribute('type', 'variables_get');
- if (Blockly.Blocks['variables_set']) {
- block.setAttribute('gap', 8);
- }
- var field = goog.dom.createDom('field', null, variableList[i]);
- field.setAttribute('name', 'VAR');
- block.appendChild(field);
- xmlList.push(block);
- }
- }
- }
- if (Blockly.Blocks['variables_getself']) {
- var block = goog.dom.createDom('block');
- block.setAttribute('type', 'variables_getself');
- block.setAttribute('gap', 16);
- xmlList.push(block);
- }
- if (Blockly.Blocks['variables_setself']) {
- var block = goog.dom.createDom('block');
- block.setAttribute('type', 'variables_setself');
- block.setAttribute('gap', 16);
- xmlList.push(block);
- }
- return xmlList;
- };
- Blockly.Variables.generateUniqueName = function(workspace) {
- var variableList = workspace.variableList;
- var newName = '';
- if (variableList.length) {
- var nameSuffix = 1;
- var letters = 'ijkmnopqrstuvwxyzabcdefgh';
- var letterIndex = 0;
- var potName = letters.charAt(letterIndex);
- while (!newName) {
- var inUse = false;
- for (var i = 0; i < variableList.length; i++) {
- if (variableList[i].toLowerCase() == potName) {
-
- inUse = true;
- break;
- }
- }
- if (inUse) {
-
- letterIndex++;
- if (letterIndex == letters.length) {
-
-
- letterIndex = 0;
- nameSuffix++;
- }
- potName = letters.charAt(letterIndex);
- if (nameSuffix > 1) {
- potName += nameSuffix;
- }
- } else {
-
- newName = potName;
- }
- }
- } else {
- newName = 'i';
- }
- return newName;
- };
- 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);
- });
- } else {
- workspace.createVariable(text);
- if (opt_callback) {
- opt_callback(text);
- }
- }
- } else {
-
- if (opt_callback) {
- opt_callback(null);
- }
- }
- });
- };
- promptAndCheckWithAlert('');
- };
- Blockly.Variables.promptName = function(promptText, defaultText, callback) {
- Blockly.prompt(promptText, defaultText, function(newVar) {
-
-
- if (newVar) {
- newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
- if (newVar == Blockly.Msg.RENAME_VARIABLE ||
- newVar == Blockly.Msg.NEW_VARIABLE) {
-
- newVar = null;
- }
- }
- callback(newVar);
- });
- };
|