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

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