diff --git a/src/item/Layer.js b/src/item/Layer.js index 61b08807..d617e49a 100644 --- a/src/item/Layer.js +++ b/src/item/Layer.js @@ -86,8 +86,8 @@ var Layer = Group.extend(/** @lends Layer# */{ return _remove.base.call(this, notify); if (this._index != null) { var project = this._project; - if (project.activeLayer === this) - project.activeLayer = this.getNextSibling() + if (project._activeLayer === this) + project._activeLayer = this.getNextSibling() || this.getPreviousSibling(); Base.splice(project.layers, null, this._index, 1); this._installEvents(false); @@ -124,7 +124,7 @@ var Layer = Group.extend(/** @lends Layer# */{ * console.log(project.activeLayer == firstLayer); // true */ activate: function() { - this._project.activeLayer = this; + this._project._activeLayer = this; }, // Private helper for #insertAbove() / #insertBelow() diff --git a/src/project/Project.js b/src/project/Project.js index 71399733..0f4d473d 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -53,9 +53,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ // constructors. PaperScopeItem.call(this, true); this.layers = []; + this._activeLayer = null; this.symbols = []; this._currentStyle = new Style(null, null, this); - this.activeLayer = new Layer(); // If no view is provided, we create a 1x1 px canvas view just so we // have something to do size calculations with. // (e.g. PointText#_getBounds) @@ -98,15 +98,12 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ }, /** - * Checks whether the project has any content or not. Note that since - * projects by default are created with one empty layer, this returns also - * {@code true} if that layer exists but is itself empty. + * Checks whether the project has any content or not. * * @return Boolean */ isEmpty: function() { - return this.layers.length <= 1 - && (!this.activeLayer || this.activeLayer.isEmpty()); + return this.layers.length === 0; }, /** @@ -183,11 +180,11 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ if (child instanceof Layer) { Base.splice(this.layers, [child]); // Also activate this layer if there was none before - if (!this.activeLayer) - this.activeLayer = child; + if (!this._activeLayer) + this._activeLayer = child; } else if (child instanceof Item) { // Anything else than layers needs to be added to a layer first - (this.activeLayer + (this._activeLayer // NOTE: If there is no layer and this project is not the active // one, passing insert: false and calling addChild on the // project will handle it correctly. @@ -772,7 +769,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ this.activate(); // Provide the activeLayer as a possible target for layers, but only if // it's empty. - var layer = this.activeLayer; + var layer = this._activeLayer; return Base.importJSON(json, layer && layer.isEmpty() && layer); }, @@ -815,7 +812,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ */ /** - * {@grouptitle Project Hierarchy} + * {@grouptitle Project Content} * * The layers contained within the project. * @@ -827,9 +824,12 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ * The layer which is currently active. New items will be created on this * layer by default. * - * @name Project#activeLayer * @type Layer + * @bean */ + getActiveLayer: function() { + return this._activeLayer || new Layer({ project: this }); + }, /** * The symbols contained within the project. diff --git a/test/tests/Item_Getting.js b/test/tests/Item_Getting.js index f32d5852..08c0a53b 100644 --- a/test/tests/Item_Getting.js +++ b/test/tests/Item_Getting.js @@ -45,18 +45,20 @@ test('Item#matches()', function() { }); test('Project#getItems()', function() { + var layer = new Layer(); + equals(function() { var matches = paper.project.getItems({ type: 'layer' }); - return matches.length == 1 && matches[0] == paper.project.activeLayer; + return matches.length == 1 && matches[0] == layer; }, true); equals(function() { var matches = paper.project.getItems({ - 'class': Item + class: Item }); - return matches.length == 1 && matches[0] == paper.project.activeLayer; + return matches.length == 1 && matches[0] == layer; }, true); var path = new Path(); @@ -69,12 +71,11 @@ test('Project#getItems()', function() { equals(function() { var matches = paper.project.getItems({ - 'constructor': Path + constructor: Path }); return matches.length == 1 && matches[0] === path; }, true); - var group = new Group(); equals(function() { var matches = paper.project.getItems({ diff --git a/test/tests/Project.js b/test/tests/Project.js index a42ba560..32a9e239 100644 --- a/test/tests/Project.js +++ b/test/tests/Project.js @@ -29,6 +29,7 @@ test('clear()', function() { var project = new Project(); new Layer(); new Layer(); + new Layer(); equals(project.layers.length, 3); project.clear(); equals(project.layers.length, 0);