1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Get the best-available indexedDB object.
6 window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
9 var gUIHideCountdown = 0;
11 var gAction, gActionLabel;
12 var gOSMAPIURL = "http://api.openstreetmap.org/";
14 window.onload = function() {
15 gAction = document.getElementById("action");
16 gActionLabel = document.getElementById("actionlabel");
18 var mSel = document.getElementById("mapSelector");
19 for (var mapStyle in gMapStyles) {
20 var opt = document.createElement("option");
22 opt.text = gMapStyles[mapStyle].name;
26 var areas = document.getElementsByClassName('overlayArea');
27 for (var i = 0; i <= areas.length - 1; i++) {
28 areas[i].addEventListener("mouseup", uiEvHandler, false);
29 areas[i].addEventListener("mousemove", uiEvHandler, false);
30 areas[i].addEventListener("mousedown", uiEvHandler, false);
31 areas[i].addEventListener("mouseout", uiEvHandler, false);
33 areas[i].addEventListener("touchstart", uiEvHandler, false);
34 areas[i].addEventListener("touchmove", uiEvHandler, false);
35 areas[i].addEventListener("touchend", uiEvHandler, false);
36 areas[i].addEventListener("touchcancel", uiEvHandler, false);
37 areas[i].addEventListener("touchleave", uiEvHandler, false);
40 document.getElementById("body").addEventListener("keydown", uiEvHandler, false);
42 if (navigator.platform.length == "") {
43 // For Firefox OS, don't display the "save" button.
44 // Do this by setting the debugHide class for testing in debug mode.
45 document.getElementById("saveTrackButton").classList.add("debugHide");
48 // Without OAuth, the login data is useless
49 //document.getElementById("uploadSettingsArea").classList.remove("debugHide");
50 // As login data is useless for now, always enable upload button
51 document.getElementById("uploadTrackButton").disabled = false;
54 // Note that GPX upload returns an error 500 on the dev API right now.
55 gOSMAPIURL = "http://api06.dev.openstreetmap.org/";
62 var waitForInitAndDraw = function() {
63 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
64 if (gWaitCounter <= 0)
67 console.log("Loading failed (waiting for init).");
69 gMapPrefsLoaded = true;
71 gActionLabel.textContent = "";
72 gAction.style.display = "none";
73 setTracking(document.getElementById("trackCheckbox"));
74 gPrefs.get(gDebug ? "osm_dev_user" : "osm_user", function(aValue) {
76 document.getElementById("uploadUser").value = aValue;
77 document.getElementById("uploadTrackButton").disabled = false;
80 gPrefs.get(gDebug ? "osm_dev_pwd" : "osm_pwd", function(aValue) {
81 var upwd = document.getElementById("uploadPwd");
83 document.getElementById("uploadPwd").value = aValue;
87 setTimeout(waitForInitAndDraw, 100);
93 window.onresize = function() {
99 var request = window.indexedDB.open("MainDB-lantea", 2);
100 request.onerror = function(event) {
101 // Errors can be handled here. Error codes explain in:
102 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
104 console.log("error opening mainDB: " + event.target.errorCode);
106 request.onsuccess = function(event) {
107 mainDB = request.result;
109 request.onupgradeneeded = function(event) {
110 mainDB = request.result;
111 var ver = mainDB.version || 0; // version is empty string for a new DB
113 console.log("mainDB has version " + ver + ", upgrade needed.");
114 if (!mainDB.objectStoreNames.contains("prefs")) {
115 // Create a "prefs" objectStore.
116 var prefsStore = mainDB.createObjectStore("prefs");
118 if (!mainDB.objectStoreNames.contains("track")) {
119 // Create a "track" objectStore.
120 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
122 if (!mainDB.objectStoreNames.contains("tilecache")) {
123 // Create a "tilecache" objectStore.
124 var tilecacheStore = mainDB.createObjectStore("tilecache");
126 mainDB.onversionchange = function(event) {
135 if (gUIHideCountdown <= 0) {
136 var areas = document.getElementsByClassName('overlayArea');
137 for (var i = 0; i <= areas.length - 1; i++) {
138 areas[i].classList.remove("hidden");
140 setTimeout(maybeHideUI, 1000);
142 gUIHideCountdown = 5;
145 function maybeHideUI() {
147 if (gUIHideCountdown <= 0) {
148 var areas = document.getElementsByClassName('overlayArea');
149 for (var i = 0; i <= areas.length - 1; i++) {
150 areas[i].classList.add("hidden");
154 setTimeout(maybeHideUI, 1000);
158 function toggleTrackArea() {
159 var fs = document.getElementById("trackArea");
160 if (fs.style.display != "block") {
161 fs.style.display = "block";
165 fs.style.display = "none";
169 function toggleSettings() {
170 var fs = document.getElementById("settingsArea");
171 if (fs.style.display != "block") {
172 fs.style.display = "block";
176 fs.style.display = "none";
180 function toggleFullscreen() {
181 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
182 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
183 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
184 if (document.cancelFullScreen) {
185 document.cancelFullScreen();
186 } else if (document.mozCancelFullScreen) {
187 document.mozCancelFullScreen();
188 } else if (document.webkitCancelFullScreen) {
189 document.webkitCancelFullScreen();
193 var elem = document.getElementById("body");
194 if (elem.requestFullScreen) {
195 elem.requestFullScreen();
196 } else if (elem.mozRequestFullScreen) {
197 elem.mozRequestFullScreen();
198 } else if (elem.webkitRequestFullScreen) {
199 elem.webkitRequestFullScreen();
204 function showUploadDialog() {
205 var dia = document.getElementById("dialogArea");
206 var areas = dia.children;
207 for (var i = 0; i <= areas.length - 1; i++) {
208 areas[i].style.display = "none";
210 document.getElementById("uploadDialog").style.display = "block";
211 document.getElementById("uploadTrackButton").disabled = true;
212 dia.classList.remove("hidden");
215 function cancelDialog() {
216 document.getElementById("dialogArea").classList.add("hidden");
217 document.getElementById("uploadTrackButton").disabled = false;
221 handleEvent: function(aEvent) {
222 var touchEvent = aEvent.type.indexOf('touch') != -1;
224 switch (aEvent.type) {
238 function setUploadField(aField) {
241 gPrefs.set(gDebug ? "osm_dev_user" : "osm_user", aField.value);
242 document.getElementById("uploadTrackButton").disabled = !aField.value.length;
245 gPrefs.set(gDebug ? "osm_dev_pwd" : "osm_pwd", aField.value);
250 function makeISOString(aTimestamp) {
251 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
252 var tsDate = new Date(aTimestamp);
253 return tsDate.getUTCFullYear() + "-" +
254 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
255 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
256 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
257 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
258 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
261 function convertTrack(aTargetFormat) {
263 switch (aTargetFormat) {
265 out += '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
266 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
268 out += ' <trk>' + "\n";
269 out += ' <trkseg>' + "\n";
270 for (var i = 0; i < gTrack.length; i++) {
271 if (gTrack[i].beginSegment && i > 0) {
272 out += ' </trkseg>' + "\n";
273 out += ' <trkseg>' + "\n";
275 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
276 gTrack[i].coords.longitude + '">' + "\n";
277 if (gTrack[i].coords.altitude) {
278 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
280 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
281 out += ' </trkpt>' + "\n";
283 out += ' </trkseg>' + "\n";
284 out += ' </trk>' + "\n";
286 out += '</gpx>' + "\n";
289 out = JSON.stringify(gTrack);
297 function saveTrack() {
299 var outDataURI = "data:application/gpx+xml," +
300 encodeURIComponent(convertTrack("gpx"));
301 window.open(outDataURI, 'GPX Track');
305 function saveTrackDump() {
307 var outDataURI = "data:application/json," +
308 encodeURIComponent(convertTrack("json"));
309 window.open(outDataURI, 'JSON dump');
313 function uploadTrack() {
314 var dia = document.getElementById("dialogArea");
315 var areas = dia.children;
316 for (var i = 0; i <= areas.length - 1; i++) {
317 areas[i].style.display = "none";
319 document.getElementById("uploadStatus").style.display = "block";
321 // See http://wiki.openstreetmap.org/wiki/Api06#Uploading_traces
322 var trackBlob = new Blob([convertTrack("gpx")],
323 { "type" : "application/gpx+xml" });
324 var formData = new FormData();
325 formData.append("file", trackBlob);
326 var desc = document.getElementById("uploadDesc").value;
327 formData.append("description",
328 desc.length ? desc : "Track recorded via Lantea Maps");
329 //formData.append("tags", "");
330 formData.append("visibility",
331 document.getElementById("uploadVisibility").value);
332 var XHR = new XMLHttpRequest();
333 XHR.onreadystatechange = function() {
334 if (XHR.readyState == 4 && XHR.status == 200) {
336 reportUploadStatus(true);
337 } else if (XHR.readyState == 4 && XHR.status != 200) {
338 // fetched the wrong page or network error...
339 reportUploadStatus(false);
342 XHR.open("POST", gOSMAPIURL + "api/0.6/gpx/create", true);
343 // Cross-Origin XHR doesn't allow username/password (HTTP Auth).
344 // So, we'll ask the user for entering credentials with rather ugly UI.
345 XHR.withCredentials = true;
350 reportUploadStatus(false, e);
354 function reportUploadStatus(aSuccess, aMessage) {
355 document.getElementById("uploadStatusCloseButton").disabled = false;
356 document.getElementById("uploadInProgress").style.display = "none";
358 document.getElementById("uploadSuccess").style.display = "block";
361 document.getElementById("uploadErrorMsg").textContent = aMessage;
362 document.getElementById("uploadError").style.display = "block";
365 document.getElementById("uploadFailed").style.display = "block";
372 get: function(aKey, aCallback) {
375 var transaction = mainDB.transaction([this.objStore]);
376 var request = transaction.objectStore(this.objStore).get(aKey);
377 request.onsuccess = function(event) {
378 aCallback(request.result, event);
380 request.onerror = function(event) {
381 // Errors can be handled here.
382 aCallback(undefined, event);
386 set: function(aKey, aValue, aCallback) {
390 var transaction = mainDB.transaction([this.objStore], "readwrite");
391 var objStore = transaction.objectStore(this.objStore);
392 var request = objStore.put(aValue, aKey);
393 request.onsuccess = function(event) {
396 aCallback(success, event);
398 request.onerror = function(event) {
399 // Errors can be handled here.
401 aCallback(success, event);
405 unset: function(aKey, aCallback) {
409 var transaction = mainDB.transaction([this.objStore], "readwrite");
410 var request = transaction.objectStore(this.objStore).delete(aKey);
411 request.onsuccess = function(event) {
414 aCallback(success, event);
416 request.onerror = function(event) {
417 // Errors can be handled here.
419 aCallback(success, event);
427 getList: function(aCallback) {
430 var transaction = mainDB.transaction([this.objStore]);
431 var objStore = transaction.objectStore(this.objStore);
432 if (objStore.getAll) { // currently Mozilla-specific
433 objStore.getAll().onsuccess = function(event) {
434 aCallback(event.target.result);
437 else { // Use cursor (standard method).
439 objStore.openCursor().onsuccess = function(event) {
440 var cursor = event.target.result;
442 tPoints.push(cursor.value);
452 push: function(aValue, aCallback) {
455 var transaction = mainDB.transaction([this.objStore], "readwrite");
456 var objStore = transaction.objectStore(this.objStore);
457 var request = objStore.add(aValue);
458 request.onsuccess = function(event) {
460 aCallback(request.result, event);
462 request.onerror = function(event) {
463 // Errors can be handled here.
465 aCallback(false, event);
469 clear: function(aCallback) {
473 var transaction = mainDB.transaction([this.objStore], "readwrite");
474 var request = transaction.objectStore(this.objStore).clear();
475 request.onsuccess = function(event) {
478 aCallback(success, event);
480 request.onerror = function(event) {
481 // Errors can be handled here.
483 aCallback(success, event);