X-Git-Url: https://git-public.kairo.at/?p=lantea.git;a=blobdiff_plain;f=js%2Fui.js;h=9edf4dd67c15f4fdd61dae9791cd3bddf8166ddc;hp=f451fbe52932ec53f900bcd15943431e99b49d76;hb=da8fd4b697c86d4c61acbe4c95764ab7d27d4198;hpb=e1c3d337e88cb06b988d97e060938466a9815634 diff --git a/js/ui.js b/js/ui.js index f451fbe..9edf4dd 100644 --- a/js/ui.js +++ b/js/ui.js @@ -13,15 +13,15 @@ var gUIHideCountdown = 0; var gWaitCounter = 0; var gTrackUpdateInterval; var gAction, gActionLabel; -var gBackendURL = "https://backend.lantea.kairo.at"; +var authData = null, userData = null; +var gBackendURL = "https://backend.lantea.kairo.at/"; var gAuthClientID = "lantea"; window.onload = function() { - if (/\/login\.html/.test(window.location)) { - // If we are in the login window, call a function to complete the process and don't do anything else here. - completeLoginWindow(); - return; - } + // Assign click functions to buttons. + document.getElementById("zoomInButton").onclick = gMap.zoomIn; + document.getElementById("zoomOutButton").onclick = gMap.zoomOut; + gAction = document.getElementById("action"); gActionLabel = document.getElementById("actionlabel"); @@ -73,13 +73,9 @@ window.onload = function() { // Set up the login area. document.getElementById("loginbtn").onclick = startLogin; document.getElementById("logoutbtn").onclick = doLogout; - prepareLoginButton(function() { - // Anything that needs the backend should only be triggered from in here. - // That makes sure that the first call the the backend is oauth_state and no other is running in parallel. - // If we call multiple backend methods at once and no session is open, we create multiple sessions, which calls for confusion later on. - - // Call any UI preparation that needs the backend. - }); + // Put in a logged-out state by default. + // Opening the track drawer will update this correctly. + displayLogout(); gAction.addEventListener("dbinit-done", initMap, false); gAction.addEventListener("mapinit-done", postInit, false); @@ -120,6 +116,24 @@ window.onresize = function() { } function startLogin() { + var logerr = document.getElementById("loginerror"); + logerr.classList.add("hidden"); + logerr.title = ""; + if (!authData || !authData["state"]) { + // We have no oAuth state, try to fetch it and call ourselves again if it worked. + prepareLoginButton(function() { + if (authData && authData["state"]) { + startLogin(); + } + else if (!userData) { + // Only warn if we didn't actually end up being logged in. + console.log("No OAuth state and fetching fails, client or server may be offline."); + logerr.classList.remove("hidden"); + logerr.title = "Client or server may be offline."; + } + }); + return; + } 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')) { @@ -127,6 +141,8 @@ function startLogin() { } else { console.log("Opening Sign In window failed."); + logerr.classList.remove("hidden"); + logerr.title = "Opening Sign-In window failed."; } } @@ -175,16 +191,6 @@ function prepareLoginButton(aCallback) { ); } -function completeLoginWindow() { - if (window.opener) { - window.opener.finishLogin(getParameterByName("code"), getParameterByName("state")); - window.close(); - } - else { - document.getElementById("logininfo").textContent = "You have called this document outside of the login flow, which is not supported."; - } -} - function finishLogin(aCode, aState) { if (aState == authData["state"]) { fetchBackend("login?code=" + aCode + "&state=" + aState + "&redirect_uri=" + encodeURIComponent(getRedirectURI()), "GET", null, @@ -237,11 +243,15 @@ function initDB(aEvent) { 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); + console.log("error opening mainDB: " + event.target.error); + showDBErrorDialog(); + if (gDebug) { + console.log("error code: " + event.target.error.code + + " - name: " + event.target.error.name); + } }; request.onsuccess = function(event) { - mainDB = request.result; + mainDB = event.target.result; var throwEv = new CustomEvent("dbinit-done"); gAction.dispatchEvent(throwEv); }; @@ -380,6 +390,16 @@ function showGLWarningDialog() { dia.classList.remove("hidden"); } +function showDBErrorDialog() { + var dia = document.getElementById("dialogArea"); + var areas = dia.children; + for (var i = 0; i <= areas.length - 1; i++) { + areas[i].style.display = "none"; + } + document.getElementById("DBError").style.display = "block"; + dia.classList.remove("hidden"); +} + function showFirstRunDialog() { var dia = document.getElementById("dialogArea"); var areas = dia.children; @@ -527,21 +547,31 @@ function uploadTrack() { if (aStatusCode >= 400) { reportUploadStatus(false, aResult); } - else { + else if (aResult["id"]) { reportUploadStatus(true); } + else { // If no ID is returned, we assume a general error. + reportUploadStatus(false); + } } ); } -function reportUploadStatus(aSuccess, aMessage) { +function reportUploadStatus(aSuccess, aResponse) { document.getElementById("uploadStatusCloseButton").disabled = false; document.getElementById("uploadInProgress").style.display = "none"; if (aSuccess) { document.getElementById("uploadSuccess").style.display = "block"; } - else if (aMessage) { - document.getElementById("uploadErrorMsg").textContent = aMessage; + else if (aResponse && aResponse["message"]) { + document.getElementById("uploadErrorMsg").textContent = aResponse["message"]; + if (aResponse["errortype"]) { + document.getElementById("uploadErrorMsg").textContent += " (" + aResponse["errortype"] + ")"; + } + document.getElementById("uploadError").style.display = "block"; + } + else if (aResponse) { + document.getElementById("uploadErrorMsg").textContent = aResponse; document.getElementById("uploadError").style.display = "block"; } else { @@ -549,6 +579,13 @@ function reportUploadStatus(aSuccess, aMessage) { } } +function setMapStyle() { + var mapSel = document.getElementById("mapSelector"); + if (mapSel.selectedIndex >= 0 && gMap.activeMap != mapSel.value) { + gMap.setActiveMap(mapSel.value); + } +} + var gPrefs = { objStore: "prefs", @@ -719,11 +756,3 @@ function fetchBackend(aEndpoint, aMethod, aSendData, aCallback, aCallbackForward aCallback(e, 500, aCallbackForwards); } } - -function getParameterByName(aName) { - // from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript - name = aName.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); - var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), - results = regex.exec(location.search); - return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); -}