conveyal/modeify

View on GitHub
client/components/conveyal/transitive.js/master/lib/core/pathsegment.js

Summary

Maintainability
A
25 mins
Test Coverage
/**
 * Dependencies
 */

var each = require('each');

var PatternGroup = require('./patterngroup');
var LabelEdgeGroup = require('../labeler/labeledgegroup.js');

var segmentId = 0;

/**
 * Expose `PathSegment`
 */

module.exports = PathSegment;

/**
 *
 */

function PathSegment(type, path) {
  this.id = segmentId++;
  this.type = type;
  this.path = path;
  this.points = [];
  this.edges = [];
  this.renderedSegments = [];
  this.patternGroup = new PatternGroup();
}

PathSegment.prototype.clearGraphData = function() {
  this.edges = [];
  this.points.forEach(function(point) {
    point.graphVertex = null;
  });
  this.renderLength = null;
};

PathSegment.prototype.getId = function() {
  return this.id;
};

PathSegment.prototype.getType = function() {
  return this.type;
};

PathSegment.prototype.addRenderedSegment = function(rSegment) {
  this.renderedSegments.push(rSegment);
};

PathSegment.prototype.addEdge = function(graphEdge, originVertex) {
  this.edges.push({
    graphEdge: graphEdge,
    forward: (originVertex === graphEdge.fromVertex)
  });
};

PathSegment.prototype.insertEdgeAt = function(index, graphEdge, originVertex) {
  var edgeInfo = {
    graphEdge: graphEdge,
    forward: (originVertex === graphEdge.fromVertex)
  };
  this.edges.splice(index, 0, edgeInfo);
};

PathSegment.prototype.removeEdge = function(graphEdge) {
  var index = null;
  for (var i = 0; i < this.edges.length; i++) {
    if (this.edges[i].graphEdge === graphEdge) {
      index = i;
      break;
    }
  }
  if (index !== null) this.edges.splice(index, 1);
};

PathSegment.prototype.getEdgeIndex = function(graphEdge) {
  for (var i = 0; i < this.edges.length; i++) {
    if (this.edges[i].graphEdge === graphEdge) return i;
  }
  return -1;
};

/**
 * Get graph vertices
 */

PathSegment.prototype.getGraphVertices = function() {
  var vertices = [];
  this.edges.forEach(function(edge, i) {
    if (i === 0) {
      vertices.push(edge.graphEdge.fromVertex);
    }
    vertices.push(edge.graphEdge.toVertex);
  });
  return vertices;
};

PathSegment.prototype.vertexArray = function() {

  var vertex = this.startVertex();
  var array = [vertex];

  this.edges.forEach(function(edgeInfo) {
    vertex = edgeInfo.graphEdge.oppositeVertex(vertex);
    array.push(vertex);
  });

  return array;
};

PathSegment.prototype.startVertex = function() {
  if (this.points[0].multipoint) return this.points[0].multipoint.graphVertex;
  if (!this.edges || this.edges.length === 0) return null;

  var firstGraphEdge = this.edges[0].graphEdge;
  return this.edges[0].forward ? firstGraphEdge.fromVertex : firstGraphEdge.toVertex;

  /*if (this.graphEdges.length === 1) return this.graphEdges[0].fromVertex;
  var first = this.graphEdges[0],
    next = this.graphEdges[1];
  if (first.toVertex == next.toVertex || first.toVertex == next.fromVertex)
    return first.fromVertex;
  if (first.fromVertex == next.toVertex || first.fromVertex == next.fromVertex)
    return first.toVertex;
  return null;*/
};

PathSegment.prototype.endVertex = function() {
  if (this.points[this.points.length - 1].multipoint) return this.points[this.points
    .length - 1].multipoint.graphVertex;
  if (!this.edges || this.edges.length === 0) return null;

  var lastGraphEdge = this.edges[this.edges.length - 1].graphEdge;
  return this.edges[this.edges.length - 1].forward ? lastGraphEdge.toVertex : lastGraphEdge.fromVertex;

  /*if (this.graphEdges.length === 1) return this.graphEdges[0].toVertex;
  var last = this.graphEdges[this.graphEdges.length - 1],
    prev = this.graphEdges[this.graphEdges.length - 2];
  if (last.toVertex == prev.toVertex || last.toVertex == prev.fromVertex)
    return last.fromVertex;
  if (last.fromVertex == prev.toVertex || last.fromVertex == prev.fromVertex)
    return last.toVertex;
  return null;*/
};

PathSegment.prototype.addPattern = function(pattern, fromIndex, toIndex) {
  if ((toIndex - fromIndex + 1) > this.points.length) {
    this.points = [];
    var lastStop = null;
    for (var i = fromIndex; i <= toIndex; i++) {
      var stop = pattern.stops[i];
      if (lastStop !== stop) this.points.push(stop);
      lastStop = stop;
    }
  }
  this.patternGroup.addPattern(pattern);
  //this.pattern = pattern;
};

PathSegment.prototype.getPattern = function() {
  return this.patternGroup.patterns[0];
};

PathSegment.prototype.getPatterns = function() {
  return this.patternGroup.patterns;
};

PathSegment.prototype.getMode = function() {
  return this.patternGroup.patterns[0].route.route_type;
};

PathSegment.prototype.toString = function() {
  var startVertex = this.startVertex(),
    endVertex = this.endVertex();
  return 'PathSegment id=' + this.id + ' type=' + this.type + ' from ' +
    (startVertex ? startVertex.toString() : '(unknown)') + ' to ' +
    (endVertex ? endVertex.toString() : '(unknown)');
};

PathSegment.prototype.getLabelEdgeGroups = function() {
  var edgeGroups = [];
  each(this.renderedSegments, function(rSegment) {
    if (!rSegment.isFocused()) return;
    var currentGroup = new LabelEdgeGroup(rSegment);
    each(rSegment.renderedEdges, function(rEdge) {
      currentGroup.addEdge(rEdge);
      if (rEdge.graphEdge.toVertex.point.containsSegmentEndPoint()) {
        edgeGroups.push(currentGroup);
        currentGroup = new LabelEdgeGroup(rSegment);
      }
    }, this);
  }, this);

  return edgeGroups;
};