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);
34 if (/Mozilla\/5.0 \(Mobile;/.test(navigator.useragent)) {
35 // For Firefox OS, don't display the "save" button.
36 // Do this by setting the debugHide class for testing in debug mode.
37 document.getElementById("saveTrackButton").classList.add("debugHide");
45 window.onresize = function() {
51 var request = iDB.open("MainDB", 1);
52 request.onerror = function(event) {
53 // Errors can be handled here. Error codes explain in:
54 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
55 //document.getElementById("debug").textContent =
56 // "error opening mainDB: " + event.target.errorCode;
58 request.onsuccess = function(event) {
59 //document.getElementById("debug").textContent = "mainDB opened.";
60 mainDB = request.result;
62 request.onupgradeneeded = function(event) {
63 mainDB = request.result;
64 //document.getElementById("debug").textContent = "mainDB upgraded.";
65 // Create a "prefs" objectStore.
66 var prefsStore = mainDB.createObjectStore("prefs");
67 // Create a "track" objectStore.
68 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
69 mainDB.onversionchange = function(event) {
78 if (gUIHideCountdown <= 0) {
79 var areas = document.getElementsByClassName('overlayArea');
80 for (var i = 0; i <= areas.length - 1; i++) {
81 areas[i].classList.remove("hidden");
83 setTimeout(maybeHideUI, 1000);
88 function maybeHideUI() {
90 if (gUIHideCountdown <= 0) {
91 var areas = document.getElementsByClassName('overlayArea');
92 for (var i = 0; i <= areas.length - 1; i++) {
93 areas[i].classList.add("hidden");
97 setTimeout(maybeHideUI, 1000);
101 function toggleTrackArea() {
102 var fs = document.getElementById("trackArea");
103 if (fs.style.display != "block") {
104 fs.style.display = "block";
108 fs.style.display = "none";
112 function toggleSettings() {
113 var fs = document.getElementById("settingsArea");
114 if (fs.style.display != "block") {
115 fs.style.display = "block";
119 fs.style.display = "none";
123 function toggleFullscreen() {
124 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
125 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
126 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
127 if (document.cancelFullScreen) {
128 document.cancelFullScreen();
129 } else if (document.mozCancelFullScreen) {
130 document.mozCancelFullScreen();
131 } else if (document.webkitCancelFullScreen) {
132 document.webkitCancelFullScreen();
136 var elem = document.getElementById("body");
137 if (elem.requestFullScreen) {
138 elem.requestFullScreen();
139 } else if (elem.mozRequestFullScreen) {
140 elem.mozRequestFullScreen();
141 } else if (elem.webkitRequestFullScreen) {
142 elem.webkitRequestFullScreen();
148 handleEvent: function(aEvent) {
149 var touchEvent = aEvent.type.indexOf('touch') != -1;
151 switch (aEvent.type) {
164 function makeISOString(aTimestamp) {
165 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
166 var tsDate = new Date(aTimestamp);
167 return tsDate.getUTCFullYear() + "-" +
168 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
169 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
170 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
171 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
172 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
175 function saveTrack() {
177 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
178 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
179 out += ' <trk>' + "\n";
180 out += ' <trkseg>' + "\n";
181 for (var i = 0; i < gTrack.length; i++) {
182 if (gTrack[i].beginSegment && i > 0) {
183 out += ' </trkseg>' + "\n";
184 out += ' <trkseg>' + "\n";
186 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
187 gTrack[i].coords.longitude + '">' + "\n";
188 if (gTrack[i].coords.altitude) {
189 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
191 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
192 out += ' </trkpt>' + "\n";
194 out += ' </trkseg>' + "\n";
195 out += ' </trk>' + "\n";
196 out += '</gpx>' + "\n";
197 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
198 window.open(outDataURI, 'GPX Track');
202 function saveTrackDump() {
204 var out = JSON.stringify(gTrack);
205 var outDataURI = "data:application/json," + encodeURIComponent(out);
206 window.open(outDataURI, 'JSON dump');
213 get: function(aKey, aCallback) {
216 var transaction = mainDB.transaction([this.objStore]);
217 var request = transaction.objectStore(this.objStore).get(aKey);
218 request.onsuccess = function(event) {
219 aCallback(request.result, event);
221 request.onerror = function(event) {
222 // Errors can be handled here.
223 aCallback(undefined, event);
227 set: function(aKey, aValue, aCallback) {
231 var transaction = mainDB.transaction([this.objStore], "readwrite");
232 var objStore = transaction.objectStore(this.objStore);
233 var request = objStore.put(aValue, aKey);
234 request.onsuccess = function(event) {
237 aCallback(success, event);
239 request.onerror = function(event) {
240 // Errors can be handled here.
242 aCallback(success, event);
246 unset: function(aKey, aCallback) {
250 var transaction = mainDB.transaction([this.objStore], "readwrite");
251 var request = transaction.objectStore(this.objStore).delete(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);
268 getList: function(aCallback) {
271 var transaction = mainDB.transaction([this.objStore]);
272 var objStore = transaction.objectStore(this.objStore);
273 if (objStore.getAll) { // currently Mozilla-specific
274 objStore.getAll().onsuccess = function(event) {
275 aCallback(event.target.result);
278 else { // Use cursor (standard method).
280 objStore.openCursor().onsuccess = function(event) {
281 var cursor = event.target.result;
283 tPoints.push(cursor.value);
293 push: function(aValue, aCallback) {
296 var transaction = mainDB.transaction([this.objStore], "readwrite");
297 var objStore = transaction.objectStore(this.objStore);
298 var request = objStore.add(aValue);
299 request.onsuccess = function(event) {
301 aCallback(request.result, event);
303 request.onerror = function(event) {
304 // Errors can be handled here.
306 aCallback(false, event);
310 clear: function(aCallback) {
314 var transaction = mainDB.transaction([this.objStore], "readwrite");
315 var request = transaction.objectStore(this.objStore).clear();
316 request.onsuccess = function(event) {
319 aCallback(success, event);
321 request.onerror = function(event) {
322 // Errors can be handled here.
324 aCallback(success, event);