123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 'use strict';
- goog.provide('Blockly.FieldVariable');
- goog.require('Blockly.FieldDropdown');
- goog.require('Blockly.Msg');
- goog.require('Blockly.Variables');
- goog.require('goog.asserts');
- goog.require('goog.string');
- Blockly.FieldVariable = function(varname, opt_validator) {
- Blockly.FieldVariable.superClass_.constructor.call(this,
- Blockly.FieldVariable.dropdownCreate, opt_validator);
- this.setValue(varname || '');
- };
- goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);
- Blockly.FieldVariable.prototype.newVarItemIndex_ = -1;
- Blockly.FieldVariable.prototype.renameVarItemIndex_ = -1;
- Blockly.FieldVariable.prototype.deleteVarItemIndex_ = -1;
- Blockly.FieldVariable.prototype.init = function() {
- if (this.fieldGroup_) {
-
- return;
- }
- Blockly.FieldVariable.superClass_.init.call(this);
- if (!this.getValue()) {
-
- var workspace =
- this.sourceBlock_.isInFlyout ?
- this.sourceBlock_.workspace.targetWorkspace :
- this.sourceBlock_.workspace;
- this.setValue(Blockly.Variables.generateUniqueName(workspace));
- }
-
-
-
- if (!this.sourceBlock_.isInFlyout) {
- this.sourceBlock_.workspace.createVariable(this.getValue());
- }
- };
- Blockly.FieldVariable.prototype.setSourceBlock = function(block) {
- goog.asserts.assert(!block.isShadow(),
- 'Variable fields are not allowed to exist on shadow blocks.');
- Blockly.FieldVariable.superClass_.setSourceBlock.call(this, block);
- };
- Blockly.FieldVariable.prototype.getValue = function() {
- return this.getText();
- };
- Blockly.FieldVariable.prototype.setValue = function(newValue) {
- if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
- Blockly.Events.fire(new Blockly.Events.Change(
- this.sourceBlock_, 'field', this.name, this.value_, newValue));
- }
- this.value_ = newValue;
- this.setText(newValue);
- };
- Blockly.FieldVariable.dropdownCreate = function() {
- if (this.sourceBlock_ && this.sourceBlock_.workspace) {
-
-
- var variableList = this.sourceBlock_.workspace.variableList.slice(0);
- } else {
- var variableList = [];
- }
-
- var name = this.getText();
- if (name && variableList.indexOf(name) == -1) {
- variableList.push(name);
- }
- variableList.sort(goog.string.caseInsensitiveCompare);
-
- this.newVarItemIndex_ = variableList.length;
- variableList.push(Blockly.Msg.NEW_VARIABLE);
- this.renameVarItemIndex_ = variableList.length;
- variableList.push(Blockly.Msg.RENAME_VARIABLE);
- this.deleteVarItemIndex_ = variableList.length;
- variableList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name));
-
-
- var options = [];
- for (var i = 0; i < variableList.length; i++) {
- options[i] = [variableList[i], variableList[i]];
- }
- return options;
- };
- Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
- var menuLength = menu.getChildCount();
- var itemText = menuItem.getValue();
- if (this.sourceBlock_) {
- var workspace = this.sourceBlock_.workspace;
- if (this.renameVarItemIndex_ >= 0 &&
- menu.getChildAt(this.renameVarItemIndex_) === menuItem) {
-
- var oldName = this.getText();
- Blockly.hideChaff();
- Blockly.Variables.promptName(
- Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldName), oldName,
- function(newName) {
- if (newName) {
- workspace.renameVariable(oldName, newName);
- }
- });
- return;
- } else if (this.newVarItemIndex_ >= 0 &&
- menu.getChildAt(this.newVarItemIndex_) == menuItem) {
- var that = this;
- Blockly.Variables.createVariable(workspace, function(new_name) {
- if (new_name !== null && new_name !== undefined) {
- that.setValue(new_name);
- }
- });
- return;
- } else if (this.deleteVarItemIndex_ >= 0 &&
- menu.getChildAt(this.deleteVarItemIndex_) === menuItem) {
-
- workspace.deleteVariable(this.getText());
- return;
- }
-
- itemText = this.callValidator(itemText);
- }
- if (itemText !== null) {
- this.setValue(itemText);
- }
- };
|