rendered_connection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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.select();
  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.unselect();
  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. * Add highlighting around this illegal connection for BlocksCAD.
  194. */
  195. Blockly.Connection.prototype.highlightBad = function() {
  196. var steps;
  197. if (this.type == Blockly.INPUT_VALUE || this.type == Blockly.OUTPUT_VALUE) {
  198. steps = 'm -10,5 l 15,15 m -15,0 l 15,-15';
  199. } else {
  200. steps = 'm -15,-5 l 15,15 m -15,0 l 15,-15 ';
  201. }
  202. var xy = this.sourceBlock_.getRelativeToSurfaceXY();
  203. var x = this.x_ - xy.x;
  204. var y = this.y_ - xy.y;
  205. Blockly.Connection.highlightedPathBad_ = Blockly.createSvgElement('path',
  206. {'class': 'blocklyHighlightedConnectionPathBad',
  207. 'd': steps,
  208. transform: 'translate(' + x + ',' + y + ')' +
  209. (this.sourceBlock_.RTL ? ' scale(-1 1)' : '')},
  210. this.sourceBlock_.getSvgRoot());
  211. };
  212. /**
  213. * Unhide this connection, as well as all down-stream connections on any block
  214. * attached to this connection. This happens when a block is expanded.
  215. * Also unhides down-stream comments.
  216. * @return {!Array.<!Blockly.Block>} List of blocks to render.
  217. */
  218. Blockly.RenderedConnection.prototype.unhideAll = function() {
  219. this.setHidden(false);
  220. // All blocks that need unhiding must be unhidden before any rendering takes
  221. // place, since rendering requires knowing the dimensions of lower blocks.
  222. // Also, since rendering a block renders all its parents, we only need to
  223. // render the leaf nodes.
  224. var renderList = [];
  225. if (this.type != Blockly.INPUT_VALUE && this.type != Blockly.NEXT_STATEMENT) {
  226. // Only spider down.
  227. return renderList;
  228. }
  229. var block = this.targetBlock();
  230. if (block) {
  231. var connections;
  232. if (block.isCollapsed()) {
  233. // This block should only be partially revealed since it is collapsed.
  234. connections = [];
  235. block.outputConnection && connections.push(block.outputConnection);
  236. block.nextConnection && connections.push(block.nextConnection);
  237. block.previousConnection && connections.push(block.previousConnection);
  238. } else {
  239. // Show all connections of this block.
  240. connections = block.getConnections_(true);
  241. }
  242. for (var i = 0; i < connections.length; i++) {
  243. renderList.push.apply(renderList, connections[i].unhideAll());
  244. }
  245. if (!renderList.length) {
  246. // Leaf block.
  247. renderList[0] = block;
  248. }
  249. }
  250. return renderList;
  251. };
  252. /**
  253. * Remove the highlighting around this connection.
  254. * It could be a legal or illegal connection (BlocksCAD)
  255. */
  256. Blockly.RenderedConnection.prototype.unhighlight = function() {
  257. if (Blockly.Connection.highlightedPath_) {
  258. goog.dom.removeNode(Blockly.Connection.highlightedPath_);
  259. delete Blockly.Connection.highlightedPath_;
  260. }
  261. if (Blockly.Connection.highlightedPathBad_) {
  262. goog.dom.removeNode(Blockly.Connection.highlightedPathBad_);
  263. delete Blockly.Connection.highlightedPathBad_;
  264. }
  265. };
  266. /**
  267. * Set whether this connections is hidden (not tracked in a database) or not.
  268. * @param {boolean} hidden True if connection is hidden.
  269. */
  270. Blockly.RenderedConnection.prototype.setHidden = function(hidden) {
  271. this.hidden_ = hidden;
  272. if (hidden && this.inDB_) {
  273. this.db_.removeConnection_(this);
  274. } else if (!hidden && !this.inDB_) {
  275. this.db_.addConnection(this);
  276. }
  277. };
  278. /**
  279. * Hide this connection, as well as all down-stream connections on any block
  280. * attached to this connection. This happens when a block is collapsed.
  281. * Also hides down-stream comments.
  282. */
  283. Blockly.RenderedConnection.prototype.hideAll = function() {
  284. this.setHidden(true);
  285. if (this.targetConnection) {
  286. var blocks = this.targetBlock().getDescendants();
  287. for (var i = 0; i < blocks.length; i++) {
  288. var block = blocks[i];
  289. // Hide all connections of all children.
  290. var connections = block.getConnections_(true);
  291. for (var j = 0; j < connections.length; j++) {
  292. connections[j].setHidden(true);
  293. }
  294. // Close all bubbles of all children.
  295. var icons = block.getIcons();
  296. for (var j = 0; j < icons.length; j++) {
  297. icons[j].setVisible(false);
  298. }
  299. }
  300. }
  301. };
  302. /**
  303. * Check if the two connections can be dragged to connect to each other.
  304. * @param {!Blockly.Connection} candidate A nearby connection to check.
  305. * @param {number} maxRadius The maximum radius allowed for connections.
  306. * @return {boolean} True if the connection is allowed, false otherwise.
  307. */
  308. Blockly.RenderedConnection.prototype.isConnectionAllowed = function(candidate,
  309. maxRadius) {
  310. if (this.distanceFrom(candidate) > maxRadius) {
  311. return false;
  312. }
  313. return Blockly.RenderedConnection.superClass_.isConnectionAllowed.call(this,
  314. candidate);
  315. };
  316. /**
  317. * Disconnect two blocks that are connected by this connection.
  318. * @param {!Blockly.Block} parentBlock The superior block.
  319. * @param {!Blockly.Block} childBlock The inferior block.
  320. * @private
  321. */
  322. Blockly.RenderedConnection.prototype.disconnectInternal_ = function(parentBlock,
  323. childBlock) {
  324. Blockly.RenderedConnection.superClass_.disconnectInternal_.call(this,
  325. parentBlock, childBlock);
  326. // Rerender the parent so that it may reflow.
  327. if (parentBlock.rendered) {
  328. parentBlock.render();
  329. }
  330. if (childBlock.rendered) {
  331. childBlock.updateDisabled();
  332. childBlock.render();
  333. }
  334. };
  335. /**
  336. * Respawn the shadow block if there was one connected to the this connection.
  337. * Render/rerender blocks as needed.
  338. * @private
  339. */
  340. Blockly.RenderedConnection.prototype.respawnShadow_ = function() {
  341. var parentBlock = this.getSourceBlock();
  342. // Respawn the shadow block if there is one.
  343. var shadow = this.getShadowDom();
  344. if (parentBlock.workspace && shadow && Blockly.Events.recordUndo) {
  345. Blockly.RenderedConnection.superClass_.respawnShadow_.call(this);
  346. var blockShadow = this.targetBlock();
  347. if (!blockShadow) {
  348. throw 'Couldn\'t respawn the shadow block that should exist here.';
  349. }
  350. blockShadow.initSvg();
  351. blockShadow.render(false);
  352. if (parentBlock.rendered) {
  353. parentBlock.render();
  354. }
  355. }
  356. };
  357. /**
  358. * Find all nearby compatible connections to this connection.
  359. * Type checking does not apply, since this function is used for bumping.
  360. * @param {number} maxLimit The maximum radius to another connection.
  361. * @return {!Array.<!Blockly.Connection>} List of connections.
  362. * @private
  363. */
  364. Blockly.RenderedConnection.prototype.neighbours_ = function(maxLimit) {
  365. return this.dbOpposite_.getNeighbours(this, maxLimit);
  366. };
  367. /**
  368. * Connect two connections together. This is the connection on the superior
  369. * block. Rerender blocks as needed.
  370. * @param {!Blockly.Connection} childConnection Connection on inferior block.
  371. * @private
  372. */
  373. Blockly.RenderedConnection.prototype.connect_ = function(childConnection) {
  374. Blockly.RenderedConnection.superClass_.connect_.call(this, childConnection);
  375. var parentConnection = this;
  376. var parentBlock = parentConnection.getSourceBlock();
  377. var childBlock = childConnection.getSourceBlock();
  378. if (parentBlock.rendered) {
  379. parentBlock.updateDisabled();
  380. }
  381. if (childBlock.rendered) {
  382. childBlock.updateDisabled();
  383. }
  384. if (parentBlock.rendered && childBlock.rendered) {
  385. if (parentConnection.type == Blockly.NEXT_STATEMENT ||
  386. parentConnection.type == Blockly.PREVIOUS_STATEMENT) {
  387. // Child block may need to square off its corners if it is in a stack.
  388. // Rendering a child will render its parent.
  389. childBlock.render();
  390. } else {
  391. // Child block does not change shape. Rendering the parent node will
  392. // move its connected children into position.
  393. parentBlock.render();
  394. }
  395. }
  396. };