rendered_connection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2016 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 Components for creating connections between blocks.
  22. * @author fenichel@google.com (Rachel Fenichel)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.RenderedConnection');
  26. goog.require('Blockly.Connection');
  27. /**
  28. * Class for a connection between blocks that may be rendered on screen.
  29. * @param {!Blockly.Block} source The block establishing this connection.
  30. * @param {number} type The type of the connection.
  31. * @extends {Blockly.Connection}
  32. * @constructor
  33. */
  34. Blockly.RenderedConnection = function(source, type) {
  35. Blockly.RenderedConnection.superClass_.constructor.call(this, source, type);
  36. this.offsetInBlock_ = new goog.math.Coordinate(0, 0);
  37. };
  38. goog.inherits(Blockly.RenderedConnection, Blockly.Connection);
  39. /**
  40. * Returns the distance between this connection and another connection.
  41. * @param {!Blockly.Connection} otherConnection The other connection to measure
  42. * the distance to.
  43. * @return {number} The distance between connections.
  44. */
  45. Blockly.RenderedConnection.prototype.distanceFrom = function(otherConnection) {
  46. var xDiff = this.x_ - otherConnection.x_;
  47. var yDiff = this.y_ - otherConnection.y_;
  48. return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  49. };
  50. /**
  51. * Move the block(s) belonging to the connection to a point where they don't
  52. * visually interfere with the specified connection.
  53. * @param {!Blockly.Connection} staticConnection The connection to move away
  54. * from.
  55. * @private
  56. */
  57. Blockly.RenderedConnection.prototype.bumpAwayFrom_ = function(staticConnection) {
  58. if (Blockly.dragMode_ != Blockly.DRAG_NONE) {
  59. // Don't move blocks around while the user is doing the same.
  60. return;
  61. }
  62. // Move the root block.
  63. var rootBlock = this.sourceBlock_.getRootBlock();
  64. if (rootBlock.isInFlyout) {
  65. // Don't move blocks around in a flyout.
  66. return;
  67. }
  68. var reverse = false;
  69. if (!rootBlock.isMovable()) {
  70. // Can't bump an uneditable block away.
  71. // Check to see if the other block is movable.
  72. rootBlock = staticConnection.getSourceBlock().getRootBlock();
  73. if (!rootBlock.isMovable()) {
  74. return;
  75. }
  76. // Swap the connections and move the 'static' connection instead.
  77. staticConnection = this;
  78. reverse = true;
  79. }
  80. // Raise it to the top for extra visibility.
  81. var selected = Blockly.selected == rootBlock;
  82. selected || rootBlock.addSelect();
  83. var dx = (staticConnection.x_ + Blockly.SNAP_RADIUS) - this.x_;
  84. var dy = (staticConnection.y_ + Blockly.SNAP_RADIUS) - this.y_;
  85. if (reverse) {
  86. // When reversing a bump due to an uneditable block, bump up.
  87. dy = -dy;
  88. }
  89. if (rootBlock.RTL) {
  90. dx = -dx;
  91. }
  92. rootBlock.moveBy(dx, dy);
  93. selected || rootBlock.removeSelect();
  94. };
  95. /**
  96. * Change the connection's coordinates.
  97. * @param {number} x New absolute x coordinate.
  98. * @param {number} y New absolute y coordinate.
  99. */
  100. Blockly.RenderedConnection.prototype.moveTo = function(x, y) {
  101. // Remove it from its old location in the database (if already present)
  102. if (this.inDB_) {
  103. this.db_.removeConnection_(this);
  104. }
  105. this.x_ = x;
  106. this.y_ = y;
  107. // Insert it into its new location in the database.
  108. if (!this.hidden_) {
  109. this.db_.addConnection(this);
  110. }
  111. };
  112. /**
  113. * Change the connection's coordinates.
  114. * @param {number} dx Change to x coordinate.
  115. * @param {number} dy Change to y coordinate.
  116. */
  117. Blockly.RenderedConnection.prototype.moveBy = function(dx, dy) {
  118. this.moveTo(this.x_ + dx, this.y_ + dy);
  119. };
  120. /**
  121. * Move this connection to the location given by its offset within the block and
  122. * the coordinate of the block's top left corner.
  123. * @param {!goog.math.Coordinate} blockTL The coordinate of the top left corner
  124. * of the block.
  125. */
  126. Blockly.RenderedConnection.prototype.moveToOffset = function(blockTL) {
  127. this.moveTo(blockTL.x + this.offsetInBlock_.x,
  128. blockTL.y + this.offsetInBlock_.y);
  129. };
  130. /**
  131. * Set the offset of this connection relative to the top left of its block.
  132. * @param {number} x The new relative x.
  133. * @param {number} y The new relative y.
  134. */
  135. Blockly.RenderedConnection.prototype.setOffsetInBlock = function(x, y) {
  136. this.offsetInBlock_.x = x;
  137. this.offsetInBlock_.y = y;
  138. };
  139. /**
  140. * Move the blocks on either side of this connection right next to each other.
  141. * @private
  142. */
  143. Blockly.RenderedConnection.prototype.tighten_ = function() {
  144. var dx = this.targetConnection.x_ - this.x_;
  145. var dy = this.targetConnection.y_ - this.y_;
  146. if (dx != 0 || dy != 0) {
  147. var block = this.targetBlock();
  148. var svgRoot = block.getSvgRoot();
  149. if (!svgRoot) {
  150. throw 'block is not rendered.';
  151. }
  152. var xy = Blockly.getRelativeXY_(svgRoot);
  153. block.getSvgRoot().setAttribute('transform',
  154. 'translate(' + (xy.x - dx) + ',' + (xy.y - dy) + ')');
  155. block.moveConnections_(-dx, -dy);
  156. }
  157. };
  158. /**
  159. * Find the closest compatible connection to this connection.
  160. * @param {number} maxLimit The maximum radius to another connection.
  161. * @param {number} dx Horizontal offset between this connection's location
  162. * in the database and the current location (as a result of dragging).
  163. * @param {number} dy Vertical offset between this connection's location
  164. * in the database and the current location (as a result of dragging).
  165. * @return {!{connection: ?Blockly.Connection, radius: number}} Contains two
  166. * properties: 'connection' which is either another connection or null,
  167. * and 'radius' which is the distance.
  168. */
  169. Blockly.RenderedConnection.prototype.closest = function(maxLimit, dx, dy) {
  170. return this.dbOpposite_.searchForClosest(this, maxLimit, dx, dy);
  171. };
  172. /**
  173. * Add highlighting around this connection.
  174. */
  175. Blockly.RenderedConnection.prototype.highlight = function() {
  176. var steps;
  177. if (this.type == Blockly.INPUT_VALUE || this.type == Blockly.OUTPUT_VALUE) {
  178. steps = 'm 0,0 ' + Blockly.BlockSvg.TAB_PATH_DOWN + ' v 5';
  179. } else {
  180. steps = 'm -20,0 h 5 ' + Blockly.BlockSvg.NOTCH_PATH_LEFT + ' h 5';
  181. }
  182. var xy = this.sourceBlock_.getRelativeToSurfaceXY();
  183. var x = this.x_ - xy.x;
  184. var y = this.y_ - xy.y;
  185. Blockly.Connection.highlightedPath_ = Blockly.createSvgElement('path',
  186. {'class': 'blocklyHighlightedConnectionPath',
  187. 'd': steps,
  188. transform: 'translate(' + x + ',' + y + ')' +
  189. (this.sourceBlock_.RTL ? ' scale(-1 1)' : '')},
  190. this.sourceBlock_.getSvgRoot());
  191. };
  192. /**
  193. * Unhide this connection, as well as all down-stream connections on any block
  194. * attached to this connection. This happens when a block is expanded.
  195. * Also unhides down-stream comments.
  196. * @return {!Array.<!Blockly.Block>} List of blocks to render.
  197. */
  198. Blockly.RenderedConnection.prototype.unhideAll = function() {
  199. this.setHidden(false);
  200. // All blocks that need unhiding must be unhidden before any rendering takes
  201. // place, since rendering requires knowing the dimensions of lower blocks.
  202. // Also, since rendering a block renders all its parents, we only need to
  203. // render the leaf nodes.
  204. var renderList = [];
  205. if (this.type != Blockly.INPUT_VALUE && this.type != Blockly.NEXT_STATEMENT) {
  206. // Only spider down.
  207. return renderList;
  208. }
  209. var block = this.targetBlock();
  210. if (block) {
  211. var connections;
  212. if (block.isCollapsed()) {
  213. // This block should only be partially revealed since it is collapsed.
  214. connections = [];
  215. block.outputConnection && connections.push(block.outputConnection);
  216. block.nextConnection && connections.push(block.nextConnection);
  217. block.previousConnection && connections.push(block.previousConnection);
  218. } else {
  219. // Show all connections of this block.
  220. connections = block.getConnections_(true);
  221. }
  222. for (var i = 0; i < connections.length; i++) {
  223. renderList.push.apply(renderList, connections[i].unhideAll());
  224. }
  225. if (!renderList.length) {
  226. // Leaf block.
  227. renderList[0] = block;
  228. }
  229. }
  230. return renderList;
  231. };
  232. /**
  233. * Remove the highlighting around this connection.
  234. */
  235. Blockly.RenderedConnection.prototype.unhighlight = function() {
  236. goog.dom.removeNode(Blockly.Connection.highlightedPath_);
  237. delete Blockly.Connection.highlightedPath_;
  238. };
  239. /**
  240. * Set whether this connections is hidden (not tracked in a database) or not.
  241. * @param {boolean} hidden True if connection is hidden.
  242. */
  243. Blockly.RenderedConnection.prototype.setHidden = function(hidden) {
  244. this.hidden_ = hidden;
  245. if (hidden && this.inDB_) {
  246. this.db_.removeConnection_(this);
  247. } else if (!hidden && !this.inDB_) {
  248. this.db_.addConnection(this);
  249. }
  250. };
  251. /**
  252. * Hide this connection, as well as all down-stream connections on any block
  253. * attached to this connection. This happens when a block is collapsed.
  254. * Also hides down-stream comments.
  255. */
  256. Blockly.RenderedConnection.prototype.hideAll = function() {
  257. this.setHidden(true);
  258. if (this.targetConnection) {
  259. var blocks = this.targetBlock().getDescendants();
  260. for (var i = 0; i < blocks.length; i++) {
  261. var block = blocks[i];
  262. // Hide all connections of all children.
  263. var connections = block.getConnections_(true);
  264. for (var j = 0; j < connections.length; j++) {
  265. connections[j].setHidden(true);
  266. }
  267. // Close all bubbles of all children.
  268. var icons = block.getIcons();
  269. for (var j = 0; j < icons.length; j++) {
  270. icons[j].setVisible(false);
  271. }
  272. }
  273. }
  274. };
  275. /**
  276. * Check if the two connections can be dragged to connect to each other.
  277. * @param {!Blockly.Connection} candidate A nearby connection to check.
  278. * @param {number} maxRadius The maximum radius allowed for connections.
  279. * @return {boolean} True if the connection is allowed, false otherwise.
  280. */
  281. Blockly.RenderedConnection.prototype.isConnectionAllowed = function(candidate,
  282. maxRadius) {
  283. if (this.distanceFrom(candidate) > maxRadius) {
  284. return false;
  285. }
  286. return Blockly.RenderedConnection.superClass_.isConnectionAllowed.call(this,
  287. candidate);
  288. };
  289. /**
  290. * Disconnect two blocks that are connected by this connection.
  291. * @param {!Blockly.Block} parentBlock The superior block.
  292. * @param {!Blockly.Block} childBlock The inferior block.
  293. * @private
  294. */
  295. Blockly.RenderedConnection.prototype.disconnectInternal_ = function(parentBlock,
  296. childBlock) {
  297. Blockly.RenderedConnection.superClass_.disconnectInternal_.call(this,
  298. parentBlock, childBlock);
  299. // Rerender the parent so that it may reflow.
  300. if (parentBlock.rendered) {
  301. parentBlock.render();
  302. }
  303. if (childBlock.rendered) {
  304. childBlock.updateDisabled();
  305. childBlock.render();
  306. }
  307. };
  308. /**
  309. * Respawn the shadow block if there was one connected to the this connection.
  310. * Render/rerender blocks as needed.
  311. * @private
  312. */
  313. Blockly.RenderedConnection.prototype.respawnShadow_ = function() {
  314. var parentBlock = this.getSourceBlock();
  315. // Respawn the shadow block if there is one.
  316. var shadow = this.getShadowDom();
  317. if (parentBlock.workspace && shadow && Blockly.Events.recordUndo) {
  318. Blockly.RenderedConnection.superClass_.respawnShadow_.call(this);
  319. var blockShadow = this.targetBlock();
  320. if (!blockShadow) {
  321. throw 'Couldn\'t respawn the shadow block that should exist here.';
  322. }
  323. blockShadow.initSvg();
  324. blockShadow.render(false);
  325. if (parentBlock.rendered) {
  326. parentBlock.render();
  327. }
  328. }
  329. };
  330. /**
  331. * Find all nearby compatible connections to this connection.
  332. * Type checking does not apply, since this function is used for bumping.
  333. * @param {number} maxLimit The maximum radius to another connection.
  334. * @return {!Array.<!Blockly.Connection>} List of connections.
  335. * @private
  336. */
  337. Blockly.RenderedConnection.prototype.neighbours_ = function(maxLimit) {
  338. return this.dbOpposite_.getNeighbours(this, maxLimit);
  339. };
  340. /**
  341. * Connect two connections together. This is the connection on the superior
  342. * block. Rerender blocks as needed.
  343. * @param {!Blockly.Connection} childConnection Connection on inferior block.
  344. * @private
  345. */
  346. Blockly.RenderedConnection.prototype.connect_ = function(childConnection) {
  347. Blockly.RenderedConnection.superClass_.connect_.call(this, childConnection);
  348. var parentConnection = this;
  349. var parentBlock = parentConnection.getSourceBlock();
  350. var childBlock = childConnection.getSourceBlock();
  351. if (parentBlock.rendered) {
  352. parentBlock.updateDisabled();
  353. }
  354. if (childBlock.rendered) {
  355. childBlock.updateDisabled();
  356. }
  357. if (parentBlock.rendered && childBlock.rendered) {
  358. if (parentConnection.type == Blockly.NEXT_STATEMENT ||
  359. parentConnection.type == Blockly.PREVIOUS_STATEMENT) {
  360. // Child block may need to square off its corners if it is in a stack.
  361. // Rendering a child will render its parent.
  362. childBlock.render();
  363. } else {
  364. // Child block does not change shape. Rendering the parent node will
  365. // move its connected children into position.
  366. parentBlock.render();
  367. }
  368. }
  369. };