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