All files / lib/styler styler.js

0% Statements 0/57
0% Branches 0/40
0% Functions 0/9
0% Lines 0/52

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 186 187 188 189                                                                                                                                                                                                                                                                                                                                                                                         
import Route from '../core/route'
import RoutePattern from '../core/pattern'
import { otpModeToGtfsType } from '../util'
 
import styles from './styles'
 
/**
 * Element Types
 */
 
const types = [
  'labels',
  'segments',
  'segments_front',
  'segments_halo',
  'segment_labels',
  'segment_label_containers',
  'stops_merged',
  'stops_pattern',
  'places',
  'places_icon',
  'multipoints_merged',
  'multipoints_pattern',
  'wireframe_vertices',
  'wireframe_edges'
]
 
/**
 * Styler object
 */
 
export default class Styler {
  constructor(styles, transitive) {
    // if (!(this instanceof Styler)) return new Styler(styles)
    this.transitive = transitive
 
    // reset styles
    this.reset()
 
    // load styles
    if (styles) this.load(styles)
  }
 
  /**
   * Clear all current styles
   */
 
  clear() {
    for (const i in types) {
      this[types[i]] = {}
    }
  }
 
  /**
   * Reset to the predefined styles
   */
 
  reset() {
    for (const i in types) {
      const type = types[i]
      this[type] = Object.assign({}, styles[type] || {})
      for (const key in this[type]) {
        if (!Array.isArray(this[type][key])) this[type][key] = [this[type][key]]
      }
    }
  }
 
  /**
   * Load rules
   *
   * @param {Object} a set of style rules
   */
 
  load(styles) {
    for (const i in types) {
      const type = types[i]
      if (styles[type]) {
        for (const key in styles[type]) {
          this[type][key] = (this[type][key] || []).concat(styles[type][key])
        }
      }
    }
  }
 
  /**
   * Compute a style rule based on the current display and data
   *
   * @param {Array} array of rules
   * @param {Object} the Display object
   * @param {Object} data associated with this object
   * @param {Number} index of this object
   */
 
  compute(rules, display, data, index) {
    let computed
    for (const i in rules) {
      const rule = rules[i]
      const val =
        typeof rule === 'function'
          ? rule.call(this, display, data, index, styles.utils)
          : rule
      if (val !== undefined && val !== null) computed = val
    }
    return computed
  }
 
  compute2(type, attr, data, index) {
    let computed
    const rules = this[type][attr]
    if (!rules) return null
    for (const rule of rules) {
      const val =
        typeof rule === 'function'
          ? rule.call(this, this.transitive.display, data, index, styles.utils)
          : rule
      if (val !== undefined && val !== null) computed = val
    }
    return computed
  }
 
  /**
   * Return the collection of default segment styles for a mode.
   *
   * @param {String} an OTP mode string
   */
 
  getModeStyles(mode, display) {
    const modeStyles = {}
 
    // simulate a segment w/ the specified style
    const segment = {
      focused: true,
      isFocused: function () {
        return true
      }
    }
 
    if (
      mode === 'WALK' ||
      mode === 'BICYCLE' ||
      mode === 'BICYCLE_RENT' ||
      mode === 'CAR' ||
      mode === 'CAR_RENT' ||
      mode === 'MICROMOBILITY' ||
      mode === 'MICROMOBILITY_RENT' ||
      mode === 'SCOOTER'
    ) {
      segment.type = mode
    } else {
      // assume a transit mode
      segment.type = 'TRANSIT'
      segment.mode = otpModeToGtfsType(mode)
      const route = new Route({
        agency_id: '',
        route_id: '',
        route_long_name: '',
        route_short_name: '',
        route_type: segment.mode
      })
      const pattern = new RoutePattern({})
      route.addPattern(pattern)
      segment.patterns = [pattern]
    }
 
    for (const attrName in this.segments) {
      const rules = this.segments[attrName]
      for (const i in rules) {
        const rule = rules[i]
        const val = isFunction(rule)
          ? rule.call(this, display, segment, 0, styles.utils)
          : rule
        if (val !== undefined && val !== null) {
          modeStyles[attrName] = val
        }
      }
    }
 
    return modeStyles
  }
}
 
/**
 * Is function?
 */
 
function isFunction(val) {
  return Object.prototype.toString.call(val) === '[object Function]'
}