Fix Item#isDescendant / Item#isAncestor, implement Item#isGroupedWith and add tests.

This commit is contained in:
Jonathan Puckey 2011-02-24 18:09:48 +01:00
parent 6cea290dd8
commit 014d1053a7
2 changed files with 44 additions and 3 deletions

View file

@ -411,7 +411,7 @@ Item = Base.extend({
* @return {@true if it is inside the specified item} * @return {@true if it is inside the specified item}
*/ */
isDescendant: function(item) { isDescendant: function(item) {
var parent = this; var parent = this.parent;
while(parent) { while(parent) {
if (parent == item) if (parent == item)
return true; return true;
@ -436,7 +436,7 @@ Item = Base.extend({
* @return {@true if the item is an ancestor of the specified item} * @return {@true if the item is an ancestor of the specified item}
*/ */
isAncestor: function(item) { isAncestor: function(item) {
var parent = item; var parent = item.parent;
while(parent) { while(parent) {
if (parent == this) if (parent == this)
return true; return true;
@ -451,7 +451,21 @@ Item = Base.extend({
* @param item * @param item
* @return {@true if the items are grouped together} * @return {@true if the items are grouped together}
*/ */
// TODO: isGroupedWith isGroupedWith: function(item) {
var parent = this.parent;
while(parent) {
// Find group parents. Check for parent.parent, since don't want
// top level layers, because they also inherit from Group
console.log(parent.parent);
if(parent.parent
&& (parent instanceof Group || parent instanceof CompoundPath)
&& item.isDescendant(parent))
return true;
// Keep walking up otherwise
parent = parent.parent
}
return false;
},
getBounds: function() { getBounds: function() {
// TODO: Implement for items other than paths // TODO: Implement for items other than paths

View file

@ -89,6 +89,33 @@ test('isDescendant(item) / isAncestor(item)', function() {
equals(path.isAncestor(doc.activeLayer), false); equals(path.isAncestor(doc.activeLayer), false);
equals(doc.activeLayer.isAncestor(path), true); equals(doc.activeLayer.isAncestor(path), true);
// an item can't be its own descendant:
equals(doc.activeLayer.isDescendant(doc.activeLayer), false);
// an item can't be its own ancestor:
equals(doc.activeLayer.isAncestor(doc.activeLayer), false);
});
test('isGroupedWith', function() {
var doc = new Doc();
var path = new Path();
var secondPath = new Path();
var group = new Group([path]);
var secondGroup = new Group([secondPath]);
equals(path.isGroupedWith(secondPath), false);
secondGroup.appendTop(path);
equals(path.isGroupedWith(secondPath), true);
equals(path.isGroupedWith(group), false);
equals(path.isDescendant(secondGroup), true);
equals(secondGroup.isDescendant(path), false);
equals(secondGroup.isDescendant(secondGroup), false);
equals(path.isGroupedWith(secondGroup), false);
Paper.document.activeLayer.appendTop(path);
equals(path.isGroupedWith(secondPath), false);
Paper.document.activeLayer.appendTop(secondPath);
equals(path.isGroupedWith(secondPath), false);
}); });
test('getPreviousSibling() / getNextSibling()', function() { test('getPreviousSibling() / getNextSibling()', function() {