icon.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.bindEvent_(this.iconGroup_, 'mouseup', this, this.iconClick_);
  78. this.updateEditable();
  79. };
  80. /**
  81. * Dispose of this icon.
  82. */
  83. Blockly.Icon.prototype.dispose = function() {
  84. // Dispose of and unlink the icon.
  85. goog.dom.removeNode(this.iconGroup_);
  86. this.iconGroup_ = null;
  87. // Dispose of and unlink the bubble.
  88. this.setVisible(false);
  89. this.block_ = null;
  90. };
  91. /**
  92. * Add or remove the UI indicating if this icon may be clicked or not.
  93. */
  94. Blockly.Icon.prototype.updateEditable = function() {
  95. };
  96. /**
  97. * Is the associated bubble visible?
  98. * @return {boolean} True if the bubble is visible.
  99. */
  100. Blockly.Icon.prototype.isVisible = function() {
  101. return !!this.bubble_;
  102. };
  103. /**
  104. * Clicking on the icon toggles if the bubble is visible.
  105. * @param {!Event} e Mouse click event.
  106. * @private
  107. */
  108. Blockly.Icon.prototype.iconClick_ = function(e) {
  109. if (this.block_.workspace.isDragging()) {
  110. // Drag operation is concluding. Don't open the editor.
  111. return;
  112. }
  113. if (!this.block_.isInFlyout && !Blockly.isRightButton(e)) {
  114. this.setVisible(!this.isVisible());
  115. }
  116. };
  117. /**
  118. * Change the colour of the associated bubble to match its block.
  119. */
  120. Blockly.Icon.prototype.updateColour = function() {
  121. if (this.isVisible()) {
  122. this.bubble_.setColour(this.block_.getColour());
  123. }
  124. };
  125. /**
  126. * Render the icon.
  127. * @param {number} cursorX Horizontal offset at which to position the icon.
  128. * @return {number} Horizontal offset for next item to draw.
  129. */
  130. Blockly.Icon.prototype.renderIcon = function(cursorX) {
  131. if (this.collapseHidden && this.block_.isCollapsed()) {
  132. this.iconGroup_.setAttribute('display', 'none');
  133. return cursorX;
  134. }
  135. this.iconGroup_.setAttribute('display', 'block');
  136. var TOP_MARGIN = 5;
  137. var width = this.SIZE;
  138. if (this.block_.RTL) {
  139. cursorX -= width;
  140. }
  141. this.iconGroup_.setAttribute('transform',
  142. 'translate(' + cursorX + ',' + TOP_MARGIN + ')');
  143. this.computeIconLocation();
  144. if (this.block_.RTL) {
  145. cursorX -= Blockly.BlockSvg.SEP_SPACE_X;
  146. } else {
  147. cursorX += width + Blockly.BlockSvg.SEP_SPACE_X;
  148. }
  149. return cursorX;
  150. };
  151. /**
  152. * Notification that the icon has moved. Update the arrow accordingly.
  153. * @param {!goog.math.Coordinate} xy Absolute location.
  154. */
  155. Blockly.Icon.prototype.setIconLocation = function(xy) {
  156. this.iconXY_ = xy;
  157. if (this.isVisible()) {
  158. this.bubble_.setAnchorLocation(xy);
  159. }
  160. };
  161. /**
  162. * Notification that the icon has moved, but we don't really know where.
  163. * Recompute the icon's location from scratch.
  164. */
  165. Blockly.Icon.prototype.computeIconLocation = function() {
  166. // Find coordinates for the centre of the icon and update the arrow.
  167. var blockXY = this.block_.getRelativeToSurfaceXY();
  168. var iconXY = Blockly.getRelativeXY_(this.iconGroup_);
  169. var newXY = new goog.math.Coordinate(
  170. blockXY.x + iconXY.x + this.SIZE / 2,
  171. blockXY.y + iconXY.y + this.SIZE / 2);
  172. if (!goog.math.Coordinate.equals(this.getIconLocation(), newXY)) {
  173. this.setIconLocation(newXY);
  174. }
  175. };
  176. /**
  177. * Returns the center of the block's icon relative to the surface.
  178. * @return {!goog.math.Coordinate} Object with x and y properties.
  179. */
  180. Blockly.Icon.prototype.getIconLocation = function() {
  181. return this.iconXY_;
  182. };
  183. /**
  184. * Create the icon on the block.
  185. * @private
  186. */
  187. Blockly.Icon.prototype.createIconOld = function() {
  188. if (this.iconGroup_) {
  189. // Icon already exists.
  190. return;
  191. }
  192. /* Here's the markup that will be generated:
  193. <g class="blocklyIconGroup"></g>
  194. */
  195. this.iconGroup_ = Blockly.createSvgElement('g', {}, null);
  196. this.block_.getSvgRoot().appendChild(this.iconGroup_);
  197. Blockly.bindEvent_(this.iconGroup_, 'mouseup', this, this.iconClick_);
  198. this.updateEditable();
  199. };