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 var iDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
9 var gUIHideCountdown = 0;
11 window.onload = function() {
12 var mSel = document.getElementById("mapSelector");
13 for (var mapStyle in gMapStyles) {
14 var opt = document.createElement("option");
16 opt.text = gMapStyles[mapStyle].name;
20 var areas = document.getElementsByClassName('overlayArea');
21 for (var i = 0; i <= areas.length - 1; i++) {
22 areas[i].addEventListener("mouseup", uiEvHandler, false);
23 areas[i].addEventListener("mousemove", uiEvHandler, false);
24 areas[i].addEventListener("mousedown", uiEvHandler, false);
25 areas[i].addEventListener("mouseout", uiEvHandler, false);
27 areas[i].addEventListener("touchstart", uiEvHandler, false);
28 areas[i].addEventListener("touchmove", uiEvHandler, false);
29 areas[i].addEventListener("touchend", uiEvHandler, false);
30 areas[i].addEventListener("touchcancel", uiEvHandler, false);
31 areas[i].addEventListener("touchleave", uiEvHandler, false);
40 window.onresize = function() {
46 var request = iDB.open("MainDB", 1);
47 request.onerror = function(event) {
48 // Errors can be handled here. Error codes explain in:
49 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
50 //document.getElementById("debug").textContent =
51 // "error opening mainDB: " + event.target.errorCode;
53 request.onsuccess = function(event) {
54 //document.getElementById("debug").textContent = "mainDB opened.";
55 mainDB = request.result;
57 request.onupgradeneeded = function(event) {
58 mainDB = request.result;
59 //document.getElementById("debug").textContent = "mainDB upgraded.";
60 // Create a "prefs" objectStore.
61 var prefsStore = mainDB.createObjectStore("prefs");
62 // Create a "track" objectStore.
63 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
64 mainDB.onversionchange = function(event) {
73 if (gUIHideCountdown <= 0) {
74 var areas = document.getElementsByClassName('overlayArea');
75 for (var i = 0; i <= areas.length - 1; i++) {
76 areas[i].classList.remove("hidden");
78 setTimeout(maybeHideUI, 1000);
83 function maybeHideUI() {
85 if (gUIHideCountdown <= 0) {
86 var areas = document.getElementsByClassName('overlayArea');
87 for (var i = 0; i <= areas.length - 1; i++) {
88 areas[i].classList.add("hidden");
92 setTimeout(maybeHideUI, 1000);
96 function toggleTrackArea() {
97 var fs = document.getElementById("trackArea");
98 if (fs.style.display != "block") {
99 fs.style.display = "block";
103 fs.style.display = "none";
107 function toggleSettings() {
108 var fs = document.getElementById("settingsArea");
109 if (fs.style.display != "block") {
110 fs.style.display = "block";
114 fs.style.display = "none";
118 function toggleFullscreen() {
119 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
120 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
121 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
122 if (document.cancelFullScreen) {
123 document.cancelFullScreen();
124 } else if (document.mozCancelFullScreen) {
125 document.mozCancelFullScreen();
126 } else if (document.webkitCancelFullScreen) {
127 document.webkitCancelFullScreen();
131 var elem = document.getElementById("body");
132 if (elem.requestFullScreen) {
133 elem.requestFullScreen();
134 } else if (elem.mozRequestFullScreen) {
135 elem.mozRequestFullScreen();
136 } else if (elem.webkitRequestFullScreen) {
137 elem.webkitRequestFullScreen();
143 handleEvent: function(aEvent) {
144 var touchEvent = aEvent.type.indexOf('touch') != -1;
146 switch (aEvent.type) {
159 function makeISOString(aTimestamp) {
160 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
161 var tsDate = new Date(aTimestamp);
162 return tsDate.getUTCFullYear() + "-" +
163 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
164 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
165 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
166 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
167 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
170 function saveTrack() {
172 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
173 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
174 out += ' <trk>' + "\n";
175 out += ' <trkseg>' + "\n";
176 for (var i = 0; i < gTrack.length; i++) {
177 if (gTrack[i].beginSegment && i > 0) {
178 out += ' </trkseg>' + "\n";
179 out += ' <trkseg>' + "\n";
181 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
182 gTrack[i].coords.longitude + '">' + "\n";
183 if (gTrack[i].coords.altitude) {
184 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
186 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
187 out += ' </trkpt>' + "\n";
189 out += ' </trkseg>' + "\n";
190 out += ' </trk>' + "\n";
191 out += '</gpx>' + "\n";
192 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
193 window.open(outDataURI, 'GPX Track');
197 function saveTrackDump() {
199 var out = JSON.stringify(gTrack);
200 var outDataURI = "data:application/json," + encodeURIComponent(out);
201 window.open(outDataURI, 'JSON dump');
208 get: function(aKey, aCallback) {
211 var transaction = mainDB.transaction([this.objStore]);
212 var request = transaction.objectStore(this.objStore).get(aKey);
213 request.onsuccess = function(event) {
214 aCallback(request.result, event);
216 request.onerror = function(event) {
217 // Errors can be handled here.
218 aCallback(undefined, event);
222 set: function(aKey, aValue, aCallback) {
226 var transaction = mainDB.transaction([this.objStore], "readwrite");
227 var objStore = transaction.objectStore(this.objStore);
228 var request = objStore.put(aValue, aKey);
229 request.onsuccess = function(event) {
232 aCallback(success, event);
234 request.onerror = function(event) {
235 // Errors can be handled here.
237 aCallback(success, event);
241 unset: function(aKey, aCallback) {
245 var transaction = mainDB.transaction([this.objStore], "readwrite");
246 var request = transaction.objectStore(this.objStore).delete(aKey);
247 request.onsuccess = function(event) {
250 aCallback(success, event);
252 request.onerror = function(event) {
253 // Errors can be handled here.
255 aCallback(success, event);
263 getList: function(aCallback) {
266 var transaction = mainDB.transaction([this.objStore]);
267 var objStore = transaction.objectStore(this.objStore);
268 if (objStore.getAll) { // currently Mozilla-specific
269 objStore.getAll().onsuccess = function(event) {
270 aCallback(event.target.result);
273 else { // Use cursor (standard method).
275 objStore.openCursor().onsuccess = function(event) {
276 var cursor = event.target.result;
278 tPoints.push(cursor.value);
288 push: function(aValue, aCallback) {
291 var transaction = mainDB.transaction([this.objStore], "readwrite");
292 var objStore = transaction.objectStore(this.objStore);
293 var request = objStore.add(aValue);
294 request.onsuccess = function(event) {
296 aCallback(request.result, event);
298 request.onerror = function(event) {
299 // Errors can be handled here.
301 aCallback(false, event);
305 clear: function(aCallback) {
309 var transaction = mainDB.transaction([this.objStore], "readwrite");
310 var request = transaction.objectStore(this.objStore).clear();
311 request.onsuccess = function(event) {
314 aCallback(success, event);
316 request.onerror = function(event) {
317 // Errors can be handled here.
319 aCallback(success, event);