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