diff options
| author | Joseph Caburnay <jcb@odoo.com> | 2020-05-05 07:47:10 +0000 |
|---|---|---|
| committer | Pierre Masereel <pim@odoo.com> | 2020-05-12 12:48:58 +0000 |
| commit | c6397ab3b3654da336a4269a81d5d91016baf520 (patch) | |
| tree | 31ce3a609f2b16076240953f142dbdf63b7c16af /addons/point_of_sale/static/src/js/barcode_reader.js | |
| parent | cbd63605702cb7fdc7c85132f96e7d04b0e559a7 (diff) | |
[REF] point_of_sale: migration to OWL framework
Diffstat (limited to 'addons/point_of_sale/static/src/js/barcode_reader.js')
| -rw-r--r-- | addons/point_of_sale/static/src/js/barcode_reader.js | 93 |
1 files changed, 59 insertions, 34 deletions
diff --git a/addons/point_of_sale/static/src/js/barcode_reader.js b/addons/point_of_sale/static/src/js/barcode_reader.js index c332591b715..52e5c649942 100644 --- a/addons/point_of_sale/static/src/js/barcode_reader.js +++ b/addons/point_of_sale/static/src/js/barcode_reader.js @@ -16,7 +16,8 @@ var BarcodeReader = core.Class.extend({ init: function (attributes) { this.pos = attributes.pos; - this.action_callback = {}; + this.action_callbacks = {}; + this.exclusive_callbacks = {}; this.proxy = attributes.proxy; this.remote_scanning = false; this.remote_active = 0; @@ -34,21 +35,6 @@ var BarcodeReader = core.Class.extend({ this.barcode_parser = barcode_parser; }, - save_callbacks: function () { - var callbacks = {}; - for (var name in this.action_callback) { - callbacks[name] = this.action_callback[name]; - } - this.action_callback_stack.push(callbacks); - }, - - restore_callbacks: function () { - if (this.action_callback_stack.length) { - var callbacks = this.action_callback_stack.pop(); - this.action_callback = callbacks; - } - }, - // when a barcode is scanned and parsed, the callback corresponding // to its type is called with the parsed_barcode as a parameter. // (parsed_barcode is the result of parse_barcode(barcode)) @@ -59,36 +45,75 @@ var BarcodeReader = core.Class.extend({ // // possible actions include : // 'product' | 'cashier' | 'client' | 'discount' - set_action_callback: function (action, callback) { - if (arguments.length == 2) { - this.action_callback[action] = callback; + set_action_callback: function (name, callback) { + if (this.action_callbacks[name]) { + this.action_callbacks[name].add(callback); } else { - var actions = arguments[0]; - for (var action in actions) { - this.set_action_callback(action,actions[action]); + this.action_callbacks[name] = new Set([callback]); + } + }, + + remove_action_callback: function(name, callback) { + if (!callback) { + delete this.action_callbacks[name]; + return; + } + const callbacks = this.action_callbacks[name]; + if (callbacks) { + callbacks.delete(callback); + if (callbacks.size === 0) { + delete this.action_callbacks[name]; } } }, - //remove all action callbacks - reset_action_callbacks: function () { - for (var action in this.action_callback) { - this.action_callback[action] = undefined; + /** + * Allow setting of exclusive callbacks. If there are exclusive callbacks, + * these callbacks are called neglecting the regular callbacks. This is + * useful for rendered Components that wants to take exclusive access + * to the barcode reader. + * + * @param {String} name + * @param {Function} callback function that takes parsed barcode + */ + set_exclusive_callback: function (name, callback) { + if (this.exclusive_callbacks[name]) { + this.exclusive_callbacks[name].add(callback); + } else { + this.exclusive_callbacks[name] = new Set([callback]); } }, - scan: function (code) { - if (!code) { + remove_exclusive_callback: function (name, callback) { + if (!callback) { + delete this.exclusive_callbacks[name]; return; } - var parsed_result = this.barcode_parser.parse_barcode(code); - if (this.action_callback[parsed_result.type]) { - this.action_callback[parsed_result.type](parsed_result); - } else if (this.action_callback.error) { - this.action_callback.error(parsed_result); + const callbacks = this.exclusive_callbacks[name]; + if (callbacks) { + callbacks.delete(callback); + if (callbacks.size === 0) { + delete this.exclusive_callbacks[name]; + } + } + }, + + scan: function (code) { + if (!code) return; + + const callbacks = Object.keys(this.exclusive_callbacks).length + ? this.exclusive_callbacks + : this.action_callbacks; + + const parsed_result = this.barcode_parser.parse_barcode(code); + if (callbacks[parsed_result.type]) { + [...callbacks[parsed_result.type]].map((cb) => cb(parsed_result)); + } else if (callbacks.error) { + [...callbacks.error].map((cb) => cb(parsed_result)); } else { - console.warn("Ignored Barcode Scan:", parsed_result); + console.warn('Ignored Barcode Scan:', parsed_result); } + }, // the barcode scanner will listen on the hw_proxy/scanner interface for |
