X-Git-Url: https://git-public.kairo.at/?p=lantea.git;a=blobdiff_plain;f=js%2Fui.js;h=e20a4ae800238573b162bdfe942a66c634a38257;hp=7526763ee78e184c467375382ee02881a5dd1e3f;hb=fd6126e2c117b7712e947151e72d37530034bd50;hpb=6ba597ed42a0ebbb340112ab1b9a8316dc8f26cd diff --git a/js/ui.js b/js/ui.js index 7526763..e20a4ae 100644 --- a/js/ui.js +++ b/js/ui.js @@ -13,6 +13,7 @@ var gUIHideCountdown = 0; var gWaitCounter = 0; var gTrackUpdateInterval; var gAction, gActionLabel; +var authData = null, userData = null; var gBackendURL = "https://backend.lantea.kairo.at"; var gAuthClientID = "lantea"; @@ -68,13 +69,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); @@ -115,6 +112,19 @@ window.onresize = function() { } function startLogin() { + 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."); + } + }); + 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')) { @@ -512,21 +522,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 { @@ -704,11 +724,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, " ")); -}