Mercurial > gemma
comparison client/src/store/map.js @ 3062:87e0422cffa7
client: draw/cut tools work now with multiple maps
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Tue, 16 Apr 2019 12:49:29 +0200 |
parents | 1ef2f4179d30 |
children | 333aff79197d |
comparison
equal
deleted
inserted
replaced
3061:563176194d74 | 3062:87e0422cffa7 |
---|---|
37 zoom: 11 | 37 zoom: 11 |
38 }, | 38 }, |
39 identifyToolEnabled: true, // event binding (singleclick, dblclick) | 39 identifyToolEnabled: true, // event binding (singleclick, dblclick) |
40 identifiedFeatures: [], // map features identified by clicking on the map | 40 identifiedFeatures: [], // map features identified by clicking on the map |
41 currentMeasurement: null, // distance or area from line-/polygon-/cutTool | 41 currentMeasurement: null, // distance or area from line-/polygon-/cutTool |
42 lineTool: null, // open layers interaction object (Draw) | 42 lineToolEnabled: false, |
43 polygonTool: null, // open layers interaction object (Draw) | 43 polygonToolEnabled: false, |
44 cutTool: null, // open layers interaction object (Draw) | 44 cutToolEnabled: false, |
45 isolinesLegendImgDataURL: "", | 45 isolinesLegendImgDataURL: "", |
46 differencesLegendImgDataURL: "" | 46 differencesLegendImgDataURL: "" |
47 }; | 47 }; |
48 }; | 48 }; |
49 | 49 |
89 ); | 89 ); |
90 }, | 90 }, |
91 setCurrentMeasurement: (state, measurement) => { | 91 setCurrentMeasurement: (state, measurement) => { |
92 state.currentMeasurement = measurement; | 92 state.currentMeasurement = measurement; |
93 }, | 93 }, |
94 lineTool: (state, lineTool) => { | 94 lineToolEnabled: (state, enabled) => { |
95 state.lineTool = lineTool; | 95 state.lineToolEnabled = enabled; |
96 }, | 96 state.openLayersMaps.forEach(m => { |
97 polygonTool: (state, polygonTool) => { | 97 let tool = m |
98 state.polygonTool = polygonTool; | 98 .getInteractions() |
99 }, | 99 .getArray() |
100 cutTool: (state, cutTool) => { | 100 .find(i => i.get("id") === "linetool"); |
101 state.cutTool = cutTool; | 101 if (tool) { |
102 tool.setActive(enabled); | |
103 } | |
104 }); | |
105 }, | |
106 polygonToolEnabled: (state, enabled) => { | |
107 state.polygonToolEnabled = enabled; | |
108 state.openLayersMaps.forEach(m => { | |
109 let tool = m | |
110 .getInteractions() | |
111 .getArray() | |
112 .find(i => i.get("id") === "polygontool"); | |
113 if (tool) { | |
114 tool.setActive(enabled); | |
115 } | |
116 }); | |
117 }, | |
118 cutToolEnabled: (state, enabled) => { | |
119 state.cutToolEnabled = enabled; | |
120 state.openLayersMaps.forEach(m => { | |
121 let tool = m | |
122 .getInteractions() | |
123 .getArray() | |
124 .find(i => i.get("id") === "cuttool"); | |
125 if (tool) { | |
126 tool.setActive(enabled); | |
127 } | |
128 }); | |
102 }, | 129 }, |
103 isolinesLegendImgDataURL: (state, isolinesLegendImgDataURL) => { | 130 isolinesLegendImgDataURL: (state, isolinesLegendImgDataURL) => { |
104 state.isolinesLegendImgDataURL = isolinesLegendImgDataURL; | 131 state.isolinesLegendImgDataURL = isolinesLegendImgDataURL; |
105 }, | 132 }, |
106 differencesLegendImgDataURL: (state, differencesLegendImgDataURL) => { | 133 differencesLegendImgDataURL: (state, differencesLegendImgDataURL) => { |
107 state.differencesLegendImgDataURL = differencesLegendImgDataURL; | 134 state.differencesLegendImgDataURL = differencesLegendImgDataURL; |
108 } | 135 } |
109 }, | 136 }, |
110 actions: { | 137 actions: { |
111 openLayersMap({ commit, dispatch }, map) { | 138 openLayersMap({ state, commit, dispatch }, map) { |
112 const drawVectorSrc = map.getLayer("DRAWTOOL").getSource(); | 139 const drawVectorSrc = map.getLayer("DRAWTOOL").getSource(); |
113 const cutVectorSrc = map.getLayer("CUTTOOL").getSource(); | 140 const cutVectorSrc = map.getLayer("CUTTOOL").getSource(); |
114 | 141 |
115 // init line tool | 142 // init line tool |
116 const lineTool = new Draw({ | 143 const lineTool = new Draw({ |
117 source: drawVectorSrc, | 144 source: drawVectorSrc, |
118 type: "LineString", | 145 type: "LineString", |
119 maxPoints: 2 | 146 maxPoints: 2 |
120 }); | 147 }); |
148 lineTool.set("id", "linetool"); | |
121 lineTool.setActive(false); | 149 lineTool.setActive(false); |
122 lineTool.on("drawstart", () => { | 150 lineTool.on("drawstart", () => { |
123 drawVectorSrc.clear(); | 151 state.openLayersMaps.forEach(m => { |
152 m.getLayer("DRAWTOOL") | |
153 .getSource() | |
154 .clear(); | |
155 }); | |
124 commit("setCurrentMeasurement", null); | 156 commit("setCurrentMeasurement", null); |
125 }); | 157 }); |
126 lineTool.on("drawend", event => { | 158 lineTool.on("drawend", event => { |
127 commit("setCurrentMeasurement", { | 159 commit("setCurrentMeasurement", { |
128 quantity: app.$gettext("Length"), | 160 quantity: app.$gettext("Length"), |
136 const polygonTool = new Draw({ | 168 const polygonTool = new Draw({ |
137 source: drawVectorSrc, | 169 source: drawVectorSrc, |
138 type: "Polygon", | 170 type: "Polygon", |
139 maxPoints: 50 | 171 maxPoints: 50 |
140 }); | 172 }); |
173 polygonTool.set("id", "polygontool"); | |
141 polygonTool.setActive(false); | 174 polygonTool.setActive(false); |
142 polygonTool.on("drawstart", () => { | 175 polygonTool.on("drawstart", () => { |
143 drawVectorSrc.clear(); | 176 state.openLayersMaps.forEach(m => { |
177 m.getLayer("DRAWTOOL") | |
178 .getSource() | |
179 .clear(); | |
180 }); | |
144 commit("setCurrentMeasurement", null); | 181 commit("setCurrentMeasurement", null); |
145 }); | 182 }); |
146 polygonTool.on("drawend", event => { | 183 polygonTool.on("drawend", event => { |
147 const areaSize = getArea(event.feature.getGeometry()); | 184 const areaSize = getArea(event.feature.getGeometry()); |
148 commit("setCurrentMeasurement", { | 185 commit("setCurrentMeasurement", { |
172 stroke: new Stroke({ color: "#fff", width: 1.5 }), | 209 stroke: new Stroke({ color: "#fff", width: 1.5 }), |
173 radius: 6 | 210 radius: 6 |
174 }) | 211 }) |
175 }) | 212 }) |
176 }); | 213 }); |
214 cutTool.set("id", "cuttool"); | |
177 cutTool.setActive(false); | 215 cutTool.setActive(false); |
178 cutTool.on("drawstart", () => { | 216 cutTool.on("drawstart", () => { |
179 commit("identifyToolEnabled", false); | 217 commit("identifyToolEnabled", false); |
180 cutVectorSrc.clear(); | 218 state.openLayersMaps.forEach(m => { |
219 m.getLayer("CUTTOOL") | |
220 .getSource() | |
221 .clear(); | |
222 }); | |
181 }); | 223 }); |
182 cutTool.on("drawend", event => { | 224 cutTool.on("drawend", event => { |
183 commit("fairwayprofile/selectedCut", null, { root: true }); | 225 commit("fairwayprofile/selectedCut", null, { root: true }); |
184 dispatch("fairwayprofile/cut", event.feature, { root: true }).then(() => | 226 dispatch("fairwayprofile/cut", event.feature, { root: true }).then(() => |
185 // This setTimeout is an ugly workaround. If we would enable the | 227 // This setTimeout is an ugly workaround. If we would enable the |
191 | 233 |
192 map.addInteraction(lineTool); | 234 map.addInteraction(lineTool); |
193 map.addInteraction(cutTool); | 235 map.addInteraction(cutTool); |
194 map.addInteraction(polygonTool); | 236 map.addInteraction(polygonTool); |
195 | 237 |
196 commit("lineTool", lineTool); | 238 // If there are multiple maps and you enable one of the draw tools, when |
197 commit("polygonTool", polygonTool); | 239 // moving the mouse to another map, the cursor for the draw tool stays |
198 commit("cutTool", cutTool); | 240 // visible in the first map, right next to the edge where the cursor left |
241 // the map. So here we disable all draw layers except the ones in the map | |
242 // that the user currently hovering with the mouse. | |
243 map.getTargetElement().addEventListener("mouseenter", () => { | |
244 if ( | |
245 state.lineToolEnabled || | |
246 state.polygonToolEnabled || | |
247 state.cutToolEnabled | |
248 ) { | |
249 state.openLayersMaps.forEach(m => { | |
250 let lineTool = m | |
251 .getInteractions() | |
252 .getArray() | |
253 .find(i => i.get("id") === "linetool"); | |
254 let polygonTool = m | |
255 .getInteractions() | |
256 .getArray() | |
257 .find(i => i.get("id") === "polygontool"); | |
258 let cutTool = m | |
259 .getInteractions() | |
260 .getArray() | |
261 .find(i => i.get("id") === "cuttool"); | |
262 if (lineTool) lineTool.setActive(false); | |
263 if (polygonTool) polygonTool.setActive(false); | |
264 if (cutTool) cutTool.setActive(false); | |
265 }); | |
266 let lineTool = map | |
267 .getInteractions() | |
268 .getArray() | |
269 .find(i => i.get("id") === "linetool"); | |
270 let polygonTool = map | |
271 .getInteractions() | |
272 .getArray() | |
273 .find(i => i.get("id") === "polygontool"); | |
274 let cutTool = map | |
275 .getInteractions() | |
276 .getArray() | |
277 .find(i => i.get("id") === "cuttool"); | |
278 if (lineTool && state.lineToolEnabled) lineTool.setActive(true); | |
279 if (polygonTool && state.polygonToolEnabled) | |
280 polygonTool.setActive(true); | |
281 if (cutTool && state.cutToolEnabled) cutTool.setActive(true); | |
282 } | |
283 }); | |
284 | |
199 commit("addOpenLayersMap", map); | 285 commit("addOpenLayersMap", map); |
200 }, | 286 }, |
201 initIdentifyTool({ state, rootState, commit, dispatch }, map) { | 287 initIdentifyTool({ state, rootState, commit, dispatch }, map) { |
202 map.on(["singleclick", "dblclick"], event => { | 288 map.on(["singleclick", "dblclick"], event => { |
203 if (!state.identifyToolEnabled) return; | 289 if (!state.identifyToolEnabled) return; |