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");
53 gOSMAPIURL = "http://api06.dev.openstreetmap.org/";
60 var waitForInitAndDraw = function() {
61 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
62 if (gWaitCounter <= 0)
65 console.log("Loading failed (waiting for init).");
67 gMapPrefsLoaded = true;
69 gActionLabel.textContent = "";
70 gAction.style.display = "none";
71 setTracking(document.getElementById("trackCheckbox"));
72 gPrefs.get(gDebug ? "osm_dev_user" : "osm_user", function(aValue) {
74 document.getElementById("uploadUser").value = aValue;
75 document.getElementById("uploadTrackButton").disabled = false;
78 gPrefs.get(gDebug ? "osm_dev_pwd" : "osm_pwd", function(aValue) {
79 var upwd = document.getElementById("uploadPwd");
81 document.getElementById("uploadPwd").value = aValue;
85 setTimeout(waitForInitAndDraw, 100);
91 window.onresize = function() {
97 var request = window.indexedDB.open("MainDB-lantea", 2);
98 request.onerror = function(event) {
99 // Errors can be handled here. Error codes explain in:
100 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
102 console.log("error opening mainDB: " + event.target.errorCode);
104 request.onsuccess = function(event) {
105 mainDB = request.result;
107 request.onupgradeneeded = function(event) {
108 mainDB = request.result;
109 var ver = mainDB.version || 0; // version is empty string for a new DB
111 console.log("mainDB has version " + ver + ", upgrade needed.");
112 if (!mainDB.objectStoreNames.contains("prefs")) {
113 // Create a "prefs" objectStore.
114 var prefsStore = mainDB.createObjectStore("prefs");
116 if (!mainDB.objectStoreNames.contains("track")) {
117 // Create a "track" objectStore.
118 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
120 if (!mainDB.objectStoreNames.contains("tilecache")) {
121 // Create a "tilecache" objectStore.
122 var tilecacheStore = mainDB.createObjectStore("tilecache");
124 mainDB.onversionchange = function(event) {
133 if (gUIHideCountdown <= 0) {
134 var areas = document.getElementsByClassName('overlayArea');
135 for (var i = 0; i <= areas.length - 1; i++) {
136 areas[i].classList.remove("hidden");
138 setTimeout(maybeHideUI, 1000);
140 gUIHideCountdown = 5;
143 function maybeHideUI() {
145 if (gUIHideCountdown <= 0) {
146 var areas = document.getElementsByClassName('overlayArea');
147 for (var i = 0; i <= areas.length - 1; i++) {
148 areas[i].classList.add("hidden");
152 setTimeout(maybeHideUI, 1000);
156 function toggleTrackArea() {
157 var fs = document.getElementById("trackArea");
158 if (fs.style.display != "block") {
159 fs.style.display = "block";
163 fs.style.display = "none";
167 function toggleSettings() {
168 var fs = document.getElementById("settingsArea");
169 if (fs.style.display != "block") {
170 fs.style.display = "block";
174 fs.style.display = "none";
178 function toggleFullscreen() {
179 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
180 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
181 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
182 if (document.cancelFullScreen) {
183 document.cancelFullScreen();
184 } else if (document.mozCancelFullScreen) {
185 document.mozCancelFullScreen();
186 } else if (document.webkitCancelFullScreen) {
187 document.webkitCancelFullScreen();
191 var elem = document.getElementById("body");
192 if (elem.requestFullScreen) {
193 elem.requestFullScreen();
194 } else if (elem.mozRequestFullScreen) {
195 elem.mozRequestFullScreen();
196 } else if (elem.webkitRequestFullScreen) {
197 elem.webkitRequestFullScreen();
203 handleEvent: function(aEvent) {
204 var touchEvent = aEvent.type.indexOf('touch') != -1;
206 switch (aEvent.type) {
220 function setUploadField(aField) {
223 gPrefs.set(gDebug ? "osm_dev_user" : "osm_user", aField.value);
224 document.getElementById("uploadTrackButton").disabled = !aField.value.length;
227 gPrefs.set(gDebug ? "osm_dev_pwd" : "osm_pwd", aField.value);
232 function makeISOString(aTimestamp) {
233 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
234 var tsDate = new Date(aTimestamp);
235 return tsDate.getUTCFullYear() + "-" +
236 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
237 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
238 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
239 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
240 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
243 function convertTrack(aTargetFormat) {
245 switch (aTargetFormat) {
247 out += '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
248 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
250 out += ' <trk>' + "\n";
251 out += ' <trkseg>' + "\n";
252 for (var i = 0; i < gTrack.length; i++) {
253 if (gTrack[i].beginSegment && i > 0) {
254 out += ' </trkseg>' + "\n";
255 out += ' <trkseg>' + "\n";
257 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
258 gTrack[i].coords.longitude + '">' + "\n";
259 if (gTrack[i].coords.altitude) {
260 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
262 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
263 out += ' </trkpt>' + "\n";
265 out += ' </trkseg>' + "\n";
266 out += ' </trk>' + "\n";
268 out += '</gpx>' + "\n";
271 out = JSON.stringify(gTrack);
279 function saveTrack() {
281 var outDataURI = "data:application/gpx+xml," +
282 encodeURIComponent(convertTrack("gpx"));
283 window.open(outDataURI, 'GPX Track');
287 function saveTrackDump() {
289 var outDataURI = "data:application/json," +
290 encodeURIComponent(convertTrack("json"));
291 window.open(outDataURI, 'JSON dump');
295 function uploadTrack() {
296 // See http://wiki.openstreetmap.org/wiki/Api06#Uploading_traces
297 var trackBlob = new Blob([convertTrack("gpx")], { "type" : "application/gpx+xml" });
298 var formData = new FormData();
299 formData.append("file", trackBlob);
300 formData.append("description", "Track recorded via Lantea Maps");
301 //formData.append("tags", "");
302 formData.append("visibility", "private");
303 var XHR = new XMLHttpRequest();
304 XHR.onreadystatechange = function() {
305 if (XHR.readyState == 4 && XHR.status == 200) {
307 reportUploadStatus(true);
308 } else if (XHR.readyState == 4 && XHR.status != 200) {
309 // fetched the wrong page or network error...
310 reportUploadStatus(false);
313 XHR.open("POST", gOSMAPIURL + "api/0.6/gpx/create", true);
314 // Cross-Origin XHR doesn't allow username/password (HTTP Auth), so need to look into OAuth.
315 // But for now, we'll ask the user for entering credentials with this.
316 XHR.withCredentials = true;
321 reportUploadStatus(false, e);
325 function reportUploadStatus(aSuccess, aMessage) {
326 alert(aMessage ? aMessage : (aSuccess ? "success" : "failure"));
332 get: function(aKey, aCallback) {
335 var transaction = mainDB.transaction([this.objStore]);
336 var request = transaction.objectStore(this.objStore).get(aKey);
337 request.onsuccess = function(event) {
338 aCallback(request.result, event);
340 request.onerror = function(event) {
341 // Errors can be handled here.
342 aCallback(undefined, event);
346 set: function(aKey, aValue, aCallback) {
350 var transaction = mainDB.transaction([this.objStore], "readwrite");
351 var objStore = transaction.objectStore(this.objStore);
352 var request = objStore.put(aValue, aKey);
353 request.onsuccess = function(event) {
356 aCallback(success, event);
358 request.onerror = function(event) {
359 // Errors can be handled here.
361 aCallback(success, event);
365 unset: function(aKey, aCallback) {
369 var transaction = mainDB.transaction([this.objStore], "readwrite");
370 var request = transaction.objectStore(this.objStore).delete(aKey);
371 request.onsuccess = function(event) {
374 aCallback(success, event);
376 request.onerror = function(event) {
377 // Errors can be handled here.
379 aCallback(success, event);
387 getList: function(aCallback) {
390 var transaction = mainDB.transaction([this.objStore]);
391 var objStore = transaction.objectStore(this.objStore);
392 if (objStore.getAll) { // currently Mozilla-specific
393 objStore.getAll().onsuccess = function(event) {
394 aCallback(event.target.result);
397 else { // Use cursor (standard method).
399 objStore.openCursor().onsuccess = function(event) {
400 var cursor = event.target.result;
402 tPoints.push(cursor.value);
412 push: function(aValue, aCallback) {
415 var transaction = mainDB.transaction([this.objStore], "readwrite");
416 var objStore = transaction.objectStore(this.objStore);
417 var request = objStore.add(aValue);
418 request.onsuccess = function(event) {
420 aCallback(request.result, event);
422 request.onerror = function(event) {
423 // Errors can be handled here.
425 aCallback(false, event);
429 clear: function(aCallback) {
433 var transaction = mainDB.transaction([this.objStore], "readwrite");
434 var request = transaction.objectStore(this.objStore).clear();
435 request.onsuccess = function(event) {
438 aCallback(success, event);
440 request.onerror = function(event) {
441 // Errors can be handled here.
443 aCallback(success, event);