Commit | Line | Data |
---|---|---|
feec2d1c 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/. */ | |
4 | ||
5 | var gStardate, gSDBase; | |
14a57547 | 6 | var gSounds = {}; |
feec2d1c RK |
7 | |
8 | window.onload = function() { | |
9 | setTimeout(updateStardate, 0); | |
14a57547 RK |
10 | gSounds.scan = new Audio("sound/scan.opus"); |
11 | gSounds.scan.loop = true; | |
12 | gSounds.launch = new Audio("sound/launch.opus"); | |
13 | gSounds.shutdown = new Audio("sound/shutdown.opus"); | |
14 | gSounds.keyaction = new Audio("sound/key-action.opus"); | |
15 | gSounds.keypress = new Audio("sound/key-press.opus"); | |
16 | ||
84436ccb RK |
17 | document.getElementById("fullScreenButton").addEventListener("click", |
18 | function(aEvent) { toggleFullscreen(); }, false); | |
19 | ||
20 | var navItems = document.getElementById("navlist").children; | |
21 | for (var i = 0; i <= navItems.length - 1; i++) { | |
22 | navItems[i].addEventListener("click", | |
23 | function(aEvent) { | |
24 | switchModule(aEvent.target.id.replace("nav", "")); | |
25 | }, false); | |
26 | } | |
27 | ||
14a57547 RK |
28 | gSounds.launch.play(); |
29 | window.addEventListener("beforeunload", function( event ) { | |
30 | gSounds.shutdown.play(); | |
31 | }, false); | |
feec2d1c RK |
32 | } |
33 | ||
34 | function updateStardate() { | |
35 | if (!gStardate) | |
36 | gStardate = document.getElementById("stardate"); | |
37 | ||
38 | var curDate = new Date(); | |
39 | ||
40 | if (!gSDBase) | |
41 | gSDBase = new Date("September 8, 1966 20:00:00 EST"); | |
42 | ||
43 | var sdateval = (curDate - gSDBase) / (86400 * 365.2425); | |
44 | gStardate.textContent = sdateval.toFixed(1); | |
45 | ||
46 | setTimeout(updateStardate, 5*60*1000); | |
eabed1a4 RK |
47 | } |
48 | ||
14a57547 RK |
49 | function toggleFullscreen() { |
50 | gSounds.keyaction.play(); | |
51 | if ((document.fullScreenElement && document.fullScreenElement !== null) || | |
52 | (document.mozFullScreenElement && document.mozFullScreenElement !== null) || | |
53 | (document.webkitFullScreenElement && document.webkitFullScreenElement !== null)) { | |
54 | if (document.cancelFullScreen) { | |
55 | document.cancelFullScreen(); | |
56 | } else if (document.mozCancelFullScreen) { | |
57 | document.mozCancelFullScreen(); | |
58 | } else if (document.webkitCancelFullScreen) { | |
59 | document.webkitCancelFullScreen(); | |
60 | } | |
61 | } | |
62 | else { | |
63 | var elem = document.getElementById("body"); | |
64 | if (elem.requestFullScreen) { | |
65 | elem.requestFullScreen(); | |
66 | } else if (elem.mozRequestFullScreen) { | |
67 | elem.mozRequestFullScreen(); | |
68 | } else if (elem.webkitRequestFullScreen) { | |
69 | elem.webkitRequestFullScreen(); | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
84436ccb | 74 | function switchModule(aModname) { |
14a57547 | 75 | gSounds.keyaction.play(); |
eabed1a4 RK |
76 | var sections = document.getElementsByTagName('section'); |
77 | for (var i = 0; i <= sections.length - 1; i++) { | |
1753d94a RK |
78 | if (sections[i].classList.contains("active")) { |
79 | window["gMod" + sections[i].id.replace("sect", "")].deactivate(); | |
80 | sections[i].classList.remove("active"); | |
81 | } | |
eabed1a4 RK |
82 | } |
83 | var navs = document.getElementById('navlist').children; | |
84 | for (var i = 0; i <= navs.length - 1; i++) { | |
85 | navs[i].classList.remove("active"); | |
86 | } | |
87 | ||
84436ccb | 88 | var navItem = document.getElementById("nav" + aModname); |
1b0ec1c4 | 89 | navItem.classList.add("active"); |
84436ccb | 90 | document.getElementById("sect" + aModname).classList.add("active"); |
1b0ec1c4 | 91 | document.getElementById("mainHeader").textContent = |
84436ccb | 92 | (aModname == "Other") ? "Web Tricorder" : navItem.textContent; |
1753d94a | 93 | |
84436ccb | 94 | window["gMod" + aModname].activate(); |
1753d94a RK |
95 | } |
96 | ||
97 | var gModPos = { | |
98 | activate: function() { | |
99 | if (navigator.geolocation) { | |
14a57547 | 100 | gSounds.scan.play(); |
1753d94a RK |
101 | document.getElementById("posunavail").style.display = "none"; |
102 | document.getElementById("posavail").style.display = "block"; | |
103 | this.watchID = navigator.geolocation.watchPosition( | |
104 | function(position) { | |
33a80b5e RK |
105 | document.getElementById("posLat").textContent = |
106 | position.coords.latitude + "°"; | |
107 | document.getElementById("posLong").textContent = | |
108 | position.coords.longitude + "°"; | |
109 | document.getElementById("posAlt").textContent = | |
110 | position.coords.altitude.toFixed(0) + " m"; | |
111 | document.getElementById("posAcc").textContent = | |
112 | position.coords.accuracy.toFixed(0) + " m"; | |
113 | document.getElementById("posAltAcc").textContent = | |
114 | position.coords.altitudeAccuracy.toFixed(0) + " m"; | |
115 | document.getElementById("posHead").textContent = | |
116 | position.coords.heading ? position.coords.heading.toFixed(0) + "°" : "---"; | |
117 | document.getElementById("posSpd").textContent = | |
118 | position.coords.speed ? position.coords.speed.toFixed(1) + " m/s" : "---"; | |
1753d94a RK |
119 | var locTime = new Date(position.timestamp); |
120 | document.getElementById("posTime").textContent = locTime.toISOString(); | |
121 | }, | |
122 | function(error) { | |
123 | // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors | |
730b3151 RK |
124 | if (error.message) { |
125 | document.getElementById("posLat").textContent = error.message; | |
126 | document.getElementById("posLong").textContent = "..."; | |
127 | document.getElementById("posAlt").textContent = "..."; | |
128 | document.getElementById("posAcc").textContent = "..."; | |
129 | document.getElementById("posAltAcc").textContent = "..."; | |
130 | document.getElementById("posHead").textContent = "..."; | |
131 | document.getElementById("posSpd").textContent = "..."; | |
132 | document.getElementById("posTime").textContent = "..."; | |
48fd8f1a | 133 | setTimeout(function() { gModPos.deactivate(); }, 5000); |
730b3151 RK |
134 | } |
135 | else { | |
136 | document.getElementById("posunavail").style.display = "block"; | |
137 | document.getElementById("posavail").style.display = "none"; | |
138 | } | |
e6b9b946 | 139 | gSounds.scan.pause(); |
1753d94a | 140 | }, |
48fd8f1a | 141 | {enableHighAccuracy: true, maximumAge: 10000, timeout: 60000} |
1753d94a RK |
142 | ); |
143 | } | |
144 | else { | |
145 | document.getElementById("posunavail").style.display = "block"; | |
146 | document.getElementById("posavail").style.display = "none"; | |
147 | } | |
148 | }, | |
149 | deactivate: function() { | |
14a57547 | 150 | gSounds.scan.pause(); |
1753d94a RK |
151 | if (this.watchID) { |
152 | navigator.geolocation.clearWatch(this.watchID); | |
153 | } | |
154 | document.getElementById("posunavail").style.display = "block"; | |
155 | document.getElementById("posavail").style.display = "none"; | |
156 | document.getElementById("posLat").textContent = "..."; | |
157 | document.getElementById("posLong").textContent = "..."; | |
158 | document.getElementById("posAlt").textContent = "..."; | |
159 | document.getElementById("posAcc").textContent = "..."; | |
160 | document.getElementById("posAltAcc").textContent = "..."; | |
161 | document.getElementById("posHead").textContent = "..."; | |
162 | document.getElementById("posSpd").textContent = "..."; | |
163 | document.getElementById("posTime").textContent = "..."; | |
164 | }, | |
165 | watchID: null, | |
166 | } | |
167 | ||
168 | var gModGrav = { | |
169 | activate: function() { | |
14a57547 | 170 | gSounds.scan.play(); |
1753d94a RK |
171 | document.getElementById("gravunavail").style.display = "none"; |
172 | document.getElementById("gravavail").style.display = "block"; | |
173 | window.addEventListener("deviceorientation", this.orientEvent, true); | |
174 | window.addEventListener("devicemotion", this.motionEvent, true); | |
730b3151 RK |
175 | setTimeout(function() { |
176 | if ((document.getElementById("gravAlpha").textContent == "...") && | |
177 | (document.getElementById("gravX").textContent == "...")) { | |
178 | gModGrav.deactivate(); | |
179 | } | |
180 | }, 3000); | |
1753d94a RK |
181 | }, |
182 | deactivate: function() { | |
14a57547 | 183 | gSounds.scan.pause(); |
1753d94a RK |
184 | window.removeEventListener("deviceorientation", this.orientEvent, true); |
185 | window.removeEventListener("devicemotion", this.motionEvent, true); | |
186 | document.getElementById("gravunavail").style.display = "block"; | |
187 | document.getElementById("gravavail").style.display = "none"; | |
730b3151 RK |
188 | //document.getElementById("gravAbs").textContent = "..."; |
189 | document.getElementById("gravAlpha").textContent = "..."; | |
190 | document.getElementById("gravBeta").textContent = "..."; | |
191 | document.getElementById("gravGamma").textContent = "..."; | |
33a80b5e | 192 | document.getElementById("gravTotal").textContent = "..."; |
730b3151 RK |
193 | document.getElementById("gravX").textContent = "..."; |
194 | document.getElementById("gravY").textContent = "..."; | |
195 | document.getElementById("gravZ").textContent = "..."; | |
196 | //document.getElementById("gravRot").textContent = "..."; | |
1753d94a RK |
197 | }, |
198 | orientEvent: function(orientData) { | |
b5c483f2 RK |
199 | //document.getElementById("gravAbs").textContent = orientData.absolute; |
200 | document.getElementById("gravAlpha").textContent = orientData.alpha.toFixed(1) + "°"; | |
201 | document.getElementById("gravBeta").textContent = orientData.beta.toFixed(1) + "°"; | |
202 | document.getElementById("gravGamma").textContent = orientData.gamma.toFixed(1) + "°"; | |
1753d94a RK |
203 | }, |
204 | motionEvent: function(event) { | |
33a80b5e RK |
205 | var gravTotal = |
206 | Math.sqrt(Math.pow(event.accelerationIncludingGravity.x, 2) + | |
207 | Math.pow(event.accelerationIncludingGravity.y, 2) + | |
208 | Math.pow(event.accelerationIncludingGravity.z, 2)); | |
209 | document.getElementById("gravTotal").textContent = gravTotal.toFixed(2) + " m/s²"; | |
b5c483f2 RK |
210 | document.getElementById("gravX").textContent = event.accelerationIncludingGravity.x.toFixed(2) + " m/s²"; |
211 | document.getElementById("gravY").textContent = event.accelerationIncludingGravity.y.toFixed(2) + " m/s²"; | |
212 | document.getElementById("gravZ").textContent = event.accelerationIncludingGravity.z.toFixed(2) + " m/s²"; | |
213 | //document.getElementById("gravRot").textContent = event.rotationRate; | |
1753d94a RK |
214 | }, |
215 | } | |
216 | ||
84436ccb RK |
217 | |
218 | var gModDev = { | |
219 | activate: function() { | |
220 | gSounds.scan.play(); | |
221 | this.batteryTimer = | |
222 | setInterval(function () { gModDev.updateBattery(); }, 100); | |
223 | }, | |
224 | deactivate: function() { | |
225 | clearTimeout(this.batteryTimer); | |
226 | gSounds.scan.pause(); | |
227 | }, | |
228 | updateBattery: function() { | |
229 | document.getElementById("devBattLevel").textContent = (navigator.battery.level * 100).toFixed(1) + "%"; | |
230 | document.getElementById("devBattStatus").textContent = | |
231 | navigator.battery.charging ? "charging, " + navigator.battery.chargingTime + "s remaining" | |
232 | : navigator.battery.dischargingTime + "s usage remaining"; | |
233 | }, | |
234 | batteryTimer: null, | |
235 | } | |
236 | ||
237 | var gModNull = { | |
238 | activate: function() { | |
239 | //gSounds.scan.play(); | |
240 | }, | |
241 | deactivate: function() { | |
242 | gSounds.scan.pause(); | |
243 | }, | |
244 | } | |
245 | ||
d3e5ba11 | 246 | var gModOther = { |
14a57547 RK |
247 | activate: function() { |
248 | //gSounds.scan.play(); | |
249 | }, | |
250 | deactivate: function() { | |
251 | gSounds.scan.pause(); | |
252 | }, | |
1753d94a RK |
253 | } |
254 | ||
255 | var gModNull = { | |
14a57547 RK |
256 | activate: function() { |
257 | //gSounds.scan.play(); | |
258 | }, | |
259 | deactivate: function() { | |
260 | gSounds.scan.pause(); | |
261 | }, | |
eabed1a4 | 262 | } |