// The ShapeList manages the list of polylines which form a V2 layout. var ShapeList = Fiber.extend(function(base){ var exports = {} exports.init = function(){ this.shapes = [] this.workline = null } exports.add = function(shape){ this.shapes.push(shape) } exports.remove = function(shape){ var index = this.shapes.indexOf(shape) if (index !== -1) { this.shapes.splice(index, 1) } } exports.destroy = function(){ this.shapes.forEach(function(shape){ shape.destroy() }) this.shapes = [] } exports.count = function(){ return this.shapes.length } exports.removeSegment = function (segment){ var shape = segment.shape var head = shape.getHeadAtIndex(segment.head) var tail = shape.getTailAtIndex(segment.tail) this.remove(shape) shape.destroy() if (head) { this.add(head) head.build() } if (tail) { this.add(tail) tail.build() } } exports.findClosestPoint = function (p){ var point for (var i = 0; i < this.shapes.length; i++) { point = this.shapes[i].hasPointNear(p) if (point) return { point: point, shape: this.shapes[i] } } return null } exports.findClosestEndPoint = function (p){ var point for (var i = 0; i < this.shapes.length; i++) { point = this.shapes[i].hasEndPointNear(p) if (point) return { point: point, shape: this.shapes[i] } } return null } exports.findClosestSegment = function (p){ var segment = null, closest_segment = null for (var i = 0; i < this.shapes.length; i++) { segment = this.shapes[i].hasSegmentNear(p, 10) if (segment && (! closest_segment || segment.distance < closest_segment.distance)) { closest_segment = segment closest_segment.shape = this.shapes[i] } } return closest_segment } exports.forEach = function(fn){ this.shapes.forEach(fn) } exports.getAllSegments = function(){ var segments = [] this.shapes.forEach(function(shape){ segments = segments.concat( shape.getSegments() ) }) return segments } exports.serialize = function(){ return this.shapes.map(function(shape){ return shape.serialize() }) } exports.deserialize = function(data){ data && data.forEach(function(points){ var line = new Polyline() line.deserialize(points) line.build() }.bind(this)) } return exports })