From 1753d94ae19a06d64123c99d7d5c3184db00e155 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Sat, 24 Nov 2012 16:38:09 +0100 Subject: [PATCH] implement position and gravity modules in a simple way --- index.html | 40 ++++++++++++++++++-- js/tricorder.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 133 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 9d407e0..cd2e16c 100644 --- a/index.html +++ b/index.html @@ -53,14 +53,46 @@ Web Tricorder
-
-

This is the main area for tricorder output.

+
+

Please select a data input module from the left-side navigation.

-

This is the main area for position tricorder output.

+

+Position watching is unavailable on this tricorder device. +
Please talk to your superior officer or Starfleet contact to acquire +a better device. +

+
-

This is the main area for gravity tricorder output.

+

+Gravity sensors are unavailable on this tricorder device. +
Please talk to your superior officer or Starfleet contact to acquire +a better device. +

+

This is the main area for acoustics tricorder output.

diff --git a/js/tricorder.js b/js/tricorder.js index c1b61e7..f82d992 100644 --- a/js/tricorder.js +++ b/js/tricorder.js @@ -26,7 +26,10 @@ function updateStardate() { function switchModule(modname) { var sections = document.getElementsByTagName('section'); for (var i = 0; i <= sections.length - 1; i++) { - sections[i].classList.remove("active"); + if (sections[i].classList.contains("active")) { + window["gMod" + sections[i].id.replace("sect", "")].deactivate(); + sections[i].classList.remove("active"); + } } var navs = document.getElementById('navlist').children; for (var i = 0; i <= navs.length - 1; i++) { @@ -35,4 +38,97 @@ function switchModule(modname) { document.getElementById("nav" + modname).classList.add("active"); document.getElementById("sect" + modname).classList.add("active"); + + window["gMod" + modname].activate(); +} + +var gModPos = { + activate: function() { + if (navigator.geolocation) { + document.getElementById("posunavail").style.display = "none"; + document.getElementById("posavail").style.display = "block"; + this.watchID = navigator.geolocation.watchPosition( + function(position) { + document.getElementById("posLat").textContent = position.coords.latitude; + document.getElementById("posLong").textContent = position.coords.longitude; + document.getElementById("posAlt").textContent = position.coords.altitude; + document.getElementById("posAcc").textContent = position.coords.accuracy; + document.getElementById("posAltAcc").textContent = position.coords.altitudeAccuracy; + document.getElementById("posHead").textContent = position.coords.heading || "---"; + document.getElementById("posSpd").textContent = position.coords.speed || "---"; + var locTime = new Date(position.timestamp); + document.getElementById("posTime").textContent = locTime.toISOString(); + }, + function(error) { + // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors + document.getElementById("posLat").textContent = error.message; + document.getElementById("posLong").textContent = "..."; + document.getElementById("posAlt").textContent = "..."; + document.getElementById("posAcc").textContent = "..."; + document.getElementById("posAltAcc").textContent = "..."; + document.getElementById("posHead").textContent = "..."; + document.getElementById("posSpd").textContent = "..."; + document.getElementById("posTime").textContent = "..."; + }, + {enableHighAccuracy: true} + ); + } + else { + document.getElementById("posunavail").style.display = "block"; + document.getElementById("posavail").style.display = "none"; + } + }, + deactivate: function() { + if (this.watchID) { + navigator.geolocation.clearWatch(this.watchID); + } + document.getElementById("posunavail").style.display = "block"; + document.getElementById("posavail").style.display = "none"; + document.getElementById("posLat").textContent = "..."; + document.getElementById("posLong").textContent = "..."; + document.getElementById("posAlt").textContent = "..."; + document.getElementById("posAcc").textContent = "..."; + document.getElementById("posAltAcc").textContent = "..."; + document.getElementById("posHead").textContent = "..."; + document.getElementById("posSpd").textContent = "..."; + document.getElementById("posTime").textContent = "..."; + }, + watchID: null, +} + +var gModGrav = { + activate: function() { + document.getElementById("gravunavail").style.display = "none"; + document.getElementById("gravavail").style.display = "block"; + window.addEventListener("deviceorientation", this.orientEvent, true); + window.addEventListener("devicemotion", this.motionEvent, true); + }, + deactivate: function() { + window.removeEventListener("deviceorientation", this.orientEvent, true); + window.removeEventListener("devicemotion", this.motionEvent, true); + document.getElementById("gravunavail").style.display = "block"; + document.getElementById("gravavail").style.display = "none"; + }, + orientEvent: function(orientData) { + document.getElementById("gravAbs").textContent = orientData.absolute; + document.getElementById("gravAlpha").textContent = orientData.alpha; + document.getElementById("gravBeta").textContent = orientData.beta; + document.getElementById("gravGamma").textContent = orientData.gamma; + }, + motionEvent: function(event) { + document.getElementById("gravX").textContent = event.accelerationIncludingGravity.x; + document.getElementById("gravY").textContent = event.accelerationIncludingGravity.y; + document.getElementById("gravZ").textContent = event.accelerationIncludingGravity.z; + document.getElementById("gravRot").textContent = event.rotationRate; + }, +} + +var gModAcou = { + activate: function() {}, + deactivate: function() {}, +} + +var gModNull = { + activate: function() {}, + deactivate: function() {}, } -- 2.43.0