scratch-blocks/core/field_variable_getter.js

104 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-02-22 13:49:39 -08:00
/**
* @license
* Visual Blocks Editor
*
* Copyright 2017 Google Inc.
* https://developers.google.com/blockly/
*
* 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.
*/
/**
* @fileoverview Variable getter field. Appears as a label but has a variable
* picker in the right-click menu.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';
goog.provide('Blockly.FieldVariableGetter');
goog.require('Blockly.Field');
/**
* Class for a variable getter field.
* @param {string} text The initial content of the field.
* @param {string} name Optional CSS class for the field's text.
* @extends {Blockly.FieldLabel}
* @constructor
*
*/
Blockly.FieldVariableGetter = function(text, name) {
Blockly.FieldVariableGetter.superClass_.constructor.call(this, text);
this.name_ = name;
};
goog.inherits(Blockly.FieldVariableGetter, Blockly.Field);
/**
* Editable fields usually show some sort of UI for the user to change them.
* This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
2017-02-22 13:49:39 -08:00
*/
Blockly.FieldVariableGetter.prototype.EDITABLE = false;
/**
* Serializable fields are saved by the XML renderer, non-serializable fields
* are not. This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
*/
Blockly.FieldVariableGetter.prototype.SERIALIZABLE = true;
2017-02-22 13:49:39 -08:00
/**
* Install this field on a block.
*/
Blockly.FieldVariableGetter.prototype.init = function() {
if (this.fieldGroup_) {
// Field has already been initialized once.
return;
}
Blockly.FieldVariableGetter.superClass_.init.call(this);
if (!this.getValue()) {
// Variables without names get uniquely named for this workspace.
var workspace =
this.sourceBlock_.isInFlyout ?
this.sourceBlock_.workspace.targetWorkspace :
this.sourceBlock_.workspace;
this.setValue(Blockly.Variables.generateUniqueName(workspace));
}
// If the selected variable doesn't exist yet, create it.
// For instance, some blocks in the toolbox have variable dropdowns filled
// in by default.
if (!this.sourceBlock_.isInFlyout) {
this.sourceBlock_.workspace.createVariable(this.getValue());
}
};
/**
* This field is editable, but only through the right-click menu.
* @private
*/
Blockly.FieldVariableGetter.prototype.showEditor_ = function() {
// nop.
};
/**
* Add or remove the UI indicating if this field is editable or not.
* This field is editable, but only through the right-click menu.
* Suppress default editable behaviour.
*/
Blockly.FieldVariableGetter.prototype.updateEditable = function() {
// nop.
};