From 8962f2b17cc2075611a41846ae4932ca03bdc68f Mon Sep 17 00:00:00 2001
From: juanf <juanf@ethz.ch>
Date: Tue, 9 May 2023 12:38:32 +0200
Subject: [PATCH] SSDM-12100 : Improved barcode reading from the menu

---
 .../html/js/controllers/MainController.js     |  2 -
 .../eln-lims/html/js/util/BarcodeUtil.js      | 83 +++++++++++++++----
 .../js/views/SideMenu/SideMenuWidgetView.js   |  2 +-
 3 files changed, 66 insertions(+), 21 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
index 78714cef595..d988b90acbb 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/MainController.js
@@ -252,8 +252,6 @@ function MainController(profile) {
                                                         // Keep Alive
                                                         localReference.serverFacade.scheduleKeepAlive();
 
-                                                        // Barcode reading
-                                                        BarcodeUtil.enableAutomaticBarcodeReading();
                                                     });
                                                 };
                                                 
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
index 48f205f4ff3..811f6446d25 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
@@ -30,31 +30,79 @@ var BarcodeUtil = new function() {
         }
     }
 
+    this.readBarcodeFromScannerOrCamera = function($container) {
+        if(!$container) {
+            mainController.changeView("showBlancPage");
+            var content = mainController.currentView.content;
+            content.empty();
+            $container = content;
+        }
+
+		var $form = $("<div>");
+
+        var $toggleSwitch = $("<fieldset>");
+            $toggleSwitch.append($("<legend>").text("Device"));
+
+
+
+        var $device = $("<div>", { class : "switch-toggle switch-candy-blue" });
+        var $scannerInput = $("<input>", { id : "scanner", name : "device", type : "radio", checked: "checked" });
+        $device.append($scannerInput);
+        $device.append($("<label>", { for : "scanner", onclick : "" }).append("Scanner"));
+        var $cameraInput = $("<input>", { id : "camera", name : "device", type : "radio" });
+        $device.append($cameraInput);
+        $device.append($("<label>", { for : "camera",  onclick : "" }).append("Camera"));
+        $device.append($("<a>"));
+
+        $toggleSwitch.append($device);
+
+        $form.append($("<legend>").text("Read Barcode: "));
+
+        var $cameraContainer = $("<div>");
+
+        $form.append($toggleSwitch);
+        $form.append($cameraContainer);
+
+        var onDeviceChange = function() {
+            var isScanner = $scannerInput.is(":checked");
+            if(isScanner) {
+                _this.enableAutomaticBarcodeReading();
+                _this.disableBarcodeReadingFromCamera();
+                $cameraContainer.empty();
+            }
+            var isCamera = $cameraInput.is(":checked");
+            if(isCamera) {
+                _this.disableAutomaticBarcodeReading();
+                _this.enableBarcodeReadingFromCamera($cameraContainer);
+            }
+        }
+
+        $cameraInput.change(onDeviceChange);
+        $scannerInput.change(onDeviceChange);
+
+        $container.append($form);
+
+        onDeviceChange(); // Enable default device
+    }
+
     var codeReader = null;
 
-    this.readBarcodeFromCamera = function() {
+    this.disableBarcodeReadingFromCamera = function() {
         if(codeReader != null) {
             codeReader.reset();
             codeReader = null;
         }
+    }
+
+    this.enableBarcodeReadingFromCamera = function($container) {
+        _this.disableBarcodeReadingFromCamera();
 
         // Steals the main controller to show the video feed and a cancel button
-        mainController.changeView("showBlancPage");
-        var content = mainController.currentView.content;
-        content.empty();
-        content.append(FormUtil.getButtonWithIcon("glyphicon-minus-sign", function() {
-            if(codeReader != null) {
-                codeReader.reset();
-                codeReader = null;
-            }
-            // On cancel go back to previews view
-            mainController.backButtonLogic();
-            mainController.backButtonLogic();
-        }));
+        var content = $container;
         var $videoCameraSelection = $("<select>", { id: "videoCameraSelect", style : "margin-left: 4px;"});
         content.append($videoCameraSelection);
-        content.append($("<legend>").text("Read Barcode:"));
-        var $video = $("<video>", { id : "video", width : "100%", height : "100%" });
+        content.append($("<legend>").append("Camera: ").append($videoCameraSelection));
+        var $video = $("<video>", { id : "video", width : "50%", height : "50%", style : "display: block; margin: 0 auto;" });
         content.append($video);
 
         // Starts the camera reading code
@@ -73,9 +121,8 @@ var BarcodeUtil = new function() {
                         }
                         if (err && !(err instanceof ZXing.NotFoundException)) {
                             Util.showError("Failed to read barcode");
-                            // On cancel go back to previews view
-                            mainController.backButtonLogic();
-                            mainController.backButtonLogic();
+                            _this.disableBarcodeReadingFromCamera();
+                            $container.empty();
                         }
                     };
 
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
index 5b9446efdad..567b27e90c4 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SideMenu/SideMenuWidgetView.js
@@ -152,7 +152,7 @@ function SideMenuWidgetView(sideMenuWidgetController, sideMenuWidgetModel) {
         var barcodeReaderBtn = FormUtil.getButtonWithIcon(
             "glyphicon-barcode",
             function() {
-                BarcodeUtil.readBarcodeFromCamera();
+                BarcodeUtil.readBarcodeFromScannerOrCamera();
             },
             null,
             null,
-- 
GitLab