Skip to content

Commit 719ae23

Browse files
committed
Remove global views list, link View to Project and allow projects to only have one view.
1 parent ea689fa commit 719ae23

4 files changed

Lines changed: 93 additions & 118 deletions

File tree

src/core/PaperScope.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
4949
// Whenever a PaperScope is created, it automatically becomes the active
5050
// one.
5151
paper = this;
52-
this.view = null;
53-
this.views = [];
5452
this.project = null;
5553
this.projects = [];
5654
this.tool = null;
@@ -87,16 +85,13 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
8785
*/
8886

8987
/**
90-
* The active view of the active project.
88+
* The reference to the active project's view.
9189
* @name PaperScope#view
9290
* @type View
9391
*/
94-
95-
/**
96-
* The list of view of the active project.
97-
* @name PaperScope#views
98-
* @type View[]
99-
*/
92+
getView: function() {
93+
return this.project.view;
94+
},
10095

10196
/**
10297
* The reference to the active tool.
@@ -159,19 +154,14 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
159154
// Make sure this is the active scope, so the created project and view
160155
// are automatically associated with it.
161156
paper = this;
162-
this.project = new Project();
163-
// Create a view for the canvas.
164-
if (canvas)
165-
this.view = new View(canvas);
157+
this.project = new Project(canvas);
166158
},
167159

168160
clear: function() {
169161
// Remove all projects, views and tools.
162+
// This also removes the installed event handlers.
170163
for (var i = this.projects.length - 1; i >= 0; i--)
171164
this.projects[i].remove();
172-
// This also removes the installed event handlers.
173-
for (var i = this.views.length - 1; i >= 0; i--)
174-
this.views[i].remove();
175165
for (var i = this.tools.length - 1; i >= 0; i--)
176166
this.tools[i].remove();
177167
},
@@ -181,15 +171,6 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
181171
delete PaperScope._scopes[this._id];
182172
},
183173

184-
_needsRedraw: function() {
185-
// Make sure we're not looping through the view list each time...
186-
if (!this._redrawNotified) {
187-
for (var i = this.views.length - 1; i >= 0; i--)
188-
this.views[i]._redrawNeeded = true;
189-
this._redrawNotified = true;
190-
}
191-
},
192-
193174
statics: /** @lends PaperScope */{
194175
_scopes: {},
195176
_id: 0,
@@ -205,16 +186,6 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
205186
if (typeof id === 'object')
206187
id = id.getAttribute('id');
207188
return this._scopes[id] || null;
208-
},
209-
210-
/**
211-
* Iterates over all active scopes and calls the passed iterator
212-
* function for each of them.
213-
*
214-
* @param iter the iterator function.
215-
*/
216-
each: function(iter) {
217-
Base.each(this._scopes, iter);
218189
}
219190
}
220191
});

src/docs/global.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,11 @@
3434
*/
3535

3636
/**
37-
* The active view of the active project.
37+
* The reference to the active project's view.
3838
* @name view
3939
* @type View
4040
*/
4141

42-
/**
43-
* The list of view of the active project.
44-
* @name views
45-
* @type View[]
46-
*/
47-
4842
/**
4943
* The reference to the active tool.
5044
* @name tool

src/project/Project.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,30 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
4444
*
4545
* When working with PaperScript, a project is automatically created for us
4646
* and the {@link PaperScope#project} variable points to it.
47+
*
48+
* @param {View|HTMLCanvasElement} view Either a view object or an HTML
49+
* Canvas element that should be wrapped in a newly created view.
4750
*/
48-
initialize: function() {
49-
// Activate straight away so paper.project is set, as required by
50-
// Layer and DoumentView constructors.
51+
initialize: function(view) {
52+
// Activate straight away by passing true to base(), so paper.project is
53+
// set, as required by Layer and DoumentView constructors.
5154
this.base(true);
5255
this._currentStyle = new PathStyle();
5356
this._selectedItems = {};
5457
this._selectedItemCount = 0;
5558
this.layers = [];
5659
this.symbols = [];
5760
this.activeLayer = new Layer();
61+
if (view)
62+
this.view = view instanceof View ? view : View.create(view);
5863
// Change tracking, not in use for now. Activate once required:
5964
// this._changes = [];
6065
// this._changesById = {};
6166
},
6267

6368
_needsRedraw: function() {
64-
if (this._scope)
65-
this._scope._needsRedraw();
69+
if (this.view)
70+
this.view._redrawNeeded = true;
6671
},
6772

6873
/**
@@ -74,10 +79,21 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
7479
*/
7580

7681
/**
77-
* Removes this project from the {@link PaperScope#projects} list.
78-
*
79-
* @name Project#remove
80-
* @function
82+
* Removes this project from the {@link PaperScope#projects} list, and also
83+
* removes its view, if one was defined.
84+
*/
85+
remove: function() {
86+
if (!this.base())
87+
return false;
88+
if (this.view)
89+
this.view.remove();
90+
return true;
91+
},
92+
93+
/**
94+
* The reference to the project's view.
95+
* @name Project#view
96+
* @type View
8197
*/
8298

8399
/**

0 commit comments

Comments
 (0)