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