/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*/
// general stuff
if (units=='imperial') {
factor_kmh = 0.62; //to mph
unit_kmh = 'mph';
factor_m = 3.28; // to feet
unit_m = 'ft';
factor_km = 0.62; // to miles
unit_km = 'mi';
}
else {
factor_kmh = 1;
unit_kmh = 'km/h';
factor_m = 1;
unit_m = 'm';
factor_km = 1;
unit_km = 'km';
}
var latest = 0;
var latestTime = 0;
var live = 0;
var chart;
var altitudes = new Array();
var altTimeout;
function displayChart() {
if (chart) { google.visualization.events.removeAllListeners(chart); }
var data = new google.visualization.DataTable();
data.addColumn('number', 'id');
data.addColumn('number', 'altitude');
var altLen = altitudes.length;
for (var i=0; i0) {
clearMap();
displayTrack(xml,update);
}
xhr = null;
}
}
xhr.open('GET','getpositions.php?trackid='+trackid+'&userid='+userid,true);
xhr.send();
}
function parsePosition(p) {
// read data
var latitude = getNode(p,'latitude');
var longitude = getNode(p,'longitude');
var altitude = getNode(p,'altitude'); // may be null
if (altitude != null) { altitude = parseInt(altitude); }
var speed = getNode(p,'speed'); // may be null
if (speed != null) { speed = parseInt(speed); }
var bearing = getNode(p,'bearing'); // may be null
if (bearing != null) { bearing = parseInt(bearing); }
var accuracy = getNode(p,'accuracy'); // may be null
if (accuracy != null) { accuracy = parseInt(accuracy); }
var comments = getNode(p,'comments'); // may be null
var username = getNode(p,'username');
var trackname = getNode(p,'trackname');
var tid = getNode(p,'trackid');
var dateoccured = getNode(p,'dateoccured');
var distance = parseInt(getNode(p,'distance'));
var seconds = parseInt(getNode(p,'seconds'));
return {
'latitude': latitude,
'longitude': longitude,
'altitude': altitude,
'speed': speed,
'bearing': bearing,
'accuracy': accuracy,
'comments': comments,
'username': username,
'trackname': trackname,
'tid': tid,
'dateoccured': dateoccured,
'distance': distance,
'seconds': seconds
};
}
function load(type,userid,trackid) {
var url = 'download.php?type='+type+'&userid='+userid+'&trackid='+trackid;
window.location.assign(url);
}
function updateSummary(l,d,s) {
var t = document.getElementById('summary');
if (latest==0){
t.innerHTML = ''+lang['summary']+'
'+
lang['tdistance']+': '+(d.toKm()*factor_km).toFixed(2)+' '+unit_km+'
'+
lang['ttime']+': '+s.toHMS();
}
else {
t.innerHTML = ''+lang['latest']+':
'+l;
}
}
function getNode(p,name) {
return ((p.getElementsByTagName(name)[0].childNodes[0]) ? p.getElementsByTagName(name)[0].childNodes[0].nodeValue : null);
}
// seconds to (d) H:M:S
Number.prototype.toHMS = function(){
var s = this;
var d = Math.floor(s / 86400);
var h = Math.floor((s % 86400) / 3600);
var m = Math.floor(((s % 86400) % 3600) / 60);
s = ((s % 86400) % 3600) % 60;
return ((d>0)?(d + ' d '):'') + (('00'+h).slice(-2)) + ':' + (('00'+m).slice(-2)) + ':' + (('00'+s).slice(-2)) + '';
}
// meters to km
Number.prototype.toKm = function() {
return Math.round(this/10)/100;
}
// m/s to km/h
Number.prototype.toKmH = function() {
return Math.round(this*3600/10)/100;
}
// negate value
function toggleLatest() {
if (latest==0) {
latest = 1;
loadTrack(userid,0,1);
}
else {
latest = 0;
loadTrack(userid,trackid,1);
}
}
function setTrack(t) {
document.getElementsByName('track')[0].value = t;
}
function selectTrack(f) {
trackid=f.options[f.selectedIndex].value;
document.getElementById('latest').checked = false;
if (latest==1) { toggleLatest(); }
loadTrack(userid,trackid,1);
}
function selectUser(f) {
userid=f.options[f.selectedIndex].value;
if (f.options[0].disabled==false) {
f.options[0].disabled = true;
}
document.getElementById('latest').checked = false;
if (latest==1) { toggleLatest(); }
getTracks(userid);
}
function getTracks(userid) {
var xhr = getXHR();
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200) {
var xml = xhr.responseXML;
var trackSelect = document.getElementsByName('track')[0];
clearOptions(trackSelect);
var tracks = xml.getElementsByTagName('track');
if (tracks.length>0) {
fillOptions(xml);
} else {
clearMap();
}
xhr = null;
}
}
xhr.open('GET','gettracks.php?userid='+userid,true);
xhr.send();
}
function fillOptions(xml) {
var trackSelect = document.getElementsByName('track')[0];
var tracks = xml.getElementsByTagName('track');
var trackLen = tracks.length;
for (var i=0; i5000) { loadTime = 0; alert('Sorry, can\'t load '+api+' API'); return; }
if (loadedAPI!==api) {
setTimeout(function() { loadTime += 50; waitAndLoad(api,url); }, 50);
return;
}
if(!isScriptLoaded(url[1])){
addScript(url[1]);
}
loadTime = 0;
waitAndInit(api);
}
function waitAndInit(api) {
// wait till main api loads
if (loadTime>10000) { loadTime = 0; alert('Sorry, can\'t load '+api+' API'); return; }
try {
init();
}
catch(e) {
setTimeout(function() { loadTime += 50; waitAndInit(api); }, 50);
return;
}
loadTime = 0;
zoomToBounds(savedBounds);
loadTrack(userid,trackid,0);
// save current api as default
setCookie('api',api,30);
}
function addScript(url) {
var tag = document.createElement('script');
tag.setAttribute('type','text/javascript');
tag.setAttribute('src', url);
if (typeof tag!='undefined') {
document.getElementsByTagName('head')[0].appendChild(tag);
}
}
function isScriptLoaded(url) {
scripts = document.getElementsByTagName('script');
for (var i = scripts.length; i--;) {
// check if url matches src
var scriptUrl = scripts[i].src.replace(/https?:/,'');
if (scriptUrl != '' && url.indexOf(scriptUrl) !== -1) return true;
}
return false;
}
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = '; expires='+date.toGMTString();
}
else {
var expires = '';
}
document.cookie = 'ulogger_'+name+'='+value+expires+'; path=/';
}
function setLang(lang) {
setCookie('lang',lang,30);
location.reload();
}
function setUnits(unit) {
units = unit;
setCookie('units',unit,30);
location.reload();
}
function showModal(contentHTML) {
var div = document.createElement('div');
div.setAttribute('id', 'modal');
div.innerHTML = '';
document.body.appendChild(div);
var modalBody = document.getElementById('modal-body');
modalBody.innerHTML = contentHTML;
}
function removeModal() {
document.body.removeChild(document.getElementById('modal'));
}
function userMenu() {
var dropdown = document.getElementById('user_dropdown');
if (dropdown.classList.contains('show')) {
dropdown.classList.remove('show');
} else {
dropdown.classList.add('show');
window.addEventListener('click', removeOnClick, true);
}
}
function removeOnClick(event) {
var parent = event.target.parentElement;
var dropdown = document.getElementById('user_dropdown');
dropdown.classList.remove('show');
window.removeEventListener('click', removeOnClick, true);
if (!parent.classList.contains('dropdown')) {
event.stopPropagation();
}
}