| 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | Copyright (C) 2018 - 2019 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 18 | MA 02110-1301 USA.
|
|---|
| 19 |
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | /** @namespace GnuBoat JavaScript namespace */
|
|---|
| 23 | var GnuBoat = GnuBoat || {};
|
|---|
| 24 | /** @namespace GnuBoat Webserver JavaScript namespace */
|
|---|
| 25 | GnuBoat.Webserver = GnuBoat.Webserver || {};
|
|---|
| 26 |
|
|---|
| 27 | GnuBoat.Webserver.initialize = function()
|
|---|
| 28 | {
|
|---|
| 29 | GnuBoat.Webserver.private.mapSetup();
|
|---|
| 30 | $("#loginForm").validate({
|
|---|
| 31 | rules: {
|
|---|
| 32 | loginUsername: {
|
|---|
| 33 | required: true
|
|---|
| 34 | },
|
|---|
| 35 | loginPassword: {
|
|---|
| 36 | required: true
|
|---|
| 37 | }
|
|---|
| 38 | },
|
|---|
| 39 | submitHandler: function(form){
|
|---|
| 40 | GnuBoat.Webserver.login(form);
|
|---|
| 41 | }
|
|---|
| 42 | });
|
|---|
| 43 |
|
|---|
| 44 | $("#login").hide();
|
|---|
| 45 | $("#myWaters").hide();
|
|---|
| 46 | $("#linkMyGnuboat").click(function(){ $("#mapViewport").hide(); GnuBoat.Webserver.loginCheck(); });
|
|---|
| 47 | $("#linkMap").click(function(){ $("#mapViewport").show(); });
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * @brief Sets the webservice base url, used by any other functions
|
|---|
| 52 | */
|
|---|
| 53 | GnuBoat.Webserver.setServer = function(baseUrl)
|
|---|
| 54 | {
|
|---|
| 55 | if(baseUrl.slice(-1) != "/")
|
|---|
| 56 | baseUrl += "/";
|
|---|
| 57 | GnuBoat.Webserver.private.serverBaseUrl = baseUrl;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | GnuBoat.Webserver.private = {
|
|---|
| 61 | loggedin: false,
|
|---|
| 62 | map: null,
|
|---|
| 63 | mapSetup: function ()
|
|---|
| 64 | {
|
|---|
| 65 | GnuBoat.Webserver.private.map = L.map('mapViewport').setView([48.43, 10.04], 17);
|
|---|
| 66 |
|
|---|
| 67 | var layer_openstreetmap = L.tileLayer(
|
|---|
| 68 | 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|---|
| 69 | { attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://www.openseamap.org">OpenSeaMap</a>, <a href="http://gnuboat.engsas.de">GNUBoat</a> contributors'}
|
|---|
| 70 | ).addTo(GnuBoat.Webserver.private.map);
|
|---|
| 71 | var layer_openseamap = L.tileLayer(
|
|---|
| 72 | 'http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png',
|
|---|
| 73 | {attribute: '© <a href="http://www.openseamap.org">OpenSeaMap</a> contributors'}
|
|---|
| 74 | ).addTo(GnuBoat.Webserver.private.map);
|
|---|
| 75 |
|
|---|
| 76 | // add GNUBoat layers
|
|---|
| 77 | var layer_gnuboat_accuracy_low = L.tileLayer(
|
|---|
| 78 | './tiles/gnuboat_10_100/{z}/{x}/{y}.png',
|
|---|
| 79 | {tms: true, opacity: 0.5, zIndex: 98, attribute: '© <a href="http://gnuboat.engsas.de">GNUBoat</a> contributors'}
|
|---|
| 80 | );
|
|---|
| 81 | var layer_gnuboat_accuracy_high = L.tileLayer(
|
|---|
| 82 | './tiles/gnuboat_100_100/{z}/{x}/{y}.png',
|
|---|
| 83 | {tms: true, opacity: 0.5, zIndex: 98, attribute: '© <a href="http://gnuboat.engsas.de">GNUBoat</a> contributors'}
|
|---|
| 84 | ).addTo(GnuBoat.Webserver.private.map);
|
|---|
| 85 |
|
|---|
| 86 | // add layer control
|
|---|
| 87 | var baselayers = {
|
|---|
| 88 | "Map": layer_openstreetmap,
|
|---|
| 89 | };
|
|---|
| 90 | // overlays
|
|---|
| 91 | var overlays = {"Seamarks": layer_openseamap,
|
|---|
| 92 | "GNUBoat Low Accuracy": layer_gnuboat_accuracy_low,
|
|---|
| 93 | "GNUBoat High Accuracy": layer_gnuboat_accuracy_high,
|
|---|
| 94 | };
|
|---|
| 95 | GnuBoat.Webserver.private.mapControl = L.control.layers(
|
|---|
| 96 | baselayers, overlays,
|
|---|
| 97 | {hideSingleBase: true, sortLayers: true, }
|
|---|
| 98 | ).addTo(GnuBoat.Webserver.private.map);
|
|---|
| 99 |
|
|---|
| 100 | // add scale
|
|---|
| 101 | L.control.scale().addTo(GnuBoat.Webserver.private.map);
|
|---|
| 102 | }
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | GnuBoat.Webserver.login = function(form)
|
|---|
| 106 | {
|
|---|
| 107 | Cookies.set('username', $("#loginUsername").val());
|
|---|
| 108 | Cookies.set('password', $("#loginPassword").val());
|
|---|
| 109 | GnuBoat.Webserver.private.loggedin = false;
|
|---|
| 110 |
|
|---|
| 111 | // getting my waters
|
|---|
| 112 | $.ajax({
|
|---|
| 113 | dataType: "json",
|
|---|
| 114 | url: GnuBoat.Webserver.private.serverBaseUrl +'waters?manager='+ encodeURIComponent(Cookies.get('username')),
|
|---|
| 115 | }).done(function(data) {
|
|---|
| 116 | GnuBoat.Webserver.private.loggedin = true;
|
|---|
| 117 | GnuBoat.Webserver.loginCheck();
|
|---|
| 118 |
|
|---|
| 119 | $("#myWatersDiv").empty();
|
|---|
| 120 | if(data.length < 1){
|
|---|
| 121 | $("<p>No Waters found!</p>").appendTo("#myWatersDiv");
|
|---|
| 122 | }
|
|---|
| 123 | else{
|
|---|
| 124 | $('<select id="myWatersSelect" class="pure-input-1"></select>').appendTo("#myWatersDiv");
|
|---|
| 125 | data.forEach(function(value, index){
|
|---|
| 126 | $("<option value=\""+ value.water_id +"\">"+ value.name +"</option>").appendTo("#myWatersSelect");
|
|---|
| 127 | })
|
|---|
| 128 | }
|
|---|
| 129 | $("#myWaters").show();
|
|---|
| 130 | }).fail(function() {
|
|---|
| 131 | alert('request failed');
|
|---|
| 132 | });
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | GnuBoat.Webserver.loginCheck = function()
|
|---|
| 136 | {
|
|---|
| 137 | console.log([Cookies.get('username'), Cookies.get('username') == undefined, Cookies.get('password'), Cookies.get('password') == undefined, GnuBoat.Webserver.private.loggedin]);
|
|---|
| 138 | if(Cookies.get('username') == undefined || Cookies.get('password') == undefined || !GnuBoat.Webserver.private.loggedin){
|
|---|
| 139 | $("#login").show();
|
|---|
| 140 | return;
|
|---|
| 141 | }
|
|---|
| 142 | $("#login").hide();
|
|---|
| 143 | };
|
|---|