Merge Item#_remove() and Layer#_remove().

And add Project#_changed(), so it can be called through #_getOwner().
This commit is contained in:
Jürg Lehni 2016-01-16 14:21:05 +01:00
parent 1e7faaa95f
commit 2669d06642
5 changed files with 48 additions and 74 deletions

View file

@ -208,24 +208,8 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
// child triggers this notification on the parent.
Item._clearBoundsCache(this);
}
if (project) {
if (flags & /*#=*/ChangeFlag.APPEARANCE) {
project._needsUpdate = true;
}
// Have project keep track of changed items so they can be iterated.
// This can be used for example to update the SVG tree. Needs to be
// activated in Project
if (project._changes) {
var entry = project._changesById[this._id];
if (entry) {
entry.flags |= flags;
} else {
entry = { item: this, flags: flags };
project._changesById[this._id] = entry;
project._changes.push(entry);
}
}
}
if (project)
project._changed(flags, this);
// If this item is a symbol's definition, notify it of the change too
if (symbol)
symbol._changed(flags);
@ -1081,15 +1065,10 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
setMatrix: function() {
// Use Matrix#initialize to easily copy over values.
// NOTE: calling initialize() also calls #_changed() for us, through its
// call to #set() / #reset(), and this also handles _applyMatrix for us.
var matrix = this._matrix;
matrix.initialize.apply(matrix, arguments);
if (this._applyMatrix) {
// Directly apply the internal matrix. This will also call
// _changed() for us.
this.transform(null, true);
} else {
this._changed(/*#=*/Change.GEOMETRY);
}
},
/**
@ -2389,23 +2368,25 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* Removes the item from its parent's children list.
*/
_remove: function(notifySelf, notifyParent) {
var parent = this._parent;
if (parent) {
var owner = this._getOwner(),
project = this._project,
index = this._index;
if (owner && index != null) {
// Only required for layers, but not enough to merit an override.
if (project._activeLayer === this)
project._activeLayer = this.getNextSibling()
|| this.getPreviousSibling();
if (this._name)
this._removeNamed();
if (this._index != null)
Base.splice(parent._children, null, this._index, 1);
Base.splice(owner._children, null, index, 1);
this._installEvents(false);
// Notify self of the insertion change. We only need this
// notification if we're tracking changes for now.
if (notifySelf) {
var project = this._project;
if (project && project._changes)
this._changed(/*#=*/Change.INSERTION);
}
// Notify parent of changed children
if (notifySelf && project._changes)
this._changed(/*#=*/Change.INSERTION);
// Notify owner of changed children (this can be the project too).
if (notifyParent)
parent._changed(/*#=*/Change.CHILDREN);
owner._changed(/*#=*/Change.CHILDREN, this);
this._parent = null;
return true;
}

View file

@ -68,42 +68,12 @@ var Layer = Group.extend(/** @lends Layer# */{
/**
* Private helper to return the owner, either the parent, or the project
* for top-level layers.
* for top-level layers, if they are inserted in it.
*/
_getOwner: function() {
return this._parent || this._index != null && this._project;
},
/**
* Removes the layer from its project's layers list
* or its parent's children list.
*/
_remove: function _remove(notifySelf, notifyParent) {
if (this._parent)
return _remove.base.call(this, notifySelf, notifyParent);
if (this._index != null) {
var project = this._project;
if (project._activeLayer === this)
project._activeLayer = this.getNextSibling()
|| this.getPreviousSibling();
Base.splice(project._children, null, this._index, 1);
this._installEvents(false);
// Notify self of the insertion change. We only need this
// notification if we're tracking changes for now.
if (notifySelf && project._changes)
this._changed(/*#=*/Change.INSERTION);
// Notify parent of changed children
if (notifyParent) {
// TODO: project._changed(/*#=*/Change.LAYERS);
// Tell project we need a redraw. This is similar to _changed()
// mechanism.
project._needsUpdate = true;
}
return true;
}
return false;
},
isInserted: function isInserted() {
return this._parent ? isInserted.base.call(this) : this._index != null;
},

View file

@ -80,6 +80,32 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
return Base.serialize(this._children, options, true, dictionary);
},
/**
* Private notifier that is called whenever a change occurs in the project.
*
* @param {ChangeFlag} flags describes what exactly has changed
* @param {Item} item the item that has caused the change
*/
_changed: function(flags, item) {
if (flags & /*#=*/ChangeFlag.APPEARANCE) {
this._needsUpdate = true;
}
// Have project keep track of changed items so they can be iterated.
// This can be used for example to update the SVG tree. Needs to be
// activated in Project
var changes = this._changes;
if (changes && item) {
var changesById = this._changesById,
id = item._id,
entry = changesById[id];
if (entry) {
entry.flags |= flags;
} else {
changes.push(changesById[id] = { item: item, flags: flags });
}
}
},
/**
* Activates this project, so all newly created items will be placed
* in it.

View file

@ -93,13 +93,11 @@ var Symbol = Base.extend(/** @lends Symbol# */{
* @param {ChangeFlag} flags describes what exactly has changed
*/
_changed: function(flags) {
if (flags & /*#=*/ChangeFlag.GEOMETRY) {
if (flags & /*#=*/ChangeFlag.GEOMETRY)
// Clear cached bounds of all items that this symbol is linked to.
Item._clearBoundsCache(this);
}
if (flags & /*#=*/ChangeFlag.APPEARANCE) {
this.project._needsUpdate = true;
}
if (flags & /*#=*/ChangeFlag.APPEARANCE)
this.project._changed(flags);
},
/**

View file

@ -265,8 +265,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @param {ChangeFlag} flags describes what exactly has changed
*/
_changed: function(flags) {
if (flags & /*#=*/ChangeFlag.APPEARANCE)
this._project._needsUpdate = true;
this._project._changed(flats);
},
_transform: function(matrix) {