From f52b8e11bb28569b6224591d68460dc1092fb998 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 11 Feb 2022 18:18:17 +0100 Subject: [PATCH] Support camera barcode scanning in recipepicker (references #1562) --- changelog/66_3.2.0_2022-02-11.md | 1 + config-dist.php | 4 +--- public/viewjs/components/barcodescanner.js | 15 ++++++++++++++- public/viewjs/components/productpicker.js | 2 +- public/viewjs/components/recipepicker.js | 22 ++++++++++++++++++++++ public/viewjs/mealplan.js | 10 ++++++++++ views/components/barcodescanner.blade.php | 2 +- views/components/recipepicker.blade.php | 5 ++++- 8 files changed, 54 insertions(+), 7 deletions(-) diff --git a/changelog/66_3.2.0_2022-02-11.md b/changelog/66_3.2.0_2022-02-11.md index 4fd07ee7..ff7500f1 100644 --- a/changelog/66_3.2.0_2022-02-11.md +++ b/changelog/66_3.2.0_2022-02-11.md @@ -64,6 +64,7 @@ - New input shorthand `[+/-]n[d/m/y]` for date fields to quickly input a date relative to today (adding (**+**) or subtracting (**-**) the **n**umber of **d**ays/**m**onths/**y**ears, see the full list of available shorthands [here](https://github.com/grocy/grocy#input-shorthands-for-date-fields)) - When using LDAP authentication, the configured `LDAP_UID_ATTR` is now used to compare if the user already exists instead of the username entered on the login page (that prevents creating multiple users if you enter the username in different notations) (thanks @FloSet) - When using reverse proxy authentication (`ReverseProxyAuthMiddleware`), it's now also possible to pass the username in an environment variable instead of an HTTP header (new `config.php` option `REVERSE_PROXY_AUTH_USE_ENV`) (thanks @Forceu) +- The `config.php` option `DISABLE_BROWSER_BARCODE_CAMERA_SCANNING` has been renamed to `FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING` - Fixed that when having a quantity unit matching any application string, the translation of that string was used to display that unit - Fixed that the logout button/menu was missing when using external authentication (e.g. LDAP) - New translations: (thanks all the translators) diff --git a/config-dist.php b/config-dist.php index 201e1ca5..90abe86f 100644 --- a/config-dist.php +++ b/config-dist.php @@ -90,9 +90,6 @@ Setting('LDAP_BIND_PW', ''); // Password for the above account Setting('LDAP_USER_FILTER', ''); // Example value "(OU=grocy_users)" Setting('LDAP_UID_ATTR', ''); // Windows AD: "sAMAccountName", OpenLDAP: "uid", GLAuth: "cn" -// Set this to true if you want to disable the ability to scan a barcode via the device camera (Browser API) -Setting('DISABLE_BROWSER_BARCODE_CAMERA_SCANNING', false); - // Default permissions for new users // the array needs to contain the technical/constant names // See the file controllers/Users/User.php for possible values @@ -147,6 +144,7 @@ Setting('FEATURE_FLAG_CHORES_ASSIGNMENTS', true); Setting('FEATURE_FLAG_THERMAL_PRINTER', false); // Feature settings +Setting('FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING', false); // Set this to true if you want to disable the ability to scan a barcode via the device camera (Browser API) Setting('FEATURE_FLAG_AUTO_TORCH_ON_WITH_CAMERA', true); // Enables the torch automatically (if the device has one) diff --git a/public/viewjs/components/barcodescanner.js b/public/viewjs/components/barcodescanner.js index fe209a29..293a4197 100644 --- a/public/viewjs/components/barcodescanner.js +++ b/public/viewjs/components/barcodescanner.js @@ -279,8 +279,14 @@ $(document).on("click", "#barcodescanner-start-button", async function(e) Grocy.Components.BarcodeScanner.StartScanning(); }); -setTimeout(function() +Grocy.Components.BarcodeScanner.InitDone = false; +Grocy.Components.BarcodeScanner.Init = function() { + if (Grocy.Components.BarcodeScanner.InitDone) + { + return; + } + $(".barcodescanner-input:visible").each(function() { if ($(this).hasAttr("disabled")) @@ -291,5 +297,12 @@ setTimeout(function() { $(this).after(''); } + + Grocy.Components.BarcodeScanner.InitDone = true; }); +} + +setTimeout(function() +{ + Grocy.Components.BarcodeScanner.Init(); }, 50); diff --git a/public/viewjs/components/productpicker.js b/public/viewjs/components/productpicker.js index 65260d75..a757625b 100644 --- a/public/viewjs/components/productpicker.js +++ b/public/viewjs/components/productpicker.js @@ -239,7 +239,7 @@ $('#product_id_text_input').on('blur', function(e) } }; - if (!Grocy.FeatureFlags.DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) + if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) { buttons.retrycamerascanning = { label: 'C ', diff --git a/public/viewjs/components/recipepicker.js b/public/viewjs/components/recipepicker.js index e7c198c0..d2ab085c 100644 --- a/public/viewjs/components/recipepicker.js +++ b/public/viewjs/components/recipepicker.js @@ -101,3 +101,25 @@ $('#recipe_id_text_input').on('blur', function(e) } } }); + +$(document).on("Grocy.BarcodeScanned", function(e, barcode, target) +{ + if (!(target == "@recipepicker" || target == "undefined" || target == undefined)) // Default target + { + return; + } + + // Don't know why the blur event does not fire immediately ... this works... + Grocy.Components.RecipePicker.GetInputElement().focusout(); + Grocy.Components.RecipePicker.GetInputElement().focus(); + Grocy.Components.RecipePicker.GetInputElement().blur(); + + Grocy.Components.RecipePicker.GetInputElement().val(barcode); + + setTimeout(function() + { + Grocy.Components.RecipePicker.GetInputElement().focusout(); + Grocy.Components.RecipePicker.GetInputElement().focus(); + Grocy.Components.RecipePicker.GetInputElement().blur(); + }, 200); +}); diff --git a/public/viewjs/mealplan.js b/public/viewjs/mealplan.js index e482e215..1d8e67c5 100644 --- a/public/viewjs/mealplan.js +++ b/public/viewjs/mealplan.js @@ -449,6 +449,11 @@ $(document).on("click", ".copy-day-button", function(e) $("#add-recipe-modal").on("shown.bs.modal", function(e) { + if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) + { + Grocy.Components.BarcodeScanner.Init(); + } + Grocy.Components.RecipePicker.GetInputElement().focus(); }) @@ -459,6 +464,11 @@ $("#add-note-modal").on("shown.bs.modal", function(e) $("#add-product-modal").on("shown.bs.modal", function(e) { + if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) + { + Grocy.Components.BarcodeScanner.Init(); + } + Grocy.Components.ProductPicker.GetInputElement().focus(); }) diff --git a/views/components/barcodescanner.blade.php b/views/components/barcodescanner.blade.php index 2bcf671f..9f28729a 100644 --- a/views/components/barcodescanner.blade.php +++ b/views/components/barcodescanner.blade.php @@ -1,4 +1,4 @@ -@if (!GROCY_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) +@if (!GROCY_FEATURE_FLAG_DISABLE_BROWSER_BARCODE_CAMERA_SCANNING) @once @push('componentScripts') diff --git a/views/components/recipepicker.blade.php b/views/components/recipepicker.blade.php index 419c0bb1..21bf624d 100644 --- a/views/components/recipepicker.blade.php +++ b/views/components/recipepicker.blade.php @@ -24,9 +24,10 @@ @endif -
{{ $__t('You have to select a recipe') }}
+ +@include('components.barcodescanner')