Move block database to workspace.

This commit is contained in:
Neil Fraser 2016-04-05 18:43:39 -07:00
parent 4e42a1b78e
commit 61b3fbfe11
5 changed files with 29 additions and 30 deletions

View file

@ -52,9 +52,9 @@ goog.require('goog.string');
*/
Blockly.Block = function(workspace, prototypeName, opt_id) {
/** @type {string} */
this.id = (opt_id && !Blockly.Block.getById(opt_id)) ?
this.id = (opt_id && !workspace.getBlockById(opt_id)) ?
opt_id : Blockly.genUid();
Blockly.Block.BlockDB_[this.id] = this;
workspace.blockDB_[this.id] = this;
/** @type {Blockly.Connection} */
this.outputConnection = null;
/** @type {Blockly.Connection} */
@ -178,6 +178,8 @@ Blockly.Block.prototype.dispose = function(healStack) {
// Remove this block from the workspace's list of top-most blocks.
if (this.workspace) {
this.workspace.removeTopBlock(this);
// Remove from block database.
delete this.workspace.blockDB_[this.id];
this.workspace = null;
}
@ -204,8 +206,6 @@ Blockly.Block.prototype.dispose = function(healStack) {
}
connections[i].dispose();
}
// Remove from block database.
delete Blockly.Block.BlockDB_[this.id];
Blockly.Events.enable();
};
@ -1294,18 +1294,3 @@ Blockly.Block.prototype.moveBy = function(dx, dy) {
event.recordNew();
Blockly.Events.fire(event);
};
/**
* Database of all blocks.
* @private
*/
Blockly.Block.BlockDB_ = Object.create(null);
/**
* Find the block with the specified ID.
* @param {string} id ID of block to find.
* @return {Blockly.Block} The sought after block or null if not found.
*/
Blockly.Block.getById = function(id) {
return Blockly.Block.BlockDB_[id] || null;
};

View file

@ -66,7 +66,7 @@ Blockly.ConnectionDB.prototype.addConnection = function(connection) {
* Find the given connection.
* Starts by doing a binary search to find the approximate location, then
* linearly searches nearby for the exact connection.
* @param {Blockly.Connection} conn The connection to find.
* @param {!Blockly.Connection} conn The connection to find.
* @return {number} The index of the connection, or -1 if the connection was
* not found.
*/
@ -105,7 +105,7 @@ Blockly.ConnectionDB.prototype.findConnection = function(conn) {
* Finds a candidate position for inserting this connection into the list.
* This will be in the correct y order but makes no guarantees about ordering in
* the x axis.
* @param {Blockly.Connection} connection The connection to insert.
* @param {!Blockly.Connection} connection The connection to insert.
* @return {number} The candidate index.
* @private
*/
@ -223,7 +223,7 @@ Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) {
/**
* Find the closest compatible connection to this connection.
* @param {Blockly.Connection} conn The connection searching for a compatible
* @param {!Blockly.Connection} conn The connection searching for a compatible
* mate.
* @param {number} maxRadius The maximum radius to another connection.
* @param {number} dx Horizontal offset between this connection's location

View file

@ -327,7 +327,7 @@ Blockly.Events.Create.prototype.run = function(forward) {
Blockly.Xml.domToWorkspace(xml, workspace);
} else {
for (var i = 0, id; id = this.ids[i]; i++) {
var block = Blockly.Block.getById(id);
var block = workspace.getBlockById(id);
if (block) {
block.dispose(false, true);
} else if (id == this.blockId) {
@ -375,9 +375,10 @@ Blockly.Events.Delete.prototype.toJson = function() {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.Delete.prototype.run = function(forward) {
var workspace = Blockly.Workspace.getById(this.workspaceId);
if (forward) {
for (var i = 0, id; id = this.ids[i]; i++) {
var block = Blockly.Block.getById(id);
var block = workspace.getBlockById(id);
if (block) {
block.dispose(false, true);
} else if (id == this.blockId) {
@ -386,7 +387,6 @@ Blockly.Events.Delete.prototype.run = function(forward) {
}
}
} else {
var workspace = Blockly.Workspace.getById(this.workspaceId);
var xml = goog.dom.createDom('xml');
xml.appendChild(this.oldXml);
Blockly.Xml.domToWorkspace(xml, workspace);
@ -445,7 +445,8 @@ Blockly.Events.Change.prototype.isNull = function() {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.Change.prototype.run = function(forward) {
var block = Blockly.Block.getById(this.blockId);
var workspace = Blockly.Workspace.getById(this.workspaceId);
var block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn("Can't change non-existant block: " + this.blockId);
return;
@ -552,7 +553,8 @@ Blockly.Events.Move.prototype.recordNew = function() {
* @private
*/
Blockly.Events.Move.prototype.currentLocation_ = function() {
var block = Blockly.Block.getById(this.blockId);
var workspace = Blockly.Workspace.getById(this.workspaceId);
var block = workspace.getBlockById(this.blockId);
var location = {};
var parent = block.getParent();
if (parent) {
@ -582,7 +584,8 @@ Blockly.Events.Move.prototype.isNull = function() {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.Move.prototype.run = function(forward) {
var block = Blockly.Block.getById(this.blockId);
var workspace = Blockly.Workspace.getById(this.workspaceId);
var block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn("Can't move non-existant block: " + this.blockId);
return;
@ -592,7 +595,7 @@ Blockly.Events.Move.prototype.run = function(forward) {
var coordinate = forward ? this.newCoordinate : this.oldCoordinate;
var parentBlock = null;
if (parentId) {
parentBlock = Blockly.Block.getById(parentId);
parentBlock = workspace.getBlockById(parentId);
if (!parentBlock) {
console.warn("Can't connect to non-existant block: " + parentId);
return;

View file

@ -51,6 +51,8 @@ Blockly.Workspace = function(opt_options) {
this.undoStack_ = [];
/** @type {!Array.<!Blockly.Events.Abstract>} */
this.redoStack_ = [];
/** @type {!Object} */
this.blockDB_ = Object.create(null);
};
/**
@ -289,6 +291,15 @@ Blockly.Workspace.prototype.fireChangeListener = function(event) {
}
};
/**
* Find the block on this workspace with the specified ID.
* @param {string} id ID of block to find.
* @return {Blockly.Block} The sought after block or null if not found.
*/
Blockly.Workspace.prototype.getBlockById = function(id) {
return this.blockDB_[id] || null;
};
/**
* Database of all workspaces.
* @private

View file

@ -438,7 +438,7 @@ Blockly.WorkspaceSvg.prototype.highlightBlock = function(id) {
}
var block = null;
if (id) {
block = Blockly.Block.getById(id);
block = this.getBlockById(id);
if (!block) {
return;
}