From a9ea10ca7f8043a11304d66abe135cd8706b1e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Apr 2011 10:04:36 +0100 Subject: [PATCH] Move version of #getBounds() that handles children to Item, to eliminate duplicated code. --- src/item/Group.js | 19 ------------------- src/item/Item.js | 19 ++++++++++++++++++- src/path/CompoundPath.js | 21 --------------------- 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/src/item/Group.js b/src/item/Group.js index f5b09e04..cb42d7d4 100644 --- a/src/item/Group.js +++ b/src/item/Group.js @@ -28,25 +28,6 @@ var Group = this.Group = Item.extend({ this.setClipped(false); }, - getBounds: function() { - if (this.children.length) { - var rect = this.children[0].getBounds(); - var x1 = rect.x; - var y1 = rect.y; - var x2 = rect.x + rect.width; - var y2 = rect.y + rect.height; - for (var i = 1, l = this.children.length; i < l; i++) { - var rect2 = this.children[i].getBounds(); - x1 = Math.min(rect2.x, x1); - y1 = Math.min(rect2.y, y1); - x2 = Math.max(rect2.x + rect2.width, x1 + x2 - x1); - y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1); - } - } - return LinkedRectangle.create(this, 'setBounds', - x1, y1, x2 - x1, y2 - y1); - }, - /** * Specifies whether the group item is to be clipped. * When setting to true, the first child in the group is automatically diff --git a/src/item/Item.js b/src/item/Item.js index ab89cf12..7b3e90b2 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -430,7 +430,24 @@ var Item = this.Item = Base.extend({ }, getBounds: function() { - // TODO: Implement for items other than paths + if (this.children && this.children.length) { + var rect = this.children[0].getBounds(), + x1 = rect.x, + y1 = rect.y, + x2 = rect.x + rect.width, + y2 = rect.y + rect.height; + for (var i = 1, l = this.children.length; i < l; i++) { + rect = this.children[i].getBounds(); + x1 = Math.min(rect.x, x1); + y1 = Math.min(rect.y, y1); + x2 = Math.max(rect.x + rect.width, x1 + x2 - x1); + y2 = Math.max(rect.y + rect.height, y1 + y2 - y1); + } + return LinkedRectangle.create(this, 'setBounds', + x1, y1, x2 - x1, y2 - y1); + } + // TODO: What to return if nothing is defined, e.g. empty Groups? + // Scriptographer behaves weirdly then too. return new Rectangle(); }, diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index 872fec2d..15d17f52 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -24,27 +24,6 @@ var CompoundPath = this.CompoundPath = PathItem.extend({ } }, - // TODO: have getBounds of Group / Layer / CompoundPath use the same - // code (from a utility script?) - getBounds: function() { - if (this.children.length) { - var rect = this.children[0].getBounds(), - x1 = rect.x, - y1 = rect.y, - x2 = rect.x + rect.width, - y2 = rect.y + rect.height; - for (var i = 1, l = this.children.length; i < l; i++) { - var rect2 = this.children[i].getBounds(); - x1 = Math.min(rect2.x, x1); - y1 = Math.min(rect2.y, y1); - x2 = Math.max(rect2.x + rect2.width, x1 + x2 - x1); - y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1); - } - } - return LinkedRectangle.create(this, 'setBounds', - x1, y1, x2 - x1, y2 - y1); - }, - /** * If this is a compound path with only one path inside, * the path is moved outside and the compound path is erased.