view assets/shared.js @ 114:4060bc56cab7

livemap: Show speed in tooltip
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 31 Dec 2020 19:36:06 +0100
parents 03b95c2693ec
children
line wrap: on
line source

var urlParams = new URLSearchParams(window.location.search);

// 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;
}

var secretFieldInitialized = false;
function getSecret() {
    var inputSecret = document.getElementById('inputSecret');
    var userSecret = inputSecret.value;
    var urlSecret = urlParams.get('secret');

    if (!secretFieldInitialized) {
        secretFieldInitialized = true;
        inputSecret.value = urlSecret;
        return urlSecret ? urlSecret : '';
    }
    return userSecret ? userSecret : '';
}
function getLimit() {
    var limit = urlParams.get('limit');
    return limit ? limit : 256;
}

// 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);
}

function shortTimestamp(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = month < 10 ? `0${month}` : month;
    var day = date.getDate();
    day = day < 10 ? `0${day}` : day;
    var dateStr = `${year}-${month}-${day}`;
    return date.toLocaleTimeString('de-DE') + ', ' + dateStr;
}
function locationTooltip(properties) {
    let date = shortTimestamp(new Date(properties.time));
    let speed = properties.speed.toFixed(1);
    return `${speed} km/h - ${date}`;
}