Skip to content
Snippets Groups Projects
map.js 3.40 KiB
import { mapUtil } from "@/assets/js/map-util.js";


const map = {
  namespaced: true,
  state: {
    basemaps: [
      {
        "id": 1,
        "title": "Fond par défaut",
        "layers": [
          {
            "id": 1,
            "title": "Open street map",
            "opacity": "1.00",
            "order": 0,
            "queryable": false
          }
        ]
      }
    ],
    features: [],
    layers: [
      {
        "id": 1,
        "options": {
          "maxZoom": 20,
          "attribution": "© les contributeurs d’OpenStreetMap"
        },
        "title": "Open street map",
        "service": "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
        "schema_type": "tms"
      }
    ],
    serviceMap: "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
    optionsMap: {
      "attribution": "&copy; contributeurs d'<a href=\"https://osm.org/copyright\">OpenStreetMap</a>",
      "maxZoom": 20
    }
  },
  mutations: {
    CREATE_BASEMAP(state, dataKey) {
      state.basemaps = [...state.basemaps, { dataKey }]
    },
    DELETE_BASEMAP(state, dataKey) {
      state.basemaps = state.basemaps.filter(el => el.dataKey !== dataKey)
    },
    UPDATE_BASEMAPS(state, basemaps) {
      state.basemaps = basemaps;
    }
  },

  actions: {
    INITIATE_MAP({ state, rootGetters }) {
      const project = rootGetters.project
      let mapDefaultViewCenter = [37.7749, -122.4194]; // defaultMapView.center;
      let mapDefaultViewZoom = 13; // defaultMapView.zoom;
      mapUtil.createMap({
        mapDefaultViewCenter,
        mapDefaultViewZoom,
      });

      // Load the layers.
      // - if one basemap exists, check in the localstorage if one active basemap is set
      // - if no current active basemap, get the first index
      // - if not, load the default map and service options

      // todo : create endpoints to get : 'baseMaps' ,'project' ,'layers' ,'serviceMap' ,'optionsMap' ,'features'
      let layersToLoad = null;
      if (state.baseMaps && state.baseMaps.length > 0) {
        // Use active one if exists, otherwise index 0 (first basemap in the list)
        const mapOptions =
          JSON.parse(localStorage.getItem("geocontrib-map-options")) || {};
        const basemapIndex =
          mapOptions &&
            mapOptions[project] &&
            mapOptions[project]["current-basemap-index"]
            ? mapOptions[project]["current-basemap-index"]
            : 0;
        layersToLoad = state.baseMaps[basemapIndex].layers;
        layersToLoad.forEach((layerToLoad) => {
          state.layers.forEach((layer) => {
            if (layer.id === layerToLoad.id) {
              layerToLoad = Object.assign(layerToLoad, layer);
            }
          });
        });
        layersToLoad.reverse();
      }
      mapUtil.addLayers(layersToLoad, state.serviceMap, state.optionsMap);

      // Remove multiple interactions with the map
      mapUtil.getMap().dragging.disable();
      mapUtil.getMap().doubleClickZoom.disable();
      mapUtil.getMap().scrollWheelZoom.disable();

      // Add the features
      const featureGroup = mapUtil.addFeatures(state.features);
      if (featureGroup && featureGroup.getLayers().length > 0) {
        mapUtil.getMap().fitBounds(featureGroup.getBounds());
      }

    },
    SAVE_BASEMAPS({ state }) {
      const data = JSON.stringify(state.basemaps);
      console.log("SAVE_BASEMAPS", data);
      // todo : call axios POST
    }
  }
}
export default map