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