Move version of #getBounds() that handles children to Item, to eliminate duplicated code.

This commit is contained in:
Jürg Lehni 2011-04-28 10:04:36 +01:00
parent 6468732d96
commit a9ea10ca7f
3 changed files with 18 additions and 41 deletions

View file

@ -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

View file

@ -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();
},

View file

@ -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.