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