improve status messages
[tricorder.git] / js / tricorder.js
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   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);
47 }
48
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
74 function switchModule(aModname) {
75   gSounds.keyaction.play();
76   var sections = document.getElementsByTagName('section');
77   for (var i = 0; i <= sections.length - 1; i++) {
78     if (sections[i].classList.contains("active")) {
79       window["gMod" + sections[i].id.replace("sect", "")].deactivate();
80       sections[i].classList.remove("active");
81     }
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
88   var navItem = document.getElementById("nav" + aModname);
89   navItem.classList.add("active");
90   document.getElementById("sect" + aModname).classList.add("active");
91   document.getElementById("mainHeader").textContent =
92       (aModname == "Other") ? "Web Tricorder" : navItem.textContent;
93
94   window["gMod" + aModname].activate();
95 }
96
97 var gModPos = {
98   activate: function() {
99     if (navigator.geolocation) {
100       gSounds.scan.play();
101       document.getElementById("posunavail").style.display = "none";
102       document.getElementById("posavail").style.display = "block";
103       this.watchID = navigator.geolocation.watchPosition(
104         function(position) {
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" : "---";
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
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 = "...";
133             setTimeout(function() { gModPos.deactivate(); }, 5000);
134           }
135           else {
136             document.getElementById("posunavail").style.display = "block";
137             document.getElementById("posavail").style.display = "none";
138           }
139           gSounds.scan.pause();
140         },
141         {enableHighAccuracy: true, maximumAge: 10000, timeout: 60000}
142       );
143     }
144     else {
145       document.getElementById("posunavail").style.display = "block";
146       document.getElementById("posavail").style.display = "none";
147     }
148   },
149   deactivate: function() {
150     gSounds.scan.pause();
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() {
170     gSounds.scan.play();
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);
175     setTimeout(function() {
176       if ((document.getElementById("gravAlpha").textContent == "...") &&
177           (document.getElementById("gravX").textContent == "...")) {
178         gModGrav.deactivate();
179       }
180     }, 3000);
181   },
182   deactivate: function() {
183     gSounds.scan.pause();
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";
188     //document.getElementById("gravAbs").textContent = "...";
189     document.getElementById("gravAlpha").textContent = "...";
190     document.getElementById("gravBeta").textContent = "...";
191     document.getElementById("gravGamma").textContent = "...";
192     document.getElementById("gravTotal").textContent = "...";
193     document.getElementById("gravX").textContent = "...";
194     document.getElementById("gravY").textContent = "...";
195     document.getElementById("gravZ").textContent = "...";
196     //document.getElementById("gravRot").textContent = "...";
197   },
198   orientEvent: function(orientData) {
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) + "°";
203   },
204   motionEvent: function(event) {
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²";
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;
214   },
215 }
216
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 =
230         (navigator.battery.level * 100).toFixed(1) + "%";
231     if (navigator.battery.charging) {
232       if (navigator.battery.chargingTime == 0 ||
233           navigator.battery.chargingTime == Infinity) {
234         document.getElementById("devBattStatus").textContent = "charging";
235       }
236       else {
237         document.getElementById("devBattStatus").textContent = 
238             "charging, " + navigator.battery.chargingTime + "s remaining";
239       }
240     }
241     else {
242       if (navigator.battery.dischargingTime == 0 ||
243           navigator.battery.dischargingTime == Infinity) {
244         document.getElementById("devBattStatus").textContent = "discharging";
245       }
246       else {
247         document.getElementById("devBattStatus").textContent = 
248             navigator.battery.dischargingTime + "s usage remaining";
249       }
250     }
251   },
252   batteryTimer: null,
253 }
254
255 var gModNull = {
256   activate: function() {
257     //gSounds.scan.play();
258   },
259   deactivate: function() {
260     gSounds.scan.pause();
261   },
262 }
263
264 var gModOther = {
265   activate: function() {
266     //gSounds.scan.play();
267   },
268   deactivate: function() {
269     gSounds.scan.pause();
270   },
271 }
272
273 var gModNull = {
274   activate: function() {
275     //gSounds.scan.play();
276   },
277   deactivate: function() {
278     gSounds.scan.pause();
279   },
280 }