make switch actually look good and work correctly
[tricorder.git] / js / tricorder.js
index 3ab7ee948fe0f41b15b6723efffcf05cff3403f8..92885dae4b231310554f4ee36c8795553ba6ac6c 100644 (file)
@@ -40,6 +40,10 @@ window.onresize = function() {
 }
 
 function updateStardate() {
+  // Stardate rules foggy at best. See http://en.wikipedia.org/wiki/Stardate
+  // and the Memory Alpha article linked there for more details.
+  // We roughly lean on TNG scale by splitting an Earth year into exactly 1000
+  // units, but we put the 0 point at the TV premiere of The Original Series.
   if (!gStardate)
     gStardate = document.getElementById("stardate");
 
@@ -319,18 +323,16 @@ var gModEnv = {
       }
     }, 5000);
     try {
-      for (var cameraId of window.navigator.mozCameras.getListOfCameras()) {
-        window.navigator.mozCameras.getCamera({camera: cameraId}, function(aCamera) {
+      var cameras = navigator.mozCameras.getListOfCameras();
+      for (i = 0; i < cameras.length; i++) {
+        var promise = navigator.mozCameras.getCamera(cameras[i], {},
+          function(aCamera) {
             if (aCamera.capabilities.flashModes.indexOf('torch') !== -1) {
-              this.flashCamera = aCamera;
+              gModEnv.foundFlashCamera(aCamera);
             }
-        });
-      }
-      if (this.flashCamera) {
-        document.getElementById("envFlashOn").onclick = gModEnv.switchFlashlight(true);
-        document.getElementById("envFlashOff").onclick = gModEnv.switchFlashlight(false);
-        document.getElementById("envFlashUnavail").style.display = "none";
-        document.getElementById("envFlashAvail").style.display = "block";
+          },
+          function(aError) { console.log("camera error: " + aError); }
+        );
       }
     } catch (e) {
       // camera api not supported
@@ -338,6 +340,13 @@ var gModEnv = {
       document.getElementById("envFlashAvail").style.display = "none";
     }
   },
+  foundFlashCamera(aCamera) {
+    this.flashCamera = aCamera;
+    document.getElementById("envFlashOn").onclick = function() { console.log("on"); gModEnv.switchFlashlight(true); };
+    document.getElementById("envFlashOff").onclick = function() { console.log("off"); gModEnv.switchFlashlight(false); };
+    document.getElementById("envFlashUnavail").style.display = "none";
+    document.getElementById("envFlashAvail").style.display = "block";
+  },
   deactivate: function() {
     gSounds.scan.pause();
     window.removeEventListener("devicelight", this.lightEvent, true);
@@ -367,6 +376,8 @@ var gModEnv = {
   switchFlashlight: function(aEnabled) {
     if (this.flashCamera) {
       this.flashCamera.flashMode = aEnabled ? 'torch' : 'off';
+      document.getElementById("envFlashOn").disabled = aEnabled;
+      document.getElementById("envFlashOff").disabled = !aEnabled;
     }
   }
 }