Source

services/location-service.js

/**
 * @namespace Services
 * @category Services
 * @module location-service
 * */

import store from '@/store'
/**
 * Uses store.state.location_module.subMenuView to set SubMenu (also know as DetailsView/FenĂȘtre d'information) view from anywhere in the Vue tree
 */
export function setSubMenuViewProgramatically(viewName) {
  //SubMenu component will detect the change and switch to that view (also work if the component if no yet instantiated)
  store.state.location_module.subMenuView = viewName
  //Reset the store variable after the change
  setTimeout(() => (store.state.location_module.subMenuView = ''), 2000)
}

export function sortMainResults(results, sortingType) {
  if (
    !['CONTACT_ON', 'DATETIME_DESC', 'DATETIME_ASC', 'DEFAULT'].includes(
      sortingType
    )
  ) {
    throw new Error('INVALID_SORTING_TYPE')
  }

  return results.sort((a, b) => {
    if (sortingType === 'CONTACT_ON') {
      return a.vehicleContactOn ? -1 : 1
    }
    if (sortingType === 'DATETIME_DESC') {
      return a.timestamp > b.timestamp ? -1 : 1
    }
    if (sortingType === 'DATETIME_ASC') {
      return a.timestamp > b.timestamp ? 1 : -1
    }
  })
}

/**
 * Will generate different colors for trip history polylines or circuit polylines (Location - History - Vehicle/Driver/Circuit)
 * @returns
 */
export function createCircuitColorsManager() {
  const colors = [
    '#8B008B',
    '#DC143C',
    '#006400',
    '#00FFFF',
    '#7FFFD4',
    '#0000FF',
    '#A52A2A',
    '#5F9EA0',
    '#7FFF00',
    '#D2691E',
    '#FF7F50',
    '#6495ED',
    '#FF8C00',
    '#FFD700',
    '#FA8072',
    '#A0522D',
    '#6A5ACD',
    '#708090',
    '#00FF7F',
    '#008080',
    '#EE82EE',
    '#FFFF00',
    '#9ACD32',
    '#BF0D00',
    '#BF8303',
    '#845986',
    '#383086',
    '#7F4986',
    '#6A8677',
    '#4D3C16',
    '#164D39',
    '#B8860B',
  ]
  let colorsInUse = {}
  return {
    resetState() {
      colorsInUse = {}
    },
    /**
     * If every color is in use, it will start all over again
     * @param {Number} identifier [Optional] Associate a color with an unique identifier (i.g vehicle id)
     */
    getUnusedColorAsString(identifier = null) {
      var x
      if (identifier) {
        for (x in colors) {
          if (
            !!colorsInUse[colors[x]] &&
            colorsInUse[colors[x]].identifier == identifier
          ) {
            return colors[x]
          }
        }
      }
      for (x in colors) {
        if (!colorsInUse[colors[x]]) {
          colorsInUse[colors[x]] = {
            identifier,
          }
          return colors[x]
        }
      }
      this.reset()
      return this.getUnusedColorAsString()
    },
  }
}