1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is Lantea mapping/tracking web app.
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.
22 * Robert Kaiser <kairo@kairo.at>
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.
36 * ***** END LICENSE BLOCK ***** */
38 // Get the best-available indexedDB object.
39 var iDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
42 window.onload = function() {
43 var mSel = document.getElementById("mapSelector");
44 for (var mapStyle in gMapStyles) {
45 var opt = document.createElement("option");
47 opt.text = gMapStyles[mapStyle].name;
57 window.onresize = function() {
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;
70 request.onsuccess = function(event) {
71 //document.getElementById("debug").textContent = "mainDB opened.";
72 mainDB = request.result;
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) {
89 function toggleTrackArea() {
90 var fs = document.getElementById("trackArea");
91 if (fs.style.display != "block") {
92 fs.style.display = "block";
95 fs.style.display = "none";
99 function toggleSettings() {
100 var fs = document.getElementById("settingsArea");
101 if (fs.style.display != "block") {
102 fs.style.display = "block";
105 fs.style.display = "none";
109 function 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";
120 function saveTrack() {
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++) {
127 if (gTrack[i].beginSegment && i > 0) {
128 out += ' </trkseg>' + "\n";
129 out += ' <trkseg>' + "\n";
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";
136 out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n";
137 out += ' </trkpt>' + "\n";
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');
150 get: function(aKey, aCallback) {
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);
158 request.onerror = function(event) {
159 // Errors can be handled here.
160 aCallback(undefined, event);
164 set: function(aKey, aValue, aCallback) {
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) {
175 aCallback(success, event);
177 request.onerror = function(event) {
178 // Errors can be handled here.
180 aCallback(success, event);
184 unset: function(aKey, aCallback) {
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) {
194 aCallback(success, event);
196 request.onerror = function(event) {
197 // Errors can be handled here.
199 aCallback(success, event);
207 getList: function(aCallback) {
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);
217 else { // Use cursor (standard method).
219 objStore.openCursor().onsuccess = function(event) {
220 var cursor = event.target.result;
222 tPoints.push(cursor.value);
232 push: function(aValue, aCallback) {
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) {
241 aCallback(request.result, event);
243 request.onerror = function(event) {
244 // Errors can be handled here.
246 aCallback(false, event);
250 clear: function(aCallback) {
254 var transaction = mainDB.transaction([this.objStore],
255 IDBTransaction.READ_WRITE);
256 var request = transaction.objectStore(this.objStore).clear();
257 request.onsuccess = function(event) {
260 aCallback(success, event);
262 request.onerror = function(event) {
263 // Errors can be handled here.
265 aCallback(success, event);