Make Path#_add() handle both addition and insertion, and use it consequently so segment.path always gets correctly set.

This commit is contained in:
Jürg Lehni 2011-03-03 13:10:17 +00:00
parent 9a2462d147
commit e089e080c2

View file

@ -13,7 +13,7 @@ Path = PathItem.extend({
|| typeof segments[0] != 'object') || typeof segments[0] != 'object')
segments = arguments; segments = arguments;
for (var i = 0, l = segments.length; i < l; i++) for (var i = 0, l = segments.length; i < l; i++)
this._add(new Segment(segments[i])); this._add(Segment.read(segments[i]));
}, },
/** /**
@ -117,22 +117,28 @@ Path = PathItem.extend({
* Private method that adds a segment to the segment list. It assumes that * Private method that adds a segment to the segment list. It assumes that
* the passed object is a segment already and does not perform any checks. * the passed object is a segment already and does not perform any checks.
*/ */
_add: function(segment) { _add: function(segment, index) {
// If this segment belongs to another path already, clone it before
// adding.
if (segment.path)
segment = new Segment(segment);
segment.path = this; segment.path = this;
this._segments.push(segment); if (index == undefined) {
this._segments.push(segment);
} else {
this._segments.splice(index, 0, segment);
}
return segment;
}, },
add: function() { add: function() {
var segment = Segment.read(arguments); var segment = Segment.read(arguments);
if (segment) return segment ? this._add(segment) : null;
this._add(segment);
return segment;
}, },
// TODO: make sure that if the segment belongs to another path, it clones
// the segment. Otherwise it returns the same segment.
insert: function(index, segment) { insert: function(index, segment) {
this._segments.splice(index, 0, new Segment(segment)); var segment = Segment.read(arguments, 1);
return segment ? this._add(segment, index) : null;
}, },
/** /**