Define PaperScopeItem as a private base class for all classes that have lists and references in the PaperScope (Project, View, Tool), so they can share functionality (#initialize(), #activate(), #remove()), and add support for multiple tools. Closes #27

This commit is contained in:
Jürg Lehni 2011-08-02 10:08:08 +01:00
parent 2d5788540d
commit fe97b94340
6 changed files with 139 additions and 64 deletions

View file

@ -23,17 +23,17 @@
* center, both useful for constructing artwork that should appear centered on
* screen.
*/
var View = this.View = Base.extend(/** @lends View# */{
var View = this.View = PaperScopeItem.extend(/** @lends View# */{
_list: 'views',
_reference: 'view',
/**
* Creates a view object
* @param {HTMLCanvasElement|String} canvas The canvas object that this
* view should wrap, or the String id that represents it
*/
initialize: function(canvas) {
// Associate this view with the active paper scope.
this._scope = paper;
// Push it onto project.views and set index:
this._index = this._scope.views.push(this) - 1;
this.base();
// Handle canvas argument
var size;
if (typeof canvas === 'string')
@ -109,6 +109,32 @@ var View = this.View = Base.extend(/** @lends View# */{
this._scope._redrawNotified = false;
},
/**
* Makes this view the active one, meaning {@link PaperScope#view} will
* point to it.
*
* @name View#activate
* @function
*/
/**
* Removes thsi view from the {@link PaperScope#views} list and frees the
* associated canvas.
*/
remove: function() {
if (!this.base())
return false;
// Clear focus if removed view had it
if (View._focused == this)
View._focused = null;
delete View._views[this._id];
// Uninstall event handlers again for this view.
DomEvent.remove(this._canvas, this._events);
// Clearing _onFrame makes the frame handler stop automatically.
this._canvas = this._events = this._onFrame = null;
return true;
},
/**
* The underlying native canvas element.
*
@ -239,6 +265,12 @@ var View = this.View = Base.extend(/** @lends View# */{
this._inverse = null;
},
/**
* Draws the view.
*
* @name View#draw
* @function
*/
draw: function(checkRedraw) {
if (checkRedraw && !this._redrawNeeded)
return false;
@ -264,25 +296,6 @@ var View = this.View = Base.extend(/** @lends View# */{
return true;
},
activate: function() {
this._scope.view = this;
},
remove: function() {
if (this._index == null)
return false;
// Clear focus if removed view had it
if (View._focused == this)
View._focused = null;
delete View._views[this._id];
Base.splice(this._scope.views, null, this._index, 1);
// Uninstall event handlers again for this view.
DomEvent.remove(this._canvas, this._events);
// Clearing _onFrame makes the frame handler stop automatically.
this._scope = this._canvas = this._events = this._onFrame = null;
return true;
},
// TODO: getInvalidBounds
// TODO: invalidate(rect)
// TODO: style: artwork / preview / raster / opaque / ink