icon.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2013 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 Object representing an icon on a block.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Icon');
  26. goog.require('goog.dom');
  27. goog.require('goog.math.Coordinate');
  28. /**
  29. * Class for an icon.
  30. * @param {Blockly.Block} block The block associated with this icon.
  31. * @constructor
  32. */
  33. Blockly.Icon = function(block) {
  34. this.block_ = block;
  35. };
  36. /**
  37. * Does this icon get hidden when the block is collapsed.
  38. */
  39. Blockly.Icon.prototype.collapseHidden = true;
  40. /**
  41. * Height and width of icons.
  42. */
  43. Blockly.Icon.prototype.SIZE = 17;
  44. /**
  45. * Bubble UI (if visible).
  46. * @type {Blockly.Bubble}
  47. * @private
  48. */
  49. Blockly.Icon.prototype.bubble_ = null;
  50. /**
  51. * Absolute coordinate of icon's center.
  52. * @type {goog.math.Coordinate}
  53. * @private
  54. */
  55. Blockly.Icon.prototype.iconXY_ = null;
  56. /**
  57. * Create the icon on the block.
  58. */
  59. Blockly.Icon.prototype.createIcon = function() {
  60. if (this.iconGroup_) {
  61. // Icon already exists.
  62. return;
  63. }
  64. /* Here's the markup that will be generated:
  65. <g class="blocklyIconGroup">
  66. ...
  67. </g>
  68. */
  69. this.iconGroup_ = Blockly.createSvgElement('g',
  70. {'class': 'blocklyIconGroup'}, null);
  71. if (this.block_.isInFlyout) {
  72. Blockly.addClass_(/** @type {!Element} */ (this.iconGroup_),
  73. 'blocklyIconGroupReadonly');
  74. }
  75. this.drawIcon_(this.iconGroup_);
  76. this.block_.getSvgRoot().appendChild(this.iconGroup_);
  77. Blockly.bindEventWithChecks_(this.iconGroup_, 'mouseup', this,
  78. this.iconClick_);
  79. this.updateEditable();
  80. };
  81. /**
  82. * Dispose of this icon.
  83. */
  84. Blockly.Icon.prototype.dispose = function() {
  85. // Dispose of and unlink the icon.
  86. goog.dom.removeNode(this.iconGroup_);
  87. this.iconGroup_ = null;
  88. // Dispose of and unlink the bubble.
  89. this.setVisible(false);
  90. this.block_ = null;
  91. };
  92. /**
  93. * Add or remove the UI indicating if this icon may be clicked or not.
  94. */
  95. Blockly.Icon.prototype.updateEditable = function() {
  96. };
  97. /**
  98. * Is the associated bubble visible?
  99. * @return {boolean} True if the bubble is visible.
  100. */
  101. Blockly.Icon.prototype.isVisible = function() {
  102. return !!this.bubble_;
  103. };
  104. /**
  105. * Clicking on the icon toggles if the bubble is visible.
  106. * @param {!Event} e Mouse click event.
  107. * @private
  108. */
  109. Blockly.Icon.prototype.iconClick_ = function(e) {
  110. if (this.block_.workspace.isDragging()) {
  111. // Drag operation is concluding. Don't open the editor.
  112. return;
  113. }
  114. if (!this.block_.isInFlyout && !Blockly.isRightButton(e)) {
  115. this.setVisible(!this.isVisible());
  116. }
  117. };
  118. /**
  119. * Change the colour of the associated bubble to match its block.
  120. */
  121. Blockly.Icon.prototype.updateColour = function() {
  122. if (this.isVisible()) {
  123. this.bubble_.setColour(this.block_.getColour());
  124. }
  125. };
  126. /**
  127. * Render the icon.
  128. * @param {number} cursorX Horizontal offset at which to position the icon.
  129. * @return {number} Horizontal offset for next item to draw.
  130. */
  131. Blockly.Icon.prototype.renderIcon = function(cursorX) {
  132. if (this.collapseHidden && this.block_.isCollapsed()) {
  133. this.iconGroup_.setAttribute('display', 'none');
  134. return cursorX;
  135. }
  136. this.iconGroup_.setAttribute('display', 'block');
  137. var TOP_MARGIN = 5;
  138. var width = this.SIZE;
  139. if (this.block_.RTL) {
  140. cursorX -= width;
  141. }
  142. this.iconGroup_.setAttribute('transform',
  143. 'translate(' + cursorX + ',' + TOP_MARGIN + ')');
  144. this.computeIconLocation();
  145. if (this.block_.RTL) {
  146. cursorX -= Blockly.BlockSvg.SEP_SPACE_X;
  147. } else {
  148. cursorX += width + Blockly.BlockSvg.SEP_SPACE_X;
  149. }
  150. return cursorX;
  151. };
  152. /**
  153. * Notification that the icon has moved. Update the arrow accordingly.
  154. * @param {!goog.math.Coordinate} xy Absolute location.
  155. */
  156. Blockly.Icon.prototype.setIconLocation = function(xy) {
  157. this.iconXY_ = xy;
  158. if (this.isVisible()) {
  159. this.bubble_.setAnchorLocation(xy);
  160. }
  161. };
  162. /**
  163. * Notification that the icon has moved, but we don't really know where.
  164. * Recompute the icon's location from scratch.
  165. */
  166. Blockly.Icon.prototype.computeIconLocation = function() {
  167. // Find coordinates for the centre of the icon and update the arrow.
  168. var blockXY = this.block_.getRelativeToSurfaceXY();
  169. var iconXY = Blockly.getRelativeXY_(this.iconGroup_);
  170. var newXY = new goog.math.Coordinate(
  171. blockXY.x + iconXY.x + this.SIZE / 2,
  172. blockXY.y + iconXY.y + this.SIZE / 2);
  173. if (!goog.math.Coordinate.equals(this.getIconLocation(), newXY)) {
  174. this.setIconLocation(newXY);
  175. }
  176. };
  177. /**
  178. * Returns the center of the block's icon relative to the surface.
  179. * @return {!goog.math.Coordinate} Object with x and y properties.
  180. */
  181. Blockly.Icon.prototype.getIconLocation = function() {
  182. return this.iconXY_;
  183. };