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