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 if (navigator.platform.length == "") {
40 // For Firefox OS, don't display the "save" button.
41 // Do this by setting the debugHide class for testing in debug mode.
42 document.getElementById("saveTrackButton").classList.add("debugHide");
49 var waitForInitAndDraw = function() {
50 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
51 if (gWaitCounter <= 0)
54 console.log("Loading failed (waiting for init).");
56 gMapPrefsLoaded = true;
58 gActionLabel.textContent = "";
59 gAction.style.display = "none";
60 setTracking(document.getElementById("trackCheckbox"));
63 setTimeout(waitForInitAndDraw, 100);
69 window.onresize = function() {
75 var request = window.indexedDB.open("MainDB", 2);
76 request.onerror = function(event) {
77 // Errors can be handled here. Error codes explain in:
78 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
80 console.log("error opening mainDB: " + event.target.errorCode);
82 request.onsuccess = function(event) {
83 mainDB = request.result;
85 request.onupgradeneeded = function(event) {
86 mainDB = request.result;
87 var ver = mainDB.version || 0; // version is empty string for a new DB
89 console.log("mainDB has version " + ver + ", upgrade needed.");
90 if (!mainDB.objectStoreNames.contains("prefs")) {
91 // Create a "prefs" objectStore.
92 var prefsStore = mainDB.createObjectStore("prefs");
94 if (!mainDB.objectStoreNames.contains("track")) {
95 // Create a "track" objectStore.
96 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
98 if (!mainDB.objectStoreNames.contains("tilecache")) {
99 // Create a "tilecache" objectStore.
100 var tilecacheStore = mainDB.createObjectStore("tilecache");
102 mainDB.onversionchange = function(event) {
111 if (gUIHideCountdown <= 0) {
112 var areas = document.getElementsByClassName('overlayArea');
113 for (var i = 0; i <= areas.length - 1; i++) {
114 areas[i].classList.remove("hidden");
116 setTimeout(maybeHideUI, 1000);
118 gUIHideCountdown = 5;
121 function maybeHideUI() {
123 if (gUIHideCountdown <= 0) {
124 var areas = document.getElementsByClassName('overlayArea');
125 for (var i = 0; i <= areas.length - 1; i++) {
126 areas[i].classList.add("hidden");
130 setTimeout(maybeHideUI, 1000);
134 function toggleTrackArea() {
135 var fs = document.getElementById("trackArea");
136 if (fs.style.display != "block") {
137 fs.style.display = "block";
141 fs.style.display = "none";
145 function toggleSettings() {
146 var fs = document.getElementById("settingsArea");
147 if (fs.style.display != "block") {
148 fs.style.display = "block";
152 fs.style.display = "none";
156 function toggleFullscreen() {
157 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
158 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
159 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
160 if (document.cancelFullScreen) {
161 document.cancelFullScreen();
162 } else if (document.mozCancelFullScreen) {
163 document.mozCancelFullScreen();
164 } else if (document.webkitCancelFullScreen) {
165 document.webkitCancelFullScreen();
169 var elem = document.getElementById("body");
170 if (elem.requestFullScreen) {
171 elem.requestFullScreen();
172 } else if (elem.mozRequestFullScreen) {
173 elem.mozRequestFullScreen();
174 } else if (elem.webkitRequestFullScreen) {
175 elem.webkitRequestFullScreen();
181 handleEvent: function(aEvent) {
182 var touchEvent = aEvent.type.indexOf('touch') != -1;
184 switch (aEvent.type) {
197 function makeISOString(aTimestamp) {
198 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
199 var tsDate = new Date(aTimestamp);
200 return tsDate.getUTCFullYear() + "-" +
201 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
202 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
203 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
204 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
205 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
208 function saveTrack() {
210 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
211 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
212 out += ' <trk>' + "\n";
213 out += ' <trkseg>' + "\n";
214 for (var i = 0; i < gTrack.length; i++) {
215 if (gTrack[i].beginSegment && i > 0) {
216 out += ' </trkseg>' + "\n";
217 out += ' <trkseg>' + "\n";
219 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
220 gTrack[i].coords.longitude + '">' + "\n";
221 if (gTrack[i].coords.altitude) {
222 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
224 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
225 out += ' </trkpt>' + "\n";
227 out += ' </trkseg>' + "\n";
228 out += ' </trk>' + "\n";
229 out += '</gpx>' + "\n";
230 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
231 window.open(outDataURI, 'GPX Track');
235 function saveTrackDump() {
237 var out = JSON.stringify(gTrack);
238 var outDataURI = "data:application/json," + encodeURIComponent(out);
239 window.open(outDataURI, 'JSON dump');
246 get: function(aKey, aCallback) {
249 var transaction = mainDB.transaction([this.objStore]);
250 var request = transaction.objectStore(this.objStore).get(aKey);
251 request.onsuccess = function(event) {
252 aCallback(request.result, event);
254 request.onerror = function(event) {
255 // Errors can be handled here.
256 aCallback(undefined, event);
260 set: function(aKey, aValue, aCallback) {
264 var transaction = mainDB.transaction([this.objStore], "readwrite");
265 var objStore = transaction.objectStore(this.objStore);
266 var request = objStore.put(aValue, aKey);
267 request.onsuccess = function(event) {
270 aCallback(success, event);
272 request.onerror = function(event) {
273 // Errors can be handled here.
275 aCallback(success, event);
279 unset: function(aKey, aCallback) {
283 var transaction = mainDB.transaction([this.objStore], "readwrite");
284 var request = transaction.objectStore(this.objStore).delete(aKey);
285 request.onsuccess = function(event) {
288 aCallback(success, event);
290 request.onerror = function(event) {
291 // Errors can be handled here.
293 aCallback(success, event);
301 getList: function(aCallback) {
304 var transaction = mainDB.transaction([this.objStore]);
305 var objStore = transaction.objectStore(this.objStore);
306 if (objStore.getAll) { // currently Mozilla-specific
307 objStore.getAll().onsuccess = function(event) {
308 aCallback(event.target.result);
311 else { // Use cursor (standard method).
313 objStore.openCursor().onsuccess = function(event) {
314 var cursor = event.target.result;
316 tPoints.push(cursor.value);
326 push: function(aValue, aCallback) {
329 var transaction = mainDB.transaction([this.objStore], "readwrite");
330 var objStore = transaction.objectStore(this.objStore);
331 var request = objStore.add(aValue);
332 request.onsuccess = function(event) {
334 aCallback(request.result, event);
336 request.onerror = function(event) {
337 // Errors can be handled here.
339 aCallback(false, event);
343 clear: function(aCallback) {
347 var transaction = mainDB.transaction([this.objStore], "readwrite");
348 var request = transaction.objectStore(this.objStore).clear();
349 request.onsuccess = function(event) {
352 aCallback(success, event);
354 request.onerror = function(event) {
355 // Errors can be handled here.
357 aCallback(success, event);