zoom_controls.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2015 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 zoom icons.
  22. * @author carloslfu@gmail.com (Carlos Galarza)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.ZoomControls');
  26. goog.require('Blockly.Touch');
  27. goog.require('goog.dom');
  28. /**
  29. * Class for a zoom controls.
  30. * @param {!Blockly.Workspace} workspace The workspace to sit in.
  31. * @constructor
  32. */
  33. Blockly.ZoomControls = function(workspace) {
  34. this.workspace_ = workspace;
  35. };
  36. /**
  37. * Width of the zoom controls.
  38. * @type {number}
  39. * @private
  40. */
  41. Blockly.ZoomControls.prototype.WIDTH_ = 32;
  42. /**
  43. * Height of the zoom controls.
  44. * @type {number}
  45. * @private
  46. */
  47. Blockly.ZoomControls.prototype.HEIGHT_ = 110;
  48. /**
  49. * Distance between zoom controls and bottom edge of workspace.
  50. * @type {number}
  51. * @private
  52. */
  53. Blockly.ZoomControls.prototype.MARGIN_BOTTOM_ = 20;
  54. /**
  55. * Distance between zoom controls and right edge of workspace.
  56. * @type {number}
  57. * @private
  58. */
  59. Blockly.ZoomControls.prototype.MARGIN_SIDE_ = 20;
  60. /**
  61. * The SVG group containing the zoom controls.
  62. * @type {Element}
  63. * @private
  64. */
  65. Blockly.ZoomControls.prototype.svgGroup_ = null;
  66. /**
  67. * Left coordinate of the zoom controls.
  68. * @type {number}
  69. * @private
  70. */
  71. Blockly.ZoomControls.prototype.left_ = 0;
  72. /**
  73. * Top coordinate of the zoom controls.
  74. * @type {number}
  75. * @private
  76. */
  77. Blockly.ZoomControls.prototype.top_ = 0;
  78. /**
  79. * Create the zoom controls.
  80. * @return {!Element} The zoom controls SVG group.
  81. */
  82. Blockly.ZoomControls.prototype.createDom = function() {
  83. var workspace = this.workspace_;
  84. /* Here's the markup that will be generated:
  85. <g class="blocklyZoom">
  86. <clippath id="blocklyZoomoutClipPath837493">
  87. <rect width="32" height="32" y="77"></rect>
  88. </clippath>
  89. <image width="96" height="124" x="-64" y="-15" xlink:href="media/sprites.png"
  90. clip-path="url(#blocklyZoomoutClipPath837493)"></image>
  91. <clippath id="blocklyZoominClipPath837493">
  92. <rect width="32" height="32" y="43"></rect>
  93. </clippath>
  94. <image width="96" height="124" x="-32" y="-49" xlink:href="media/sprites.png"
  95. clip-path="url(#blocklyZoominClipPath837493)"></image>
  96. <clippath id="blocklyZoomresetClipPath837493">
  97. <rect width="32" height="32"></rect>
  98. </clippath>
  99. <image width="96" height="124" y="-92" xlink:href="media/sprites.png"
  100. clip-path="url(#blocklyZoomresetClipPath837493)"></image>
  101. </g>
  102. */
  103. this.svgGroup_ = Blockly.createSvgElement('g',
  104. {'class': 'blocklyZoom'}, null);
  105. var rnd = String(Math.random()).substring(2);
  106. var clip = Blockly.createSvgElement('clipPath',
  107. {'id': 'blocklyZoomoutClipPath' + rnd},
  108. this.svgGroup_);
  109. Blockly.createSvgElement('rect',
  110. {'width': 32, 'height': 32, 'y': 77},
  111. clip);
  112. var zoomoutSvg = Blockly.createSvgElement('image',
  113. {'width': Blockly.SPRITE.width,
  114. 'height': Blockly.SPRITE.height, 'x': -64,
  115. 'y': -15,
  116. 'clip-path': 'url(#blocklyZoomoutClipPath' + rnd + ')'},
  117. this.svgGroup_);
  118. zoomoutSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
  119. workspace.options.pathToMedia + Blockly.SPRITE.url);
  120. var clip = Blockly.createSvgElement('clipPath',
  121. {'id': 'blocklyZoominClipPath' + rnd},
  122. this.svgGroup_);
  123. Blockly.createSvgElement('rect',
  124. {'width': 32, 'height': 32, 'y': 43},
  125. clip);
  126. var zoominSvg = Blockly.createSvgElement('image',
  127. {'width': Blockly.SPRITE.width,
  128. 'height': Blockly.SPRITE.height,
  129. 'x': -32,
  130. 'y': -49,
  131. 'clip-path': 'url(#blocklyZoominClipPath' + rnd + ')'},
  132. this.svgGroup_);
  133. zoominSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
  134. workspace.options.pathToMedia + Blockly.SPRITE.url);
  135. var clip = Blockly.createSvgElement('clipPath',
  136. {'id': 'blocklyZoomresetClipPath' + rnd},
  137. this.svgGroup_);
  138. Blockly.createSvgElement('rect',
  139. {'width': 32, 'height': 32},
  140. clip);
  141. var zoomresetSvg = Blockly.createSvgElement('image',
  142. {'width': Blockly.SPRITE.width,
  143. 'height': Blockly.SPRITE.height, 'y': -92,
  144. 'clip-path': 'url(#blocklyZoomresetClipPath' + rnd + ')'},
  145. this.svgGroup_);
  146. zoomresetSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
  147. workspace.options.pathToMedia + Blockly.SPRITE.url);
  148. // Attach event listeners.
  149. Blockly.bindEventWithChecks_(zoomresetSvg, 'mousedown', null, function(e) {
  150. workspace.markFocused();
  151. workspace.setScale(workspace.options.zoomOptions.startScale);
  152. workspace.scrollCenter();
  153. Blockly.Touch.clearTouchIdentifier(); // Don't block future drags.
  154. e.stopPropagation(); // Don't start a workspace scroll.
  155. e.preventDefault(); // Stop double-clicking from selecting text.
  156. });
  157. Blockly.bindEventWithChecks_(zoominSvg, 'mousedown', null, function(e) {
  158. workspace.markFocused();
  159. workspace.zoomCenter(1);
  160. Blockly.Touch.clearTouchIdentifier(); // Don't block future drags.
  161. e.stopPropagation(); // Don't start a workspace scroll.
  162. e.preventDefault(); // Stop double-clicking from selecting text.
  163. });
  164. Blockly.bindEventWithChecks_(zoomoutSvg, 'mousedown', null, function(e) {
  165. workspace.markFocused();
  166. workspace.zoomCenter(-1);
  167. Blockly.Touch.clearTouchIdentifier(); // Don't block future drags.
  168. e.stopPropagation(); // Don't start a workspace scroll.
  169. e.preventDefault(); // Stop double-clicking from selecting text.
  170. });
  171. return this.svgGroup_;
  172. };
  173. /**
  174. * Initialize the zoom controls.
  175. * @param {number} bottom Distance from workspace bottom to bottom of controls.
  176. * @return {number} Distance from workspace bottom to the top of controls.
  177. */
  178. Blockly.ZoomControls.prototype.init = function(bottom) {
  179. this.bottom_ = this.MARGIN_BOTTOM_ + bottom;
  180. return this.bottom_ + this.HEIGHT_;
  181. };
  182. /**
  183. * Dispose of this zoom controls.
  184. * Unlink from all DOM elements to prevent memory leaks.
  185. */
  186. Blockly.ZoomControls.prototype.dispose = function() {
  187. if (this.svgGroup_) {
  188. goog.dom.removeNode(this.svgGroup_);
  189. this.svgGroup_ = null;
  190. }
  191. this.workspace_ = null;
  192. };
  193. /**
  194. * Move the zoom controls to the bottom-right corner.
  195. */
  196. Blockly.ZoomControls.prototype.position = function() {
  197. var metrics = this.workspace_.getMetrics();
  198. if (!metrics) {
  199. // There are no metrics available (workspace is probably not visible).
  200. return;
  201. }
  202. if (this.workspace_.RTL) {
  203. this.left_ = this.MARGIN_SIDE_ + Blockly.Scrollbar.scrollbarThickness;
  204. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
  205. this.left_ += metrics.flyoutWidth;
  206. if (this.workspace_.toolbox_) {
  207. this.left_ += metrics.absoluteLeft;
  208. }
  209. }
  210. } else {
  211. this.left_ = metrics.viewWidth + metrics.absoluteLeft -
  212. this.WIDTH_ - this.MARGIN_SIDE_ - Blockly.Scrollbar.scrollbarThickness;
  213. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_RIGHT) {
  214. this.left_ -= metrics.flyoutWidth;
  215. }
  216. }
  217. this.top_ = metrics.viewHeight + metrics.absoluteTop -
  218. this.HEIGHT_ - this.bottom_;
  219. if (metrics.toolboxPosition == Blockly.TOOLBOX_AT_BOTTOM) {
  220. this.top_ -= metrics.flyoutHeight;
  221. }
  222. this.svgGroup_.setAttribute('transform',
  223. 'translate(' + this.left_ + ',' + this.top_ + ')');
  224. };