1 |
|
---|
2 |
|
---|
3 | function MapEditor(id) {
|
---|
4 | this.id = id;
|
---|
5 | this.shiftKeyPressed = false;
|
---|
6 | this.ctrlKeyPressed = false;
|
---|
7 | this.beingDragged = false;
|
---|
8 | this.allowDeselect = true;
|
---|
9 | this.colors = ['#1E90FF', '#FF1493', '#4B0082', '#32CD32', '#FF8C00', '#000000'];
|
---|
10 | this.selectedColor;
|
---|
11 | this.colorButtons = {};
|
---|
12 | this.thicknessValue = 1;
|
---|
13 | this.opacityValue = 0.4;
|
---|
14 | this.overlays = [];
|
---|
15 | this.selectedShapes = [];
|
---|
16 | this.listenersArray = [];
|
---|
17 | this.mapsArray = [];
|
---|
18 | this.drawingManager;
|
---|
19 | this.selectedShape;
|
---|
20 | this.savedOverlays = null;
|
---|
21 | this.map = null;
|
---|
22 | this.counter = 0;
|
---|
23 | this.branchNum = 1;
|
---|
24 | this.mouseState = "up";
|
---|
25 | this.thicknessRangeListener = this.thicknessValue; // ????
|
---|
26 | this.resizable = false;
|
---|
27 | this.dontResize = false;
|
---|
28 | this.polyOptions = {
|
---|
29 | fillColor: '#CA4A2F',
|
---|
30 | strokeWeight: this.thicknessValue,
|
---|
31 | fillOpacity: this.opacityValue,
|
---|
32 | editable: true,
|
---|
33 | geodesic: false,
|
---|
34 | draggable: true
|
---|
35 | };
|
---|
36 | this.mapEditorHistory = new MapEditorHistory(this);
|
---|
37 | }
|
---|
38 |
|
---|
39 | MapEditor.prototype.initMapEditorControls = function () {
|
---|
40 | var that = this;
|
---|
41 |
|
---|
42 |
|
---|
43 | var draggableCB = document.querySelector("input[name=draggableCB]");
|
---|
44 | draggableCB.addEventListener('change', function () {
|
---|
45 | if (this.checked) {
|
---|
46 | for (var i = 0; i < that.overlays.length; i++) {
|
---|
47 | console.log("checked");
|
---|
48 | that.overlays[i].draggable = false;
|
---|
49 | that.polyOptions.draggable = false;
|
---|
50 | }
|
---|
51 | } else {
|
---|
52 | for (var i = 0; i < that.overlays.length; i++) {
|
---|
53 | console.log("not checked");
|
---|
54 | that.overlays[i].draggable = true;
|
---|
55 | that.polyOptions.draggable = true;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | });
|
---|
59 |
|
---|
60 |
|
---|
61 | //Update thickness
|
---|
62 | var thicknessSlider = document.getElementById("thicknessRange" + "-" + this.id);
|
---|
63 | var thicknessSliderOutput = document.getElementById("thicknessRangeVal" + "-" + this.id);
|
---|
64 | thicknessSliderOutput.innerHTML = ((thicknessSlider.value / 20) * 100) / 100;
|
---|
65 |
|
---|
66 | thicknessSlider.oninput = function () {
|
---|
67 | that.shapeSpecsChangeMD();
|
---|
68 | thicknessSliderOutput.innerHTML = ((this.value / 20) * 100) / 100;
|
---|
69 | that.thicknessValue = this.value / 20;
|
---|
70 | that.polyOptions.strokeWeight = that.thicknessValue;
|
---|
71 | that.setSelectedThickness(that.thicknessValue);
|
---|
72 |
|
---|
73 | }
|
---|
74 | //Update opacity
|
---|
75 | var opacitySlider = document.getElementById("colourOpacity" + "-" + this.id);
|
---|
76 | var opacitySliderOutput = document.getElementById("opacityRangeVal" + "-" + this.id);
|
---|
77 | opacitySliderOutput.innerHTML = "% " + Math.round(opacitySlider.value);
|
---|
78 |
|
---|
79 | opacitySlider.oninput = function () {
|
---|
80 | that.shapeSpecsChangeMD();
|
---|
81 | opacitySliderOutput.innerHTML = "% " + Math.round(this.value * 100) / 100;
|
---|
82 | that.opacityValue = this.value / 100;
|
---|
83 | that.polyOptions.fillOpacity = that.opacityValue;
|
---|
84 | that.setSelectedOpacity(that.opacityValue);
|
---|
85 | }
|
---|
86 |
|
---|
87 | document.getElementById("color-palette1" + "-" + this.id).addEventListener("mousedown", function() { that.shapeSpecsChangeMD() });
|
---|
88 | document.getElementById("thicknessRange" + "-" + this.id).addEventListener("mouseup", function () { that.shapeSpecsChangeMU() });
|
---|
89 | document.getElementById("colourOpacity" + "-" + this.id).addEventListener("mouseup", function() { that.shapeSpecsChangeMU() } );
|
---|
90 | // document.onmousemove = function (ev) {
|
---|
91 | // //How can I know the state of mouse from here
|
---|
92 | // if (that.mouseState == 'down') {
|
---|
93 | // //console.log('mouse down state')
|
---|
94 | // }
|
---|
95 | // if (that.mouseState == 'up') {
|
---|
96 | // //console.log('mouse up state')
|
---|
97 | // }
|
---|
98 | // }
|
---|
99 |
|
---|
100 | document.onmousedown = function (ev) {
|
---|
101 | that.mouseState = "down";
|
---|
102 | // console.log('Down State you can now start dragging');
|
---|
103 | //do not write any code here in this function
|
---|
104 | }
|
---|
105 |
|
---|
106 | document.onmouseup = function (ev) {
|
---|
107 | that.mouseState = "up";
|
---|
108 | // console.log('up state you cannot drag now because you are not holding your mouse')
|
---|
109 | //do not write any code here in this function
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | //prompts the user to save the changes before reloading/leaving the page
|
---|
114 | window.onbeforeunload = function (e) {
|
---|
115 | var currentOverlays = JSON.stringify(ShapesUtil.overlayToJSON(that.overlays));
|
---|
116 | var enableMessage = currentOverlays !== that.savedOverlays;
|
---|
117 | var message = "Changes are not saved. Are you sure you want to leave?";
|
---|
118 |
|
---|
119 | console.log(currentOverlays);
|
---|
120 | console.log(that.savedOverlays);
|
---|
121 | console.log(enableMessage);
|
---|
122 |
|
---|
123 |
|
---|
124 | e = e || window.event;
|
---|
125 | // For IE and Firefox
|
---|
126 | if (e) {
|
---|
127 |
|
---|
128 | if(enableMessage){
|
---|
129 | if(currentOverlays !== "[]") {
|
---|
130 | alert(message);
|
---|
131 | e.returnValue = message;
|
---|
132 |
|
---|
133 | // For Safari
|
---|
134 | return message;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | MapEditor.prototype.settingThePath = function () {
|
---|
142 | var that = this;
|
---|
143 | this.listenersArray = []
|
---|
144 | this.counter = 0;
|
---|
145 | this.branchNum = 1;
|
---|
146 |
|
---|
147 | for (var i = 0; i < this.selectedShapes.length * 2; i++) {
|
---|
148 | for (var j = 1; j < 6; j++) {
|
---|
149 | var path = "//*[@id='map-" + this.id + "']/div/div/div[1]/div[3]/div/div[3]/div[" + this.branchNum + "]/div[" + j + "]/div";
|
---|
150 | this.listenersArray[this.counter] = this.getElementByXpath(path);
|
---|
151 | if (this.listenersArray[this.counter] !== (undefined || null)) {
|
---|
152 | this.listenersArray[this.counter].addEventListener("mousemove", function () {
|
---|
153 | that.resizable = true;
|
---|
154 | that.shapeResize();
|
---|
155 | });
|
---|
156 | this.listenersArray[this.counter].addEventListener("mouseout", function () {
|
---|
157 | if (this.mouseDown) {
|
---|
158 | that.resizable = true;
|
---|
159 | that.shapeResize();
|
---|
160 | }
|
---|
161 | });
|
---|
162 | }
|
---|
163 | this.counter++;
|
---|
164 | }
|
---|
165 | this.branchNum++;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | MapEditor.prototype.shapeResize = function () {
|
---|
170 | if (this.mouseState == "down") {
|
---|
171 | if (this.selectedShapes.length > 0) {
|
---|
172 | if (this.resizable) {
|
---|
173 | if (this.dontResize == false) {
|
---|
174 | this.mapEditorHistory.historyOverlayPush();
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | MapEditor.prototype.shapeSpecsChangeMD = function () {
|
---|
183 | if (this.selectedShapes.length > 0) {
|
---|
184 | this.mapEditorHistory.historyOverlayPush();
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | MapEditor.prototype.shapeSpecsChangeMU = function () {
|
---|
189 | if (this.selectedShapes.length > 0) {
|
---|
190 | this.mapEditorHistory.presentOverlayPush();
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | MapEditor.prototype.makeColorButton = function (color) {
|
---|
195 | var that = this;
|
---|
196 |
|
---|
197 | var button = document.createElement('span');
|
---|
198 | button.className = 'color-buttons1';
|
---|
199 | button.style.backgroundColor = color;
|
---|
200 | google.maps.event.addDomListener(button, 'click', function () {
|
---|
201 | that.selectColor(color);
|
---|
202 | that.setSelectedShapeColor(color);
|
---|
203 | that.shapeSpecsChangeMU();
|
---|
204 | });
|
---|
205 | return button;
|
---|
206 | }
|
---|
207 |
|
---|
208 | MapEditor.prototype.buildColorPalette = function () {
|
---|
209 | var colorPalette = document.getElementById("color-palette1" + "-" + this.id);
|
---|
210 | for (var i = 0; i < this.colors.length; ++i) {
|
---|
211 | var currColor = this.colors[i];
|
---|
212 | var colorButton = this.makeColorButton(currColor);
|
---|
213 | colorPalette.appendChild(colorButton);
|
---|
214 | this.colorButtons[currColor] = colorButton;
|
---|
215 | }
|
---|
216 | this.selectColor(this.colors[0]);
|
---|
217 | };
|
---|
218 |
|
---|
219 | MapEditor.prototype.selectColor = function (color) {
|
---|
220 | this.selectedColor = color;
|
---|
221 | for (var i = 0; i < this.colors.length; ++i) {
|
---|
222 | var currColor = this.colors[i];
|
---|
223 | this.colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
|
---|
224 | }
|
---|
225 |
|
---|
226 | // Retrieves the current options from the drawing manager and replaces the
|
---|
227 | // stroke or fill color as appropriate.
|
---|
228 | var polylineOptions = this.drawingManager.get('polylineOptions');
|
---|
229 | polylineOptions.strokeColor = color;
|
---|
230 | this.drawingManager.set('polylineOptions', polylineOptions);
|
---|
231 |
|
---|
232 | var rectangleOptions = this.drawingManager.get('rectangleOptions');
|
---|
233 | rectangleOptions.fillColor = color;
|
---|
234 | this.drawingManager.set('rectangleOptions', rectangleOptions);
|
---|
235 |
|
---|
236 | var circleOptions = this.drawingManager.get('circleOptions');
|
---|
237 | circleOptions.fillColor = color;
|
---|
238 | this.drawingManager.set('circleOptions', circleOptions);
|
---|
239 |
|
---|
240 | var polygonOptions = this.drawingManager.get('polygonOptions');
|
---|
241 | polygonOptions.fillColor = color;
|
---|
242 | this.drawingManager.set('polygonOptions', polygonOptions);
|
---|
243 | }
|
---|
244 |
|
---|
245 | MapEditor.prototype.initMapEditor = function () {
|
---|
246 | var that = this;
|
---|
247 |
|
---|
248 | this.map = new google.maps.Map(document.getElementById("map-" + this.id), {
|
---|
249 | center: {
|
---|
250 | lat: -37.7891,
|
---|
251 | lng: 175.3180
|
---|
252 | },
|
---|
253 | zoom: 14,
|
---|
254 | mapId: this.id,
|
---|
255 | });
|
---|
256 | this.mapsArray.push(this.map);
|
---|
257 | // Add a style-selector control to the map.
|
---|
258 | var styleControl = document.getElementById('style-selector-control' + "-" + this.id);
|
---|
259 | this.map.controls[google.maps.ControlPosition.TOP_RIGHT].push(styleControl);
|
---|
260 |
|
---|
261 | // Set the map's style to the initial value of the selector.
|
---|
262 | var styleSelector = document.getElementById('style-selector' + "-" + this.id);
|
---|
263 | //console.log(styleSelector);
|
---|
264 | //map = google.maps.Map(document.getElementById('map' +"-"+ this.id));
|
---|
265 | this.map.setOptions({
|
---|
266 | styles: styles[styleSelector.value]
|
---|
267 |
|
---|
268 | });
|
---|
269 |
|
---|
270 | // Apply new JSON when the user selects a different style.
|
---|
271 | styleSelector.addEventListener('change', function () {
|
---|
272 | that.map.setOptions({
|
---|
273 | styles: styles[styleSelector.value]
|
---|
274 | });
|
---|
275 | });
|
---|
276 |
|
---|
277 | this.drawingManager = new google.maps.drawing.DrawingManager({
|
---|
278 | drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
|
---|
279 | drawingControl: true,
|
---|
280 | drawingControlOptions: {
|
---|
281 | position: google.maps.ControlPosition.TOP_CENTER,
|
---|
282 | drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
|
---|
283 | },
|
---|
284 | markerOptions: {
|
---|
285 | draggable: true
|
---|
286 | },
|
---|
287 | circleOptions: this.polyOptions,
|
---|
288 | polylineOptions: this.polyOptions,
|
---|
289 | polygonOptions: this.polyOptions,
|
---|
290 | rectangleOptions: this.polyOptions,
|
---|
291 | });
|
---|
292 |
|
---|
293 | this.drawingManager.setMap(this.map);
|
---|
294 |
|
---|
295 | google.maps.event.addListener(this.drawingManager, "drawingmode_changed", function () {
|
---|
296 | if (that.shiftKeyPressed != true && that.drawingManager.drawingMode !== null) {
|
---|
297 | that.deselectAll();
|
---|
298 | }
|
---|
299 | that.settingThePath();
|
---|
300 |
|
---|
301 | })
|
---|
302 |
|
---|
303 | // store reference to added overlay
|
---|
304 | google.maps.event.addListener(this.drawingManager, 'overlaycomplete', function (e) {
|
---|
305 |
|
---|
306 | that.allowDeselect = true;
|
---|
307 | that.mapEditorHistory.historyOverlayPush();
|
---|
308 | that.overlays.push(e.overlay); // store reference to added overlay
|
---|
309 | var newShape = e.overlay;
|
---|
310 | newShape.type = e.type;
|
---|
311 | that.mapEditorHistory.presentOverlayPush();
|
---|
312 |
|
---|
313 | if (e.type !== google.maps.drawing.OverlayType.MARKER) {
|
---|
314 | that.addShapeListeners(newShape, e);
|
---|
315 | that.setSelection(newShape, e);
|
---|
316 | } else {
|
---|
317 | that.addMarkerListeners(newShape, e);
|
---|
318 | that.setSelection(newShape, e);
|
---|
319 | }
|
---|
320 | });
|
---|
321 |
|
---|
322 | //Clears selection if clicked on the map when shift is not presseed
|
---|
323 | google.maps.event.addListener(this.map, 'click', function (e) {
|
---|
324 | var c = document.body.childNodes;
|
---|
325 | if (e.target && e.target.matches("a.classA")) {
|
---|
326 | console.log("Anchor element clicked!");
|
---|
327 | }
|
---|
328 | if (that.shiftKeyPressed == false) {
|
---|
329 | that.clearSelection();
|
---|
330 | that.selectedShape = null;
|
---|
331 | }
|
---|
332 | });
|
---|
333 |
|
---|
334 | google.maps.event.addListener(this.map, 'mousedown', function (e) {
|
---|
335 | that.dontResize = true;
|
---|
336 | });
|
---|
337 |
|
---|
338 | google.maps.event.addListener(this.map, 'mouseup', function (e) {
|
---|
339 | that.dontResize = false;
|
---|
340 | });
|
---|
341 |
|
---|
342 | //Keyboard shortcuts
|
---|
343 | var mapAndControls = document.getElementById("map-and-controls-" + this.id);
|
---|
344 | console.log(mapAndControls);
|
---|
345 |
|
---|
346 | //Sets shift as unpressed
|
---|
347 | mapAndControls.addEventListener('keyup', function (event) {
|
---|
348 | if (event.keyCode == '16') {
|
---|
349 | that.shiftKeyPressed = false;
|
---|
350 | }
|
---|
351 | });
|
---|
352 |
|
---|
353 | mapAndControls.addEventListener('keydown', function (event) {
|
---|
354 |
|
---|
355 | // https://stackoverflow.com/questions/2220196/how-to-decode-character-pressed-from-jquerys-keydowns-event-handler
|
---|
356 | var keyCode = String.fromCharCode(event.which);
|
---|
357 | // console.log("Key pressed: " + keyCode);
|
---|
358 |
|
---|
359 | //Sets shift as pressed
|
---|
360 | if (event.keyCode == '16') {
|
---|
361 | that.shiftKeyPressed = true;
|
---|
362 | }
|
---|
363 | else if (keyCode == 'Y' && (event.ctrlKey || event.metaKey)) {
|
---|
364 | that.mapEditorHistory.redo();
|
---|
365 | }
|
---|
366 | else if (keyCode == 'Z' && (event.ctrlKey || event.metaKey) ) {
|
---|
367 | if (that.shiftKeyPressed == false) {
|
---|
368 | that.mapEditorHistory.undo();
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | else if (keyCode == 'A' && (event.ctrlKey || event.metaKey)) {
|
---|
373 | event.preventDefault();
|
---|
374 | that.drawingManager.setDrawingMode(null);
|
---|
375 | that.selectAll();
|
---|
376 | }
|
---|
377 | else if (keyCode == 'D' && (event.ctrlKey || event.metaKey)) {
|
---|
378 | event.preventDefault();
|
---|
379 | that.deselectAll();
|
---|
380 | }
|
---|
381 | else if (keyCode == '0' || keyCode == 'Ã' || keyCode == 'G') {
|
---|
382 |
|
---|
383 | that.drawingManager.setDrawingMode(null);
|
---|
384 | } else if (keyCode == '1') {
|
---|
385 | that.drawingManager.setDrawingMode('marker');
|
---|
386 | } else if (keyCode == '2') {
|
---|
387 | that.drawingManager.setDrawingMode('circle');
|
---|
388 | } else if (keyCode == '3') {
|
---|
389 | that.drawingManager.setDrawingMode('polygon');
|
---|
390 | } else if (keyCode == '4') {
|
---|
391 | that.drawingManager.setDrawingMode('polyline');
|
---|
392 | } else if (keyCode == '5') {
|
---|
393 | that.drawingManager.setDrawingMode('rectangle');
|
---|
394 | } else if (keyCode == 'S') {
|
---|
395 | that.saveToArchives();
|
---|
396 | } else if (keyCode == 'Q') {
|
---|
397 | that.printHistory();
|
---|
398 | }
|
---|
399 | // console.log(keyCode);
|
---|
400 | });
|
---|
401 |
|
---|
402 |
|
---|
403 | this.buildColorPalette();
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 |
|
---|
408 | var collection = gs.cgiParams.c;
|
---|
409 | var site_name = gs.xsltParams.site_name;
|
---|
410 | var nodeID = this.id; // documentID, hopefully contains section ID too
|
---|
411 | var metaname = "GPS.mapOverlay";
|
---|
412 |
|
---|
413 | // collection, site, documentID, metadataName, metadataPosition, responseFunction
|
---|
414 | gs.functions.getArchivesMetadata(collection, site_name, nodeID, metaname, 0, function(responseText){
|
---|
415 | // responseText is of type GSMetadata
|
---|
416 |
|
---|
417 | // called when data has been retrieved from archives
|
---|
418 | var JSONString = responseText.getValue();
|
---|
419 | if(JSONString !== "")
|
---|
420 | {
|
---|
421 | that.LOAD(JSONString);
|
---|
422 | that.savedOverlays = JSONString;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | ); // TODO: responseFunction in setMeta call
|
---|
426 | }
|
---|
427 |
|
---|
428 | //Deletes a vertex if clicked on it
|
---|
429 | MapEditor.prototype.vertexAndPolyDel = function (e, newShape) {
|
---|
430 | var vertex = e.vertex;
|
---|
431 | if (e.vertex !== undefined) {
|
---|
432 | if (newShape.type === google.maps.drawing.OverlayType.POLYGON) {
|
---|
433 | var path = newShape.getPaths().getAt(e.path);
|
---|
434 | path.removeAt(e.vertex);
|
---|
435 | if (path.length < 3) {
|
---|
436 | newShape.setMap(null);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | if (newShape.type === google.maps.drawing.OverlayType.POLYLINE) {
|
---|
440 | var path = newShape.getPath();
|
---|
441 | path.removeAt(e.vertex);
|
---|
442 | if (path.length < 2) {
|
---|
443 | newShape.setMap(null);
|
---|
444 | }
|
---|
445 | }
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | MapEditor.prototype.addMarkerListeners = function (newShape, e) {
|
---|
450 | var that = this;
|
---|
451 | //cLICK EVENT IF A MARKER IS CREATED
|
---|
452 | google.maps.event.addListener(newShape, 'click', function (e) {
|
---|
453 | if(that.shiftKeyPressed){
|
---|
454 |
|
---|
455 | } else {
|
---|
456 | that.mapEditorHistory.historyOverlayPush();
|
---|
457 | newShape.setMap(null);
|
---|
458 | that.mapEditorHistory.presentOverlayPush();
|
---|
459 | }
|
---|
460 |
|
---|
461 | });
|
---|
462 |
|
---|
463 | google.maps.event.addListener(newShape, 'dragstart', function (e) {
|
---|
464 | that.beingDragged = true;
|
---|
465 | that.mapEditorHistory.historyOverlayPush();
|
---|
466 |
|
---|
467 | });
|
---|
468 |
|
---|
469 | google.maps.event.addListener(newShape, 'dragend', function () {
|
---|
470 | that.beingDragged = false;
|
---|
471 | that.mapEditorHistory.presentOverlayPush();
|
---|
472 | that.allowDeselect = false;
|
---|
473 | });
|
---|
474 | }
|
---|
475 |
|
---|
476 | MapEditor.prototype.addShapeListeners = function (newShape, e) {
|
---|
477 | var that = this;
|
---|
478 | // Add an event listener that selects the newly-drawn shape when the user
|
---|
479 | // mouses down on it.
|
---|
480 | google.maps.event.addListener(newShape, 'click', function (e) {
|
---|
481 |
|
---|
482 | that.vertexAndPolyDel(e, newShape);
|
---|
483 |
|
---|
484 | });
|
---|
485 |
|
---|
486 | google.maps.event.addListener(newShape, 'dragstart', function (e) {
|
---|
487 | that.allowDeselect = false;
|
---|
488 | that.mapEditorHistory.historyOverlayPush();
|
---|
489 | });
|
---|
490 |
|
---|
491 | google.maps.event.addListener(newShape, 'dragend', function () {
|
---|
492 | that.beingDragged = false;
|
---|
493 | that.mapEditorHistory.presentOverlayPush();
|
---|
494 | that.settingThePath();
|
---|
495 |
|
---|
496 | that.allowDeselect = false;
|
---|
497 | that.setSelection(newShape, e);
|
---|
498 | });
|
---|
499 |
|
---|
500 | //Store information after the event ends
|
---|
501 | google.maps.event.addListener(newShape, 'bounds_changed', function (e) {
|
---|
502 | if (that.beingDragged == false) {
|
---|
503 | that.mapEditorHistory.presentOverlayPush();
|
---|
504 | }
|
---|
505 | });
|
---|
506 |
|
---|
507 | //Add an event listener to select a shape if the mouse hovers over it
|
---|
508 | google.maps.event.addListener(newShape, 'mousedown', function (e) {
|
---|
509 | if (e.target && e.target.matches("a.classA")) {
|
---|
510 | console.log("Anchor element clicked!");
|
---|
511 | }
|
---|
512 | if (e.vertex !== undefined || e.edge !== undefined) {
|
---|
513 | that.mapEditorHistory.historyOverlayPush()
|
---|
514 | }
|
---|
515 | if (that.drawingManager.drawingMode == null) {
|
---|
516 | that.setSelection(newShape, e);
|
---|
517 | }
|
---|
518 | });
|
---|
519 |
|
---|
520 | google.maps.event.addListener(newShape, 'mouseup', function (e) {
|
---|
521 | if (e.vertex !== undefined || e.edge !== undefined) {
|
---|
522 | that.mapEditorHistory.presentOverlayPush()
|
---|
523 | } else {
|
---|
524 | //that.setSelection(newShape, e);
|
---|
525 | }
|
---|
526 |
|
---|
527 | });
|
---|
528 | }
|
---|
529 | MapEditor.prototype.clearSelection = function () {
|
---|
530 | if (this.selectedShape) {
|
---|
531 | if (this.shiftKeyPressed == false) {
|
---|
532 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
533 | if(this.selectedShapes[i].type !== 'marker') {
|
---|
534 | this.selectedShapes[i].setEditable(false);
|
---|
535 | }
|
---|
536 | }
|
---|
537 | this.selectedShapes = [];
|
---|
538 | }
|
---|
539 | this.selectedShape = null;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | //Set selection for the selected overlay
|
---|
544 | MapEditor.prototype.setSelection = function (shape, e) {
|
---|
545 | //var that = this;
|
---|
546 | if (shape.type !== 'marker') {
|
---|
547 | if (this.shiftKeyPressed == false) {
|
---|
548 | if (e !== null) {
|
---|
549 | if (e.vertex == undefined) {
|
---|
550 | if (e.edge == undefined) {
|
---|
551 | this.clearSelection();
|
---|
552 | shape.setEditable(true);
|
---|
553 | }
|
---|
554 | }
|
---|
555 | }
|
---|
556 | }
|
---|
557 | if (this.selectedShapes.includes(shape)) {
|
---|
558 | if (e !== null) {
|
---|
559 | if (e.vertex == undefined) {
|
---|
560 | if (e.edge == undefined) {
|
---|
561 | this.allowDeselect = true;
|
---|
562 | this.removeFromSelectedShapes(shape);
|
---|
563 | }
|
---|
564 | }
|
---|
565 | }
|
---|
566 | } else {
|
---|
567 | this.allowDeselect = false;
|
---|
568 | shape.setEditable(true);
|
---|
569 | this.selectedShapes.push(shape);
|
---|
570 | }
|
---|
571 |
|
---|
572 | //Send the values to be updated
|
---|
573 | var thi = shape.strokeWeight;
|
---|
574 | var opa = shape.fillOpacity;
|
---|
575 | var fCol = shape.fillColor;
|
---|
576 | var sCol = shape.strokeColor;
|
---|
577 | this.updateMenuValues(thi, opa, fCol, sCol);
|
---|
578 |
|
---|
579 | } else if (shape.type == 'marker') {
|
---|
580 | this.allowDeselect = false;
|
---|
581 | this.selectedShapes.push(shape);
|
---|
582 | }
|
---|
583 | this.selectedShape = shape;
|
---|
584 | this.settingThePath();
|
---|
585 | }
|
---|
586 |
|
---|
587 | MapEditor.prototype.removeFromSelectedShapes = function (shape) {
|
---|
588 | if (this.selectedShapes.includes(shape)) {
|
---|
589 | if (this.allowDeselect) {
|
---|
590 | const index = this.selectedShapes.indexOf(shape);
|
---|
591 | this.selectedShapes.splice(index, 1);
|
---|
592 | shape.setEditable(false);
|
---|
593 | }
|
---|
594 | this.allowDeselect = true;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | //Set selected thickness
|
---|
599 | MapEditor.prototype.setSelectedThickness = function (sWeight) {
|
---|
600 | if (this.selectedShapes.length > 0) {
|
---|
601 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
602 | this.selectedShapes[i].set('strokeWeight', sWeight);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | //Set selected opacity
|
---|
608 | MapEditor.prototype.setSelectedOpacity = function (fOpacity) {
|
---|
609 |
|
---|
610 | if (this.selectedShapes.length > 0) {
|
---|
611 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
612 | this.selectedShapes[i].set('fillOpacity', fOpacity);
|
---|
613 | }
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | //set selected fill colour
|
---|
618 | MapEditor.prototype.setSelectedShapeColor = function (color) {
|
---|
619 | if (this.selectedShapes.length > 0) {
|
---|
620 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
621 | this.selectedShapes[i].set('fillColor', color);
|
---|
622 | this.selectedShapes[i].set('strokeColor', color);
|
---|
623 | }
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | MapEditor.prototype.getElementByXpath = function (path) {
|
---|
628 | return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
---|
629 | }
|
---|
630 |
|
---|
631 | MapEditor.prototype.updateMenuValues = function (thi, opa, fCol, sCol) {
|
---|
632 | //Update thickness slider and value on the settings menu
|
---|
633 | var thicknessSliderOutput = document.getElementById("thicknessRangeVal" + "-" + this.id);
|
---|
634 | thicknessSliderOutput.innerHTML = thi;
|
---|
635 | document.getElementById("thicknessRange" + "-" + this.id).value = Math.round((thi * 20) * 100) / 100;
|
---|
636 |
|
---|
637 | //Update the opacity slider and value on the settings menu
|
---|
638 | var opacitySliderOutput = document.getElementById("opacityRangeVal" + "-" + this.id);
|
---|
639 | opacitySliderOutput.innerHTML = "% " + Math.round(opa * 100) * 100 / 100;
|
---|
640 | document.getElementById("colourOpacity" + "-" + this.id).value = (opa * 100) * 100 / 100;
|
---|
641 |
|
---|
642 | if (this.drawingManager.drawingMode == null) {
|
---|
643 | this.selectColor(fCol);
|
---|
644 | }
|
---|
645 | }
|
---|
646 | MapEditor.prototype.selectAll = function () {
|
---|
647 | this.shiftKeyPressed = true;
|
---|
648 | var e = new Object();
|
---|
649 | e.vertex = undefined;
|
---|
650 | this.selectedShapes = [];
|
---|
651 | for (var i = 0; i < this.overlays.length; i++) {
|
---|
652 | this.setSelection(this.overlays[i], e);
|
---|
653 | }
|
---|
654 | this.shiftKeyPressed = false;
|
---|
655 | }
|
---|
656 | MapEditor.prototype.deselectAll = function () {
|
---|
657 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
658 | if (this.selectedShapes[i].type !== google.maps.drawing.OverlayType.MARKER) {
|
---|
659 | this.selectedShapes[i].setEditable(false);
|
---|
660 | }
|
---|
661 |
|
---|
662 | }
|
---|
663 | this.selectedShapes = [];
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | MapEditor.prototype.saveToArchives = function () {
|
---|
668 | var that = this;
|
---|
669 | console.log("Save pressed");
|
---|
670 |
|
---|
671 | var json_overlays = ShapesUtil.overlayToJSON(this.overlays);
|
---|
672 | var collection = gs.cgiParams.c;
|
---|
673 | var site_name = gs.xsltParams.site_name;
|
---|
674 | var nodeID = this.id; // documentID, hopefully contains section ID too
|
---|
675 | var metaname = "GPS.mapOverlay";
|
---|
676 |
|
---|
677 | // collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction
|
---|
678 | gs.functions.setArchivesMetadata(collection, site_name, nodeID, metaname, 0, JSON.stringify(json_overlays), null, "override", function(){
|
---|
679 | console.log("SAVED");
|
---|
680 | }
|
---|
681 | );
|
---|
682 |
|
---|
683 | gs.functions.getArchivesMetadata(collection, site_name, nodeID, metaname, 0, function(responseText){
|
---|
684 | var JSONString = responseText.getValue();
|
---|
685 | that.savedOverlays = JSONString;
|
---|
686 | }
|
---|
687 | );
|
---|
688 |
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | MapEditor.prototype.printHistory = function () {
|
---|
693 | console.log("prev", this.mapEditorHistory.prevOverlays);
|
---|
694 | console.log("present ", this.mapEditorHistory.presentOverlays);
|
---|
695 | console.log("undone ", this.mapEditorHistory.undoneOverlays);
|
---|
696 | console.log(this.selectedShapes);
|
---|
697 |
|
---|
698 | //var collection = gs.cgiParams.c;
|
---|
699 | //console.log(collection);
|
---|
700 | //console.log(this.overlays);
|
---|
701 | //var json_overlays = ShapesUtil.overlayToJSON(this.overlays);
|
---|
702 |
|
---|
703 |
|
---|
704 | }
|
---|
705 |
|
---|
706 | // to be called after reading back in stored JSON from archives meta
|
---|
707 | MapEditor.prototype.LOAD = function (json_str) {
|
---|
708 | this.mapEditorHistory.historyOverlayPush();
|
---|
709 |
|
---|
710 | var map_editor = Object.values(gsmap_store)[0];
|
---|
711 |
|
---|
712 | var new_overlays = ShapesUtil.JSONToOverlays(json_str);
|
---|
713 | for (var i=0; i<map_editor.overlays.length; i++) {
|
---|
714 | map_editor.overlays[i].setMap(null);
|
---|
715 | }
|
---|
716 |
|
---|
717 | map_editor.overlays = new_overlays;
|
---|
718 |
|
---|
719 | for (var i=0; i<map_editor.overlays.length; i++) {
|
---|
720 | map_editor.overlays[i].setMap(map_editor.map);
|
---|
721 | }
|
---|
722 |
|
---|
723 | this.mapEditorHistory.presentOverlayPush();
|
---|
724 |
|
---|
725 |
|
---|
726 | //When the overlays are loaded from the metadate, they cannot be selected when clicked on them.
|
---|
727 | //One fix for this issue is to delete all the sapes present then bring them back by 'undo', then they can be normally selected
|
---|
728 | this.deleteAllShapes();
|
---|
729 | this.mapEditorHistory.undo();
|
---|
730 | this.mapEditorHistory.prevOverlays = [];
|
---|
731 | }
|
---|
732 |
|
---|
733 | MapEditor.prototype.deleteSelectedShapes = function () {
|
---|
734 | if(this.selectedShapes.length !== 0) {
|
---|
735 | this.mapEditorHistory.historyOverlayPush();
|
---|
736 | for (var i = 0; i < this.selectedShapes.length; i++) {
|
---|
737 | this.selectedShapes[i].setMap(null);
|
---|
738 |
|
---|
739 | if (this.overlays.includes(this.selectedShapes[i])) {
|
---|
740 | const index = this.overlays.indexOf(this.selectedShapes[i]);
|
---|
741 | this.overlays.splice(index, 1);
|
---|
742 | // this.selectedShapes[i].setEditable(false);
|
---|
743 | }
|
---|
744 | }
|
---|
745 | this.selectedShapes = [];
|
---|
746 | this.mapEditorHistory.presentOverlayPush();
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 | MapEditor.prototype.draggableState = function () {
|
---|
751 |
|
---|
752 | var draggableCB = document.querySelector("input[name=draggableCB]");
|
---|
753 | draggableCB.addEventListener('change', function () {
|
---|
754 | if (this.checked) {
|
---|
755 | this.overlays.draggable = false;
|
---|
756 | } else {
|
---|
757 | this.overlays.draggable = false;
|
---|
758 | }
|
---|
759 | });
|
---|
760 | }
|
---|
761 |
|
---|
762 | MapEditor.prototype.deleteAllShapes = function () {
|
---|
763 | if(this.overlays.length !== 0) {
|
---|
764 | //console.log("deleteAllShape() this.id = " + this.id);
|
---|
765 | this.mapEditorHistory.historyOverlayPush();
|
---|
766 | for (var i = 0; i < this.overlays.length; i++) {
|
---|
767 | this.overlays[i].setMap(null);
|
---|
768 | }
|
---|
769 | this.overlays = [];
|
---|
770 | this.mapEditorHistory.presentOverlayPush();
|
---|
771 | }
|
---|
772 | } |
---|