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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Non-editable text field. Used for titles, labels, etc.
|
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.FieldLabel');
|
|
|
|
|
|
|
|
goog.require('Blockly.Field');
|
|
|
|
goog.require('Blockly.Tooltip');
|
2015-02-06 15:27:25 -08:00
|
|
|
goog.require('goog.dom');
|
2014-12-24 00:22:01 -08:00
|
|
|
goog.require('goog.math.Size');
|
2018-01-10 15:25:12 -05:00
|
|
|
goog.require('goog.userAgent');
|
2013-10-30 14:46:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for a non-editable field.
|
|
|
|
* @param {string} text The initial content of the field.
|
2015-08-26 14:27:30 +01:00
|
|
|
* @param {string=} opt_class Optional CSS class for the field's text.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @extends {Blockly.Field}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-08-26 14:27:30 +01:00
|
|
|
Blockly.FieldLabel = function(text, opt_class) {
|
2016-07-26 11:52:05 -04:00
|
|
|
this.size_ = new goog.math.Size(0, 0);
|
2015-08-26 14:27:30 +01:00
|
|
|
this.class_ = opt_class;
|
2016-01-20 19:11:03 -08:00
|
|
|
this.setValue(text);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
goog.inherits(Blockly.FieldLabel, Blockly.Field);
|
|
|
|
|
2018-04-17 14:15:18 -07:00
|
|
|
/**
|
|
|
|
* Construct a FieldLabel from a JSON arg object,
|
|
|
|
* dereferencing any string table references.
|
|
|
|
* @param {!Object} options A JSON object with options (text, and class).
|
|
|
|
* @returns {!Blockly.FieldLabel} The new field instance.
|
|
|
|
* @package
|
|
|
|
* @nocollapse
|
|
|
|
*/
|
|
|
|
Blockly.FieldLabel.fromJson = function(options) {
|
|
|
|
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
|
|
|
return new Blockly.FieldLabel(text, options['class']);
|
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2017-10-20 15:15:00 -07:00
|
|
|
* Editable fields usually show some sort of UI for the user to change them.
|
|
|
|
* @type {boolean}
|
|
|
|
* @public
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.FieldLabel.prototype.EDITABLE = false;
|
|
|
|
|
2017-10-20 15:15:00 -07:00
|
|
|
/**
|
|
|
|
* Serializable fields are saved by the XML renderer, non-serializable fields
|
|
|
|
* are not. Editable fields should be serialized.
|
|
|
|
* @type {boolean}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
Blockly.FieldLabel.prototype.SERIALIZABLE = false;
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Install this text on a block.
|
|
|
|
*/
|
2016-04-04 17:47:15 -07:00
|
|
|
Blockly.FieldLabel.prototype.init = function() {
|
|
|
|
if (this.textElement_) {
|
2014-12-23 11:22:02 -08:00
|
|
|
// Text has already been initialized once.
|
|
|
|
return;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2014-12-24 00:22:01 -08:00
|
|
|
// Build the DOM.
|
2017-02-02 14:17:43 -05:00
|
|
|
this.textElement_ = Blockly.utils.createSvgElement('text',
|
2018-05-02 15:32:28 -07:00
|
|
|
{
|
|
|
|
'class': 'blocklyText',
|
|
|
|
'y': Blockly.BlockSvg.FIELD_TOP_PADDING,
|
|
|
|
'text-anchor': 'middle',
|
|
|
|
'dominant-baseline': 'middle',
|
|
|
|
'dy': goog.userAgent.EDGE_OR_IE ? Blockly.Field.IE_TEXT_OFFSET : '0'
|
|
|
|
}, null);
|
2015-08-26 14:27:30 +01:00
|
|
|
if (this.class_) {
|
2017-02-02 14:17:43 -05:00
|
|
|
Blockly.utils.addClass(this.textElement_, this.class_);
|
2015-08-26 14:27:30 +01:00
|
|
|
}
|
2015-01-05 12:04:01 -08:00
|
|
|
if (!this.visible_) {
|
|
|
|
this.textElement_.style.display = 'none';
|
|
|
|
}
|
2016-04-04 17:47:15 -07:00
|
|
|
this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
|
2013-10-30 14:46:03 -07:00
|
|
|
|
|
|
|
// Configure the field to be transparent with respect to tooltips.
|
|
|
|
this.textElement_.tooltip = this.sourceBlock_;
|
2014-09-08 14:26:52 -07:00
|
|
|
Blockly.Tooltip.bindMouseEvents(this.textElement_);
|
2014-12-24 00:22:01 -08:00
|
|
|
// Force a render.
|
2017-02-02 14:17:43 -05:00
|
|
|
this.render_();
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose of all DOM objects belonging to this text.
|
|
|
|
*/
|
|
|
|
Blockly.FieldLabel.prototype.dispose = function() {
|
|
|
|
goog.dom.removeNode(this.textElement_);
|
|
|
|
this.textElement_ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the group element for this field.
|
|
|
|
* Used for measuring the size and for positioning.
|
|
|
|
* @return {!Element} The group element.
|
|
|
|
*/
|
2014-12-23 11:22:02 -08:00
|
|
|
Blockly.FieldLabel.prototype.getSvgRoot = function() {
|
2013-10-30 14:46:03 -07:00
|
|
|
return /** @type {!Element} */ (this.textElement_);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the tooltip text for this field.
|
|
|
|
* @param {string|!Element} newTip Text for tooltip or a parent element to
|
|
|
|
* link to for its tooltip.
|
|
|
|
*/
|
|
|
|
Blockly.FieldLabel.prototype.setTooltip = function(newTip) {
|
|
|
|
this.textElement_.tooltip = newTip;
|
|
|
|
};
|
2018-04-17 14:15:18 -07:00
|
|
|
|
|
|
|
Blockly.Field.register('field_label', Blockly.FieldLabel);
|