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;
12 window.onload = function() {
13 var mSel = document.getElementById("mapSelector");
14 for (var mapStyle in gMapStyles) {
15 var opt = document.createElement("option");
17 opt.text = gMapStyles[mapStyle].name;
21 var areas = document.getElementsByClassName('overlayArea');
22 for (var i = 0; i <= areas.length - 1; i++) {
23 areas[i].addEventListener("mouseup", uiEvHandler, false);
24 areas[i].addEventListener("mousemove", uiEvHandler, false);
25 areas[i].addEventListener("mousedown", uiEvHandler, false);
26 areas[i].addEventListener("mouseout", uiEvHandler, false);
28 areas[i].addEventListener("touchstart", uiEvHandler, false);
29 areas[i].addEventListener("touchmove", uiEvHandler, false);
30 areas[i].addEventListener("touchend", uiEvHandler, false);
31 areas[i].addEventListener("touchcancel", uiEvHandler, false);
32 areas[i].addEventListener("touchleave", uiEvHandler, false);
35 if (navigator.platform.length == "") {
36 // For Firefox OS, don't display the "save" button.
37 // Do this by setting the debugHide class for testing in debug mode.
38 document.getElementById("saveTrackButton").classList.add("debugHide");
45 var waitForInitAndDraw = function() {
46 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
47 if (gWaitCounter <= 0)
50 document.getElementById("debug").textContent = "Loading failed (waiting for init).";
52 gMapPrefsLoaded = true;
54 setTracking(document.getElementById("trackCheckbox"));
57 setTimeout(waitForInitAndDraw, 100);
63 window.onresize = function() {
69 var request = window.indexedDB.open("MainDB", 1);
70 request.onerror = function(event) {
71 // Errors can be handled here. Error codes explain in:
72 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
73 //document.getElementById("debug").textContent =
74 // "error opening mainDB: " + event.target.errorCode;
76 request.onsuccess = function(event) {
77 //document.getElementById("debug").textContent = "mainDB opened.";
78 mainDB = request.result;
80 request.onupgradeneeded = function(event) {
81 mainDB = request.result;
82 //document.getElementById("debug").textContent = "mainDB upgraded.";
83 // Create a "prefs" objectStore.
84 var prefsStore = mainDB.createObjectStore("prefs");
85 // Create a "track" objectStore.
86 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
87 mainDB.onversionchange = function(event) {
96 if (gUIHideCountdown <= 0) {
97 var areas = document.getElementsByClassName('overlayArea');
98 for (var i = 0; i <= areas.length - 1; i++) {
99 areas[i].classList.remove("hidden");
101 setTimeout(maybeHideUI, 1000);
103 gUIHideCountdown = 5;
106 function maybeHideUI() {
108 if (gUIHideCountdown <= 0) {
109 var areas = document.getElementsByClassName('overlayArea');
110 for (var i = 0; i <= areas.length - 1; i++) {
111 areas[i].classList.add("hidden");
115 setTimeout(maybeHideUI, 1000);
119 function toggleTrackArea() {
120 var fs = document.getElementById("trackArea");
121 if (fs.style.display != "block") {
122 fs.style.display = "block";
126 fs.style.display = "none";
130 function toggleSettings() {
131 var fs = document.getElementById("settingsArea");
132 if (fs.style.display != "block") {
133 fs.style.display = "block";
137 fs.style.display = "none";
141 function toggleFullscreen() {
142 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
143 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
144 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
145 if (document.cancelFullScreen) {
146 document.cancelFullScreen();
147 } else if (document.mozCancelFullScreen) {
148 document.mozCancelFullScreen();
149 } else if (document.webkitCancelFullScreen) {
150 document.webkitCancelFullScreen();
154 var elem = document.getElementById("body");
155 if (elem.requestFullScreen) {
156 elem.requestFullScreen();
157 } else if (elem.mozRequestFullScreen) {
158 elem.mozRequestFullScreen();
159 } else if (elem.webkitRequestFullScreen) {
160 elem.webkitRequestFullScreen();
166 handleEvent: function(aEvent) {
167 var touchEvent = aEvent.type.indexOf('touch') != -1;
169 switch (aEvent.type) {
182 function makeISOString(aTimestamp) {
183 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
184 var tsDate = new Date(aTimestamp);
185 return tsDate.getUTCFullYear() + "-" +
186 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
187 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
188 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
189 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
190 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
193 function saveTrack() {
195 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
196 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
197 out += ' <trk>' + "\n";
198 out += ' <trkseg>' + "\n";
199 for (var i = 0; i < gTrack.length; i++) {
200 if (gTrack[i].beginSegment && i > 0) {
201 out += ' </trkseg>' + "\n";
202 out += ' <trkseg>' + "\n";
204 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
205 gTrack[i].coords.longitude + '">' + "\n";
206 if (gTrack[i].coords.altitude) {
207 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
209 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
210 out += ' </trkpt>' + "\n";
212 out += ' </trkseg>' + "\n";
213 out += ' </trk>' + "\n";
214 out += '</gpx>' + "\n";
215 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
216 window.open(outDataURI, 'GPX Track');
220 function saveTrackDump() {
222 var out = JSON.stringify(gTrack);
223 var outDataURI = "data:application/json," + encodeURIComponent(out);
224 window.open(outDataURI, 'JSON dump');
231 get: function(aKey, aCallback) {
234 var transaction = mainDB.transaction([this.objStore]);
235 var request = transaction.objectStore(this.objStore).get(aKey);
236 request.onsuccess = function(event) {
237 aCallback(request.result, event);
239 request.onerror = function(event) {
240 // Errors can be handled here.
241 aCallback(undefined, event);
245 set: function(aKey, aValue, aCallback) {
249 var transaction = mainDB.transaction([this.objStore], "readwrite");
250 var objStore = transaction.objectStore(this.objStore);
251 var request = objStore.put(aValue, aKey);
252 request.onsuccess = function(event) {
255 aCallback(success, event);
257 request.onerror = function(event) {
258 // Errors can be handled here.
260 aCallback(success, event);
264 unset: function(aKey, aCallback) {
268 var transaction = mainDB.transaction([this.objStore], "readwrite");
269 var request = transaction.objectStore(this.objStore).delete(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);
286 getList: function(aCallback) {
289 var transaction = mainDB.transaction([this.objStore]);
290 var objStore = transaction.objectStore(this.objStore);
291 if (objStore.getAll) { // currently Mozilla-specific
292 objStore.getAll().onsuccess = function(event) {
293 aCallback(event.target.result);
296 else { // Use cursor (standard method).
298 objStore.openCursor().onsuccess = function(event) {
299 var cursor = event.target.result;
301 tPoints.push(cursor.value);
311 push: function(aValue, aCallback) {
314 var transaction = mainDB.transaction([this.objStore], "readwrite");
315 var objStore = transaction.objectStore(this.objStore);
316 var request = objStore.add(aValue);
317 request.onsuccess = function(event) {
319 aCallback(request.result, event);
321 request.onerror = function(event) {
322 // Errors can be handled here.
324 aCallback(false, event);
328 clear: function(aCallback) {
332 var transaction = mainDB.transaction([this.objStore], "readwrite");
333 var request = transaction.objectStore(this.objStore).clear();
334 request.onsuccess = function(event) {
337 aCallback(success, event);
339 request.onerror = function(event) {
340 // Errors can be handled here.
342 aCallback(success, event);