mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
got really tired of doing node.getParent().remove(node). To remove a node, you now just do node.remove().
I realize that this doesn't follow the JavaScript pattern of parent.remove(child), and that from an OO perspective, perhaps nodes shouldn't have the ability to destroy themselves. But, from a practical standpoint, it's a heck of a lot more convenient to just use .remove() when you want to remove something.
This commit is contained in:
parent
900f02f912
commit
0a8f0ddb74
74
dist/kinetic-core.js
vendored
74
dist/kinetic-core.js
vendored
@ -3,7 +3,7 @@
|
|||||||
* http://www.kineticjs.com/
|
* http://www.kineticjs.com/
|
||||||
* Copyright 2012, Eric Rowell
|
* Copyright 2012, Eric Rowell
|
||||||
* Licensed under the MIT or GPL Version 2 licenses.
|
* Licensed under the MIT or GPL Version 2 licenses.
|
||||||
* Date: Sep 25 2012
|
* Date: Sep 26 2012
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 - 2012 by Eric Rowell
|
* Copyright (C) 2011 - 2012 by Eric Rowell
|
||||||
*
|
*
|
||||||
@ -1384,6 +1384,40 @@ Kinetic.Node.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* remove child from container
|
||||||
|
* @name remove
|
||||||
|
* @methodOf Kinetic.Container.prototype
|
||||||
|
* @param {Node} child
|
||||||
|
*/
|
||||||
|
remove: function() {
|
||||||
|
var parent = this.getParent();
|
||||||
|
if(parent && this.index !== undefined && parent.children[this.index]._id == this._id) {
|
||||||
|
var stage = parent.getStage();
|
||||||
|
/*
|
||||||
|
* remove event listeners and references to the node
|
||||||
|
* from the ids and names hashes
|
||||||
|
*/
|
||||||
|
if(stage) {
|
||||||
|
stage._removeId(this.getId());
|
||||||
|
stage._removeName(this.getName(), this._id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Kinetic.Global._removeTempNode(this);
|
||||||
|
parent.children.splice(this.index, 1);
|
||||||
|
parent._setChildrenIndices();
|
||||||
|
|
||||||
|
// remove children
|
||||||
|
while(this.children && this.children.length > 0) {
|
||||||
|
this.children[0].remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// do extra stuff if needed
|
||||||
|
if(this._remove !== undefined) {
|
||||||
|
this._remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* get attrs
|
* get attrs
|
||||||
* @name getAttrs
|
* @name getAttrs
|
||||||
@ -2444,7 +2478,7 @@ Kinetic.Container.prototype = {
|
|||||||
*/
|
*/
|
||||||
removeChildren: function() {
|
removeChildren: function() {
|
||||||
while(this.children.length > 0) {
|
while(this.children.length > 0) {
|
||||||
this.remove(this.children[0]);
|
this.children[0].remove();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@ -2484,42 +2518,6 @@ Kinetic.Container.prototype = {
|
|||||||
// chainable
|
// chainable
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* remove child from container
|
|
||||||
* @name remove
|
|
||||||
* @methodOf Kinetic.Container.prototype
|
|
||||||
* @param {Node} child
|
|
||||||
*/
|
|
||||||
remove: function(child) {
|
|
||||||
if(child && child.index !== undefined && this.children[child.index]._id == child._id) {
|
|
||||||
var stage = this.getStage();
|
|
||||||
/*
|
|
||||||
* remove event listeners and references to the node
|
|
||||||
* from the ids and names hashes
|
|
||||||
*/
|
|
||||||
if(stage) {
|
|
||||||
stage._removeId(child.getId());
|
|
||||||
stage._removeName(child.getName(), child._id);
|
|
||||||
}
|
|
||||||
|
|
||||||
Kinetic.Global._removeTempNode(child);
|
|
||||||
this.children.splice(child.index, 1);
|
|
||||||
this._setChildrenIndices();
|
|
||||||
|
|
||||||
// remove children
|
|
||||||
while(child.children && child.children.length > 0) {
|
|
||||||
child.remove(child.children[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// do extra stuff if needed
|
|
||||||
if(child._remove !== undefined) {
|
|
||||||
child._remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// chainable
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
/**
|
/**
|
||||||
* return an array of nodes that match the selector. Use '#' for id selections
|
* return an array of nodes that match the selector. Use '#' for id selections
|
||||||
* and '.' for name selections
|
* and '.' for name selections
|
||||||
|
8
dist/kinetic-core.min.js
vendored
8
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@ -54,7 +54,7 @@ Kinetic.Container.prototype = {
|
|||||||
*/
|
*/
|
||||||
removeChildren: function() {
|
removeChildren: function() {
|
||||||
while(this.children.length > 0) {
|
while(this.children.length > 0) {
|
||||||
this.remove(this.children[0]);
|
this.children[0].remove();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@ -94,42 +94,6 @@ Kinetic.Container.prototype = {
|
|||||||
// chainable
|
// chainable
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* remove child from container
|
|
||||||
* @name remove
|
|
||||||
* @methodOf Kinetic.Container.prototype
|
|
||||||
* @param {Node} child
|
|
||||||
*/
|
|
||||||
remove: function(child) {
|
|
||||||
if(child && child.index !== undefined && this.children[child.index]._id == child._id) {
|
|
||||||
var stage = this.getStage();
|
|
||||||
/*
|
|
||||||
* remove event listeners and references to the node
|
|
||||||
* from the ids and names hashes
|
|
||||||
*/
|
|
||||||
if(stage) {
|
|
||||||
stage._removeId(child.getId());
|
|
||||||
stage._removeName(child.getName(), child._id);
|
|
||||||
}
|
|
||||||
|
|
||||||
Kinetic.Global._removeTempNode(child);
|
|
||||||
this.children.splice(child.index, 1);
|
|
||||||
this._setChildrenIndices();
|
|
||||||
|
|
||||||
// remove children
|
|
||||||
while(child.children && child.children.length > 0) {
|
|
||||||
child.remove(child.children[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// do extra stuff if needed
|
|
||||||
if(child._remove !== undefined) {
|
|
||||||
child._remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// chainable
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
/**
|
/**
|
||||||
* return an array of nodes that match the selector. Use '#' for id selections
|
* return an array of nodes that match the selector. Use '#' for id selections
|
||||||
* and '.' for name selections
|
* and '.' for name selections
|
||||||
|
34
src/Node.js
34
src/Node.js
@ -161,6 +161,40 @@ Kinetic.Node.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* remove child from container
|
||||||
|
* @name remove
|
||||||
|
* @methodOf Kinetic.Container.prototype
|
||||||
|
* @param {Node} child
|
||||||
|
*/
|
||||||
|
remove: function() {
|
||||||
|
var parent = this.getParent();
|
||||||
|
if(parent && this.index !== undefined && parent.children[this.index]._id == this._id) {
|
||||||
|
var stage = parent.getStage();
|
||||||
|
/*
|
||||||
|
* remove event listeners and references to the node
|
||||||
|
* from the ids and names hashes
|
||||||
|
*/
|
||||||
|
if(stage) {
|
||||||
|
stage._removeId(this.getId());
|
||||||
|
stage._removeName(this.getName(), this._id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Kinetic.Global._removeTempNode(this);
|
||||||
|
parent.children.splice(this.index, 1);
|
||||||
|
parent._setChildrenIndices();
|
||||||
|
|
||||||
|
// remove children
|
||||||
|
while(this.children && this.children.length > 0) {
|
||||||
|
this.children[0].remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// do extra stuff if needed
|
||||||
|
if(this._remove !== undefined) {
|
||||||
|
this._remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* get attrs
|
* get attrs
|
||||||
* @name getAttrs
|
* @name getAttrs
|
||||||
|
@ -604,14 +604,14 @@ Test.prototype.tests = {
|
|||||||
test(Kinetic.Global.shapes[circleColorKey]._id === circle._id, 'circle color key should be in shapes hash');
|
test(Kinetic.Global.shapes[circleColorKey]._id === circle._id, 'circle color key should be in shapes hash');
|
||||||
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');
|
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');
|
||||||
|
|
||||||
layer.remove(circle);
|
circle.remove();
|
||||||
|
|
||||||
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
||||||
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
|
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
|
||||||
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
|
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
|
||||||
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');
|
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');
|
||||||
|
|
||||||
layer.remove(rect);
|
rect.remove();
|
||||||
|
|
||||||
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
test(stage.ids.myCircle === undefined, 'circle still in hash');
|
||||||
test(stage.names.myRect === undefined, 'rect still in hash');
|
test(stage.names.myRect === undefined, 'rect still in hash');
|
||||||
@ -846,7 +846,7 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
test(go.tempNodes[circle._id].attrs.id === 'myCircle', 'circle should be in temp nodes');
|
test(go.tempNodes[circle._id].attrs.id === 'myCircle', 'circle should be in temp nodes');
|
||||||
|
|
||||||
layer.remove(circle);
|
circle.remove();
|
||||||
|
|
||||||
test(go.tempNodes[circle._id] === undefined, 'circle shouldn\'t be in the temp nodes hash');
|
test(go.tempNodes[circle._id] === undefined, 'circle shouldn\'t be in the temp nodes hash');
|
||||||
|
|
||||||
@ -877,7 +877,7 @@ Test.prototype.tests = {
|
|||||||
test(stage.get('.myLayer')[0] !== undefined, 'layer should exist');
|
test(stage.get('.myLayer')[0] !== undefined, 'layer should exist');
|
||||||
test(stage.get('.myCircle')[0] !== undefined, 'circle should exist');
|
test(stage.get('.myCircle')[0] !== undefined, 'circle should exist');
|
||||||
|
|
||||||
stage.remove(layer);
|
layer.remove();
|
||||||
|
|
||||||
test(stage.children.length === 0, 'stage should have 0 children');
|
test(stage.children.length === 0, 'stage should have 0 children');
|
||||||
test(stage.get('.myLayer')[0] === undefined, 'layer should not exist');
|
test(stage.get('.myLayer')[0] === undefined, 'layer should not exist');
|
||||||
@ -893,7 +893,7 @@ Test.prototype.tests = {
|
|||||||
});
|
});
|
||||||
var layer = new Kinetic.Layer();
|
var layer = new Kinetic.Layer();
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
stage.remove(layer);
|
layer.remove();
|
||||||
|
|
||||||
test(stage.children.length === 0, 'stage should have 0 children');
|
test(stage.children.length === 0, 'stage should have 0 children');
|
||||||
},
|
},
|
||||||
@ -926,8 +926,8 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
test(layer.getChildren().length === 2, 'layer should have two children');
|
test(layer.getChildren().length === 2, 'layer should have two children');
|
||||||
|
|
||||||
layer.remove(shape1);
|
shape1.remove();
|
||||||
layer.remove(shape1);
|
shape1.remove();
|
||||||
|
|
||||||
test(layer.getChildren().length === 1, 'layer should have two children');
|
test(layer.getChildren().length === 1, 'layer should have two children');
|
||||||
|
|
||||||
@ -966,8 +966,8 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
test(stage.getChildren().length === 2, 'stage should have two children');
|
test(stage.getChildren().length === 2, 'stage should have two children');
|
||||||
|
|
||||||
stage.remove(layer1);
|
layer1.remove();
|
||||||
stage.remove(layer1);
|
layer1.remove();
|
||||||
|
|
||||||
test(stage.getChildren().length === 1, 'stage should have one child');
|
test(stage.getChildren().length === 1, 'stage should have one child');
|
||||||
|
|
||||||
@ -3020,7 +3020,7 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
test(layer.children.length === 1, 'layer should have 1 children');
|
test(layer.children.length === 1, 'layer should have 1 children');
|
||||||
|
|
||||||
layer.remove(circle);
|
circle.remove();
|
||||||
|
|
||||||
test(layer.children.length === 0, 'layer should have 0 children');
|
test(layer.children.length === 0, 'layer should have 0 children');
|
||||||
//test(layer.getChild('myCircle') === undefined, 'shape should be null');
|
//test(layer.getChild('myCircle') === undefined, 'shape should be null');
|
||||||
|
Loading…
Reference in New Issue
Block a user