diff --git a/core/connection_db.js b/core/connection_db.js index 4b2615725..49bccb504 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -12,37 +12,37 @@ */ 'use strict'; -goog.provide('Blockly.ConnectionDB'); +goog.module('Blockly.ConnectionDB'); +goog.module.declareLegacyNamespace(); -goog.require('Blockly.connectionTypes'); +const Coordinate = goog.requireType('Blockly.utils.Coordinate'); +const IConnectionChecker = goog.requireType('Blockly.IConnectionChecker'); +const RenderedConnection = goog.require('Blockly.RenderedConnection'); +const connectionTypes = goog.require('Blockly.connectionTypes'); /** @suppress {extraRequire} */ goog.require('Blockly.constants'); -goog.require('Blockly.RenderedConnection'); - -goog.requireType('Blockly.IConnectionChecker'); -goog.requireType('Blockly.utils.Coordinate'); /** * Database of connections. * Connections are stored in order of their vertical component. This way * connections in an area may be looked up quickly using a binary search. - * @param {!Blockly.IConnectionChecker} checker The workspace's + * @param {!IConnectionChecker} checker The workspace's * connection type checker, used to decide if connections are valid during a * drag. * @constructor */ -Blockly.ConnectionDB = function(checker) { +const ConnectionDB = function(checker) { /** * Array of connections sorted by y position in workspace units. - * @type {!Array} + * @type {!Array} * @private */ this.connections_ = []; /** * The workspace's connection type checker, used to decide if connections are * valid during a drag. - * @type {!Blockly.IConnectionChecker} + * @type {!IConnectionChecker} * @private */ this.connectionChecker_ = checker; @@ -50,13 +50,13 @@ Blockly.ConnectionDB = function(checker) { /** * Add a connection to the database. Should not already exist in the database. - * @param {!Blockly.RenderedConnection} connection The connection to be added. + * @param {!RenderedConnection} connection The connection to be added. * @param {number} yPos The y position used to decide where to insert the * connection. * @package */ -Blockly.ConnectionDB.prototype.addConnection = function(connection, yPos) { - var index = this.calculateIndexForYPos_(yPos); +ConnectionDB.prototype.addConnection = function(connection, yPos) { + const index = this.calculateIndexForYPos_(yPos); this.connections_.splice(index, 0, connection); }; @@ -65,18 +65,18 @@ Blockly.ConnectionDB.prototype.addConnection = function(connection, yPos) { * * Starts by doing a binary search to find the approximate location, then * linearly searches nearby for the exact connection. - * @param {!Blockly.RenderedConnection} conn The connection to find. + * @param {!RenderedConnection} conn The connection to find. * @param {number} yPos The y position used to find the index of the connection. * @return {number} The index of the connection, or -1 if the connection was * not found. * @private */ -Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { +ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { if (!this.connections_.length) { return -1; } - var bestGuess = this.calculateIndexForYPos_(yPos); + const bestGuess = this.calculateIndexForYPos_(yPos); if (bestGuess >= this.connections_.length) { // Not in list return -1; @@ -84,7 +84,7 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { yPos = conn.y; // Walk forward and back on the y axis looking for the connection. - var pointer = bestGuess; + let pointer = bestGuess; while (pointer >= 0 && this.connections_[pointer].y == yPos) { if (this.connections_[pointer] == conn) { return pointer; @@ -110,14 +110,14 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { * @return {number} The candidate index. * @private */ -Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { +ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { if (!this.connections_.length) { return 0; } - var pointerMin = 0; - var pointerMax = this.connections_.length; + let pointerMin = 0; + let pointerMax = this.connections_.length; while (pointerMin < pointerMax) { - var pointerMid = Math.floor((pointerMin + pointerMax) / 2); + const pointerMid = Math.floor((pointerMin + pointerMax) / 2); if (this.connections_[pointerMid].y < yPos) { pointerMin = pointerMid + 1; } else if (this.connections_[pointerMid].y > yPos) { @@ -132,12 +132,12 @@ Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { /** * Remove a connection from the database. Must already exist in DB. - * @param {!Blockly.RenderedConnection} connection The connection to be removed. + * @param {!RenderedConnection} connection The connection to be removed. * @param {number} yPos The y position used to find the index of the connection. * @throws {Error} If the connection cannot be found in the database. */ -Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { - var index = this.findIndexOfConnection_(connection, yPos); +ConnectionDB.prototype.removeConnection = function(connection, yPos) { + const index = this.findIndexOfConnection_(connection, yPos); if (index == -1) { throw Error('Unable to find connection in connectionDB.'); } @@ -147,20 +147,20 @@ Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { /** * Find all nearby connections to the given connection. * Type checking does not apply, since this function is used for bumping. - * @param {!Blockly.RenderedConnection} connection The connection whose + * @param {!RenderedConnection} connection The connection whose * neighbours should be returned. * @param {number} maxRadius The maximum radius to another connection. - * @return {!Array} List of connections. + * @return {!Array} List of connections. */ -Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { - var db = this.connections_; - var currentX = connection.x; - var currentY = connection.y; +ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { + const db = this.connections_; + const currentX = connection.x; + const currentY = connection.y; // Binary search to find the closest y location. - var pointerMin = 0; - var pointerMax = db.length - 2; - var pointerMid = pointerMax; + let pointerMin = 0; + let pointerMax = db.length - 2; + let pointerMid = pointerMax; while (pointerMin < pointerMid) { if (db[pointerMid].y < currentY) { pointerMin = pointerMid; @@ -170,7 +170,7 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { pointerMid = Math.floor((pointerMin + pointerMax) / 2); } - var neighbours = []; + const neighbours = []; /** * Computes if the current connection is within the allowed radius of another * connection. @@ -180,9 +180,9 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { * the other connection is less than the allowed radius. */ function checkConnection_(yIndex) { - var dx = currentX - db[yIndex].x; - var dy = currentY - db[yIndex].y; - var r = Math.sqrt(dx * dx + dy * dy); + const dx = currentX - db[yIndex].x; + const dy = currentY - db[yIndex].y; + const r = Math.sqrt(dx * dx + dy * dy); if (r <= maxRadius) { neighbours.push(db[yIndex]); } @@ -213,32 +213,31 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { * @return {boolean} True if connection is in range. * @private */ -Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { +ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { return (Math.abs(this.connections_[index].y - baseY) <= maxRadius); }; /** * Find the closest compatible connection to this connection. - * @param {!Blockly.RenderedConnection} conn The connection searching for a compatible + * @param {!RenderedConnection} conn The connection searching for a compatible * mate. * @param {number} maxRadius The maximum radius to another connection. - * @param {!Blockly.utils.Coordinate} dxy Offset between this connection's + * @param {!Coordinate} dxy Offset between this connection's * location in the database and the current location (as a result of * dragging). - * @return {!{connection: Blockly.RenderedConnection, radius: number}} + * @return {!{connection: RenderedConnection, radius: number}} * Contains two properties: 'connection' which is either another * connection or null, and 'radius' which is the distance. */ -Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, - dxy) { +ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dxy) { if (!this.connections_.length) { // Don't bother. return {connection: null, radius: maxRadius}; } // Stash the values of x and y from before the drag. - var baseY = conn.y; - var baseX = conn.x; + const baseY = conn.y; + const baseX = conn.x; conn.x = baseX + dxy.x; conn.y = baseY + dxy.y; @@ -246,14 +245,14 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, // calculateIndexForYPos_ finds an index for insertion, which is always // after any block with the same y index. We want to search both forward // and back, so search on both sides of the index. - var closestIndex = this.calculateIndexForYPos_(conn.y); + const closestIndex = this.calculateIndexForYPos_(conn.y); - var bestConnection = null; - var bestRadius = maxRadius; - var temp; + let bestConnection = null; + let bestRadius = maxRadius; + let temp; // Walk forward and back on the y axis looking for the closest x,y point. - var pointerMin = closestIndex - 1; + let pointerMin = closestIndex - 1; while (pointerMin >= 0 && this.isInYRange_(pointerMin, conn.y, maxRadius)) { temp = this.connections_[pointerMin]; if (this.connectionChecker_.canConnect(conn, temp, true, bestRadius)) { @@ -263,9 +262,9 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, pointerMin--; } - var pointerMax = closestIndex; + let pointerMax = closestIndex; while (pointerMax < this.connections_.length && - this.isInYRange_(pointerMax, conn.y, maxRadius)) { + this.isInYRange_(pointerMax, conn.y, maxRadius)) { temp = this.connections_[pointerMax]; if (this.connectionChecker_.canConnect(conn, temp, true, bestRadius)) { bestConnection = temp; @@ -284,20 +283,19 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, /** * Initialize a set of connection DBs for a workspace. - * @param {!Blockly.IConnectionChecker} checker The workspace's - * connection checker, used to decide if connections are valid during a drag. - * @return {!Array} Array of databases. + * @param {!IConnectionChecker} checker The workspace's + * connection checker, used to decide if connections are valid during a + * drag. + * @return {!Array} Array of databases. */ -Blockly.ConnectionDB.init = function(checker) { +ConnectionDB.init = function(checker) { // Create four databases, one for each connection type. - var dbList = []; - dbList[Blockly.connectionTypes.INPUT_VALUE] = - new Blockly.ConnectionDB(checker); - dbList[Blockly.connectionTypes.OUTPUT_VALUE] = - new Blockly.ConnectionDB(checker); - dbList[Blockly.connectionTypes.NEXT_STATEMENT] = - new Blockly.ConnectionDB(checker); - dbList[Blockly.connectionTypes.PREVIOUS_STATEMENT] = - new Blockly.ConnectionDB(checker); + const dbList = []; + dbList[connectionTypes.INPUT_VALUE] = new ConnectionDB(checker); + dbList[connectionTypes.OUTPUT_VALUE] = new ConnectionDB(checker); + dbList[connectionTypes.NEXT_STATEMENT] = new ConnectionDB(checker); + dbList[connectionTypes.PREVIOUS_STATEMENT] = new ConnectionDB(checker); return dbList; }; + +exports = ConnectionDB; diff --git a/tests/deps.js b/tests/deps.js index 455f21cc6..b4d64e1fa 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -21,7 +21,7 @@ goog.addDependency('../../core/comment.js', ['Blockly.Comment'], ['Blockly.Bubbl goog.addDependency('../../core/component_manager.js', ['Blockly.ComponentManager'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection.js', ['Blockly.Connection'], ['Blockly.Events', 'Blockly.Events.BlockMove', 'Blockly.IASTNodeLocationWithBlock', 'Blockly.Xml', 'Blockly.connectionTypes', 'Blockly.constants', 'Blockly.utils.deprecation']); goog.addDependency('../../core/connection_checker.js', ['Blockly.ConnectionChecker'], ['Blockly.Connection', 'Blockly.IConnectionChecker', 'Blockly.connectionTypes', 'Blockly.constants', 'Blockly.registry'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.RenderedConnection', 'Blockly.connectionTypes', 'Blockly.constants']); +goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.RenderedConnection', 'Blockly.connectionTypes', 'Blockly.constants'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection_types.js', ['Blockly.connectionTypes'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/constants.js', ['Blockly.constants'], ['Blockly.connectionTypes']); goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.constants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.userAgent']);