remove debug message
[lantea.git] / js / ui.js
CommitLineData
23cd2dcc
RK
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Lantea mapping/tracking web app.
15 *
16 * The Initial Developer of the Original Code is
17 * Robert Kaiser <kairo@kairo.at>.
18 * Portions created by the Initial Developer are Copyright (C) 2011
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Robert Kaiser <kairo@kairo.at>
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
993fd081
RK
38// Get the best-available indexedDB object.
39var iDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
40var mainDB;
41
b47b4a65
RK
42window.onload = function() {
43 var mSel = document.getElementById("mapSelector");
44 for (var mapStyle in gMapStyles) {
45 var opt = document.createElement("option");
46 opt.value = mapStyle;
47 opt.text = gMapStyles[mapStyle].name;
48 mSel.add(opt, null);
49 }
23cd2dcc 50
993fd081 51 initDB();
b47b4a65
RK
52 initMap();
53 resizeAndDraw();
55c4a0b7 54 startTracking();
b47b4a65
RK
55}
56
57window.onresize = function() {
58 resizeAndDraw();
59}
60
993fd081
RK
61function initDB() {
62 // Open DB.
63 var request = iDB.open("MainDB", 1);
64 request.onerror = function(event) {
65 // Errors can be handled here. Error codes explain in:
66 // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants
67 //document.getElementById("debug").textContent =
68 // "error opening mainDB: " + event.target.errorCode;
69 };
70 request.onsuccess = function(event) {
71 //document.getElementById("debug").textContent = "mainDB opened.";
72 mainDB = request.result;
73 };
74 request.onupgradeneeded = function(event) {
75 mainDB = request.result;
76 //document.getElementById("debug").textContent = "mainDB upgraded.";
77 // Create a "prefs" objectStore.
78 var prefsStore = mainDB.createObjectStore("prefs");
79 // Create a "track" objectStore.
80 var trackStore = mainDB.createObjectStore("track", {autoIncrement: true});
81 mainDB.onversionchange = function(event) {
82 mainDB.close();
83 mainDB = undefined;
84 initDB();
85 };
86 };
87}
88
89function toggleTrackArea() {
90 var fs = document.getElementById("trackArea");
91 if (fs.style.display != "block") {
92 fs.style.display = "block";
93 }
94 else {
95 fs.style.display = "none";
96 }
97}
98
b47b4a65 99function toggleSettings() {
993fd081 100 var fs = document.getElementById("settingsArea");
b47b4a65
RK
101 if (fs.style.display != "block") {
102 fs.style.display = "block";
103 }
104 else {
105 fs.style.display = "none";
106 }
107}
99631a75
RK
108
109function makeISOString(aTimestamp) {
110 // ISO time format is YYYY-MM-DDTHH:mm:ssZ
111 var tsDate = new Date(aTimestamp);
112 return tsDate.getUTCFullYear() + "-" +
113 (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" +
114 (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" +
115 (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
116 (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" +
117 (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z";
118}
119
120function saveTrack() {
121 if (gTrack.length) {
122 var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n";
123 out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n";
124 out += ' <trk>' + "\n";
125 out += ' <trkseg>' + "\n";
126 for (var i = 0; i < gTrack.length; i++) {
993fd081
RK
127 if (gTrack[i].beginSegment && i > 0) {
128 out += ' </trkseg>' + "\n";
129 out += ' <trkseg>' + "\n";
130 }
99631a75
RK
131 out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' +
132 gTrack[i].coords.longitude + '">' + "\n";
133 if (gTrack[i].coords.altitude) {
134 out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n";
135 }
136 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
137 out += ' </trkpt>' + "\n";
99631a75
RK
138 }
139 out += ' </trkseg>' + "\n";
140 out += ' </trk>' + "\n";
141 out += '</gpx>' + "\n";
142 var outDataURI = "data:application/octet-stream," + encodeURIComponent(out);
143 window.open(outDataURI, 'GPX Track');
144 }
145}
993fd081
RK
146
147var gPrefs = {
148 objStore: "prefs",
149
150 get: function(aKey, aCallback) {
151 if (!mainDB)
152 return;
153 var transaction = mainDB.transaction([this.objStore]);
154 var request = transaction.objectStore(this.objStore).get(aKey);
155 request.onsuccess = function(event) {
156 aCallback(request.result, event);
157 };
158 request.onerror = function(event) {
159 // Errors can be handled here.
160 aCallback(undefined, event);
161 };
162 },
163
164 set: function(aKey, aValue, aCallback) {
165 if (!mainDB)
166 return;
167 var success = false;
168 var transaction = mainDB.transaction([this.objStore],
169 IDBTransaction.READ_WRITE);
170 var objStore = transaction.objectStore(this.objStore);
171 var request = objStore.add(aValue, aKey);
172 request.onsuccess = function(event) {
173 success = true;
174 if (aCallback)
175 aCallback(success, event);
176 };
177 request.onerror = function(event) {
178 // Errors can be handled here.
179 if (aCallback)
180 aCallback(success, event);
181 };
182 },
183
184 unset: function(aKey, aCallback) {
185 if (!mainDB)
186 return;
187 var success = false;
188 var transaction = mainDB.transaction([this.objStore],
189 IDBTransaction.READ_WRITE);
190 var request = transaction.objectStore(this.objStore).delete(aKey);
191 request.onsuccess = function(event) {
192 success = true;
193 if (aCallback)
194 aCallback(success, event);
195 };
196 request.onerror = function(event) {
197 // Errors can be handled here.
198 if (aCallback)
199 aCallback(success, event);
200 }
201 }
202};
203
204var gTrackStore = {
205 objStore: "track",
206
207 getList: function(aCallback) {
208 if (!mainDB)
209 return;
210 var transaction = mainDB.transaction([this.objStore]);
211 var objStore = transaction.objectStore(this.objStore);
212 if (objStore.getAll) { // currently Mozilla-specific
213 objStore.getAll().onsuccess = function(event) {
214 aCallback(event.target.result);
215 };
216 }
217 else { // Use cursor (standard method).
218 var tPoints = [];
219 objStore.openCursor().onsuccess = function(event) {
220 var cursor = event.target.result;
221 if (cursor) {
222 tPoints.push(cursor.value);
223 cursor.continue();
224 }
225 else {
226 aCallback(tPoints);
227 }
228 };
229 }
230 },
231
232 push: function(aValue, aCallback) {
233 if (!mainDB)
234 return;
235 var transaction = mainDB.transaction([this.objStore],
236 IDBTransaction.READ_WRITE);
237 var objStore = transaction.objectStore(this.objStore);
238 var request = objStore.add(aValue);
239 request.onsuccess = function(event) {
240 if (aCallback)
241 aCallback(request.result, event);
242 };
243 request.onerror = function(event) {
244 // Errors can be handled here.
245 if (aCallback)
246 aCallback(false, event);
247 };
248 },
249
250 clear: function(aCallback) {
251 if (!mainDB)
252 return;
253 var success = false;
254 var transaction = mainDB.transaction([this.objStore],
255 IDBTransaction.READ_WRITE);
256 var request = transaction.objectStore(this.objStore).clear();
257 request.onsuccess = function(event) {
258 success = true;
259 if (aCallback)
260 aCallback(success, event);
261 };
262 request.onerror = function(event) {
263 // Errors can be handled here.
264 if (aCallback)
265 aCallback(success, event);
266 }
267 }
268};