82 lines
2.8 KiB
JavaScript
82 lines
2.8 KiB
JavaScript
/** @odoo-module **/
|
|
|
|
import { browser } from "@web/core/browser/browser";
|
|
import { Dialog } from "@web/core/dialog/dialog";
|
|
import { _lt } from "@web/core/l10n/translation";
|
|
import { useChildRef, useOwnedDialogs, useService } from "@web/core/utils/hooks";
|
|
import { sprintf } from "@web/core/utils/strings";
|
|
import { isMobileOS } from "@web/core/browser/feature_detection";
|
|
import * as BarcodeScanner from "@web/webclient/barcode/barcode_scanner";
|
|
|
|
const {xml, Component} = owl;
|
|
import { standardFieldProps } from "@web/views/fields/standard_field_props";
|
|
// Import the registry
|
|
import {registry} from "@web/core/registry";
|
|
|
|
|
|
export class CodeField extends Component {
|
|
setup() {
|
|
super.setup();
|
|
}
|
|
async onBarcodeBtnClick() {
|
|
const barcode = await BarcodeScanner.scanBarcode();
|
|
if (barcode) {
|
|
await this.onBarcodeScanned(barcode);
|
|
if ("vibrate" in browser.navigator) {
|
|
browser.navigator.vibrate(100);
|
|
}
|
|
} else {
|
|
this.notification.add(this.env._t("Please, scan again !"), {
|
|
type: "warning",
|
|
});
|
|
}
|
|
}
|
|
async search(barcode) {
|
|
const results = await this.orm.call("sf.tray", "name_search", [code], {
|
|
name: barcode,
|
|
args: this.getDomain(),
|
|
operator: "ilike",
|
|
limit: 2, // If one result we set directly and if more than one we use normal flow so no need to search more
|
|
context: this.context,
|
|
});
|
|
return results.map((result) => {
|
|
const [id, displayName] = result;
|
|
return {
|
|
id,
|
|
name: displayName,
|
|
};
|
|
});
|
|
}
|
|
async onBarcodeScanned(barcode) {
|
|
const results = await this.search(barcode);
|
|
const records = results.filter((r) => !!r.id);
|
|
if (records.length === 1) {
|
|
this.update([{ id: records[0].id, name: records[0].name }]);
|
|
} else {
|
|
const searchInput = this.autocompleteContainerRef.el.querySelector("input");
|
|
searchInput.value = barcode;
|
|
searchInput.dispatchEvent(new Event("input"));
|
|
if (this.env.isSmall) {
|
|
searchInput.click();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
CodeField.template = xml`
|
|
<button
|
|
t-on-click="onBarcodeBtnClick"
|
|
type="button"
|
|
class="btn ms-3 o_barcode"
|
|
tabindex="-1"
|
|
draggable="false"
|
|
aria-label="Scan barcode"
|
|
title="Scan barcode"
|
|
data-tooltip="Scan barcode"
|
|
/>
|
|
`;
|
|
// CodeField.template = 'sf_machine_connect.CodeField';
|
|
CodeField.props = standardFieldProps;
|
|
|
|
// Add the field to the correct category
|
|
registry.category("fields").add("code", CodeField); |