| 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/. */ |
| 4 | |
| 5 | // Get the best-available indexedDB object. |
| 6 | var iDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB; |
| 7 | var mainDB; |
| 8 | |
| 9 | window.onload = function() { |
| 10 | var mSel = document.getElementById("mapSelector"); |
| 11 | for (var mapStyle in gMapStyles) { |
| 12 | var opt = document.createElement("option"); |
| 13 | opt.value = mapStyle; |
| 14 | opt.text = gMapStyles[mapStyle].name; |
| 15 | mSel.add(opt, null); |
| 16 | } |
| 17 | |
| 18 | initDB(); |
| 19 | initMap(); |
| 20 | resizeAndDraw(); |
| 21 | } |
| 22 | |
| 23 | window.onresize = function() { |
| 24 | resizeAndDraw(); |
| 25 | } |
| 26 | |
| 27 | function initDB() { |
| 28 | // Open DB. |
| 29 | var request = iDB.open("MainDB", 1); |
| 30 | request.onerror = function(event) { |
| 31 | // Errors can be handled here. Error codes explain in: |
| 32 | // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants |
| 33 | //document.getElementById("debug").textContent = |
| 34 | // "error opening mainDB: " + event.target.errorCode; |
| 35 | }; |
| 36 | request.onsuccess = function(event) { |
| 37 | //document.getElementById("debug").textContent = "mainDB opened."; |
| 38 | mainDB = request.result; |
| 39 | }; |
| 40 | request.onupgradeneeded = function(event) { |
| 41 | mainDB = request.result; |
| 42 | //document.getElementById("debug").textContent = "mainDB upgraded."; |
| 43 | // Create a "prefs" objectStore. |
| 44 | var prefsStore = mainDB.createObjectStore("prefs"); |
| 45 | // Create a "track" objectStore. |
| 46 | var trackStore = mainDB.createObjectStore("track", {autoIncrement: true}); |
| 47 | mainDB.onversionchange = function(event) { |
| 48 | mainDB.close(); |
| 49 | mainDB = undefined; |
| 50 | initDB(); |
| 51 | }; |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | function toggleTrackArea() { |
| 56 | var fs = document.getElementById("trackArea"); |
| 57 | if (fs.style.display != "block") { |
| 58 | fs.style.display = "block"; |
| 59 | } |
| 60 | else { |
| 61 | fs.style.display = "none"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function toggleSettings() { |
| 66 | var fs = document.getElementById("settingsArea"); |
| 67 | if (fs.style.display != "block") { |
| 68 | fs.style.display = "block"; |
| 69 | } |
| 70 | else { |
| 71 | fs.style.display = "none"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function makeISOString(aTimestamp) { |
| 76 | // ISO time format is YYYY-MM-DDTHH:mm:ssZ |
| 77 | var tsDate = new Date(aTimestamp); |
| 78 | return tsDate.getUTCFullYear() + "-" + |
| 79 | (tsDate.getUTCMonth() < 10 ? "0" : "") + tsDate.getUTCMonth() + "-" + |
| 80 | (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + "T" + |
| 81 | (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" + |
| 82 | (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes() + ":" + |
| 83 | (tsDate.getUTCSeconds() < 10 ? "0" : "") + tsDate.getUTCSeconds() + "Z"; |
| 84 | } |
| 85 | |
| 86 | function saveTrack() { |
| 87 | if (gTrack.length) { |
| 88 | var out = '<?xml version="1.0" encoding="UTF-8" ?>' + "\n\n"; |
| 89 | out += '<gpx version="1.0" creator="Lantea" xmlns="http://www.topografix.com/GPX/1/0">' + "\n"; |
| 90 | out += ' <trk>' + "\n"; |
| 91 | out += ' <trkseg>' + "\n"; |
| 92 | for (var i = 0; i < gTrack.length; i++) { |
| 93 | if (gTrack[i].beginSegment && i > 0) { |
| 94 | out += ' </trkseg>' + "\n"; |
| 95 | out += ' <trkseg>' + "\n"; |
| 96 | } |
| 97 | out += ' <trkpt lat="' + gTrack[i].coords.latitude + '" lon="' + |
| 98 | gTrack[i].coords.longitude + '">' + "\n"; |
| 99 | if (gTrack[i].coords.altitude) { |
| 100 | out += ' <ele>' + gTrack[i].coords.altitude + '</ele>' + "\n"; |
| 101 | } |
| 102 | out += ' <time>' + makeISOString(gTrack[i].time) + '</time>' + "\n"; |
| 103 | out += ' </trkpt>' + "\n"; |
| 104 | } |
| 105 | out += ' </trkseg>' + "\n"; |
| 106 | out += ' </trk>' + "\n"; |
| 107 | out += '</gpx>' + "\n"; |
| 108 | var outDataURI = "data:application/gpx+xml," + encodeURIComponent(out); |
| 109 | window.open(outDataURI, 'GPX Track'); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function saveTrackDump() { |
| 114 | if (gTrack.length) { |
| 115 | var out = JSON.stringify(gTrack); |
| 116 | var outDataURI = "data:application/json," + encodeURIComponent(out); |
| 117 | window.open(outDataURI, 'JSON dump'); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | var gPrefs = { |
| 122 | objStore: "prefs", |
| 123 | |
| 124 | get: function(aKey, aCallback) { |
| 125 | if (!mainDB) |
| 126 | return; |
| 127 | var transaction = mainDB.transaction([this.objStore]); |
| 128 | var request = transaction.objectStore(this.objStore).get(aKey); |
| 129 | request.onsuccess = function(event) { |
| 130 | aCallback(request.result, event); |
| 131 | }; |
| 132 | request.onerror = function(event) { |
| 133 | // Errors can be handled here. |
| 134 | aCallback(undefined, event); |
| 135 | }; |
| 136 | }, |
| 137 | |
| 138 | set: function(aKey, aValue, aCallback) { |
| 139 | if (!mainDB) |
| 140 | return; |
| 141 | var success = false; |
| 142 | var transaction = mainDB.transaction([this.objStore], |
| 143 | IDBTransaction.READ_WRITE); |
| 144 | var objStore = transaction.objectStore(this.objStore); |
| 145 | var request = objStore.put(aValue, aKey); |
| 146 | request.onsuccess = function(event) { |
| 147 | success = true; |
| 148 | if (aCallback) |
| 149 | aCallback(success, event); |
| 150 | }; |
| 151 | request.onerror = function(event) { |
| 152 | // Errors can be handled here. |
| 153 | if (aCallback) |
| 154 | aCallback(success, event); |
| 155 | }; |
| 156 | }, |
| 157 | |
| 158 | unset: function(aKey, aCallback) { |
| 159 | if (!mainDB) |
| 160 | return; |
| 161 | var success = false; |
| 162 | var transaction = mainDB.transaction([this.objStore], |
| 163 | IDBTransaction.READ_WRITE); |
| 164 | var request = transaction.objectStore(this.objStore).delete(aKey); |
| 165 | request.onsuccess = function(event) { |
| 166 | success = true; |
| 167 | if (aCallback) |
| 168 | aCallback(success, event); |
| 169 | }; |
| 170 | request.onerror = function(event) { |
| 171 | // Errors can be handled here. |
| 172 | if (aCallback) |
| 173 | aCallback(success, event); |
| 174 | } |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | var gTrackStore = { |
| 179 | objStore: "track", |
| 180 | |
| 181 | getList: function(aCallback) { |
| 182 | if (!mainDB) |
| 183 | return; |
| 184 | var transaction = mainDB.transaction([this.objStore]); |
| 185 | var objStore = transaction.objectStore(this.objStore); |
| 186 | if (objStore.getAll) { // currently Mozilla-specific |
| 187 | objStore.getAll().onsuccess = function(event) { |
| 188 | aCallback(event.target.result); |
| 189 | }; |
| 190 | } |
| 191 | else { // Use cursor (standard method). |
| 192 | var tPoints = []; |
| 193 | objStore.openCursor().onsuccess = function(event) { |
| 194 | var cursor = event.target.result; |
| 195 | if (cursor) { |
| 196 | tPoints.push(cursor.value); |
| 197 | cursor.continue(); |
| 198 | } |
| 199 | else { |
| 200 | aCallback(tPoints); |
| 201 | } |
| 202 | }; |
| 203 | } |
| 204 | }, |
| 205 | |
| 206 | push: function(aValue, aCallback) { |
| 207 | if (!mainDB) |
| 208 | return; |
| 209 | var transaction = mainDB.transaction([this.objStore], |
| 210 | IDBTransaction.READ_WRITE); |
| 211 | var objStore = transaction.objectStore(this.objStore); |
| 212 | var request = objStore.add(aValue); |
| 213 | request.onsuccess = function(event) { |
| 214 | if (aCallback) |
| 215 | aCallback(request.result, event); |
| 216 | }; |
| 217 | request.onerror = function(event) { |
| 218 | // Errors can be handled here. |
| 219 | if (aCallback) |
| 220 | aCallback(false, event); |
| 221 | }; |
| 222 | }, |
| 223 | |
| 224 | clear: function(aCallback) { |
| 225 | if (!mainDB) |
| 226 | return; |
| 227 | var success = false; |
| 228 | var transaction = mainDB.transaction([this.objStore], |
| 229 | IDBTransaction.READ_WRITE); |
| 230 | var request = transaction.objectStore(this.objStore).clear(); |
| 231 | request.onsuccess = function(event) { |
| 232 | success = true; |
| 233 | if (aCallback) |
| 234 | aCallback(success, event); |
| 235 | }; |
| 236 | request.onerror = function(event) { |
| 237 | // Errors can be handled here. |
| 238 | if (aCallback) |
| 239 | aCallback(success, event); |
| 240 | } |
| 241 | } |
| 242 | }; |