source: main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor.js@ 33042

Last change on this file since 33042 was 33042, checked in by ak19, 5 years ago

Martin's Map Editor commits. First phase of Dr Bainbridge's requests: 1. shape colour controls are no longer 2 lines, merged into one. This was a fix to the css file. 2. Removed the themes dropdown menu. 3. Moved the thickness and opacity sliders' value display from a 2nd row to just in front of their sliders (and percent sign is now sensibly placed AFTER the number). Future commits to contain input fields for thickness and opacity sliders.

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