field_label.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Non-editable text field. Used for titles, labels, etc.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldLabel');
  26. goog.require('Blockly.Field');
  27. goog.require('Blockly.Tooltip');
  28. goog.require('goog.dom');
  29. goog.require('goog.math.Size');
  30. /**
  31. * Class for a non-editable field.
  32. * @param {string} text The initial content of the field.
  33. * @param {string=} opt_class Optional CSS class for the field's text.
  34. * @extends {Blockly.Field}
  35. * @constructor
  36. */
  37. Blockly.FieldLabel = function(text, opt_class) {
  38. this.size_ = new goog.math.Size(0, 17.5);
  39. this.class_ = opt_class;
  40. this.setValue(text);
  41. };
  42. goog.inherits(Blockly.FieldLabel, Blockly.Field);
  43. /**
  44. * Editable fields are saved by the XML renderer, non-editable fields are not.
  45. */
  46. Blockly.FieldLabel.prototype.EDITABLE = false;
  47. /**
  48. * Install this text on a block.
  49. */
  50. Blockly.FieldLabel.prototype.init = function() {
  51. if (this.textElement_) {
  52. // Text has already been initialized once.
  53. return;
  54. }
  55. // Build the DOM.
  56. this.textElement_ = Blockly.createSvgElement('text',
  57. {'class': 'blocklyText', 'y': this.size_.height - 5}, null);
  58. if (this.class_) {
  59. Blockly.addClass_(this.textElement_, this.class_);
  60. }
  61. if (!this.visible_) {
  62. this.textElement_.style.display = 'none';
  63. }
  64. this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
  65. // Configure the field to be transparent with respect to tooltips.
  66. this.textElement_.tooltip = this.sourceBlock_;
  67. Blockly.Tooltip.bindMouseEvents(this.textElement_);
  68. // Force a render.
  69. this.updateTextNode_();
  70. };
  71. /**
  72. * Dispose of all DOM objects belonging to this text.
  73. */
  74. Blockly.FieldLabel.prototype.dispose = function() {
  75. goog.dom.removeNode(this.textElement_);
  76. this.textElement_ = null;
  77. };
  78. /**
  79. * Gets the group element for this field.
  80. * Used for measuring the size and for positioning.
  81. * @return {!Element} The group element.
  82. */
  83. Blockly.FieldLabel.prototype.getSvgRoot = function() {
  84. return /** @type {!Element} */ (this.textElement_);
  85. };
  86. /**
  87. * Change the tooltip text for this field.
  88. * @param {string|!Element} newTip Text for tooltip or a parent element to
  89. * link to for its tooltip.
  90. */
  91. Blockly.FieldLabel.prototype.setTooltip = function(newTip) {
  92. this.textElement_.tooltip = newTip;
  93. };