mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 06:31:15 +08:00
added hash cleanup logic when nodes are moved from a container. Also added unit tests
This commit is contained in:
parent
3c17e59eb0
commit
2035d188c8
39
dist/kinetic-core.js
vendored
39
dist/kinetic-core.js
vendored
@ -872,6 +872,18 @@ Kinetic.Container.prototype = {
|
||||
*/
|
||||
_remove: function(child) {
|
||||
if(this.children[child.index]._id == child._id) {
|
||||
var stage = this.getStage();
|
||||
stage._removeId(child);
|
||||
stage._removeName(child);
|
||||
|
||||
var go = Kinetic.GlobalObject;
|
||||
for(var n = 0; n < go.tempNodes.length; n++) {
|
||||
var node = go.tempNodes[n];
|
||||
if(node._id === child._id) {
|
||||
go.tempNodes.splice(n, 1);
|
||||
n = go.tempNodes.length;
|
||||
}
|
||||
}
|
||||
|
||||
this.children.splice(child.index, 1);
|
||||
this._setChildrenIndices();
|
||||
@ -912,6 +924,13 @@ Kinetic.Container.prototype = {
|
||||
else {
|
||||
stage._addId(child);
|
||||
stage._addName(child);
|
||||
|
||||
/*
|
||||
* pull in other nodes that are now linked
|
||||
* to a stage
|
||||
*/
|
||||
var go = Kinetic.GlobalObject;
|
||||
go._pullNodes(stage);
|
||||
}
|
||||
},
|
||||
/**
|
||||
@ -1264,10 +1283,6 @@ Kinetic.Stage.prototype = {
|
||||
layer.canvas.height = this.attrs.height;
|
||||
this._add(layer);
|
||||
|
||||
// populate stage node ids and names
|
||||
var go = Kinetic.GlobalObject;
|
||||
go._pullNodes(this);
|
||||
|
||||
// draw layer and append canvas to container
|
||||
layer.draw();
|
||||
this.content.appendChild(layer.canvas);
|
||||
@ -1812,7 +1827,9 @@ Kinetic.Stage.prototype = {
|
||||
}
|
||||
},
|
||||
_removeId: function(node) {
|
||||
|
||||
if(node.attrs.id !== undefined) {
|
||||
this.ids[node.attrs.id] = undefined;
|
||||
}
|
||||
},
|
||||
_addName: function(node) {
|
||||
var name = node.attrs.name;
|
||||
@ -1824,7 +1841,17 @@ Kinetic.Stage.prototype = {
|
||||
}
|
||||
},
|
||||
_removeName: function(node) {
|
||||
|
||||
if(node.attrs.name !== undefined) {
|
||||
var nodes = this.names[node.attrs.name];
|
||||
if(nodes !== undefined) {
|
||||
for(var n = 0; n < nodes.length; n++) {
|
||||
var no = nodes[n];
|
||||
if(no._id === node._id) {
|
||||
nodes.splice(n, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// Extend Container and Node
|
||||
|
4
dist/kinetic-core.min.js
vendored
4
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@ -33,6 +33,18 @@ Kinetic.Container.prototype = {
|
||||
*/
|
||||
_remove: function(child) {
|
||||
if(this.children[child.index]._id == child._id) {
|
||||
var stage = this.getStage();
|
||||
stage._removeId(child);
|
||||
stage._removeName(child);
|
||||
|
||||
var go = Kinetic.GlobalObject;
|
||||
for(var n = 0; n < go.tempNodes.length; n++) {
|
||||
var node = go.tempNodes[n];
|
||||
if(node._id === child._id) {
|
||||
go.tempNodes.splice(n, 1);
|
||||
n = go.tempNodes.length;
|
||||
}
|
||||
}
|
||||
|
||||
this.children.splice(child.index, 1);
|
||||
this._setChildrenIndices();
|
||||
@ -73,6 +85,13 @@ Kinetic.Container.prototype = {
|
||||
else {
|
||||
stage._addId(child);
|
||||
stage._addName(child);
|
||||
|
||||
/*
|
||||
* pull in other nodes that are now linked
|
||||
* to a stage
|
||||
*/
|
||||
var go = Kinetic.GlobalObject;
|
||||
go._pullNodes(stage);
|
||||
}
|
||||
},
|
||||
/**
|
||||
|
20
src/Stage.js
20
src/Stage.js
@ -319,10 +319,6 @@ Kinetic.Stage.prototype = {
|
||||
layer.canvas.height = this.attrs.height;
|
||||
this._add(layer);
|
||||
|
||||
// populate stage node ids and names
|
||||
var go = Kinetic.GlobalObject;
|
||||
go._pullNodes(this);
|
||||
|
||||
// draw layer and append canvas to container
|
||||
layer.draw();
|
||||
this.content.appendChild(layer.canvas);
|
||||
@ -867,7 +863,9 @@ Kinetic.Stage.prototype = {
|
||||
}
|
||||
},
|
||||
_removeId: function(node) {
|
||||
|
||||
if(node.attrs.id !== undefined) {
|
||||
this.ids[node.attrs.id] = undefined;
|
||||
}
|
||||
},
|
||||
_addName: function(node) {
|
||||
var name = node.attrs.name;
|
||||
@ -879,7 +877,17 @@ Kinetic.Stage.prototype = {
|
||||
}
|
||||
},
|
||||
_removeName: function(node) {
|
||||
|
||||
if(node.attrs.name !== undefined) {
|
||||
var nodes = this.names[node.attrs.name];
|
||||
if(nodes !== undefined) {
|
||||
for(var n = 0; n < nodes.length; n++) {
|
||||
var no = nodes[n];
|
||||
if(no._id === node._id) {
|
||||
nodes.splice(n, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// Extend Container and Node
|
||||
|
@ -328,6 +328,59 @@ Test.prototype.tests = {
|
||||
test(node.shapeType === 'Circle', 'shape type should be circle');
|
||||
test(nodes[0].shapeType === 'Rect', 'shape type should be rect');
|
||||
|
||||
},
|
||||
'STAGE - remove shape by id or name': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
height: 200
|
||||
});
|
||||
var layer = new Kinetic.Layer();
|
||||
var circle = new Kinetic.Circle({
|
||||
x: stage.getWidth() / 2,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'green',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4,
|
||||
id: 'myCircle'
|
||||
});
|
||||
|
||||
var rect = new Kinetic.Rect({
|
||||
x: 300,
|
||||
y: 100,
|
||||
width: 100,
|
||||
height: 50,
|
||||
fill: 'purple',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4,
|
||||
name: 'myRect'
|
||||
});
|
||||
|
||||
layer.add(circle);
|
||||
layer.add(rect);
|
||||
stage.add(layer);
|
||||
|
||||
var node = stage.get('#myCircle');
|
||||
var nodes = stage.get('.myRect');
|
||||
|
||||
test(stage.ids.myCircle._id === circle._id, 'circle not in ids hash');
|
||||
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
|
||||
|
||||
var node = stage.get('#myCircle');
|
||||
var parent = node.getParent();
|
||||
|
||||
parent.remove(node);
|
||||
|
||||
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
||||
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
|
||||
|
||||
var parent = nodes[0].getParent();
|
||||
parent.remove(nodes[0]);
|
||||
|
||||
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
||||
test(stage.names.myRect[0] === undefined, 'rect still in hash');
|
||||
|
||||
},
|
||||
'STAGE - remove layer with shape': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
|
Loading…
Reference in New Issue
Block a user