From: Robert Kaiser Date: Sun, 7 Jul 2013 21:39:59 +0000 (+0200) Subject: restructure init to use event chaining X-Git-Tag: legacy-preGL~22 X-Git-Url: https://git-public.kairo.at/?a=commitdiff_plain;h=582d50fc8e75b19c1d07625d4ce759ea422b08f5;p=lantea.git restructure init to use event chaining --- diff --git a/js/map.js b/js/map.js index d269a95..716a016 100644 --- a/js/map.js +++ b/js/map.js @@ -89,74 +89,88 @@ function initMap() { } } - var loopCnt = 0; - var getPersistentPrefs = function() { - if (mainDB) { - gWaitCounter++; - gPrefs.get("position", function(aValue) { - if (aValue) { - gPos = aValue; - gWaitCounter--; - } - }); - gWaitCounter++; - gPrefs.get("center_map", function(aValue) { - if (aValue === undefined) - document.getElementById("centerCheckbox").checked = true; - else - document.getElementById("centerCheckbox").checked = aValue; - setCentering(document.getElementById("centerCheckbox")); - gWaitCounter--; - }); - gWaitCounter++; - gPrefs.get("tracking_enabled", function(aValue) { - if (aValue === undefined) - document.getElementById("trackCheckbox").checked = true; - else - document.getElementById("trackCheckbox").checked = aValue; - gWaitCounter--; - }); - gWaitCounter++; - gTrackStore.getList(function(aTPoints) { - if (gDebug) - console.log(aTPoints.length + " points loaded."); - if (aTPoints.length) { - gTrack = aTPoints; - } - gWaitCounter--; - }); - } - else - setTimeout(getPersistentPrefs, 100); - loopCnt++; - if (loopCnt > 50) { - console.log("Loading prefs failed."); - } - }; - getPersistentPrefs(); - - gTrackCanvas.addEventListener("mouseup", mapEvHandler, false); - gTrackCanvas.addEventListener("mousemove", mapEvHandler, false); - gTrackCanvas.addEventListener("mousedown", mapEvHandler, false); - gTrackCanvas.addEventListener("mouseout", mapEvHandler, false); - - gTrackCanvas.addEventListener("touchstart", mapEvHandler, false); - gTrackCanvas.addEventListener("touchmove", mapEvHandler, false); - gTrackCanvas.addEventListener("touchend", mapEvHandler, false); - gTrackCanvas.addEventListener("touchcancel", mapEvHandler, false); - gTrackCanvas.addEventListener("touchleave", mapEvHandler, false); - - gTrackCanvas.addEventListener("wheel", mapEvHandler, false); - - document.getElementById("body").addEventListener("keydown", mapEvHandler, false); - - document.getElementById("copyright").innerHTML = - gMapStyles[gActiveMap].copyright; + console.log("map vars set, loading prefs..."); + loadPrefs(); +} - gLoadingTile = new Image(); - gLoadingTile.src = "style/loading.png"; - gWaitCounter++; - gLoadingTile.onload = function() { gWaitCounter--; }; +function loadPrefs(aEvent) { + if (aEvent && aEvent.type == "prefs-step") { + console.log("wait: " + gWaitCounter); + if (gWaitCounter == 0) { + gAction.removeEventListener(aEvent.type, loadPrefs, false); + gMapPrefsLoaded = true; + console.log("prefs loaded."); + + gTrackCanvas.addEventListener("mouseup", mapEvHandler, false); + gTrackCanvas.addEventListener("mousemove", mapEvHandler, false); + gTrackCanvas.addEventListener("mousedown", mapEvHandler, false); + gTrackCanvas.addEventListener("mouseout", mapEvHandler, false); + + gTrackCanvas.addEventListener("touchstart", mapEvHandler, false); + gTrackCanvas.addEventListener("touchmove", mapEvHandler, false); + gTrackCanvas.addEventListener("touchend", mapEvHandler, false); + gTrackCanvas.addEventListener("touchcancel", mapEvHandler, false); + gTrackCanvas.addEventListener("touchleave", mapEvHandler, false); + + gTrackCanvas.addEventListener("wheel", mapEvHandler, false); + + document.getElementById("body").addEventListener("keydown", mapEvHandler, false); + + document.getElementById("copyright").innerHTML = + gMapStyles[gActiveMap].copyright; + + gLoadingTile = new Image(); + gLoadingTile.src = "style/loading.png"; + gLoadingTile.onload = function() { + var throwEv = new CustomEvent("mapinit-done"); + gAction.dispatchEvent(throwEv); + }; + } + } + else { + if (aEvent) + gAction.removeEventListener(aEvent.type, loadPrefs, false); + gAction.addEventListener("prefs-step", loadPrefs, false); + gWaitCounter++; + gPrefs.get("position", function(aValue) { + if (aValue) { + gPos = aValue; + gWaitCounter--; + } + }); + gWaitCounter++; + gPrefs.get("center_map", function(aValue) { + if (aValue === undefined) + document.getElementById("centerCheckbox").checked = true; + else + document.getElementById("centerCheckbox").checked = aValue; + setCentering(document.getElementById("centerCheckbox")); + gWaitCounter--; + var throwEv = new CustomEvent("prefs-step"); + gAction.dispatchEvent(throwEv); + }); + gWaitCounter++; + gPrefs.get("tracking_enabled", function(aValue) { + if (aValue === undefined) + document.getElementById("trackCheckbox").checked = true; + else + document.getElementById("trackCheckbox").checked = aValue; + gWaitCounter--; + var throwEv = new CustomEvent("prefs-step"); + gAction.dispatchEvent(throwEv); + }); + gWaitCounter++; + gTrackStore.getList(function(aTPoints) { + if (gDebug) + console.log(aTPoints.length + " points loaded."); + if (aTPoints.length) { + gTrack = aTPoints; + } + gWaitCounter--; + var throwEv = new CustomEvent("prefs-step"); + gAction.dispatchEvent(throwEv); + }); + } } function resizeAndDraw() { diff --git a/js/ui.js b/js/ui.js index 8f036fe..560f48c 100644 --- a/js/ui.js +++ b/js/ui.js @@ -55,47 +55,41 @@ window.onload = function() { gOSMAPIURL = "http://api06.dev.openstreetmap.org/"; } + gAction.addEventListener("dbinit-done", initMap, false); + gAction.addEventListener("mapinit-done", postInit, false); + console.log("starting DB init..."); initDB(); - initMap(); - - var loopCnt = 0; - var waitForInitAndDraw = function() { - if ((gWaitCounter <= 0) || (loopCnt > 100)) { - if (gWaitCounter <= 0) - gWaitCounter = 0; - else - console.log("Loading failed (waiting for init)."); - - gMapPrefsLoaded = true; - resizeAndDraw(); - gActionLabel.textContent = ""; - gAction.style.display = "none"; - setTracking(document.getElementById("trackCheckbox")); - gPrefs.get(gDebug ? "osm_dev_user" : "osm_user", function(aValue) { - if (aValue) { - document.getElementById("uploadUser").value = aValue; - document.getElementById("uploadTrackButton").disabled = false; - } - }); - gPrefs.get(gDebug ? "osm_dev_pwd" : "osm_pwd", function(aValue) { - var upwd = document.getElementById("uploadPwd"); - if (aValue) - document.getElementById("uploadPwd").value = aValue; - }); +} + +function postInit(aEvent) { + gAction.removeEventListener(aEvent.type, postInit, false); + console.log("init done, draw map."); + gMapPrefsLoaded = true; + resizeAndDraw(); + gActionLabel.textContent = ""; + gAction.style.display = "none"; + setTracking(document.getElementById("trackCheckbox")); + gPrefs.get(gDebug ? "osm_dev_user" : "osm_user", function(aValue) { + if (aValue) { + document.getElementById("uploadUser").value = aValue; + document.getElementById("uploadTrackButton").disabled = false; } - else - setTimeout(waitForInitAndDraw, 100); - loopCnt++; - }; - waitForInitAndDraw(); + }); + gPrefs.get(gDebug ? "osm_dev_pwd" : "osm_pwd", function(aValue) { + var upwd = document.getElementById("uploadPwd"); + if (aValue) + document.getElementById("uploadPwd").value = aValue; + }); } window.onresize = function() { resizeAndDraw(); } -function initDB() { +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: @@ -105,6 +99,8 @@ function initDB() { }; request.onsuccess = function(event) { mainDB = request.result; + var throwEv = new CustomEvent("dbinit-done"); + gAction.dispatchEvent(throwEv); }; request.onupgradeneeded = function(event) { mainDB = request.result; diff --git a/manifest.appcache b/manifest.appcache index 5f98be6..cdbbc5a 100644 --- a/manifest.appcache +++ b/manifest.appcache @@ -1,6 +1,6 @@ CACHE MANIFEST -# 2013-04-18 +# 2013-07-07 manifest.webapp js/map.js js/ui.js