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");
46 // For now, only show the upload UI on Firefox OS.
47 document.getElementById("uploadTrackButton").classList.remove("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;
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();
205 handleEvent: function(aEvent) {
206 var touchEvent = aEvent.type.indexOf('touch') != -1;
208 switch (aEvent.type) {
222 function setUploadField(aField) {
225 gPrefs.set(gDebug ? "osm_dev_user" : "osm_user", aField.value);
226 document.getElementById("uploadTrackButton").disabled = !aField.value.length;
229 gPrefs.set(gDebug ? "osm_dev_pwd" : "osm_pwd", aField.value);
234 function makeISOString(aTimestamp) {
235 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
236 var tsDate = new Date(aTimestamp);
237 return tsDate.getUTCFullYear() + "-" +
238 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
239 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
240 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
241 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
242 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
245 function convertTrack(aTargetFormat) {
247 switch (aTargetFormat) {
249 out += '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
250 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
252 out += ' <trk>' + "\n";
253 out += ' <trkseg>' + "\n";
254 for (var i = 0; i < gTrack.length; i++) {
255 if (gTrack[i].beginSegment && i > 0) {
256 out += ' </trkseg>' + "\n";
257 out += ' <trkseg>' + "\n";
259 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
260 gTrack[i].coords.longitude + '">' + "\n";
261 if (gTrack[i].coords.altitude) {
262 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
264 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
265 out += ' </trkpt>' + "\n";
267 out += ' </trkseg>' + "\n";
268 out += ' </trk>' + "\n";
270 out += '</gpx>' + "\n";
273 out = JSON.stringify(gTrack);
281 function saveTrack() {
283 var outDataURI = "data:application/gpx+xml," +
284 encodeURIComponent(convertTrack("gpx"));
285 window.open(outDataURI, 'GPX Track');
289 function saveTrackDump() {
291 var outDataURI = "data:application/json," +
292 encodeURIComponent(convertTrack("json"));
293 window.open(outDataURI, 'JSON dump');
297 function uploadTrack() {
298 // See http://wiki.openstreetmap.org/wiki/Api06#Uploading_traces
299 var trackBlob = new Blob([convertTrack("gpx")], { "type" : "application/gpx+xml" });
300 var formData = new FormData();
301 formData.append("file", trackBlob);
302 formData.append("description", "Track recorded via Lantea Maps");
303 //formData.append("tags", "");
304 formData.append("visibility", "private");
305 var XHR = new XMLHttpRequest();
306 XHR.onreadystatechange = function() {
307 if (XHR.readyState == 4 && XHR.status == 200) {
309 reportUploadStatus(true);
310 } else if (XHR.readyState == 4 && XHR.status != 200) {
311 // fetched the wrong page or network error...
312 reportUploadStatus(false);
315 XHR.open("POST", gOSMAPIURL + "api/0.6/gpx/create", true);
316 // Cross-Origin XHR doesn't allow username/password (HTTP Auth), so need to look into OAuth.
317 // But for now, we'll ask the user for entering credentials with this.
318 XHR.withCredentials = true;
323 reportUploadStatus(false, e);
327 function reportUploadStatus(aSuccess, aMessage) {
328 alert(aMessage ? aMessage : (aSuccess ? "success" : "failure"));
334 get: function(aKey, aCallback) {
337 var transaction = mainDB.transaction([this.objStore]);
338 var request = transaction.objectStore(this.objStore).get(aKey);
339 request.onsuccess = function(event) {
340 aCallback(request.result, event);
342 request.onerror = function(event) {
343 // Errors can be handled here.
344 aCallback(undefined, event);
348 set: function(aKey, aValue, aCallback) {
352 var transaction = mainDB.transaction([this.objStore], "readwrite");
353 var objStore = transaction.objectStore(this.objStore);
354 var request = objStore.put(aValue, aKey);
355 request.onsuccess = function(event) {
358 aCallback(success, event);
360 request.onerror = function(event) {
361 // Errors can be handled here.
363 aCallback(success, event);
367 unset: function(aKey, aCallback) {
371 var transaction = mainDB.transaction([this.objStore], "readwrite");
372 var request = transaction.objectStore(this.objStore).delete(aKey);
373 request.onsuccess = function(event) {
376 aCallback(success, event);
378 request.onerror = function(event) {
379 // Errors can be handled here.
381 aCallback(success, event);
389 getList: function(aCallback) {
392 var transaction = mainDB.transaction([this.objStore]);
393 var objStore = transaction.objectStore(this.objStore);
394 if (objStore.getAll) { // currently Mozilla-specific
395 objStore.getAll().onsuccess = function(event) {
396 aCallback(event.target.result);
399 else { // Use cursor (standard method).
401 objStore.openCursor().onsuccess = function(event) {
402 var cursor = event.target.result;
404 tPoints.push(cursor.value);
414 push: function(aValue, aCallback) {
417 var transaction = mainDB.transaction([this.objStore], "readwrite");
418 var objStore = transaction.objectStore(this.objStore);
419 var request = objStore.add(aValue);
420 request.onsuccess = function(event) {
422 aCallback(request.result, event);
424 request.onerror = function(event) {
425 // Errors can be handled here.
427 aCallback(false, event);
431 clear: function(aCallback) {
435 var transaction = mainDB.transaction([this.objStore], "readwrite");
436 var request = transaction.objectStore(this.objStore).clear();
437 request.onsuccess = function(event) {
440 aCallback(success, event);
442 request.onerror = function(event) {
443 // Errors can be handled here.
445 aCallback(success, event);