From 014d1053a765fb70ba94c9b3789a278e34edd0f0 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Thu, 24 Feb 2011 18:09:48 +0100 Subject: [PATCH] Fix Item#isDescendant / Item#isAncestor, implement Item#isGroupedWith and add tests. --- src/item/Item.js | 20 +++++++++++++++++--- test/tests/item.js | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index fbe2aa66..4bbcf118 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -411,7 +411,7 @@ Item = Base.extend({ * @return {@true if it is inside the specified item} */ isDescendant: function(item) { - var parent = this; + var parent = this.parent; while(parent) { if (parent == item) return true; @@ -436,7 +436,7 @@ Item = Base.extend({ * @return {@true if the item is an ancestor of the specified item} */ isAncestor: function(item) { - var parent = item; + var parent = item.parent; while(parent) { if (parent == this) return true; @@ -451,7 +451,21 @@ Item = Base.extend({ * @param item * @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() { // TODO: Implement for items other than paths diff --git a/test/tests/item.js b/test/tests/item.js index 7262a714..1c141040 100644 --- a/test/tests/item.js +++ b/test/tests/item.js @@ -89,6 +89,33 @@ test('isDescendant(item) / isAncestor(item)', function() { equals(path.isAncestor(doc.activeLayer), false); 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() {