|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
goog.provide('Blockly.FieldVariable');
|
|
|
|
|
|
goog.require('Blockly.FieldDropdown');
|
|
|
goog.require('Blockly.Msg');
|
|
|
goog.require('Blockly.VariableModel');
|
|
|
goog.require('Blockly.Variables');
|
|
|
goog.require('goog.asserts');
|
|
|
goog.require('goog.string');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable = function(varname, opt_validator, opt_variableTypes) {
|
|
|
|
|
|
|
|
|
this.menuGenerator_ = Blockly.FieldVariable.dropdownCreate;
|
|
|
this.size_ = new goog.math.Size(Blockly.BlockSvg.FIELD_WIDTH,
|
|
|
Blockly.BlockSvg.FIELD_HEIGHT);
|
|
|
this.setValidator(opt_validator);
|
|
|
|
|
|
|
|
|
this.defaultVariableName = (varname || '');
|
|
|
var hasSingleVarType = opt_variableTypes && (opt_variableTypes.length == 1);
|
|
|
this.defaultType_ = hasSingleVarType ? opt_variableTypes[0] : '';
|
|
|
this.variableTypes = opt_variableTypes;
|
|
|
this.addArgType('variable');
|
|
|
|
|
|
this.value_ = null;
|
|
|
};
|
|
|
goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.fromJson = function(options) {
|
|
|
var varname = Blockly.utils.replaceMessageReferences(options['variable']);
|
|
|
var variableTypes = options['variableTypes'];
|
|
|
return new Blockly.FieldVariable(varname, null, variableTypes);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.init = function() {
|
|
|
if (this.fieldGroup_) {
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
Blockly.FieldVariable.superClass_.init.call(this);
|
|
|
|
|
|
|
|
|
this.initModel();
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.initModel = function() {
|
|
|
if (this.variable_) {
|
|
|
return;
|
|
|
}
|
|
|
this.workspace_ = this.sourceBlock_.workspace;
|
|
|
|
|
|
var variable = Blockly.Variables.getVariable(this.workspace_, this.defaultVariableName, null, this.defaultType_);
|
|
|
var vars = this.workspace_.getVariablesOfType(this.defaultType_);
|
|
|
if (this.workspace_.isFlyout && !variable && vars.length > 0) {
|
|
|
vars.sort(Blockly.VariableModel.compareByName);
|
|
|
variable = vars[0];
|
|
|
}
|
|
|
if (vars.length < 1) {
|
|
|
variable = Blockly.Variables.getOrCreateVariablePackage(
|
|
|
this.workspace_, null, this.defaultVariableName, this.defaultType_);
|
|
|
}
|
|
|
|
|
|
|
|
|
Blockly.Events.disable();
|
|
|
try {
|
|
|
this.setValue(variable.getId());
|
|
|
} finally {
|
|
|
Blockly.Events.enable();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.dispose = function() {
|
|
|
Blockly.FieldVariable.superClass_.dispose.call(this);
|
|
|
this.workspace_ = null;
|
|
|
this.variableMap_ = null;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.variable_ ? this.variable_.getId() : null;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.getText = function() {
|
|
|
return this.variable_ ? this.variable_.name : '';
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.getVariable = function() {
|
|
|
return this.variable_;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.setValue = function(id) {
|
|
|
var workspace = this.sourceBlock_.workspace;
|
|
|
var variable = Blockly.Variables.getVariable(workspace, id);
|
|
|
|
|
|
if (!variable) {
|
|
|
throw new Error('Variable id doesn\'t point to a real variable! ID was ' +
|
|
|
id);
|
|
|
}
|
|
|
|
|
|
var type = variable.type;
|
|
|
if (!this.typeIsAllowed_(type)) {
|
|
|
throw new Error('Variable type doesn\'t match this field! Type was ' +
|
|
|
type);
|
|
|
}
|
|
|
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
|
|
var oldValue = this.variable_ ? this.variable_.getId() : null;
|
|
|
Blockly.Events.fire(new Blockly.Events.BlockChange(
|
|
|
this.sourceBlock_, 'field', this.name, oldValue, id));
|
|
|
}
|
|
|
this.variable_ = variable;
|
|
|
this.value_ = id;
|
|
|
this.setText(variable.name);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.typeIsAllowed_ = function(type) {
|
|
|
var typeList = this.getVariableTypes_();
|
|
|
if (!typeList) {
|
|
|
return true;
|
|
|
}
|
|
|
for (var i = 0; i < typeList.length; i++) {
|
|
|
if (type == typeList[i]) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.getVariableTypes_ = function() {
|
|
|
|
|
|
var variableTypes = this.variableTypes;
|
|
|
if (variableTypes === null) {
|
|
|
|
|
|
if (this.sourceBlock_) {
|
|
|
var workspace = this.sourceBlock_.workspace;
|
|
|
return workspace.getVariableTypes();
|
|
|
}
|
|
|
}
|
|
|
variableTypes = variableTypes || [''];
|
|
|
if (variableTypes.length == 0) {
|
|
|
|
|
|
var name = this.getText();
|
|
|
throw new Error('\'variableTypes\' of field variable ' +
|
|
|
name + ' was an empty list');
|
|
|
}
|
|
|
return variableTypes;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.dropdownCreate = function() {
|
|
|
if (!this.variable_) {
|
|
|
throw new Error('Tried to call dropdownCreate on a variable field with no' +
|
|
|
' variable selected.');
|
|
|
}
|
|
|
var variableModelList = [];
|
|
|
var name = this.getText();
|
|
|
var workspace = null;
|
|
|
if (this.sourceBlock_) {
|
|
|
workspace = this.sourceBlock_.workspace;
|
|
|
}
|
|
|
if (workspace) {
|
|
|
var variableTypes = this.getVariableTypes_();
|
|
|
var variableModelList = [];
|
|
|
|
|
|
|
|
|
for (var i = 0; i < variableTypes.length; i++) {
|
|
|
var variableType = variableTypes[i];
|
|
|
var variables = workspace.getVariablesOfType(variableType);
|
|
|
variableModelList = variableModelList.concat(variables);
|
|
|
|
|
|
var potentialVarMap = workspace.getPotentialVariableMap();
|
|
|
if (potentialVarMap) {
|
|
|
var potentialVars = potentialVarMap.getVariablesOfType(variableType);
|
|
|
variableModelList = variableModelList.concat(potentialVars);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
variableModelList.sort(Blockly.VariableModel.compareByName);
|
|
|
|
|
|
var options = [];
|
|
|
for (var i = 0; i < variableModelList.length; i++) {
|
|
|
|
|
|
options[i] = [variableModelList[i].name, variableModelList[i].getId()];
|
|
|
}
|
|
|
if (this.defaultType_ == Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE) {
|
|
|
options.unshift(
|
|
|
[Blockly.Msg.NEW_BROADCAST_MESSAGE, Blockly.NEW_BROADCAST_MESSAGE_ID]);
|
|
|
} else {
|
|
|
|
|
|
|
|
|
if (this.defaultType_ == Blockly.LIST_VARIABLE_TYPE) {
|
|
|
var renameText = Blockly.Msg.RENAME_LIST;
|
|
|
var deleteText = Blockly.Msg.DELETE_LIST;
|
|
|
} else {
|
|
|
var renameText = Blockly.Msg.RENAME_VARIABLE;
|
|
|
var deleteText = Blockly.Msg.DELETE_VARIABLE;
|
|
|
}
|
|
|
options.push([renameText, Blockly.RENAME_VARIABLE_ID]);
|
|
|
if (deleteText) {
|
|
|
options.push(
|
|
|
[
|
|
|
deleteText.replace('%1', name),
|
|
|
Blockly.DELETE_VARIABLE_ID
|
|
|
]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return options;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
|
|
|
var id = menuItem.getValue();
|
|
|
if (this.sourceBlock_ && this.sourceBlock_.workspace) {
|
|
|
var workspace = this.sourceBlock_.workspace;
|
|
|
if (id == Blockly.RENAME_VARIABLE_ID) {
|
|
|
|
|
|
Blockly.Variables.renameVariable(workspace, this.variable_);
|
|
|
return;
|
|
|
} else if (id == Blockly.DELETE_VARIABLE_ID) {
|
|
|
|
|
|
workspace.deleteVariableById(this.variable_.getId());
|
|
|
return;
|
|
|
} else if (id == Blockly.NEW_BROADCAST_MESSAGE_ID) {
|
|
|
var thisField = this;
|
|
|
var updateField = function(varId) {
|
|
|
if (varId) {
|
|
|
thisField.setValue(varId);
|
|
|
}
|
|
|
};
|
|
|
Blockly.Variables.createVariable(workspace, updateField,
|
|
|
Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
this.setValue(id);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldVariable.prototype.referencesVariables = function() {
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
Blockly.Field.register('field_variable', Blockly.FieldVariable);
|
|
|
|