Newer
Older
Sébastien DA ROCHA
committed
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
190
const axios = require("axios");
import { mapUtil } from "@/assets/js/map-util.js";
const map = {
namespaced: true,
state: {
basemaps: null,
basemapsToDelete: [],
features: [],
geojsonFeatures: null,
layers: null
},
mutations: {
SET_LAYERS(state, layers) {
state.layers = layers;
},
SET_GEOJSON_FEATURES(state, geojsonFeatures) {
state.geojsonFeatures = geojsonFeatures;
},
SET_BASEMAPS(state, basemaps) {
state.basemaps = basemaps;
},
CREATE_BASEMAP(state, id) {
state.basemaps = [...state.basemaps, { id, layers: [] }]
},
/* DELETE_BASEMAP(state, id) {
state.basemaps = state.basemaps.filter(el => el.id !== id)
}, */
UPDATE_BASEMAPS(state, basemaps) {
state.basemaps = basemaps;
},
UPDATE_BASEMAP(state, { title, id, layers }) {
const index = state.basemaps.findIndex((el) => el.id === id);
if (index !== -1) {
if (title) {
state.basemaps[index].title = title
}
if (layers) {
state.basemaps[index].layers = layers
}
}
},
DELETE_BASEMAP(state, basemapId) {
state.basemaps = state.basemaps.filter(el => el.id !== basemapId);
state.basemapsToDelete.push(basemapId);
},
REMOVE_BASEMAP_ID_TO_DELETE(state, basemapId) {
state.basemapsToDelete = state.basemapsToDelete.filter(el => el !== basemapId);
},
REPLACE_BASEMAP_LAYERS(state, { basemapId, layers }) {
const index = state.basemaps.findIndex((el) => el.id === basemapId);
if (index !== -1) {
state.basemaps[index].layers = layers;
}
},
DELETE_BASEMAP_LAYER(state, { basemapId, layerId }) {
const index = state.basemaps.findIndex((el) => el.id === basemapId);
if (index !== -1) {
state.basemaps[index].layers = state.basemaps[index].layers.filter((el) => el.dataKey !== layerId);
}
},
UPDATE_BASEMAP_LAYER(state, { basemapId, layerId, layer }) {
const index = state.basemaps.findIndex((el) => el.id === basemapId);
if (index !== -1) {
state.basemaps[index].layers = state.basemaps[index].layers.map(
(el) => el.dataKey === layerId ? layer : el
);
}
},
},
getters: {
basemapMaxId: (state) => state.basemaps.reduce((acc, curr) => {
if (curr.id > acc) {
return curr.id;
} else {
return acc;
}
}, 0)
},
actions: {
GET_LAYERS({ commit }) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}layers/`)
.then((response) => (commit("SET_LAYERS", response.data)))
.catch((error) => {
throw error;
});
},
GET_BASEMAPS({ commit }, project_slug) {
axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}base-maps/?project__slug=${project_slug}`)
.then((response) => (commit("SET_BASEMAPS", response.data)))
.catch((error) => {
throw error;
});
},
INITIATE_MAP({ state, rootGetters }) {
const project = rootGetters.project
let mapDefaultViewCenter = [46, 2]; // defaultMapView.center;
let mapDefaultViewZoom = 5; // 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' ,'layers' ,'serviceMap' ,'optionsMap'
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, this.state.configuration.DEFAULT_BASE_MAP.SERVICE, this.state.configuration.DEFAULT_BASE_MAP.OPTIONS);
// Remove multiple interactions with the map
//mapUtil.getMap().dragging.disable();
mapUtil.getMap().doubleClickZoom.disable();
mapUtil.getMap().scrollWheelZoom.disable();
},
SAVE_BASEMAPS({ state, rootState, dispatch }, newBasemapIds) {
for (let basemap of state.basemaps) {
basemap["project"] = rootState.project_slug
// TODO: différencier PUT & POST
console.log(newBasemapIds.includes(basemap.id), newBasemapIds, basemap.id);
if (newBasemapIds.includes(basemap.id)) {
axios
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}base-maps/`, basemap)
.then((response) => (console.log(response.data)))
.catch((error) => {
throw error;
});
} else {
axios
.put(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}base-maps/${basemap.id}/`, basemap)
.then((response) => (console.log(response.data)))
.catch((error) => {
throw error;
});
}
}
//* delete in the backend the basemaps that was rewoved from the front
for (let basemapId of state.basemapsToDelete) {
dispatch("DELETE_BASEMAP", basemapId);
}
},
DELETE_BASEMAP({ commit }, basemapId) {
axios
.delete(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}base-maps/`, basemapId)
.then((response) => {
if (response && response.status === 200) {
commit("REMOVE_BASEMAP_ID_TO_DELETE", basemapId)
}
})
.catch((error) => {
throw error;
});
}
},
}
export default map