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