mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Create workspace id database.
This commit is contained in:
parent
3b3ef79fbd
commit
23251b5187
3 changed files with 37 additions and 1 deletions
|
@ -36,6 +36,9 @@ goog.require('goog.math');
|
|||
* @constructor
|
||||
*/
|
||||
Blockly.Workspace = function(opt_options) {
|
||||
/** @type {string} */
|
||||
this.id = Blockly.genUid();
|
||||
Blockly.Workspace.WorkspaceDB_[this.id] = this;
|
||||
/** @type {!Object} */
|
||||
this.options = opt_options || {};
|
||||
/** @type {boolean} */
|
||||
|
@ -56,6 +59,8 @@ Blockly.Workspace.prototype.rendered = false;
|
|||
*/
|
||||
Blockly.Workspace.prototype.dispose = function() {
|
||||
this.clear();
|
||||
// Remove from workspace database.
|
||||
delete Blockly.Workspace.WorkspaceDB_[this.id];
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -195,5 +200,20 @@ Blockly.Workspace.prototype.fireChangeEvent = function() {
|
|||
// NOP.
|
||||
};
|
||||
|
||||
/**
|
||||
* Database of all workspaces.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Workspace.WorkspaceDB_ = Object.create(null);
|
||||
|
||||
/**
|
||||
* Find the workspace with the specified ID.
|
||||
* @param {string} id ID of workspace to find.
|
||||
* @return {Blockly.Workspace} The sought after workspace or null if not found.
|
||||
*/
|
||||
Blockly.Workspace.getById = function(id) {
|
||||
return Blockly.Workspace.WorkspaceDB_[id] || null;
|
||||
};
|
||||
|
||||
// Export symbols that would otherwise be renamed by Closure compiler.
|
||||
Blockly.Workspace.prototype['clear'] = Blockly.Workspace.prototype.clear;
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
function test_getById() {
|
||||
function test_getBlockById() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
var blockA = workspace.newBlock('');
|
||||
var blockB = workspace.newBlock('');
|
||||
|
|
|
@ -66,3 +66,19 @@ function test_maxBlocksWorkspace() {
|
|||
workspace.clear();
|
||||
assertEquals('Cleared capacity.', 0, workspace.remainingCapacity());
|
||||
}
|
||||
|
||||
function test_getWorkspaceById() {
|
||||
var workspaceA = new Blockly.Workspace();
|
||||
var workspaceB = new Blockly.Workspace();
|
||||
assertEquals('Find workspaceA.', workspaceA,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('Find workspaceB.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
assertEquals('No workspace found.', null,
|
||||
Blockly.Workspace.getById('I do not exist.'));
|
||||
workspaceA.dispose();
|
||||
assertEquals('Can\'t find workspaceA.', null,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('WorkspaceB exists.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue