changeset 75:afefa4d60653

UI: Move some shared code into a shared library
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 05 Dec 2020 11:46:33 +0100
parents ebbecfa4231f
children b0a1fa90c77d
files assets/livemap.html assets/shared.js assets/trackme.html
diffstat 3 files changed, 50 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/assets/livemap.html	Sat Dec 05 11:43:38 2020 +0100
+++ b/assets/livemap.html	Sat Dec 05 11:46:33 2020 +0100
@@ -6,6 +6,7 @@
     </head>
     <body>
     <script src="thirdparty/leaflet.js"></script>
+    <script src="shared.js"></script>
 
     <span style="color: blue; font-weight: bold; margin-right: 3em;">GeoHub LiveMap</span>
     <span id="inputFields">
@@ -35,52 +36,6 @@
     var url = new URL(window.location);
     var allMarkers = [];
 
-    // Figure out client/secret from URL and/or UI field. Update UI field with URL value
-    // available.
-    function getClient() {
-        var inputClient = document.getElementById('inputClient');
-        var userClient = inputClient.value;
-        var urlClient = urlParams.get('client');
-
-        if (userClient.length == 0) {
-            inputClient.value = urlClient;
-            return urlClient;
-        }
-        return userClient;
-    }
-    function getSecret() {
-        var inputSecret = document.getElementById('inputSecret');
-        var userSecret = inputSecret.value;
-        var urlSecret = urlParams.get('secret');
-
-        if (userSecret.length == 0) {
-            inputSecret.value = urlSecret;
-            return urlSecret ? urlSecret : '';
-        }
-        return userSecret ? userSecret : '';
-    }
-    function updateURL(client, secret) {
-        var url = window.location.toString();
-
-        if (url.search('\\?') < 0) {
-            url += '?';
-        }
-
-        if (url.search('secret=') > 0) {
-            url = url.replace(/secret=[a-zA-Z0-9]*/, `secret=${secret}`);
-        } else {
-            url += `&secret=${secret}`;
-        }
-
-        if (url.search('client=') > 0) {
-            url = url.replace(/client=[a-zA-Z0-9]*/, `client=${client}`);
-        } else {
-            url += `&client=${client}`;
-        }
-
-        window.history.pushState({}, "", url);
-    }
-
     // Set the current location, called on every new point. Updates the marker and adds
     // a new point (former position).
     var current_marker = L.marker([0,0]).addTo(mymap);
@@ -100,6 +55,7 @@
             allMarkers[i].remove();
         }
     }
+
     // New points are available. Display them on the map and update the marker.
     function xhrcallback(xhr) {
         console.log('xhrcallback called.', xhr.readyState, xhr.status);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/assets/shared.js	Sat Dec 05 11:46:33 2020 +0100
@@ -0,0 +1,47 @@
+// Figure out client/secret from URL and/or UI field. Update UI field with URL value
+// available.
+function getClient() {
+    var inputClient = document.getElementById('inputClient');
+    var userClient = inputClient.value;
+    var urlClient = urlParams.get('client');
+
+    if (userClient.length == 0) {
+        inputClient.value = urlClient;
+        return urlClient;
+    }
+    return userClient;
+}
+function getSecret() {
+    var inputSecret = document.getElementById('inputSecret');
+    var userSecret = inputSecret.value;
+    var urlSecret = urlParams.get('secret');
+
+    if (userSecret.length == 0) {
+        inputSecret.value = urlSecret;
+        return urlSecret ? urlSecret : '';
+    }
+    return userSecret ? userSecret : '';
+}
+
+
+// Update URL from client/secret.
+function updateURL(client, secret) {
+    var url = window.location.toString();
+    if (url.search('\\?') < 0) {
+        url += '?';
+    }
+
+    if (url.search('secret=') > 0) {
+        url = url.replace(/secret=[a-zA-Z0-9]*/, `secret=${secret}`);
+    } else {
+        url += `&secret=${secret}`;
+    }
+
+    if (url.search('client=') > 0) {
+        url = url.replace(/client=[a-zA-Z0-9]*/, `client=${client}`);
+    } else {
+        url += `&client=${client}`;
+    }
+
+    window.history.pushState({}, "", url);
+}
--- a/assets/trackme.html	Sat Dec 05 11:43:38 2020 +0100
+++ b/assets/trackme.html	Sat Dec 05 11:46:33 2020 +0100
@@ -7,6 +7,7 @@
           border: 1px solid black;
         }
         </style>
+        <script src="shared.js"></script>
 
     </head>
 
@@ -59,53 +60,6 @@
     // Tell the user where their data is going.
     document.getElementById('uiHost').textContent = thisUrl.hostname;
 
-    // Figure out the client: Try to get it from the user entry field, if it is empty
-    // try the URL parameter, in that case fill in the field.
-    function getClient() {
-        var inputClient = document.getElementById('inputClient');
-        var userClient = inputClient.value;
-        var urlClient = urlParams.get('client');
-
-        if (userClient.length == 0) {
-            inputClient.value = urlClient;
-            return urlClient;
-        }
-        return userClient;
-    }
-    // Figure out the secret.
-    function getSecret() {
-        var inputSecret = document.getElementById('inputSecret');
-        var userSecret = inputSecret.value;
-        var urlSecret = urlParams.get('secret');
-
-        if (userSecret.length == 0) {
-            inputSecret.value = urlSecret;
-            return urlSecret;
-        }
-        return userSecret;
-    }
-    // Update URL from client/secret.
-    function updateURL(client, secret) {
-        var url = window.location.toString();
-        
-        if (url.search('\\?') < 0) {
-            url += '?';
-        }
-
-        if (url.search('secret=') > 0) {
-            url = url.replace(/secret=[a-zA-Z0-9]*/, `secret=${secret}`);
-        } else {
-            url += `&secret=${secret}`;
-        }
-
-        if (url.search('client=') > 0) {
-            url = url.replace(/client=[a-zA-Z0-9]*/, `client=${client}`);
-        } else {
-            url += `&client=${client}`;
-        }
-
-        window.history.pushState({}, "", url);
-    }
     // Update the link to the livemap if the user has changed client/secret.
     function updateLiveMap() {
         var secret = getSecret();