+
+function showUploadDialog() {
+ var dia = document.getElementById("trackDialogArea");
+ var areas = dia.children;
+ for (var i = 0; i <= areas.length - 1; i++) {
+ areas[i].style.display = "none";
+ }
+ document.getElementById("uploadDialog").style.display = "block";
+ document.getElementById("uploadTrackButton").disabled = true;
+ dia.classList.remove("hidden");
+}
+
+function showGLWarningDialog() {
+ 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("noGLwarning").style.display = "block";
+ dia.classList.remove("hidden");
+}
+
+function cancelTrackDialog() {
+ document.getElementById("trackDialogArea").classList.add("hidden");
+ document.getElementById("uploadTrackButton").disabled = false;
+}
+
+var uiEvHandler = {
+ handleEvent: function(aEvent) {
+ var touchEvent = aEvent.type.indexOf('touch') != -1;
+
+ switch (aEvent.type) {
+ case "mousedown":
+ case "touchstart":
+ case "mousemove":
+ case "touchmove":
+ case "mouseup":
+ case "touchend":
+ case "keydown":
+ showUI();
+ break;
+ }
+ }
+};
+
+function setUploadField(aField) {
+ switch (aField.id) {
+ case "uploadUser":
+ gPrefs.set(gDebug ? "osm_dev_user" : "osm_user", aField.value);
+ document.getElementById("uploadTrackButton").disabled = !aField.value.length;
+ break;
+ case "uploadPwd":
+ gPrefs.set(gDebug ? "osm_dev_pwd" : "osm_pwd", aField.value);
+ break;
+ }
+}
+
+function makeISOString(aTimestamp) {
+ // ISO time format is YYYY-MM-DDTHH:mm:ssZ
+ var tsDate = new Date(aTimestamp);
+ // Note that .getUTCMonth() returns a number between 0 and 11 (0 for January)!
+ return tsDate.getUTCFullYear() + "-" +
+ (tsDate.getUTCMonth() < 9 ? "0" : "") + (tsDate.getUTCMonth() + 1 ) + "-" +
+ (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
+ (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
+ (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
+ (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
+}
+
+function convertTrack(aTargetFormat) {
+ var out = "";
+ switch (aTargetFormat) {
+ case "gpx":
+ out += '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
+ out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
+ if (gTrack.length) {
+ out += ' <trk>' + "\n";
+ out += ' <trkseg>' + "\n";
+ for (var i = 0; i < gTrack.length; i++) {
+ if (gTrack[i].beginSegment && i > 0) {
+ out += ' </trkseg>' + "\n";
+ out += ' <trkseg>' + "\n";
+ }
+ out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
+ gTrack[i].coords.longitude + '">' + "\n";
+ if (gTrack[i].coords.altitude) {
+ out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
+ }
+ out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
+ out += ' </trkpt>' + "\n";
+ }
+ out += ' </trkseg>' + "\n";
+ out += ' </trk>' + "\n";
+ }
+ out += '</gpx>' + "\n";
+ break;
+ case "json":
+ out = JSON.stringify(gTrack);
+ break;
+ default:
+ break;
+ }
+ return out;
+}
+
+function saveTrack() {
+ if (gTrack.length) {
+ var outDataURI = "data:application/gpx+xml," +
+ encodeURIComponent(convertTrack("gpx"));
+ window.open(outDataURI, 'GPX Track');
+ }
+}
+
+function saveTrackDump() {
+ if (gTrack.length) {
+ var outDataURI = "data:application/json," +
+ encodeURIComponent(convertTrack("json"));
+ window.open(outDataURI, 'JSON dump');
+ }
+}
+
+function uploadTrack() {
+ // Hide all areas in the dialog.
+ var dia = document.getElementById("trackDialogArea");
+ var areas = dia.children;
+ for (var i = 0; i <= areas.length - 1; i++) {
+ areas[i].style.display = "none";
+ }
+ // Reset all the fields in the status area.
+ document.getElementById("uploadStatusCloseButton").disabled = true;
+ document.getElementById("uploadInProgress").style.display = "block";
+ document.getElementById("uploadSuccess").style.display = "none";
+ document.getElementById("uploadFailed").style.display = "none";
+ document.getElementById("uploadError").style.display = "none";
+ document.getElementById("uploadErrorMsg").textContent = "";
+ // Now show the status area.
+ document.getElementById("uploadStatus").style.display = "block";
+
+ // Assemble field to post to the backend.
+ var formData = new FormData();
+ formData.append("jsondata", convertTrack("json"));
+ var desc = document.getElementById("uploadDesc").value;
+ formData.append("comment",
+ desc.length ? desc : "Track recorded via Lantea Maps");
+ //formData.append("devicename", "");
+ formData.append("public",
+ document.getElementById("uploadPublic").value);
+
+ fetchBackend("save_track", "POST", formData,
+ function(aResult, aStatusCode) {
+ if (aStatusCode >= 400) {
+ reportUploadStatus(false, aResult);
+ }
+ else {
+ reportUploadStatus(true);
+ }
+ }
+ );
+}
+
+function reportUploadStatus(aSuccess, aMessage) {
+ 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;
+ document.getElementById("uploadError").style.display = "block";
+ }
+ else {
+ document.getElementById("uploadFailed").style.display = "block";
+ }
+}
+
+var gPrefs = {
+ objStore: "prefs",
+
+ get: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore]);
+ var request = transaction.objectStore(this.objStore).get(aKey);
+ request.onsuccess = function(event) {
+ aCallback(request.result, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ aCallback(undefined, event);
+ };
+ },
+
+ set: function(aKey, aValue, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var objStore = transaction.objectStore(this.objStore);
+ var request = objStore.put(aValue, aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ };
+ },
+
+ unset: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var request = transaction.objectStore(this.objStore).delete(aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ }
+ }
+};
+
+var gTrackStore = {
+ objStore: "track",
+
+ getList: function(aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore]);
+ var objStore = transaction.objectStore(this.objStore);
+ if (objStore.getAll) { // currently Mozilla-specific
+ objStore.getAll().onsuccess = function(event) {
+ aCallback(event.target.result);
+ };
+ }
+ else { // Use cursor (standard method).
+ var tPoints = [];
+ objStore.openCursor().onsuccess = function(event) {
+ var cursor = event.target.result;
+ if (cursor) {
+ tPoints.push(cursor.value);
+ cursor.continue();
+ }
+ else {
+ aCallback(tPoints);
+ }
+ };
+ }
+ },
+
+ getListStepped: function(aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore]);
+ var objStore = transaction.objectStore(this.objStore);
+ // Use cursor in reverse direction (so we get the most recent position first)
+ objStore.openCursor(null, "prev").onsuccess = function(event) {
+ var cursor = event.target.result;
+ if (cursor) {
+ aCallback(cursor.value);
+ cursor.continue();
+ }
+ else {
+ aCallback(null);
+ }
+ };
+ },
+
+ push: function(aValue, aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var objStore = transaction.objectStore(this.objStore);
+ var request = objStore.add(aValue);
+ request.onsuccess = function(event) {
+ if (aCallback)
+ aCallback(request.result, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(false, event);
+ };
+ },
+
+ clear: function(aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var request = transaction.objectStore(this.objStore).clear();
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ }
+ }
+};
+
+function fetchBackend(aEndpoint, aMethod, aSendData, aCallback, aCallbackForwards) {
+ var XHR = new XMLHttpRequest();
+ XHR.onreadystatechange = function() {
+ if (XHR.readyState == 4) {
+ // State says we are fully loaded.
+ var result = {};
+ if (XHR.getResponseHeader("Content-Type") == "application/json") {
+ // Got a JSON object, see if we have success.
+ result = JSON.parse(XHR.responseText);
+ }
+ else {
+ result = XHR.responseText;
+ }
+ aCallback(result, XHR.status, aCallbackForwards);
+ }
+ };
+ XHR.open(aMethod, gBackendURL + aEndpoint, true);
+ XHR.withCredentials = "true";
+ //XHR.setRequestHeader("Accept", "application/json");
+ try {
+ XHR.send(aSendData); // Send actual form data.
+ }
+ catch (e) {
+ 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, " "));
+}