Source

store/map_options/index.js

/**
 * @namespace Stores
 * @category Stores
 * @module map-options-store
 * */

import Vue from 'vue'
import { getVehicleSensorsConfiguration } from '@/services/history-service.js'

function createState() {
  return {
    /**
     * Definition for components that will spawn in MapOptions based on leaflet geometries.
     */
    sections: [
      {
        uniqueName: 'geocoding_routing_search_marker',
        componentToRender: 'InlineToggleButton',
        i18nLabel: 'geocoding.map_marker_label',
        willToggleLeafletMapLayerGroups: ['routingSearchMarkers'],
        materialIcon: 'MapMarkerIcon',
        showRule: 'layerGroupExists',
        initialValue: true,
      },
      {
        uniqueName: 'geocoding_routing_instructions',
        componentToRender: 'InlineToggleButton',
        i18nLabel: 'geocoding.map_options.instructions',
        willToggleLeafletMapLayerGroups: [
          'routingResultsMarkers',
          'routingInstructionsPolylines',
        ],
        materialIcon: 'MapMarkerIcon',
        showRule: 'layerGroupExists',
        initialValue: true,
      },
      {
        uniqueName: 'geocoding_address_marker',
        componentToRender: 'InlineToggleButton',
        i18nLabel: 'geocoding.map_marker_label',
        willToggleLeafletMapLayerGroups: ['locateAddressMarker'],
        materialIcon: 'MapMarkerIcon',
        showRule: 'layerGroupExists',
        initialValue: true,
      },
    ],
    sensorsConfig: {
      /*{
        id:,
        name:,
        sensors:[{
         number => "num": "1",
         "code": "TOR1",
         name => "nom": "Bac Gauche",
         active => "actif": 0,
         color => "couleur": "#fff600",
         type => "type": "cumul",
         work => "travail": "1"
        }]
    }*/
    },
    filters: [],
  }
}

export default {
  namespaced: true,
  state: createState(),
  getters: {
    sensorsConfig: (state) => state.sensorsConfig,
    speedFilter: (state) => state.filters.find((f) => f.name === 'speedFilter'),
  },
  mutations: {
    resetStore(state) {
      Object.assign(state, createState())
    },
    setSensorsConfig(state, config) {
      state.sensorsConfig = config
    },
    setFilter(state, filter) {
      let match = state.filters.find((f) => f.name === filter.name)
      if (match) {
        Object.assign(match, filter)
      } else {
        state.filters.push(filter)
      }
      Vue.$log.debug('map_options::mutation::setFilter', filter)
    },
    /**
     * Used when the user clicks on a single sensor checkbox (map options)
     */
    updateSensor(state, { sensorName, payload }) {
      //Vue.$log.debug("map_options::mutation::updateSensor", {sensorName,payload,});
      if (state.sensorsConfig && state.sensorsConfig.sensors) {
        let match = state.sensorsConfig.sensors.find(
          (s) => s.name === sensorName
        )
        if (match) {
          Object.assign(match, payload)
        }
      }
    },
    /**
     * Update multiple sensor configurations at once (used for toggle a flag "showMarkers")
     * Note: Alternatively, calling updateSensor mutation will still work but stacking vuex mutations not performant (5sec vs 1sec)
     */
    updateSensors(state, payloads) {
      if (state.sensorsConfig && state.sensorsConfig.sensors) {
        payloads.forEach((payloadItem) => {
          Object.assign(
            state.sensorsConfig.sensors.find(
              (item) => item.code == payloadItem.code
            ) || {},
            payloadItem.payload
          )
        })
      }
    },
    toggleSensorsConfigVisibility(state, isVisible) {
      state.sensorsConfig.sensors &&
        state.sensorsConfig.sensors.forEach((sensorType) => {
          sensorType.showMarkers = isVisible
        })
    },
  },
  actions: {
    setFilter({ commit }, filter) {
      commit('setFilter', filter)
    },
    updateSensor({ commit }, { sensorName, payload }) {
      commit('updateSensor', { sensorName, payload })
    },
    updateSensors({ commit }, payloads) {
      commit('updateSensors', payloads)
    },
    toggleSensorsConfigVisibility({ commit }, isVisible) {
      commit('toggleSensorsConfigVisibility', isVisible)
    },
    /**
     * Updates a sensor configuration.
     * Used to set a flag to decide whenever a sensor type (TOR1,TOR2, etc) will be render on the map
     */
    setSensorsConfig({ commit }, config) {
      ;(config?.sensors || []).forEach((sensor) => {
        sensor.showMarkers = sensor.active
      })
      commit('setSensorsConfig', config)
    },
    async syncSensorConfig({ dispatch }, { vehicleId }) {
      dispatch(
        'setSensorsConfig',
        await getVehicleSensorsConfiguration(vehicleId)
      )
    },
    resetStore({ commit }) {
      commit('resetStore')
    },
  },
}