All files / lib/renderer renderedsegment.js

0% Statements 0/77
0% Branches 0/41
0% Functions 0/19
0% Lines 0/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                                                                                                                                                                                                                                                                                                                                 
import { forEach } from 'lodash'
 
import { distance, normalizeVector, rotateVector } from '../util'
 
/**
 * RenderedSegment
 */
 
let rSegmentId = 0
 
export default class RenderedSegment {
  constructor(pathSegment) {
    this.id = rSegmentId++
    this.renderedEdges = []
    this.pathSegment = pathSegment
    if (pathSegment) this.type = pathSegment.type
    this.focused = true
  }
 
  getId() {
    return this.id
  }
 
  getType() {
    return this.type
  }
 
  addRenderedEdge(rEdge) {
    this.renderedEdges.push(rEdge)
  }
 
  render(display) {
    const styler = display.styler
 
    this.renderData = []
    forEach(this.renderedEdges, (rEdge) => {
      this.renderData = this.renderData.concat(rEdge.renderData)
    })
 
    // Check if this segment is to be drawn as an arc; if so replace render data
    if (
      this.pathSegment.journeySegment &&
      this.pathSegment.journeySegment.arc
    ) {
      const first = this.renderData[0]
      const last = this.renderData[this.renderData.length - 1]
      const v = {
        x: last.x - first.x,
        y: last.y - first.y
      }
      const vp = rotateVector(normalizeVector(v), -Math.PI / 2)
      const dist = distance(first.x, first.y, last.x, last.y)
      const arc = {
        arc: -45,
        ex: (last.x + first.x) / 2 + vp.x * (dist / 4),
        ey: (last.y + first.y) / 2 + vp.y * (dist / 4),
        radius: dist * 0.75,
        x: last.x,
        y: last.y
      }
      this.renderData = [first, arc, last]
    }
 
    display.drawPath(this.renderData, {
      fill: 'none',
      stroke: styler.compute2('segments', 'stroke', this),
      'stroke-dasharray': styler.compute2('segments', 'stroke-dasharray', this),
      'stroke-linecap': styler.compute2('segments', 'stroke-linecap', this),
      'stroke-width': styler.compute2('segments', 'stroke-width', this)
    })
  }
 
  setFocused(focused) {
    this.focused = focused
  }
 
  isFocused() {
    return this.focused
  }
 
  runFocusTransition(display, callback) {
    const newColor = display.styler.compute(
      display.styler.segments.stroke,
      display,
      this
    )
    this.lineGraph.transition().style('stroke', newColor).call(callback)
  }
 
  getZIndex() {
    return this.zIndex
  }
 
  computeLineWidth(display, includeEnvelope) {
    const styler = display.styler
    if (styler && display) {
      // compute the line width
      const env = styler.compute(styler.segments.envelope, display, this)
      if (env && includeEnvelope) {
        return parseFloat(env.substring(0, env.length - 2), 10) - 2
      } else {
        const lw = styler.compute(
          styler.segments['stroke-width'],
          display,
          this
        )
        return parseFloat(lw.substring(0, lw.length - 2), 10) - 2
      }
    }
  }
 
  compareTo(other) {
    // show transit segments in front of other types
    if (this.type === 'TRANSIT' && other.type !== 'TRANSIT') return -1
    if (other.type === 'TRANSIT' && this.type !== 'TRANSIT') return 1
 
    if (this.type === 'TRANSIT' && other.type === 'TRANSIT') {
      // for two transit segments, try sorting transit mode first
      if (this.mode && other.mode && this.mode !== other.mode) {
        return this.mode > other.mode
      }
 
      // for two transit segments of the same mode, sort by id (for display consistency)
      return this.getId() < other.getId()
    }
  }
 
  getLabelTextArray() {
    const textArray = []
    forEach(this.patterns, (pattern) => {
      // TODO: Should we attempt to extract part of the long name if short name
      // is missing?
      const shortName = pattern.route.route_short_name
      if (textArray.indexOf(shortName) === -1) textArray.push(shortName)
    })
    return textArray
  }
 
  getLabelAnchors(display, spacing) {
    const labelAnchors = []
    this.computeRenderLength(display)
    const anchorCount = Math.floor(this.renderLength / spacing)
    const pctSpacing = spacing / this.renderLength
 
    for (let i = 0; i < anchorCount; i++) {
      const t =
        i % 2 === 0
          ? 0.5 + (i / 2) * pctSpacing
          : 0.5 - ((i + 1) / 2) * pctSpacing
      const coord = this.coordAlongRenderedPath(t, display)
      if (coord) labelAnchors.push(coord)
    }
 
    return labelAnchors
  }
 
  coordAlongRenderedPath(t, display) {
    const loc = t * this.renderLength
 
    let cur = 0
    for (let i = 0; i < this.renderedEdges.length; i++) {
      const rEdge = this.renderedEdges[i]
      const edgeRenderLen = rEdge.graphEdge.getRenderLength(display)
      if (loc <= cur + edgeRenderLen) {
        const t2 = (loc - cur) / edgeRenderLen
        return rEdge.graphEdge.coordAlongEdge(t2, rEdge.renderData, display)
      }
      cur += edgeRenderLen
    }
  }
 
  computeRenderLength(display) {
    this.renderLength = 0
    forEach(this.renderedEdges, (rEdge) => {
      this.renderLength += rEdge.graphEdge.getRenderLength(display)
    })
  }
 
  toString() {
    return `RenderedSegment ${this.id} on ${
      this.pathSegment ? this.pathSegment.toString() : ' (null segment)'
    }`
  }
}