+ gMap.resizeAndDraw();
+}
+
+function startLogin() {
+ var authURL = authData["url"] + "authorize?response_type=code&client_id=" + gAuthClientID + "&scope=email" +
+ "&state=" + authData["state"] + "&redirect_uri=" + encodeURIComponent(getRedirectURI());
+ if (window.open(authURL, "KaiRoAuth", 'height=450,width=600')) {
+ console.log("Sign In window open.");
+ }
+ else {
+ console.log("Opening Sign In window failed.");
+ }
+}
+
+function getRedirectURI() {
+ return window.location.protocol + '//' + window.location.host + window.location.pathname.replace("index.html", "") + "login.html";
+}
+
+function doLogout() {
+ fetchBackend("logout", "GET", null,
+ function(aResult, aStatus) {
+ if (aStatus < 400) {
+ prepareLoginButton();
+ }
+ else {
+ console.log("Backend issue trying to log out.");
+ }
+ },
+ {}
+ );
+}
+
+function prepareLoginButton(aCallback) {
+ fetchBackend("oauth_state", "GET", null,
+ function(aResult, aStatus) {
+ if (aStatus == 200) {
+ if (aResult["logged_in"]) {
+ userData = {
+ "email": aResult["email"],
+ "permissions": aResult["permissions"],
+ };
+ authData = null;
+ displayLogin();
+ }
+ else {
+ authData = {"state": aResult["state"], "url": aResult["url"]};
+ userData = null;
+ displayLogout();
+ }
+ }
+ else {
+ console.log("Backend error " + aStatus + " fetching OAuth state: " + aResult["message"]);
+ }
+ if (aCallback) { aCallback(); }
+ },
+ {}
+ );
+}
+
+function finishLogin(aCode, aState) {
+ if (aState == authData["state"]) {
+ fetchBackend("login?code=" + aCode + "&state=" + aState + "&redirect_uri=" + encodeURIComponent(getRedirectURI()), "GET", null,
+ function(aResult, aStatus) {
+ if (aStatus == 200) {
+ userData = {
+ "email": aResult["email"],
+ "permissions": aResult["permissions"],
+ };
+ displayLogin();
+ }
+ else {
+ console.log("Login error " + aStatus + ": " + aResult["message"]);
+ prepareLoginButton();
+ }
+ },
+ {}
+ );
+ }
+ else {
+ console.log("Login state did not match, not continuing with login.");
+ }
+}
+
+function displayLogin() {
+ document.getElementById("loginbtn").classList.add("hidden");
+ document.getElementById("logindesc").classList.add("hidden");
+ document.getElementById("username").classList.remove("hidden");
+ document.getElementById("username").textContent = userData.email;
+ document.getElementById("uploadTrackButton").disabled = false;
+ document.getElementById("libraryShowLine").classList.remove("hidden");
+ document.getElementById("logoutbtn").classList.remove("hidden");
+}
+
+function displayLogout() {
+ document.getElementById("logoutbtn").classList.add("hidden");
+ document.getElementById("username").classList.add("hidden");
+ document.getElementById("username").textContent = "";
+ document.getElementById("uploadTrackButton").disabled = true;
+ document.getElementById("libraryShowLine").classList.add("hidden");
+ document.getElementById("loginbtn").classList.remove("hidden");
+ document.getElementById("logindesc").classList.remove("hidden");
+}
+
+function initDB(aEvent) {
+ // Open DB.
+ if (aEvent)
+ gAction.removeEventListener(aEvent.type, initDB, false);
+ var request = window.indexedDB.open("MainDB-lantea", 2);
+ request.onerror = function(event) {
+ // Errors can be handled here. Error codes explain in:
+ // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
+ if (gDebug)
+ console.log("error opening mainDB: " + event.target.errorCode);
+ };
+ request.onsuccess = function(event) {
+ mainDB = request.result;
+ var throwEv = new CustomEvent("dbinit-done");
+ gAction.dispatchEvent(throwEv);
+ };
+ request.onupgradeneeded = function(event) {
+ mainDB = request.result;
+ var ver = mainDB.version || 0; // version is empty string for a new DB
+ if (gDebug)
+ console.log("mainDB has version " + ver + ", upgrade needed.");
+ if (!mainDB.objectStoreNames.contains("prefs")) {
+ // Create a "prefs" objectStore.
+ var prefsStore = mainDB.createObjectStore("prefs");
+ firstRun = true;
+ }
+ if (!mainDB.objectStoreNames.contains("track")) {
+ // Create a "track" objectStore.
+ var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
+ }
+ if (!mainDB.objectStoreNames.contains("tilecache")) {
+ // Create a "tilecache" objectStore.
+ var tilecacheStore = mainDB.createObjectStore("tilecache");
+ }
+ mainDB.onversionchange = function(event) {
+ mainDB.close();
+ mainDB = undefined;
+ initDB();
+ };
+ };
+}
+
+function showUI() {
+ if (gUIHideCountdown <= 0) {
+ var areas = document.getElementsByClassName('autoFade');
+ for (var i = 0; i <= areas.length - 1; i++) {
+ areas[i].classList.remove("hidden");
+ }
+ setTimeout(maybeHideUI, 1000);
+ }
+ gUIHideCountdown = 5;
+}
+
+function maybeHideUI() {
+ gUIHideCountdown--;
+ if (gUIHideCountdown <= 0) {
+ var areas = document.getElementsByClassName('autoFade');
+ for (var i = 0; i <= areas.length - 1; i++) {
+ areas[i].classList.add("hidden");
+ }
+ }
+ else {
+ setTimeout(maybeHideUI, 1000);
+ }
+}
+
+function updateTrackInfo() {
+ document.getElementById("trackLengthNum").textContent = calcTrackLength().toFixed(1);
+ var duration = calcTrackDuration();
+ var durationM = Math.round(duration/60);
+ var durationH = Math.floor(durationM/60); durationM = durationM - durationH * 60;
+ document.getElementById("trackDurationH").style.display = durationH ? "inline" : "none";
+ document.getElementById("trackDurationHNum").textContent = durationH;
+ document.getElementById("trackDurationMNum").textContent = durationM;
+}
+
+function toggleTrackArea() {
+ var fs = document.getElementById("trackArea");
+ if (fs.classList.contains("hidden")) {
+ prepareLoginButton();
+ fs.classList.remove("hidden");
+ showUI();
+ gTrackUpdateInterval = setInterval(updateTrackInfo, 1000);
+ }
+ else {
+ clearInterval(gTrackUpdateInterval);
+ fs.classList.add("hidden");
+ }