add a comment about crashing with drawing a blob
[lantea.git] / js / ui.js
CommitLineData
a7393a71
RK
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/. */
23cd2dcc 4
993fd081 5// Get the best-available indexedDB object.
3d99a6fd 6window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
993fd081
RK
7var mainDB;
8
7a549148 9var gUIHideCountdown = 0;
4b1d0915 10var gWaitCounter = 0;
7a549148 11
b47b4a65
RK
12window.onload = function() {
13 var mSel = document.getElementById("mapSelector");
14 for (var mapStyle in gMapStyles) {
15 var opt = document.createElement("option");
16 opt.value = mapStyle;
17 opt.text = gMapStyles[mapStyle].name;
18 mSel.add(opt, null);
19 }
23cd2dcc 20
7a549148
RK
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);
27
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);
33 }
34
8e901dce 35 if (navigator.platform.length == "") {
b91b74a7
RK
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");
39 }
7a549148 40
993fd081 41 initDB();
b47b4a65 42 initMap();
4b1d0915
RK
43
44 var loopCnt = 0;
45 var waitForInitAndDraw = function() {
46 if ((gWaitCounter <= 0) || (loopCnt > 100)) {
47 if (gWaitCounter <= 0)
48 gWaitCounter = 0;
49 else
b03d1728 50 document.getElementById("debug").textContent = "Loading failed (waiting for init).";
4b1d0915
RK
51
52 gMapPrefsLoaded = true;
53 resizeAndDraw();
54 setTracking(document.getElementById("trackCheckbox"));
55 }
56 else
57 setTimeout(waitForInitAndDraw, 100);
58 loopCnt++;
59 };
60 waitForInitAndDraw();
b47b4a65
RK
61}
62
63window.onresize = function() {
64 resizeAndDraw();
65}
66
993fd081
RK
67function initDB() {
68 // Open DB.
a8634d37 69 var request = window.indexedDB.open("MainDB", 4);
993fd081
RK
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;
75 };
76 request.onsuccess = function(event) {
77 //document.getElementById("debug").textContent = "mainDB opened.";
78 mainDB = request.result;
79 };
80 request.onupgradeneeded = function(event) {
81 mainDB = request.result;
82 //document.getElementById("debug").textContent = "mainDB upgraded.";
a8634d37
RK
83 var ver = mainDB.version || 0; // version is empty string for a new DB
84 if (ver <= 1) {
85 // Create a "prefs" objectStore.
86 var prefsStore = mainDB.createObjectStore("prefs");
87 // Create a "track" objectStore.
88 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
89 }
90 if (ver <= 4) {
91 // Create a "tilecache" objectStore.
92 var tilecacheStore = mainDB.createObjectStore("tilecache");
93 }
993fd081
RK
94 mainDB.onversionchange = function(event) {
95 mainDB.close();
96 mainDB = undefined;
97 initDB();
98 };
99 };
100}
101
7a549148
RK
102function showUI() {
103 if (gUIHideCountdown <= 0) {
104 var areas = document.getElementsByClassName('overlayArea');
105 for (var i = 0; i <= areas.length - 1; i++) {
106 areas[i].classList.remove("hidden");
107 }
108 setTimeout(maybeHideUI, 1000);
109 }
110 gUIHideCountdown = 5;
111}
112
113function maybeHideUI() {
114 gUIHideCountdown--;
115 if (gUIHideCountdown <= 0) {
116 var areas = document.getElementsByClassName('overlayArea');
117 for (var i = 0; i <= areas.length - 1; i++) {
118 areas[i].classList.add("hidden");
119 }
120 }
121 else {
122 setTimeout(maybeHideUI, 1000);
123 }
124}
125
993fd081
RK
126function toggleTrackArea() {
127 var fs = document.getElementById("trackArea");
128 if (fs.style.display != "block") {
129 fs.style.display = "block";
7a549148 130 showUI();
993fd081
RK
131 }
132 else {
133 fs.style.display = "none";
134 }
135}
136
b47b4a65 137function toggleSettings() {
993fd081 138 var fs = document.getElementById("settingsArea");
b47b4a65
RK
139 if (fs.style.display != "block") {
140 fs.style.display = "block";
7a549148 141 showUI();
b47b4a65
RK
142 }
143 else {
144 fs.style.display = "none";
145 }
146}
99631a75 147
c5378747
RK
148function toggleFullscreen() {
149 if ((document.fullScreenElement && document.fullScreenElement !== null) ||
150 (document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
151 (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) {
152 if (document.cancelFullScreen) {
153 document.cancelFullScreen();
154 } else if (document.mozCancelFullScreen) {
155 document.mozCancelFullScreen();
156 } else if (document.webkitCancelFullScreen) {
157 document.webkitCancelFullScreen();
158 }
159 }
160 else {
161 var elem = document.getElementById("body");
162 if (elem.requestFullScreen) {
163 elem.requestFullScreen();
164 } else if (elem.mozRequestFullScreen) {
165 elem.mozRequestFullScreen();
166 } else if (elem.webkitRequestFullScreen) {
167 elem.webkitRequestFullScreen();
168 }
169 }
170}
171
7a549148
RK
172var uiEvHandler = {
173 handleEvent: function(aEvent) {
174 var touchEvent = aEvent.type.indexOf('touch') != -1;
175
176 switch (aEvent.type) {
177 case "mousedown":
178 case "touchstart":
179 case "mousemove":
180 case "touchmove":
181 case "mouseup":
182 case "touchend":
183 showUI();
184 break;
185 }
186 }
187};
188
99631a75
RK
189function makeISOString(aTimestamp) {
190 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
191 var tsDate = new Date(aTimestamp);
192 return tsDate.getUTCFullYear() + "-" +
193 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
194 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
195 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
196 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
197 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
198}
199
200function saveTrack() {
201 if (gTrack.length) {
202 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
203 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
204 out += ' <trk>' + "\n";
205 out += ' <trkseg>' + "\n";
206 for (var i = 0; i < gTrack.length; i++) {
993fd081
RK
207 if (gTrack[i].beginSegment && i > 0) {
208 out += ' </trkseg>' + "\n";
209 out += ' <trkseg>' + "\n";
210 }
99631a75
RK
211 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
212 gTrack[i].coords.longitude + '">' + "\n";
213 if (gTrack[i].coords.altitude) {
214 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
215 }
216 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
217 out += ' </trkpt>' + "\n";
99631a75
RK
218 }
219 out += ' </trkseg>' + "\n";
220 out += ' </trk>' + "\n";
221 out += '</gpx>' + "\n";
4b12da3a 222 var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out);
99631a75
RK
223 window.open(outDataURI, 'GPX Track');
224 }
225}
993fd081 226
4b12da3a
RK
227function saveTrackDump() {
228 if (gTrack.length) {
229 var out = JSON.stringify(gTrack);
230 var outDataURI = "data:application/json," + encodeURIComponent(out);
231 window.open(outDataURI, 'JSON dump');
232 }
233}
234
993fd081
RK
235var gPrefs = {
236 objStore: "prefs",
237
238 get: function(aKey, aCallback) {
239 if (!mainDB)
240 return;
241 var transaction = mainDB.transaction([this.objStore]);
242 var request = transaction.objectStore(this.objStore).get(aKey);
243 request.onsuccess = function(event) {
244 aCallback(request.result, event);
245 };
246 request.onerror = function(event) {
247 // Errors can be handled here.
248 aCallback(undefined, event);
249 };
250 },
251
252 set: function(aKey, aValue, aCallback) {
253 if (!mainDB)
254 return;
255 var success = false;
d4ccddb8 256 var transaction = mainDB.transaction([this.objStore], "readwrite");
993fd081 257 var objStore = transaction.objectStore(this.objStore);
3610c22d 258 var request = objStore.put(aValue, aKey);
993fd081
RK
259 request.onsuccess = function(event) {
260 success = true;
261 if (aCallback)
262 aCallback(success, event);
263 };
264 request.onerror = function(event) {
265 // Errors can be handled here.
266 if (aCallback)
267 aCallback(success, event);
268 };
269 },
270
271 unset: function(aKey, aCallback) {
272 if (!mainDB)
273 return;
274 var success = false;
d4ccddb8 275 var transaction = mainDB.transaction([this.objStore], "readwrite");
993fd081
RK
276 var request = transaction.objectStore(this.objStore).delete(aKey);
277 request.onsuccess = function(event) {
278 success = true;
279 if (aCallback)
280 aCallback(success, event);
281 };
282 request.onerror = function(event) {
283 // Errors can be handled here.
284 if (aCallback)
285 aCallback(success, event);
286 }
287 }
288};
289
290var gTrackStore = {
291 objStore: "track",
292
293 getList: function(aCallback) {
294 if (!mainDB)
295 return;
296 var transaction = mainDB.transaction([this.objStore]);
297 var objStore = transaction.objectStore(this.objStore);
298 if (objStore.getAll) { // currently Mozilla-specific
299 objStore.getAll().onsuccess = function(event) {
300 aCallback(event.target.result);
301 };
302 }
303 else { // Use cursor (standard method).
304 var tPoints = [];
305 objStore.openCursor().onsuccess = function(event) {
306 var cursor = event.target.result;
307 if (cursor) {
308 tPoints.push(cursor.value);
309 cursor.continue();
310 }
311 else {
312 aCallback(tPoints);
313 }
314 };
315 }
316 },
317
318 push: function(aValue, aCallback) {
319 if (!mainDB)
320 return;
d4ccddb8 321 var transaction = mainDB.transaction([this.objStore], "readwrite");
993fd081
RK
322 var objStore = transaction.objectStore(this.objStore);
323 var request = objStore.add(aValue);
324 request.onsuccess = function(event) {
325 if (aCallback)
326 aCallback(request.result, event);
327 };
328 request.onerror = function(event) {
329 // Errors can be handled here.
330 if (aCallback)
331 aCallback(false, event);
332 };
333 },
334
335 clear: function(aCallback) {
336 if (!mainDB)
337 return;
338 var success = false;
d4ccddb8 339 var transaction = mainDB.transaction([this.objStore], "readwrite");
993fd081
RK
340 var request = transaction.objectStore(this.objStore).clear();
341 request.onsuccess = function(event) {
342 success = true;
343 if (aCallback)
344 aCallback(success, event);
345 };
346 request.onerror = function(event) {
347 // Errors can be handled here.
348 if (aCallback)
349 aCallback(success, event);
350 }
351 }
352};