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;
13 window.onload = function() {
14 gAction = document.getElementById("action");
15 gActionLabel = document.getElementById("actionlabel");
17 var mSel = document.getElementById("mapSelector");
18 for (var mapStyle in gMapStyles) {
19 var opt = document.createElement("option");
21 opt.text = gMapStyles[mapStyle].name;
25 var areas = document.getElementsByClassName('overlayArea');
26 for (var i = 0; i <= areas.length - 1; i++) {
27 areas[i].addEventListener("mouseup", uiEvHandler, false);
28 areas[i].addEventListener("mousemove", uiEvHandler, false);
29 areas[i].addEventListener("mousedown", uiEvHandler, false);
30 areas[i].addEventListener("mouseout", uiEvHandler, false);
32 areas[i].addEventListener("touchstart", uiEvHandler, false);
33 areas[i].addEventListener("touchmove", uiEvHandler, false);
34 areas[i].addEventListener("touchend", uiEvHandler, false);
35 areas[i].addEventListener("touchcancel", uiEvHandler, false);
36 areas[i].addEventListener("touchleave", uiEvHandler, false);
39 document.getElementById("body").addEventListener("keydown", uiEvHandler, false);
41 if (navigator.platform.length == "") {
42 // For Firefox OS, don't display the "save" button.
43 // Do this by setting the debugHide class for testing in debug mode.
44 document.getElementById("saveTrackButton").classList.add("debugHide");
51 var waitForInitAndDraw = function() {
52 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
53 if (gWaitCounter <= 0)
56 console.log("Loading failed (waiting for init).");
58 gMapPrefsLoaded = true;
60 gActionLabel.textContent = "";
61 gAction.style.display = "none";
62 setTracking(document.getElementById("trackCheckbox"));
65 setTimeout(waitForInitAndDraw, 100);
71 window.onresize = function() {
77 var request = window.indexedDB.open("MainDB-lantea", 2);
78 request.onerror = function(event) {
79 // Errors can be handled here. Error codes explain in:
80 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
82 console.log("error opening mainDB: " + event.target.errorCode);
84 request.onsuccess = function(event) {
85 mainDB = request.result;
87 request.onupgradeneeded = function(event) {
88 mainDB = request.result;
89 var ver = mainDB.version || 0; // version is empty string for a new DB
91 console.log("mainDB has version " + ver + ", upgrade needed.");
92 if (!mainDB.objectStoreNames.contains("prefs")) {
93 // Create a "prefs" objectStore.
94 var prefsStore = mainDB.createObjectStore("prefs");
96 if (!mainDB.objectStoreNames.contains("track")) {
97 // Create a "track" objectStore.
98 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
100 if (!mainDB.objectStoreNames.contains("tilecache")) {
101 // Create a "tilecache" objectStore.
102 var tilecacheStore = mainDB.createObjectStore("tilecache");
104 mainDB.onversionchange = function(event) {
113 if (gUIHideCountdown <= 0) {
114 var areas = document.getElementsByClassName('overlayArea');
115 for (var i = 0; i <= areas.length - 1; i++) {
116 areas[i].classList.remove("hidden");
118 setTimeout(maybeHideUI, 1000);
120 gUIHideCountdown = 5;
123 function maybeHideUI() {
125 if (gUIHideCountdown <= 0) {
126 var areas = document.getElementsByClassName('overlayArea');
127 for (var i = 0; i <= areas.length - 1; i++) {
128 areas[i].classList.add("hidden");
132 setTimeout(maybeHideUI, 1000);
136 function toggleTrackArea() {
137 var fs = document.getElementById("trackArea");
138 if (fs.style.display != "block") {
139 fs.style.display = "block";
143 fs.style.display = "none";
147 function toggleSettings() {
148 var fs = document.getElementById("settingsArea");
149 if (fs.style.display != "block") {
150 fs.style.display = "block";
154 fs.style.display = "none";
158 function toggleFullscreen() {
159 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
160 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
161 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
162 if (document.cancelFullScreen) {
163 document.cancelFullScreen();
164 } else if (document.mozCancelFullScreen) {
165 document.mozCancelFullScreen();
166 } else if (document.webkitCancelFullScreen) {
167 document.webkitCancelFullScreen();
171 var elem = document.getElementById("body");
172 if (elem.requestFullScreen) {
173 elem.requestFullScreen();
174 } else if (elem.mozRequestFullScreen) {
175 elem.mozRequestFullScreen();
176 } else if (elem.webkitRequestFullScreen) {
177 elem.webkitRequestFullScreen();
183 handleEvent: function(aEvent) {
184 var touchEvent = aEvent.type.indexOf('touch') != -1;
186 switch (aEvent.type) {
200 function makeISOString(aTimestamp) {
201 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
202 var tsDate = new Date(aTimestamp);
203 return tsDate.getUTCFullYear() + "-" +
204 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
205 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
206 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
207 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
208 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
211 function saveTrack() {
213 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
214 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
215 out += ' <trk>' + "\n";
216 out += ' <trkseg>' + "\n";
217 for (var i = 0; i < gTrack.length; i++) {
218 if (gTrack[i].beginSegment && i > 0) {
219 out += ' </trkseg>' + "\n";
220 out += ' <trkseg>' + "\n";
222 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
223 gTrack[i].coords.longitude + '">' + "\n";
224 if (gTrack[i].coords.altitude) {
225 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
227 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
228 out += ' </trkpt>' + "\n";
230 out += ' </trkseg>' + "\n";
231 out += ' </trk>' + "\n";
232 out += '</gpx>' + "\n";
233 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
234 window.open(outDataURI, 'GPX Track');
238 function saveTrackDump() {
240 var out = JSON.stringify(gTrack);
241 var outDataURI = "data:application/json," + encodeURIComponent(out);
242 window.open(outDataURI, 'JSON dump');
249 get: function(aKey, aCallback) {
252 var transaction = mainDB.transaction([this.objStore]);
253 var request = transaction.objectStore(this.objStore).get(aKey);
254 request.onsuccess = function(event) {
255 aCallback(request.result, event);
257 request.onerror = function(event) {
258 // Errors can be handled here.
259 aCallback(undefined, event);
263 set: function(aKey, aValue, aCallback) {
267 var transaction = mainDB.transaction([this.objStore], "readwrite");
268 var objStore = transaction.objectStore(this.objStore);
269 var request = objStore.put(aValue, aKey);
270 request.onsuccess = function(event) {
273 aCallback(success, event);
275 request.onerror = function(event) {
276 // Errors can be handled here.
278 aCallback(success, event);
282 unset: function(aKey, aCallback) {
286 var transaction = mainDB.transaction([this.objStore], "readwrite");
287 var request = transaction.objectStore(this.objStore).delete(aKey);
288 request.onsuccess = function(event) {
291 aCallback(success, event);
293 request.onerror = function(event) {
294 // Errors can be handled here.
296 aCallback(success, event);
304 getList: function(aCallback) {
307 var transaction = mainDB.transaction([this.objStore]);
308 var objStore = transaction.objectStore(this.objStore);
309 if (objStore.getAll) { // currently Mozilla-specific
310 objStore.getAll().onsuccess = function(event) {
311 aCallback(event.target.result);
314 else { // Use cursor (standard method).
316 objStore.openCursor().onsuccess = function(event) {
317 var cursor = event.target.result;
319 tPoints.push(cursor.value);
329 push: function(aValue, aCallback) {
332 var transaction = mainDB.transaction([this.objStore], "readwrite");
333 var objStore = transaction.objectStore(this.objStore);
334 var request = objStore.add(aValue);
335 request.onsuccess = function(event) {
337 aCallback(request.result, event);
339 request.onerror = function(event) {
340 // Errors can be handled here.
342 aCallback(false, event);
346 clear: function(aCallback) {
350 var transaction = mainDB.transaction([this.objStore], "readwrite");
351 var request = transaction.objectStore(this.objStore).clear();
352 request.onsuccess = function(event) {
355 aCallback(success, event);
357 request.onerror = function(event) {
358 // Errors can be handled here.
360 aCallback(success, event);