field_image.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Image field. Used for titles, labels, etc.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldImage');
  26. goog.require('Blockly.Field');
  27. goog.require('goog.dom');
  28. goog.require('goog.math.Size');
  29. goog.require('goog.userAgent');
  30. /**
  31. * Class for an image.
  32. * @param {string} src The URL of the image.
  33. * @param {number} width Width of the image.
  34. * @param {number} height Height of the image.
  35. * @param {string=} opt_alt Optional alt text for when block is collapsed.
  36. * @extends {Blockly.Field}
  37. * @constructor
  38. */
  39. Blockly.FieldImage = function(src, width, height, opt_alt) {
  40. this.sourceBlock_ = null;
  41. // Ensure height and width are numbers. Strings are bad at math.
  42. this.height_ = Number(height);
  43. this.width_ = Number(width);
  44. this.size_ = new goog.math.Size(this.width_,
  45. this.height_ + 1 * Blockly.BlockSvg.INLINE_PADDING_Y);
  46. this.text_ = opt_alt || '';
  47. this.setSrc(src);
  48. };
  49. goog.inherits(Blockly.FieldImage, Blockly.Field);
  50. /**
  51. * Rectangular mask used by Firefox.
  52. * @type {Element}
  53. * @private
  54. */
  55. Blockly.FieldImage.prototype.rectElement_ = null;
  56. /**
  57. * Editable fields are saved by the XML renderer, non-editable fields are not.
  58. */
  59. Blockly.FieldImage.prototype.EDITABLE = false;
  60. /**
  61. * Install this image on a block.
  62. */
  63. Blockly.FieldImage.prototype.init = function() {
  64. if (this.fieldGroup_) {
  65. // Image has already been initialized once.
  66. return;
  67. }
  68. // Build the DOM.
  69. /** @type {SVGElement} */
  70. this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
  71. if (!this.visible_) {
  72. this.fieldGroup_.style.display = 'none';
  73. }
  74. /** @type {SVGElement} */
  75. this.imageElement_ = Blockly.createSvgElement('image',
  76. {'height': this.height_ + 'px',
  77. 'width': this.width_ + 'px'}, this.fieldGroup_);
  78. this.setSrc(this.src_);
  79. if (goog.userAgent.GECKO) {
  80. /**
  81. * Due to a Firefox bug which eats mouse events on image elements,
  82. * a transparent rectangle needs to be placed on top of the image.
  83. * @type {SVGElement}
  84. */
  85. this.rectElement_ = Blockly.createSvgElement('rect',
  86. {'height': this.height_ + 'px',
  87. 'width': this.width_ + 'px',
  88. 'fill-opacity': 0}, this.fieldGroup_);
  89. }
  90. this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
  91. // Configure the field to be transparent with respect to tooltips.
  92. var topElement = this.rectElement_ || this.imageElement_;
  93. topElement.tooltip = this.sourceBlock_;
  94. Blockly.Tooltip.bindMouseEvents(topElement);
  95. };
  96. /**
  97. * Dispose of all DOM objects belonging to this text.
  98. */
  99. Blockly.FieldImage.prototype.dispose = function() {
  100. goog.dom.removeNode(this.fieldGroup_);
  101. this.fieldGroup_ = null;
  102. this.imageElement_ = null;
  103. this.rectElement_ = null;
  104. };
  105. /**
  106. * Change the tooltip text for this field.
  107. * @param {string|!Element} newTip Text for tooltip or a parent element to
  108. * link to for its tooltip.
  109. */
  110. Blockly.FieldImage.prototype.setTooltip = function(newTip) {
  111. var topElement = this.rectElement_ || this.imageElement_;
  112. topElement.tooltip = newTip;
  113. };
  114. /**
  115. * Get the source URL of this image.
  116. * @return {string} Current text.
  117. * @override
  118. */
  119. Blockly.FieldImage.prototype.getSrc = function() {
  120. return this.src_;
  121. };
  122. /**
  123. * Set the source URL of this image.
  124. * @param {?string} src New source.
  125. * @override
  126. */
  127. Blockly.FieldImage.prototype.setSrc = function(src) {
  128. if (src === null) {
  129. // No change if null.
  130. return;
  131. }
  132. this.src_ = src;
  133. if (this.imageElement_) {
  134. this.imageElement_.setAttributeNS('http://www.w3.org/1999/xlink',
  135. 'xlink:href', goog.isString(src) ? src : '');
  136. }
  137. };
  138. /**
  139. * Set the alt text of this image.
  140. * @param {?string} alt New alt text.
  141. * @override
  142. */
  143. Blockly.FieldImage.prototype.setText = function(alt) {
  144. if (alt === null) {
  145. // No change if null.
  146. return;
  147. }
  148. this.text_ = alt;
  149. };
  150. /**
  151. * Images are fixed width, no need to render.
  152. * @private
  153. */
  154. Blockly.FieldImage.prototype.render_ = function() {
  155. // NOP
  156. };