Commit | Line | Data |
---|---|---|
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 | |
4b1d0915 | 5 | var gMapCanvas, gMapContext, gTrackCanvas, gTrackContext, gGeolocation; |
31f77b93 | 6 | var gDebug = false; |
23cd2dcc RK |
7 | |
8 | var gTileSize = 256; | |
9 | var gMaxZoom = 18; // The minimum is 0. | |
10 | ||
b054bd48 RK |
11 | var gMinTrackAccuracy = 1000; // meters |
12 | var gTrackWidth = 2; // pixels | |
13 | var gTrackColor = "#FF0000"; | |
4b1d0915 RK |
14 | var gCurLocSize = 6; // pixels |
15 | var gCurLocColor = "#A00000"; | |
b054bd48 | 16 | |
b47b4a65 RK |
17 | var gMapStyles = { |
18 | // OSM tile usage policy: http://wiki.openstreetmap.org/wiki/Tile_usage_policy | |
55c4a0b7 | 19 | // Find some more OSM ones at http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_servers |
b47b4a65 RK |
20 | osm_mapnik: |
21 | {name: "OpenStreetMap (Mapnik)", | |
22 | url: "http://tile.openstreetmap.org/{z}/{x}/{y}.png", | |
5a19ec68 | 23 | copyright: 'Map data and imagery © <a href="http://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="http://www.openstreetmap.org/copyright">ODbL/CC-BY-SA</a>'}, |
14e6d3ad RK |
24 | osm_cyclemap: |
25 | {name: "Cycle Map (OSM)", | |
26 | url: "http://[a-c].tile.opencyclemap.org/cycle/{z}/{x}/{y}.png", | |
5a19ec68 | 27 | copyright: 'Map data and imagery © <a href="http://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="http://www.openstreetmap.org/copyright">ODbL/CC-BY-SA</a>'}, |
14e6d3ad RK |
28 | osm_transmap: |
29 | {name: "Transport Map (OSM)", | |
30 | url: "http://[a-c].tile2.opencyclemap.org/transport/{z}/{x}/{y}.png", | |
5a19ec68 | 31 | copyright: 'Map data and imagery © <a href="http://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="http://www.openstreetmap.org/copyright">ODbL/CC-BY-SA</a>'}, |
b47b4a65 | 32 | mapquest_open: |
55c4a0b7 | 33 | {name: "MapQuest OSM", |
5a19ec68 RK |
34 | url: "http://otile[1-4].mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", |
35 | copyright: 'Map data © <a href="http://www.openstreetmap.org/">OpenStreetMap</a> and contributors (<a href="http://www.openstreetmap.org/copyright">ODbL/CC-BY-SA</a>), tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a>.'}, | |
55c4a0b7 RK |
36 | mapquest_aerial: |
37 | {name: "MapQuest Open Aerial", | |
68afcd96 | 38 | url: "http://otile[1-4].mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg", |
5a19ec68 RK |
39 | copyright: 'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a>, portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency.'}, |
40 | opengeoserver_arial: | |
41 | {name: "OpenGeoServer Aerial", | |
42 | url: "http://services.opengeoserver.org/tiles/1.0.0/globe.aerial_EPSG3857/{z}/{x}/{y}.png?origin=nw", | |
43 | copyright: 'Tiles by <a href="http://www.opengeoserver.org/">OpenGeoServer.org</a>, <a href="https://creativecommons.org/licenses/by/3.0/at/">CC-BY 3.0 AT</a>.'}, | |
55c4a0b7 RK |
44 | google_map: |
45 | {name: "Google Maps", | |
46 | url: " http://mt1.google.com/vt/x={x}&y={y}&z={z}", | |
47 | copyright: 'Map data and imagery © <a href="http://maps.google.com/">Google</a>'}, | |
b47b4a65 RK |
48 | }; |
49 | var gActiveMap = "osm_mapnik"; | |
23cd2dcc RK |
50 | |
51 | var gPos = {x: 35630000.0, // Current position in the map in pixels at the maximum zoom level (18) | |
52 | y: 23670000.0, // The range is 0-67108864 (2^gMaxZoom * gTileSize) | |
b395419b | 53 | z: 5}; // This could be fractional if supported being between zoom levels. |
23cd2dcc RK |
54 | |
55 | var gLastMouseX = 0; | |
56 | var gLastMouseY = 0; | |
b395419b | 57 | var gZoomFactor; |
23cd2dcc | 58 | |
b395419b | 59 | var gLoadingTile; |
23cd2dcc | 60 | |
3610c22d RK |
61 | var gMapPrefsLoaded = false; |
62 | ||
23cd2dcc | 63 | var gDragging = false; |
517c0099 | 64 | var gDragTouchID, gPinchStartWidth; |
23cd2dcc | 65 | |
55c4a0b7 RK |
66 | var gGeoWatchID; |
67 | var gTrack = []; | |
14e6d3ad | 68 | var gLastTrackPoint, gLastDrawnPoint; |
05c21757 | 69 | var gCenterPosition = true; |
55c4a0b7 | 70 | |
b054bd48 RK |
71 | var gCurPosMapCache; |
72 | ||
b47b4a65 | 73 | function initMap() { |
4b12da3a | 74 | gGeolocation = navigator.geolocation; |
4b1d0915 RK |
75 | gMapCanvas = document.getElementById("map"); |
76 | gMapContext = gMapCanvas.getContext("2d"); | |
77 | gTrackCanvas = document.getElementById("track"); | |
78 | gTrackContext = gTrackCanvas.getContext("2d"); | |
b47b4a65 RK |
79 | if (!gActiveMap) |
80 | gActiveMap = "osm_mapnik"; | |
23cd2dcc | 81 | |
4b12da3a RK |
82 | //gDebug = true; |
83 | if (gDebug) { | |
84 | gGeolocation = geofake; | |
85 | var hiddenList = document.getElementsByClassName("debugHide"); | |
86 | // last to first - list of elements with that class is changing! | |
87 | for (var i = hiddenList.length - 1; i >= 0; i--) { | |
88 | hiddenList[i].classList.remove("debugHide"); | |
89 | } | |
90 | } | |
91 | ||
582d50fc RK |
92 | console.log("map vars set, loading prefs..."); |
93 | loadPrefs(); | |
94 | } | |
b395419b | 95 | |
582d50fc RK |
96 | function loadPrefs(aEvent) { |
97 | if (aEvent && aEvent.type == "prefs-step") { | |
98 | console.log("wait: " + gWaitCounter); | |
99 | if (gWaitCounter == 0) { | |
100 | gAction.removeEventListener(aEvent.type, loadPrefs, false); | |
101 | gMapPrefsLoaded = true; | |
102 | console.log("prefs loaded."); | |
103 | ||
104 | gTrackCanvas.addEventListener("mouseup", mapEvHandler, false); | |
105 | gTrackCanvas.addEventListener("mousemove", mapEvHandler, false); | |
106 | gTrackCanvas.addEventListener("mousedown", mapEvHandler, false); | |
107 | gTrackCanvas.addEventListener("mouseout", mapEvHandler, false); | |
108 | ||
109 | gTrackCanvas.addEventListener("touchstart", mapEvHandler, false); | |
110 | gTrackCanvas.addEventListener("touchmove", mapEvHandler, false); | |
111 | gTrackCanvas.addEventListener("touchend", mapEvHandler, false); | |
112 | gTrackCanvas.addEventListener("touchcancel", mapEvHandler, false); | |
113 | gTrackCanvas.addEventListener("touchleave", mapEvHandler, false); | |
114 | ||
115 | gTrackCanvas.addEventListener("wheel", mapEvHandler, false); | |
116 | ||
117 | document.getElementById("body").addEventListener("keydown", mapEvHandler, false); | |
118 | ||
119 | document.getElementById("copyright").innerHTML = | |
120 | gMapStyles[gActiveMap].copyright; | |
121 | ||
122 | gLoadingTile = new Image(); | |
123 | gLoadingTile.src = "style/loading.png"; | |
124 | gLoadingTile.onload = function() { | |
125 | var throwEv = new CustomEvent("mapinit-done"); | |
126 | gAction.dispatchEvent(throwEv); | |
127 | }; | |
128 | } | |
129 | } | |
130 | else { | |
131 | if (aEvent) | |
132 | gAction.removeEventListener(aEvent.type, loadPrefs, false); | |
133 | gAction.addEventListener("prefs-step", loadPrefs, false); | |
134 | gWaitCounter++; | |
135 | gPrefs.get("position", function(aValue) { | |
136 | if (aValue) { | |
137 | gPos = aValue; | |
582d50fc | 138 | } |
df81068a RK |
139 | gWaitCounter--; |
140 | var throwEv = new CustomEvent("prefs-step"); | |
141 | gAction.dispatchEvent(throwEv); | |
582d50fc RK |
142 | }); |
143 | gWaitCounter++; | |
144 | gPrefs.get("center_map", function(aValue) { | |
145 | if (aValue === undefined) | |
146 | document.getElementById("centerCheckbox").checked = true; | |
147 | else | |
148 | document.getElementById("centerCheckbox").checked = aValue; | |
149 | setCentering(document.getElementById("centerCheckbox")); | |
150 | gWaitCounter--; | |
151 | var throwEv = new CustomEvent("prefs-step"); | |
152 | gAction.dispatchEvent(throwEv); | |
153 | }); | |
154 | gWaitCounter++; | |
155 | gPrefs.get("tracking_enabled", function(aValue) { | |
156 | if (aValue === undefined) | |
157 | document.getElementById("trackCheckbox").checked = true; | |
158 | else | |
159 | document.getElementById("trackCheckbox").checked = aValue; | |
160 | gWaitCounter--; | |
161 | var throwEv = new CustomEvent("prefs-step"); | |
162 | gAction.dispatchEvent(throwEv); | |
163 | }); | |
164 | gWaitCounter++; | |
fdaf08db | 165 | var trackLoadStarted = false; redrawBase = 100; |
6ddefbf9 RK |
166 | gTrackStore.getListStepped(function(aTPoint) { |
167 | if (aTPoint) { | |
168 | // Add in front and return new length. | |
169 | var tracklen = gTrack.unshift(aTPoint); | |
fdaf08db RK |
170 | // Redraw track periodically, larger distance the longer it gets. |
171 | // Initial paint will do initial track drawing. | |
172 | if (tracklen % redrawBase == 0) { | |
6ddefbf9 | 173 | drawTrack(); |
fdaf08db RK |
174 | redrawBase = tracklen; |
175 | } | |
6ddefbf9 RK |
176 | } |
177 | else { | |
178 | // Last point received. | |
179 | drawTrack(); | |
180 | } | |
181 | if (!trackLoadStarted) { | |
182 | // We have the most recent point, if present, rest will load async. | |
183 | trackLoadStarted = true; | |
184 | gWaitCounter--; | |
185 | var throwEv = new CustomEvent("prefs-step"); | |
186 | gAction.dispatchEvent(throwEv); | |
582d50fc | 187 | } |
582d50fc RK |
188 | }); |
189 | } | |
23cd2dcc RK |
190 | } |
191 | ||
192 | function resizeAndDraw() { | |
321359cd RK |
193 | var viewportWidth = Math.min(window.innerWidth, window.outerWidth); |
194 | var viewportHeight = Math.min(window.innerHeight, window.outerHeight); | |
915d4271 RK |
195 | if (gMapCanvas && gTrackCanvas) { |
196 | gMapCanvas.width = viewportWidth; | |
197 | gMapCanvas.height = viewportHeight; | |
198 | gTrackCanvas.width = viewportWidth; | |
199 | gTrackCanvas.height = viewportHeight; | |
200 | drawMap(); | |
201 | showUI(); | |
202 | } | |
23cd2dcc RK |
203 | } |
204 | ||
b5e49b95 RK |
205 | // Using scale(x, y) together with drawing old data on scaled canvas would be an improvement for zooming. |
206 | // See https://developer.mozilla.org/en-US/docs/Canvas_tutorial/Transformations#Scaling | |
207 | ||
23cd2dcc RK |
208 | function zoomIn() { |
209 | if (gPos.z < gMaxZoom) { | |
210 | gPos.z++; | |
211 | drawMap(); | |
212 | } | |
213 | } | |
214 | ||
215 | function zoomOut() { | |
216 | if (gPos.z > 0) { | |
217 | gPos.z--; | |
218 | drawMap(); | |
219 | } | |
220 | } | |
221 | ||
1222624d RK |
222 | function zoomTo(aTargetLevel) { |
223 | aTargetLevel = parseInt(aTargetLevel); | |
224 | if (aTargetLevel >= 0 && aTargetLevel <= gMaxZoom) { | |
225 | gPos.z = aTargetLevel; | |
226 | drawMap(); | |
227 | } | |
228 | } | |
229 | ||
55c4a0b7 RK |
230 | function gps2xy(aLatitude, aLongitude) { |
231 | var maxZoomFactor = Math.pow(2, gMaxZoom) * gTileSize; | |
232 | var convLat = aLatitude * Math.PI / 180; | |
233 | var rawY = (1 - Math.log(Math.tan(convLat) + | |
234 | 1 / Math.cos(convLat)) / Math.PI) / 2 * maxZoomFactor; | |
235 | var rawX = (aLongitude + 180) / 360 * maxZoomFactor; | |
236 | return {x: Math.round(rawX), | |
237 | y: Math.round(rawY)}; | |
238 | } | |
239 | ||
240 | function xy2gps(aX, aY) { | |
241 | var maxZoomFactor = Math.pow(2, gMaxZoom) * gTileSize; | |
242 | var n = Math.PI - 2 * Math.PI * aY / maxZoomFactor; | |
243 | return {latitude: 180 / Math.PI * | |
244 | Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))), | |
245 | longitude: aX / maxZoomFactor * 360 - 180}; | |
246 | } | |
247 | ||
b47b4a65 RK |
248 | function setMapStyle() { |
249 | var mapSel = document.getElementById("mapSelector"); | |
250 | if (mapSel.selectedIndex >= 0 && gActiveMap != mapSel.value) { | |
251 | gActiveMap = mapSel.value; | |
95f49ba7 RK |
252 | document.getElementById("copyright").innerHTML = |
253 | gMapStyles[gActiveMap].copyright; | |
b5c85133 | 254 | showUI(); |
b47b4a65 RK |
255 | drawMap(); |
256 | } | |
257 | } | |
258 | ||
23cd2dcc RK |
259 | // A sane mod function that works for negative numbers. |
260 | // Returns a % b. | |
261 | function mod(a, b) { | |
262 | return ((a % b) + b) % b; | |
263 | } | |
264 | ||
a8634d37 RK |
265 | function normalizeCoords(aCoords) { |
266 | var zoomFactor = Math.pow(2, aCoords.z); | |
267 | return {x: mod(aCoords.x, zoomFactor), | |
268 | y: mod(aCoords.y, zoomFactor), | |
269 | z: aCoords.z}; | |
23cd2dcc RK |
270 | } |
271 | ||
272 | // Returns true if the tile is outside the current view. | |
273 | function isOutsideWindow(t) { | |
274 | var pos = decodeIndex(t); | |
23cd2dcc | 275 | |
4b1d0915 RK |
276 | var zoomFactor = Math.pow(2, gMaxZoom - pos.z); |
277 | var wid = gMapCanvas.width * zoomFactor; | |
278 | var ht = gMapCanvas.height * zoomFactor; | |
23cd2dcc | 279 | |
4b1d0915 RK |
280 | pos.x *= zoomFactor; |
281 | pos.y *= zoomFactor; | |
23cd2dcc | 282 | |
b395419b | 283 | var sz = gTileSize * zoomFactor; |
4b1d0915 RK |
284 | if (pos.x > gPos.x + wid / 2 || pos.y > gPos.y + ht / 2 || |
285 | pos.x + sz < gPos.x - wid / 2 || pos.y - sz < gPos.y - ht / 2) | |
23cd2dcc RK |
286 | return true; |
287 | return false; | |
288 | } | |
289 | ||
290 | function encodeIndex(x, y, z) { | |
a8634d37 | 291 | var norm = normalizeCoords({x: x, y: y, z: z}); |
23cd2dcc RK |
292 | return norm.x + "," + norm.y + "," + norm.z; |
293 | } | |
294 | ||
295 | function decodeIndex(encodedIdx) { | |
4b1d0915 RK |
296 | var ind = encodedIdx.split(",", 3); |
297 | return {x: ind[0], y: ind[1], z: ind[2]}; | |
23cd2dcc RK |
298 | } |
299 | ||
b5e49b95 RK |
300 | function drawMap(aPixels, aOverdraw) { |
301 | // aPixels is an object with left/right/top/bottom members telling how many | |
302 | // pixels on the borders should actually be drawn. | |
303 | // aOverdraw is a bool that tells if we should draw placeholders or draw | |
304 | // straight over the existing content. | |
305 | if (!aPixels) | |
306 | aPixels = {left: gMapCanvas.width, right: gMapCanvas.width, | |
307 | top: gMapCanvas.height, bottom: gMapCanvas.height}; | |
308 | if (!aOverdraw) | |
309 | aOverdraw = false; | |
310 | ||
b395419b RK |
311 | document.getElementById("zoomLevel").textContent = gPos.z; |
312 | gZoomFactor = Math.pow(2, gMaxZoom - gPos.z); | |
4b1d0915 RK |
313 | var wid = gMapCanvas.width * gZoomFactor; // Width in level 18 pixels. |
314 | var ht = gMapCanvas.height * gZoomFactor; // Height in level 18 pixels. | |
b395419b | 315 | var size = gTileSize * gZoomFactor; // Tile size in level 18 pixels. |
23cd2dcc RK |
316 | |
317 | var xMin = gPos.x - wid / 2; // Corners of the window in level 18 pixels. | |
318 | var yMin = gPos.y - ht / 2; | |
319 | var xMax = gPos.x + wid / 2; | |
320 | var yMax = gPos.y + ht / 2; | |
321 | ||
3610c22d RK |
322 | if (gMapPrefsLoaded && mainDB) |
323 | gPrefs.set("position", gPos); | |
324 | ||
b5e49b95 RK |
325 | var tiles = {left: Math.ceil((xMin + aPixels.left * gZoomFactor) / size) - |
326 | (aPixels.left ? 0 : 1), | |
327 | right: Math.floor((xMax - aPixels.right * gZoomFactor) / size) - | |
328 | (aPixels.right ? 1 : 0), | |
329 | top: Math.ceil((yMin + aPixels.top * gZoomFactor) / size) - | |
330 | (aPixels.top ? 0 : 1), | |
331 | bottom: Math.floor((yMax - aPixels.bottom * gZoomFactor) / size) - | |
332 | (aPixels.bottom ? 1 : 0)}; | |
333 | ||
334 | // Go through all the tiles in the map, find out if to draw them and do so. | |
b47b4a65 | 335 | for (var x = Math.floor(xMin / size); x < Math.ceil(xMax / size); x++) { |
0118cbd3 | 336 | for (var y = Math.floor(yMin / size); y < Math.ceil(yMax / size); y++) { // slow script warnings on the tablet appear here! |
b5e49b95 | 337 | // Only go to the drawing step if we need to draw this tile. |
68afcd96 RK |
338 | if (x < tiles.left || x > tiles.right || |
339 | y < tiles.top || y > tiles.bottom) { | |
b5e49b95 RK |
340 | // Round here is **CRUCIAL** otherwise the images are filtered |
341 | // and the performance sucks (more than expected). | |
342 | var xoff = Math.round((x * size - xMin) / gZoomFactor); | |
343 | var yoff = Math.round((y * size - yMin) / gZoomFactor); | |
344 | // Draw placeholder tile unless we overdraw. | |
68afcd96 RK |
345 | if (!aOverdraw && |
346 | (x < tiles.left -1 || x > tiles.right + 1 || | |
347 | y < tiles.top -1 || y > tiles.bottom + 1)) | |
b5e49b95 RK |
348 | gMapContext.drawImage(gLoadingTile, xoff, yoff); |
349 | ||
350 | // Initiate loading/drawing of the actual tile. | |
351 | gTileService.get(gActiveMap, {x: x, y: y, z: gPos.z}, | |
352 | function(aImage, aStyle, aCoords) { | |
353 | // Only draw if this applies for the current view. | |
354 | if ((aStyle == gActiveMap) && (aCoords.z == gPos.z)) { | |
355 | var ixMin = gPos.x - wid / 2; | |
356 | var iyMin = gPos.y - ht / 2; | |
357 | var ixoff = Math.round((aCoords.x * size - ixMin) / gZoomFactor); | |
358 | var iyoff = Math.round((aCoords.y * size - iyMin) / gZoomFactor); | |
359 | var URL = window.URL; | |
360 | var imgURL = URL.createObjectURL(aImage); | |
361 | var imgObj = new Image(); | |
362 | imgObj.src = imgURL; | |
363 | imgObj.onload = function() { | |
364 | gMapContext.drawImage(imgObj, ixoff, iyoff); | |
365 | URL.revokeObjectURL(imgURL); | |
366 | } | |
a2131f63 | 367 | } |
b5e49b95 RK |
368 | }); |
369 | } | |
23cd2dcc RK |
370 | } |
371 | } | |
6ddefbf9 RK |
372 | drawTrack(); |
373 | } | |
374 | ||
375 | function drawTrack() { | |
4b1d0915 RK |
376 | gLastDrawnPoint = null; |
377 | gCurPosMapCache = undefined; | |
378 | gTrackContext.clearRect(0, 0, gTrackCanvas.width, gTrackCanvas.height); | |
14e6d3ad | 379 | if (gTrack.length) { |
55c4a0b7 | 380 | for (var i = 0; i < gTrack.length; i++) { |
14e6d3ad RK |
381 | drawTrackPoint(gTrack[i].coords.latitude, gTrack[i].coords.longitude, |
382 | (i + 1 >= gTrack.length)); | |
55c4a0b7 | 383 | } |
14e6d3ad | 384 | } |
55c4a0b7 RK |
385 | } |
386 | ||
14e6d3ad | 387 | function drawTrackPoint(aLatitude, aLongitude, lastPoint) { |
55c4a0b7 | 388 | var trackpoint = gps2xy(aLatitude, aLongitude); |
14e6d3ad RK |
389 | // lastPoint is for optimizing (not actually executing the draw until the last) |
390 | trackpoint.optimized = (lastPoint === false); | |
4b1d0915 RK |
391 | var mappos = {x: Math.round((trackpoint.x - gPos.x) / gZoomFactor + gMapCanvas.width / 2), |
392 | y: Math.round((trackpoint.y - gPos.y) / gZoomFactor + gMapCanvas.height / 2)}; | |
14e6d3ad RK |
393 | |
394 | if (!gLastDrawnPoint || !gLastDrawnPoint.optimized) { | |
4b1d0915 RK |
395 | gTrackContext.strokeStyle = gTrackColor; |
396 | gTrackContext.fillStyle = gTrackContext.strokeStyle; | |
397 | gTrackContext.lineWidth = gTrackWidth; | |
398 | gTrackContext.lineCap = "round"; | |
399 | gTrackContext.lineJoin = "round"; | |
14e6d3ad RK |
400 | } |
401 | if (!gLastDrawnPoint || gLastDrawnPoint == trackpoint) { | |
402 | // This breaks optimiziation, so make sure to close path and reset optimization. | |
403 | if (gLastDrawnPoint && gLastDrawnPoint.optimized) | |
4b1d0915 RK |
404 | gTrackContext.stroke(); |
405 | gTrackContext.beginPath(); | |
14e6d3ad | 406 | trackpoint.optimized = false; |
4b1d0915 RK |
407 | gTrackContext.arc(mappos.x, mappos.y, |
408 | gTrackContext.lineWidth, 0, Math.PI * 2, false); | |
409 | gTrackContext.fill(); | |
55c4a0b7 RK |
410 | } |
411 | else { | |
14e6d3ad | 412 | if (!gLastDrawnPoint || !gLastDrawnPoint.optimized) { |
4b1d0915 RK |
413 | gTrackContext.beginPath(); |
414 | gTrackContext.moveTo(Math.round((gLastDrawnPoint.x - gPos.x) / gZoomFactor + gMapCanvas.width / 2), | |
415 | Math.round((gLastDrawnPoint.y - gPos.y) / gZoomFactor + gMapCanvas.height / 2)); | |
14e6d3ad | 416 | } |
4b1d0915 | 417 | gTrackContext.lineTo(mappos.x, mappos.y); |
14e6d3ad | 418 | if (!trackpoint.optimized) |
4b1d0915 | 419 | gTrackContext.stroke(); |
55c4a0b7 | 420 | } |
14e6d3ad | 421 | gLastDrawnPoint = trackpoint; |
23cd2dcc RK |
422 | } |
423 | ||
b054bd48 | 424 | function drawCurrentLocation(trackPoint) { |
4b1d0915 RK |
425 | var locpoint = gps2xy(trackPoint.coords.latitude, trackPoint.coords.longitude); |
426 | var circleRadius = Math.round(gCurLocSize / 2); | |
427 | var mappos = {x: Math.round((locpoint.x - gPos.x) / gZoomFactor + gMapCanvas.width / 2), | |
428 | y: Math.round((locpoint.y - gPos.y) / gZoomFactor + gMapCanvas.height / 2)}; | |
429 | ||
430 | undrawCurrentLocation(); | |
b054bd48 RK |
431 | |
432 | // Cache overdrawn area. | |
4b1d0915 RK |
433 | gCurPosMapCache = |
434 | {point: locpoint, | |
435 | radius: circleRadius, | |
436 | data: gTrackContext.getImageData(mappos.x - circleRadius, | |
437 | mappos.y - circleRadius, | |
438 | circleRadius * 2, circleRadius * 2)}; | |
439 | ||
440 | gTrackContext.strokeStyle = gCurLocColor; | |
441 | gTrackContext.fillStyle = gTrackContext.strokeStyle; | |
442 | gTrackContext.beginPath(); | |
443 | gTrackContext.arc(mappos.x, mappos.y, | |
444 | circleRadius, 0, Math.PI * 2, false); | |
445 | gTrackContext.fill(); | |
446 | } | |
447 | ||
448 | function undrawCurrentLocation() { | |
449 | if (gCurPosMapCache) { | |
450 | var oldpoint = gCurPosMapCache.point; | |
451 | var oldmp = {x: Math.round((oldpoint.x - gPos.x) / gZoomFactor + gMapCanvas.width / 2), | |
452 | y: Math.round((oldpoint.y - gPos.y) / gZoomFactor + gMapCanvas.height / 2)}; | |
453 | gTrackContext.putImageData(gCurPosMapCache.data, | |
454 | oldmp.x - gCurPosMapCache.radius, | |
455 | oldmp.y - gCurPosMapCache.radius); | |
456 | gCurPosMapCache = undefined; | |
457 | } | |
b054bd48 RK |
458 | } |
459 | ||
23cd2dcc RK |
460 | var mapEvHandler = { |
461 | handleEvent: function(aEvent) { | |
462 | var touchEvent = aEvent.type.indexOf('touch') != -1; | |
463 | ||
8389557a RK |
464 | // Bail out if the event is happening on an input. |
465 | if (aEvent.target.tagName.toLowerCase() == "input") | |
466 | return; | |
467 | ||
1222624d RK |
468 | // Bail out on unwanted map moves, but not zoom or keyboard events. |
469 | if (aEvent.type.indexOf("mouse") === 0 || aEvent.type.indexOf("touch") === 0) { | |
23cd2dcc RK |
470 | // Bail out if this is neither a touch nor left-click. |
471 | if (!touchEvent && aEvent.button != 0) | |
472 | return; | |
473 | ||
474 | // Bail out if the started touch can't be found. | |
4b12da3a RK |
475 | if (touchEvent && gDragging && |
476 | !aEvent.changedTouches.identifiedTouch(gDragTouchID)) | |
23cd2dcc RK |
477 | return; |
478 | } | |
479 | ||
480 | var coordObj = touchEvent ? | |
4b12da3a | 481 | aEvent.changedTouches.identifiedTouch(gDragTouchID) : |
23cd2dcc RK |
482 | aEvent; |
483 | ||
484 | switch (aEvent.type) { | |
485 | case "mousedown": | |
486 | case "touchstart": | |
487 | if (touchEvent) { | |
517c0099 RK |
488 | if (aEvent.targetTouches.length == 2) { |
489 | gPinchStartWidth = Math.sqrt( | |
490 | Math.pow(aEvent.targetTouches.item(1).clientX - | |
491 | aEvent.targetTouches.item(0).clientX, 2) + | |
492 | Math.pow(aEvent.targetTouches.item(1).clientY - | |
493 | aEvent.targetTouches.item(0).clientY, 2) | |
494 | ); | |
495 | } | |
4b12da3a RK |
496 | gDragTouchID = aEvent.changedTouches.item(0).identifier; |
497 | coordObj = aEvent.changedTouches.identifiedTouch(gDragTouchID); | |
23cd2dcc | 498 | } |
4b1d0915 RK |
499 | var x = coordObj.clientX - gMapCanvas.offsetLeft; |
500 | var y = coordObj.clientY - gMapCanvas.offsetTop; | |
b395419b | 501 | |
23cd2dcc RK |
502 | if (touchEvent || aEvent.button === 0) { |
503 | gDragging = true; | |
504 | } | |
505 | gLastMouseX = x; | |
506 | gLastMouseY = y; | |
7a549148 | 507 | showUI(); |
23cd2dcc RK |
508 | break; |
509 | case "mousemove": | |
510 | case "touchmove": | |
517c0099 RK |
511 | if (touchEvent && aEvent.targetTouches.length == 2) { |
512 | curPinchStartWidth = Math.sqrt( | |
513 | Math.pow(aEvent.targetTouches.item(1).clientX - | |
514 | aEvent.targetTouches.item(0).clientX, 2) + | |
515 | Math.pow(aEvent.targetTouches.item(1).clientY - | |
516 | aEvent.targetTouches.item(0).clientY, 2) | |
517 | ); | |
003d56f8 RK |
518 | if (!gPinchStartWidth) |
519 | gPinchStartWidth = curPinchStartWidth; | |
d07d7abc | 520 | |
517c0099 RK |
521 | if (gPinchStartWidth / curPinchStartWidth > 1.7 || |
522 | gPinchStartWidth / curPinchStartWidth < 0.6) { | |
523 | var newZoomLevel = gPos.z + (gPinchStartWidth < curPinchStartWidth ? 1 : -1); | |
524 | if ((newZoomLevel >= 0) && (newZoomLevel <= gMaxZoom)) { | |
525 | // Calculate new center of the map - preserve middle of pinch. | |
526 | // This means that pixel distance between old center and middle | |
527 | // must equal pixel distance of new center and middle. | |
528 | var x = (aEvent.targetTouches.item(1).clientX + | |
529 | aEvent.targetTouches.item(0).clientX) / 2 - | |
530 | gMapCanvas.offsetLeft; | |
531 | var y = (aEvent.targetTouches.item(1).clientY + | |
532 | aEvent.targetTouches.item(0).clientY) / 2 - | |
533 | gMapCanvas.offsetTop; | |
534 | ||
535 | // Zoom factor after this action. | |
536 | var newZoomFactor = Math.pow(2, gMaxZoom - newZoomLevel); | |
537 | gPos.x -= (x - gMapCanvas.width / 2) * (newZoomFactor - gZoomFactor); | |
538 | gPos.y -= (y - gMapCanvas.height / 2) * (newZoomFactor - gZoomFactor); | |
539 | ||
540 | if (gPinchStartWidth < curPinchStartWidth) | |
541 | zoomIn(); | |
542 | else | |
543 | zoomOut(); | |
003d56f8 RK |
544 | |
545 | // Reset pinch start width and start another pinch gesture. | |
546 | gPinchStartWidth = null; | |
517c0099 RK |
547 | } |
548 | } | |
d07d7abc | 549 | // If we are in a pinch, do not drag. |
517c0099 RK |
550 | break; |
551 | } | |
4b1d0915 RK |
552 | var x = coordObj.clientX - gMapCanvas.offsetLeft; |
553 | var y = coordObj.clientY - gMapCanvas.offsetTop; | |
23cd2dcc RK |
554 | if (gDragging === true) { |
555 | var dX = x - gLastMouseX; | |
556 | var dY = y - gLastMouseY; | |
b395419b RK |
557 | gPos.x -= dX * gZoomFactor; |
558 | gPos.y -= dY * gZoomFactor; | |
747bbd55 RK |
559 | if (true) { // use optimized path |
560 | var mapData = gMapContext.getImageData(0, 0, | |
561 | gMapCanvas.width, | |
562 | gMapCanvas.height); | |
563 | gMapContext.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height); | |
564 | gMapContext.putImageData(mapData, dX, dY); | |
565 | drawMap({left: (dX > 0) ? dX : 0, | |
566 | right: (dX < 0) ? -dX : 0, | |
567 | top: (dY > 0) ? dY : 0, | |
568 | bottom: (dY < 0) ? -dY : 0}); | |
569 | } | |
570 | else { | |
571 | drawMap(false, true); | |
572 | } | |
7a549148 | 573 | showUI(); |
23cd2dcc RK |
574 | } |
575 | gLastMouseX = x; | |
576 | gLastMouseY = y; | |
577 | break; | |
578 | case "mouseup": | |
579 | case "touchend": | |
d07d7abc | 580 | gPinchStartWidth = null; |
23cd2dcc | 581 | gDragging = false; |
7a549148 | 582 | showUI(); |
23cd2dcc RK |
583 | break; |
584 | case "mouseout": | |
585 | case "touchcancel": | |
586 | case "touchleave": | |
587 | //gDragging = false; | |
588 | break; | |
5fb31b29 RK |
589 | case "wheel": |
590 | // If we'd want pixels, we'd need to calc up using aEvent.deltaMode. | |
591 | // See https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/wheel | |
592 | ||
593 | // Only accept (non-null) deltaY values | |
594 | if (!aEvent.deltaY) | |
595 | break; | |
23cd2dcc | 596 | |
55c4a0b7 RK |
597 | // Debug output: "coordinates" of the point the mouse was over. |
598 | /* | |
4b1d0915 RK |
599 | var ptCoord = {x: gPos.x + (x - gMapCanvas.width / 2) * gZoomFactor, |
600 | y: gPos.y + (x - gMapCanvas.height / 2) * gZoomFactor}; | |
55c4a0b7 RK |
601 | var gpsCoord = xy2gps(ptCoord.x, ptCoord.y); |
602 | var pt2Coord = gps2xy(gpsCoord.latitude, gpsCoord.longitude); | |
915d4271 RK |
603 | console.log(ptCoord.x + "/" + ptCoord.y + " - " + |
604 | gpsCoord.latitude + "/" + gpsCoord.longitude + " - " + | |
605 | pt2Coord.x + "/" + pt2Coord.y); | |
55c4a0b7 | 606 | */ |
4b1d0915 | 607 | |
5fb31b29 | 608 | var newZoomLevel = gPos.z + (aEvent.deltaY < 0 ? 1 : -1); |
4b1d0915 RK |
609 | if ((newZoomLevel >= 0) && (newZoomLevel <= gMaxZoom)) { |
610 | // Calculate new center of the map - same point stays under the mouse. | |
611 | // This means that the pixel distance between the old center and point | |
612 | // must equal the pixel distance of the new center and that point. | |
613 | var x = coordObj.clientX - gMapCanvas.offsetLeft; | |
614 | var y = coordObj.clientY - gMapCanvas.offsetTop; | |
615 | ||
616 | // Zoom factor after this action. | |
617 | var newZoomFactor = Math.pow(2, gMaxZoom - newZoomLevel); | |
618 | gPos.x -= (x - gMapCanvas.width / 2) * (newZoomFactor - gZoomFactor); | |
619 | gPos.y -= (y - gMapCanvas.height / 2) * (newZoomFactor - gZoomFactor); | |
620 | ||
5fb31b29 | 621 | if (aEvent.deltaY < 0) |
4b1d0915 | 622 | zoomIn(); |
5fb31b29 | 623 | else |
4b1d0915 RK |
624 | zoomOut(); |
625 | } | |
23cd2dcc | 626 | break; |
1222624d RK |
627 | case "keydown": |
628 | // Allow keyboard control to move and zoom the map. | |
629 | // Should use aEvent.key instead of aEvent.which but needs bug 680830. | |
630 | // See https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/keydown | |
631 | var dX = 0; | |
632 | var dY = 0; | |
633 | switch (aEvent.which) { | |
634 | case 39: // right | |
635 | dX = -gTileSize / 2; | |
636 | break; | |
637 | case 37: // left | |
638 | dX = gTileSize / 2; | |
639 | break; | |
640 | case 38: // up | |
641 | dY = gTileSize / 2; | |
642 | break; | |
643 | case 40: // down | |
644 | dY = -gTileSize / 2; | |
645 | break; | |
646 | case 87: // w | |
647 | case 107: // + (numpad) | |
648 | case 171: // + (normal key) | |
649 | zoomIn(); | |
650 | break; | |
651 | case 83: // s | |
652 | case 109: // - (numpad) | |
653 | case 173: // - (normal key) | |
654 | zoomOut(); | |
655 | break; | |
656 | case 48: // 0 | |
657 | case 49: // 1 | |
658 | case 50: // 2 | |
659 | case 51: // 3 | |
660 | case 52: // 4 | |
661 | case 53: // 5 | |
662 | case 54: // 6 | |
663 | case 55: // 7 | |
664 | case 56: // 8 | |
665 | zoomTo(aEvent.which - 38); | |
666 | break; | |
667 | case 57: // 9 | |
668 | zoomTo(9); | |
669 | break; | |
670 | case 96: // 0 (numpad) | |
671 | case 97: // 1 (numpad) | |
672 | case 98: // 2 (numpad) | |
673 | case 99: // 3 (numpad) | |
674 | case 100: // 4 (numpad) | |
675 | case 101: // 5 (numpad) | |
676 | case 102: // 6 (numpad) | |
677 | case 103: // 7 (numpad) | |
678 | case 104: // 8 (numpad) | |
679 | zoomTo(aEvent.which - 86); | |
680 | break; | |
681 | case 105: // 9 (numpad) | |
682 | zoomTo(9); | |
683 | break; | |
684 | default: // not supported | |
685 | console.log("key not supported: " + aEvent.which); | |
686 | break; | |
687 | } | |
688 | ||
689 | // Move if needed. | |
690 | if (dX || dY) { | |
691 | gPos.x -= dX * gZoomFactor; | |
692 | gPos.y -= dY * gZoomFactor; | |
693 | if (true) { // use optimized path | |
694 | var mapData = gMapContext.getImageData(0, 0, | |
695 | gMapCanvas.width, | |
696 | gMapCanvas.height); | |
697 | gMapContext.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height); | |
698 | gMapContext.putImageData(mapData, dX, dY); | |
699 | drawMap({left: (dX > 0) ? dX : 0, | |
700 | right: (dX < 0) ? -dX : 0, | |
701 | top: (dY > 0) ? dY : 0, | |
702 | bottom: (dY < 0) ? -dY : 0}); | |
703 | } | |
704 | else { | |
705 | drawMap(false, true); | |
706 | } | |
707 | } | |
708 | break; | |
23cd2dcc RK |
709 | } |
710 | } | |
711 | }; | |
55c4a0b7 | 712 | |
993fd081 | 713 | var geofake = { |
55c4a0b7 | 714 | tracking: false, |
4b12da3a | 715 | lastPos: {x: undefined, y: undefined}, |
55c4a0b7 RK |
716 | watchPosition: function(aSuccessCallback, aErrorCallback, aPrefObject) { |
717 | this.tracking = true; | |
718 | var watchCall = function() { | |
4b12da3a RK |
719 | // calc new position in lat/lon degrees |
720 | // 90° on Earth surface are ~10,000 km at the equator, | |
721 | // so try moving at most 10m at a time | |
722 | if (geofake.lastPos.x) | |
723 | geofake.lastPos.x += (Math.random() - .5) * 90 / 1000000 | |
724 | else | |
725 | geofake.lastPos.x = 48.208174 | |
726 | if (geofake.lastPos.y) | |
727 | geofake.lastPos.y += (Math.random() - .5) * 90 / 1000000 | |
728 | else | |
729 | geofake.lastPos.y = 16.373819 | |
55c4a0b7 | 730 | aSuccessCallback({timestamp: Date.now(), |
4b12da3a RK |
731 | coords: {latitude: geofake.lastPos.x, |
732 | longitude: geofake.lastPos.y, | |
55c4a0b7 RK |
733 | accuracy: 20}}); |
734 | if (geofake.tracking) | |
735 | setTimeout(watchCall, 1000); | |
736 | }; | |
737 | setTimeout(watchCall, 1000); | |
738 | return "foo"; | |
739 | }, | |
740 | clearWatch: function(aID) { | |
741 | this.tracking = false; | |
742 | } | |
743 | } | |
744 | ||
3610c22d RK |
745 | function setCentering(aCheckbox) { |
746 | if (gMapPrefsLoaded && mainDB) | |
747 | gPrefs.set("center_map", aCheckbox.checked); | |
748 | gCenterPosition = aCheckbox.checked; | |
749 | } | |
750 | ||
751 | function setTracking(aCheckbox) { | |
752 | if (gMapPrefsLoaded && mainDB) | |
753 | gPrefs.set("tracking_enabled", aCheckbox.checked); | |
754 | if (aCheckbox.checked) | |
755 | startTracking(); | |
756 | else | |
757 | endTracking(); | |
758 | } | |
759 | ||
55c4a0b7 | 760 | function startTracking() { |
31f0fe16 | 761 | if (gGeolocation) { |
68afcd96 RK |
762 | gActionLabel.textContent = "Establishing Position"; |
763 | gAction.style.display = "block"; | |
4b12da3a | 764 | gGeoWatchID = gGeolocation.watchPosition( |
55c4a0b7 | 765 | function(position) { |
68afcd96 RK |
766 | if (gActionLabel.textContent) { |
767 | gActionLabel.textContent = ""; | |
768 | gAction.style.display = "none"; | |
769 | } | |
55c4a0b7 | 770 | // Coords spec: https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMGeoPositionCoords |
993fd081 | 771 | var tPoint = {time: position.timestamp, |
31f0fe16 RK |
772 | coords: {latitude: position.coords.latitude, |
773 | longitude: position.coords.longitude, | |
774 | altitude: position.coords.altitude, | |
775 | accuracy: position.coords.accuracy, | |
776 | altitudeAccuracy: position.coords.altitudeAccuracy, | |
777 | heading: position.coords.heading, | |
778 | speed: position.coords.speed}, | |
993fd081 | 779 | beginSegment: !gLastTrackPoint}; |
b054bd48 RK |
780 | // Only add point to track is accuracy is good enough. |
781 | if (tPoint.coords.accuracy < gMinTrackAccuracy) { | |
782 | gLastTrackPoint = tPoint; | |
783 | gTrack.push(tPoint); | |
784 | try { gTrackStore.push(tPoint); } catch(e) {} | |
785 | var redrawn = false; | |
786 | if (gCenterPosition) { | |
787 | var posCoord = gps2xy(position.coords.latitude, | |
788 | position.coords.longitude); | |
4b1d0915 RK |
789 | if (Math.abs(gPos.x - posCoord.x) > gMapCanvas.width * gZoomFactor / 4 || |
790 | Math.abs(gPos.y - posCoord.y) > gMapCanvas.height * gZoomFactor / 4) { | |
b054bd48 RK |
791 | gPos.x = posCoord.x; |
792 | gPos.y = posCoord.y; | |
793 | drawMap(); // This draws the current point as well. | |
794 | redrawn = true; | |
795 | } | |
99631a75 | 796 | } |
b054bd48 | 797 | if (!redrawn) |
4b1d0915 | 798 | undrawCurrentLocation(); |
b054bd48 | 799 | drawTrackPoint(position.coords.latitude, position.coords.longitude, true); |
05c21757 | 800 | } |
b054bd48 | 801 | drawCurrentLocation(tPoint); |
55c4a0b7 RK |
802 | }, |
803 | function(error) { | |
804 | // Ignore erros for the moment, but this is good for debugging. | |
805 | // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors | |
915d4271 RK |
806 | if (gDebug) |
807 | console.log(error.message); | |
55c4a0b7 RK |
808 | }, |
809 | {enableHighAccuracy: true} | |
810 | ); | |
811 | } | |
812 | } | |
813 | ||
814 | function endTracking() { | |
68afcd96 RK |
815 | if (gActionLabel.textContent) { |
816 | gActionLabel.textContent = ""; | |
817 | gAction.style.display = "none"; | |
818 | } | |
55c4a0b7 | 819 | if (gGeoWatchID) { |
4b12da3a | 820 | gGeolocation.clearWatch(gGeoWatchID); |
55c4a0b7 RK |
821 | } |
822 | } | |
993fd081 RK |
823 | |
824 | function clearTrack() { | |
825 | gTrack = []; | |
826 | gTrackStore.clear(); | |
6ddefbf9 | 827 | drawTrack(); |
993fd081 | 828 | } |
a8634d37 RK |
829 | |
830 | var gTileService = { | |
831 | objStore: "tilecache", | |
832 | ||
5d67397a | 833 | ageLimit: 14 * 86400 * 1000, // 2 weeks (in ms) |
3431f496 | 834 | |
a8634d37 RK |
835 | get: function(aStyle, aCoords, aCallback) { |
836 | var norm = normalizeCoords(aCoords); | |
837 | var dbkey = aStyle + "::" + norm.x + "," + norm.y + "," + norm.z; | |
838 | this.getDBCache(dbkey, function(aResult, aEvent) { | |
839 | if (aResult) { | |
840 | // We did get a cached object. | |
a8634d37 | 841 | aCallback(aResult.image, aStyle, aCoords); |
3431f496 | 842 | // Look at the timestamp and return if it's not too old. |
5d67397a | 843 | if (aResult.timestamp + gTileService.ageLimit > Date.now()) |
3431f496 RK |
844 | return; |
845 | // Reload cached tile otherwise. | |
5d67397a RK |
846 | var oldDate = new Date(aResult.timestamp); |
847 | console.log("reload cached tile: " + dbkey + " - " + oldDate.toUTCString()); | |
a8634d37 | 848 | } |
3431f496 RK |
849 | // Retrieve image from the web and store it in the cache. |
850 | var XHR = new XMLHttpRequest(); | |
851 | XHR.open("GET", | |
852 | gMapStyles[aStyle].url | |
853 | .replace("{x}", norm.x) | |
854 | .replace("{y}", norm.y) | |
855 | .replace("{z}", norm.z) | |
856 | .replace("[a-c]", String.fromCharCode(97 + Math.floor(Math.random() * 2))) | |
857 | .replace("[1-4]", 1 + Math.floor(Math.random() * 3)), | |
858 | true); | |
859 | XHR.responseType = "blob"; | |
860 | XHR.addEventListener("load", function () { | |
861 | if (XHR.status === 200) { | |
862 | var blob = XHR.response; | |
3431f496 | 863 | aCallback(blob, aStyle, aCoords); |
6d7cdcf6 | 864 | gTileService.setDBCache(dbkey, {image: blob, timestamp: Date.now()}); |
3431f496 RK |
865 | } |
866 | }, false); | |
867 | XHR.send(); | |
a8634d37 RK |
868 | }); |
869 | }, | |
870 | ||
871 | getDBCache: function(aKey, aCallback) { | |
872 | if (!mainDB) | |
873 | return; | |
874 | var transaction = mainDB.transaction([this.objStore]); | |
875 | var request = transaction.objectStore(this.objStore).get(aKey); | |
876 | request.onsuccess = function(event) { | |
877 | aCallback(request.result, event); | |
878 | }; | |
879 | request.onerror = function(event) { | |
880 | // Errors can be handled here. | |
881 | aCallback(undefined, event); | |
882 | }; | |
883 | }, | |
884 | ||
885 | setDBCache: function(aKey, aValue, aCallback) { | |
886 | if (!mainDB) | |
887 | return; | |
888 | var success = false; | |
889 | var transaction = mainDB.transaction([this.objStore], "readwrite"); | |
890 | var objStore = transaction.objectStore(this.objStore); | |
891 | var request = objStore.put(aValue, aKey); | |
892 | request.onsuccess = function(event) { | |
893 | success = true; | |
894 | if (aCallback) | |
895 | aCallback(success, event); | |
896 | }; | |
897 | request.onerror = function(event) { | |
898 | // Errors can be handled here. | |
899 | if (aCallback) | |
900 | aCallback(success, event); | |
901 | }; | |
902 | }, | |
903 | ||
904 | unsetDBCache: function(aKey, aCallback) { | |
905 | if (!mainDB) | |
906 | return; | |
907 | var success = false; | |
908 | var transaction = mainDB.transaction([this.objStore], "readwrite"); | |
909 | var request = transaction.objectStore(this.objStore).delete(aKey); | |
910 | request.onsuccess = function(event) { | |
911 | success = true; | |
912 | if (aCallback) | |
913 | aCallback(success, event); | |
914 | }; | |
915 | request.onerror = function(event) { | |
916 | // Errors can be handled here. | |
917 | if (aCallback) | |
918 | aCallback(success, event); | |
919 | } | |
3431f496 RK |
920 | }, |
921 | ||
922 | clearDB: function(aCallback) { | |
923 | if (!mainDB) | |
924 | return; | |
925 | var success = false; | |
926 | var transaction = mainDB.transaction([this.objStore], "readwrite"); | |
927 | var request = transaction.objectStore(this.objStore).clear(); | |
928 | request.onsuccess = function(event) { | |
929 | success = true; | |
930 | if (aCallback) | |
931 | aCallback(success, event); | |
932 | }; | |
933 | request.onerror = function(event) { | |
934 | // Errors can be handled here. | |
935 | if (aCallback) | |
936 | aCallback(success, event); | |
937 | } | |
a8634d37 RK |
938 | } |
939 | }; |