diff --git a/addons/barcodes/static/src/components/barcode_scanner.js b/addons/barcodes/static/src/components/barcode_scanner.js index ae18eedcba0..acebb585da4 100644 --- a/addons/barcodes/static/src/components/barcode_scanner.js +++ b/addons/barcodes/static/src/components/barcode_scanner.js @@ -18,7 +18,7 @@ export class BarcodeScanner extends Component { let error = null; let barcode = null; try { - barcode = await scanBarcode(this.facingMode); + barcode = await scanBarcode(this.env, this.facingMode); } catch (err) { error = err.error.message; } diff --git a/addons/web/static/src/views/fields/many2one/many2one_field.js b/addons/web/static/src/views/fields/many2one/many2one_field.js index c720ac13c04..ebb6829f0ba 100644 --- a/addons/web/static/src/views/fields/many2one/many2one_field.js +++ b/addons/web/static/src/views/fields/many2one/many2one_field.js @@ -262,7 +262,7 @@ export class Many2OneField extends Component { } } async onBarcodeBtnClick() { - const barcode = await BarcodeScanner.scanBarcode(); + const barcode = await BarcodeScanner.scanBarcode(this.env); if (barcode) { await this.onBarcodeScanned(barcode); if ("vibrate" in browser.navigator) { diff --git a/addons/web/static/src/webclient/barcode/barcode_scanner.js b/addons/web/static/src/webclient/barcode/barcode_scanner.js index 105b16b206e..efe6dba4d65 100644 --- a/addons/web/static/src/webclient/barcode/barcode_scanner.js +++ b/addons/web/static/src/webclient/barcode/barcode_scanner.js @@ -2,26 +2,14 @@ /* global BarcodeDetector */ import { browser } from "@web/core/browser/browser"; -import Dialog from "@web/legacy/js/core/owl_dialog"; +import { Dialog } from "@web/core/dialog/dialog"; import { delay } from "@web/legacy/js/core/concurrency"; -import { loadJS, templates } from "@web/core/assets"; +import { loadJS } from "@web/core/assets"; import { isVideoElementReady, buildZXingBarcodeDetector } from "./ZXingBarcodeDetector"; import { CropOverlay } from "./crop_overlay"; -import { - App, - Component, - EventBus, - onMounted, - onWillStart, - onWillUnmount, - useRef, - useState, -} from "@odoo/owl"; +import { Component, onMounted, onWillStart, onWillUnmount, useRef, useState } from "@odoo/owl"; import { _t } from "@web/core/l10n/translation"; -const bus = new EventBus(); -const busOk = "BarcodeDialog-Ok"; -const busError = "BarcodeDialog-Error"; export class BarcodeDialog extends Component { /** @@ -127,8 +115,8 @@ export class BarcodeDialog extends Component { * @param {string} result found code */ onResult(result) { - this.props.onClose(); - bus.trigger(busOk, result); + this.props.close(); + this.props.onResult(result); } /** @@ -137,8 +125,8 @@ export class BarcodeDialog extends Component { * @param {Error} error */ onError(error) { - this.props.onClose(); - bus.trigger(busError, { error }); + this.props.close(); + this.props.onError(error); } /** @@ -201,22 +189,17 @@ export function isBarcodeScannerSupported() { * * @returns {Promise} resolves when a {qr,bar}code has been detected */ -export async function scanBarcode(facingMode = "environment") { +export async function scanBarcode(env, facingMode = "environment") { + let res; + let rej; const promise = new Promise((resolve, reject) => { - bus.on(busOk, null, resolve); - bus.on(busError, null, reject); + res = resolve; + rej = reject; }); - const appForBarcodeDialog = new App(BarcodeDialog, { - env: owl.Component.env, - dev: owl.Component.env.isDebug(), - templates, - translatableAttributes: ["data-tooltip"], - translateFn: _t, - props: { - onClose: () => appForBarcodeDialog.destroy(), - facingMode: facingMode, - }, + env.services.dialog.add(BarcodeDialog, { + facingMode, + onResult: (result) => res(result), + onError: (error) => rej(error), }); - await appForBarcodeDialog.mount(document.body); return promise; } diff --git a/addons/web/static/src/webclient/barcode/barcode_scanner.xml b/addons/web/static/src/webclient/barcode/barcode_scanner.xml index 49ef8666d54..90aa19ab85e 100644 --- a/addons/web/static/src/webclient/barcode/barcode_scanner.xml +++ b/addons/web/static/src/webclient/barcode/barcode_scanner.xml @@ -1,7 +1,7 @@ - + diff --git a/addons/web/static/tests/webclient/barcode/barcode_scanner_tests.js b/addons/web/static/tests/webclient/barcode/barcode_scanner_tests.js index 2bf838e1dc7..0a428616118 100644 --- a/addons/web/static/tests/webclient/barcode/barcode_scanner_tests.js +++ b/addons/web/static/tests/webclient/barcode/barcode_scanner_tests.js @@ -8,12 +8,24 @@ import { patchWithCleanup, triggerEvent, } from "@web/../tests/helpers/utils"; +import { registry } from "@web/core/registry"; +import { uiService } from "@web/core/ui/ui_service"; +import { hotkeyService } from "@web/core/hotkeys/hotkey_service"; import { scanBarcode, BarcodeDialog } from "@web/webclient/barcode/barcode_scanner"; +import { createWebClient } from "@web/../tests/webclient/helpers"; +import { dialogService } from "@web/core/dialog/dialog_service"; +import { overlayService } from "@web/core/overlay/overlay_service"; QUnit.module("Barcode scanner", {}); QUnit.test("Barcode scanner crop overlay", async (assert) => { + registry.category("services").add("ui", uiService); + registry.category("services").add("hotkey", hotkeyService); + registry.category("services").add("dialog", dialogService); + registry.category("services").add("overlay", overlayService); + + const { env } = await createWebClient({}); const firstBarcodeValue = "Odoo"; const secondBarcodeValue = "O-CMD-TEST"; @@ -70,7 +82,7 @@ QUnit.test("Barcode scanner crop overlay", async (assert) => { }, }); - const firstBarcodeFound = scanBarcode(); + const firstBarcodeFound = scanBarcode(env); await videoReady; // Needed due to the change on the props in the Crop component await nextTick(); @@ -113,7 +125,7 @@ QUnit.test("Barcode scanner crop overlay", async (assert) => { barcodeToGenerate = secondBarcodeValue; videoReady = makeDeferred(); - const secondBarcodeFound = scanBarcode(); + const secondBarcodeFound = scanBarcode(env); await videoReady; const secondValueScanned = await secondBarcodeFound; assert.strictEqual(