Files
odoo_source/addons/barcodes/static/src/js/barcode_handler_mixin.js
T
Nicolas Martinelli 0d19ee989d [FIX] barcodes: manage several barcode widgets
In some cases, several barcode widgets can be found on the same page.
This is for example the case of the stock picking: there is a widget on
the `stock.picking` model (main view) itself, and a widget on the
`stock.pack.operation` model (modal view).

In this case, when the focus is not set directly on the field which
needs to be filled in, both widgets will trigger a `barcode_scanned`
scanned event. This will call the `on_barcode_scanned` Python method
which can lead to errors/warning. For example, the `on_barcode_scanned`
method on the `stock.picking` model raises a warning in case the barcode
scanned does not match any product/package.

The fix is to make sure that the event target is located in the correct
DOM element.

opw-677506
2016-06-15 15:11:41 +02:00

53 lines
1.9 KiB
JavaScript

odoo.define('barcodes.BarcodeHandlerMixin', function(require) {
"use strict";
var core = require('web.core');
var View = require('web.View');
// Mixin implementing the common basis for barcode handlers.
// The object on which this mixin is applied must also include ParentedMixin. Example :
// Widget.extend(BarcodeHandlerMixin, { ... });
// Class.extend(PropertiesMixin, BarcodeHandlerMixin, { ... });
return {
init: function() {
var self = this;
this._super.apply(this, arguments);
this.__on_barcode_scanned = function (barcode, target) {
// Handle the case where there are several barcode widgets on the same page. Since the
// event is global on the page, all barcode widgets will be triggered. However, we only
// want to keep the event on the target widget.
if ($.contains(target, self.el)) {
self.on_barcode_scanned.call(self, barcode);
}
};
this.start_listening();
// Handlers inside a View managed by a ViewManager only listen to barcode events while their view is displayed
var view = this.findAncestor(function(ancestor) { return ancestor instanceof View });
if (view) {
view.on('attached', this, this.start_listening);
view.on('detached', this, this.stop_listening);
}
},
start_listening: function() {
if (! this.is_listening) {
core.bus.on('barcode_scanned', this, this.__on_barcode_scanned);
this.is_listening = true;
}
},
stop_listening: function() {
if (this.is_listening) {
core.bus.off('barcode_scanned', this, this.__on_barcode_scanned);
this.is_listening = false;
}
},
on_barcode_scanned: function(barcode) {
console.error('A class implementing BarcodeHandlerMixin must redefine method on_barcode_scanned.');
},
};
});