trashcan.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2011 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 a trash can icon.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Trashcan');
  26. goog.require('goog.Timer');
  27. goog.require('goog.dom');
  28. goog.require('goog.math');
  29. goog.require('goog.math.Rect');
  30. /**
  31. * Class for a trash can.
  32. * @param {!Blockly.Workspace} workspace The workspace to sit in.
  33. * @constructor
  34. */
  35. Blockly.Trashcan = function(workspace) {
  36. this.workspace_ = workspace;
  37. };
  38. /**
  39. * Width of both the trash can and lid images.
  40. * @type {number}
  41. * @private
  42. */
  43. Blockly.Trashcan.prototype.WIDTH_ = 47;
  44. /**
  45. * Height of the trashcan image (minus lid).
  46. * @type {number}
  47. * @private
  48. */
  49. Blockly.Trashcan.prototype.BODY_HEIGHT_ = 44;
  50. /**
  51. * Height of the lid image.
  52. * @type {number}
  53. * @private
  54. */
  55. Blockly.Trashcan.prototype.LID_HEIGHT_ = 16;
  56. /**
  57. * Distance between trashcan and bottom edge of workspace.
  58. * @type {number}
  59. * @private
  60. */
  61. Blockly.Trashcan.prototype.MARGIN_BOTTOM_ = 20;
  62. /**
  63. * Distance between trashcan and right edge of workspace.
  64. * @type {number}
  65. * @private
  66. */
  67. Blockly.Trashcan.prototype.MARGIN_SIDE_ = 20;
  68. /**
  69. * Extent of hotspot on all sides beyond the size of the image.
  70. * @type {number}
  71. * @private
  72. */
  73. Blockly.Trashcan.prototype.MARGIN_HOTSPOT_ = 10;
  74. /**
  75. * Location of trashcan in sprite image.
  76. * @type {number}
  77. * @private
  78. */
  79. Blockly.Trashcan.prototype.SPRITE_LEFT_ = 0;
  80. /**
  81. * Location of trashcan in sprite image.
  82. * @type {number}
  83. * @private
  84. */
  85. Blockly.Trashcan.prototype.SPRITE_TOP_ = 32;
  86. /**
  87. * Current open/close state of the lid.
  88. * @type {boolean}
  89. */
  90. Blockly.Trashcan.prototype.isOpen = false;
  91. /**
  92. * The SVG group containing the trash can.
  93. * @type {Element}
  94. * @private
  95. */
  96. Blockly.Trashcan.prototype.svgGroup_ = null;
  97. /**
  98. * The SVG image element of the trash can lid.
  99. * @type {Element}
  100. * @private
  101. */
  102. Blockly.Trashcan.prototype.svgLid_ = null;
  103. /**
  104. * Task ID of opening/closing animation.
  105. * @type {number}
  106. * @private
  107. */
  108. Blockly.Trashcan.prototype.lidTask_ = 0;
  109. /**
  110. * Current state of lid opening (0.0 = closed, 1.0 = open).
  111. * @type {number}
  112. * @private
  113. */
  114. Blockly.Trashcan.prototype.lidOpen_ = 0;
  115. /**
  116. * Left coordinate of the trash can.
  117. * @type {number}
  118. * @private
  119. */
  120. Blockly.Trashcan.prototype.left_ = 0;
  121. /**
  122. * Top coordinate of the trash can.
  123. * @type {number}
  124. * @private
  125. */
  126. Blockly.Trashcan.prototype.top_ = 0;
  127. /**
  128. * Create the trash can elements.
  129. * @return {!Element} The trash can's SVG group.
  130. */
  131. Blockly.Trashcan.prototype.createDom = function() {
  132. /* Here's the markup that will be generated:
  133. <g class="blocklyTrash">
  134. <clippath id="blocklyTrashBodyClipPath837493">
  135. <rect width="47" height="45" y="15"></rect>
  136. </clippath>
  137. <image width="64" height="92" y="-32" xlink:href="media/sprites.png"
  138. clip-path="url(#blocklyTrashBodyClipPath837493)"></image>
  139. <clippath id="blocklyTrashLidClipPath837493">
  140. <rect width="47" height="15"></rect>
  141. </clippath>
  142. <image width="84" height="92" y="-32" xlink:href="media/sprites.png"
  143. clip-path="url(#blocklyTrashLidClipPath837493)"></image>
  144. </g>
  145. */
  146. this.svgGroup_ = Blockly.createSvgElement('g',
  147. {'class': 'blocklyTrash'}, null);
  148. var rnd = String(Math.random()).substring(2);
  149. var clip = Blockly.createSvgElement('clipPath',
  150. {'id': 'blocklyTrashBodyClipPath' + rnd},
  151. this.svgGroup_);
  152. Blockly.createSvgElement('rect',
  153. {'width': this.WIDTH_, 'height': this.BODY_HEIGHT_,
  154. 'y': this.LID_HEIGHT_},
  155. clip);
  156. var body = Blockly.createSvgElement('image',
  157. {'width': Blockly.SPRITE.width, 'x': -this.SPRITE_LEFT_,
  158. 'height': Blockly.SPRITE.height, 'y': -this.SPRITE_TOP_,
  159. 'clip-path': 'url(#blocklyTrashBodyClipPath' + rnd + ')'},
  160. this.svgGroup_);
  161. body.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
  162. this.workspace_.options.pathToMedia + Blockly.SPRITE.url);
  163. var clip = Blockly.createSvgElement('clipPath',
  164. {'id': 'blocklyTrashLidClipPath' + rnd},
  165. this.svgGroup_);
  166. Blockly.createSvgElement('rect',
  167. {'width': this.WIDTH_, 'height': this.LID_HEIGHT_}, clip);
  168. this.svgLid_ = Blockly.createSvgElement('image',
  169. {'width': Blockly.SPRITE.width, 'x': -this.SPRITE_LEFT_,
  170. 'height': Blockly.SPRITE.height, 'y': -this.SPRITE_TOP_,
  171. 'clip-path': 'url(#blocklyTrashLidClipPath' + rnd + ')'},
  172. this.svgGroup_);
  173. this.svgLid_.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
  174. this.workspace_.options.pathToMedia + Blockly.SPRITE.url);
  175. Blockly.bindEventWithChecks_(this.svgGroup_, 'mouseup', this, this.click);
  176. this.animateLid_();
  177. return this.svgGroup_;
  178. };
  179. /**
  180. * Initialize the trash can.
  181. * @param {number} bottom Distance from workspace bottom to bottom of trashcan.
  182. * @return {number} Distance from workspace bottom to the top of trashcan.
  183. */
  184. Blockly.Trashcan.prototype.init = function(bottom) {
  185. this.bottom_ = this.MARGIN_BOTTOM_ + bottom;
  186. this.setOpen_(false);
  187. return this.bottom_ + this.BODY_HEIGHT_ + this.LID_HEIGHT_;
  188. };
  189. /**
  190. * Dispose of this trash can.
  191. * Unlink from all DOM elements to prevent memory leaks.
  192. */
  193. Blockly.Trashcan.prototype.dispose = function() {
  194. if (this.svgGroup_) {
  195. goog.dom.removeNode(this.svgGroup_);
  196. this.svgGroup_ = null;
  197. }
  198. this.svgLid_ = null;
  199. this.workspace_ = null;
  200. goog.Timer.clear(this.lidTask_);
  201. };
  202. /**
  203. * Move the trash can to the bottom-right corner.
  204. */
  205. Blockly.Trashcan.prototype.position = function() {
  206. var metrics = this.workspace_.getMetrics();
  207. if (!metrics) {
  208. // There are no metrics available (workspace is probably not visible).
  209. return;
  210. }
  211. if (this.workspace_.RTL) {
  212. this.left_ = this.MARGIN_SIDE_ + Blockly.Scrollbar.scrollbarThickness;
  213. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
  214. this.left_ += metrics.flyoutWidth;
  215. if (this.workspace_.toolbox_) {
  216. this.left_ += metrics.absoluteLeft;
  217. }
  218. }
  219. } else {
  220. this.left_ = metrics.viewWidth + metrics.absoluteLeft -
  221. this.WIDTH_ - this.MARGIN_SIDE_ - Blockly.Scrollbar.scrollbarThickness;
  222. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_RIGHT) {
  223. this.left_ -= metrics.flyoutWidth;
  224. }
  225. }
  226. this.top_ = metrics.viewHeight + metrics.absoluteTop -
  227. (this.BODY_HEIGHT_ + this.LID_HEIGHT_) - this.bottom_;
  228. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_BOTTOM) {
  229. this.top_ -= metrics.flyoutHeight;
  230. }
  231. this.svgGroup_.setAttribute('transform',
  232. 'translate(' + this.left_ + ',' + this.top_ + ')');
  233. };
  234. /**
  235. * Return the deletion rectangle for this trash can.
  236. * @return {goog.math.Rect} Rectangle in which to delete.
  237. */
  238. Blockly.Trashcan.prototype.getClientRect = function() {
  239. if (!this.svgGroup_) {
  240. return null;
  241. }
  242. var trashRect = this.svgGroup_.getBoundingClientRect();
  243. var left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_;
  244. var top = trashRect.top + this.SPRITE_TOP_ - this.MARGIN_HOTSPOT_;
  245. var width = this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_;
  246. var height = this.LID_HEIGHT_ + this.BODY_HEIGHT_ + 2 * this.MARGIN_HOTSPOT_;
  247. return new goog.math.Rect(left, top, width, height);
  248. };
  249. /**
  250. * Flip the lid open or shut.
  251. * @param {boolean} state True if open.
  252. * @private
  253. */
  254. Blockly.Trashcan.prototype.setOpen_ = function(state) {
  255. if (this.isOpen == state) {
  256. return;
  257. }
  258. goog.Timer.clear(this.lidTask_);
  259. this.isOpen = state;
  260. this.animateLid_();
  261. };
  262. /**
  263. * Rotate the lid open or closed by one step. Then wait and recurse.
  264. * @private
  265. */
  266. Blockly.Trashcan.prototype.animateLid_ = function() {
  267. this.lidOpen_ += this.isOpen ? 0.2 : -0.2;
  268. this.lidOpen_ = goog.math.clamp(this.lidOpen_, 0, 1);
  269. var lidAngle = this.lidOpen_ * 45;
  270. this.svgLid_.setAttribute('transform', 'rotate(' +
  271. (this.workspace_.RTL ? -lidAngle : lidAngle) + ',' +
  272. (this.workspace_.RTL ? 4 : this.WIDTH_ - 4) + ',' +
  273. (this.LID_HEIGHT_ - 2) + ')');
  274. var opacity = goog.math.lerp(0.4, 0.8, this.lidOpen_);
  275. this.svgGroup_.style.opacity = opacity;
  276. if (this.lidOpen_ > 0 && this.lidOpen_ < 1) {
  277. this.lidTask_ = goog.Timer.callOnce(this.animateLid_, 20, this);
  278. }
  279. };
  280. /**
  281. * Flip the lid shut.
  282. * Called externally after a drag.
  283. */
  284. Blockly.Trashcan.prototype.close = function() {
  285. this.setOpen_(false);
  286. };
  287. /**
  288. * Inspect the contents of the trash.
  289. */
  290. Blockly.Trashcan.prototype.click = function() {
  291. var dx = this.workspace_.startScrollX - this.workspace_.scrollX;
  292. var dy = this.workspace_.startScrollY - this.workspace_.scrollY;
  293. if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) {
  294. return;
  295. }
  296. console.log('TODO: Inspect trash.');
  297. };