From b131e9b0ef458e8cc00b0a2eb6c1b4d8dfe889a0 Mon Sep 17 00:00:00 2001 From: Akash Bhavsar Date: Mon, 26 Jun 2017 17:24:01 +0530 Subject: [FIX] barcodes: fix several issues when scanning barcodes Since the new views, most of the barcodes feature was broken. This commit re-enables the support of commands like 'edit', 'save', 'cancel', 'previous' and 'next'. Also changed javascript event handler to jquery event handler to make barcodes testable in phantomjs. --- addons/barcodes/static/src/js/barcode_form_view.js | 55 ++++++++++++++++++---- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 69a59fdaafa..37fa34dcf95 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -26,19 +26,21 @@ FormController.include({ this.activeBarcode = { form_view: { commands: { - 'O-CMD.NEW': 'createRecord', - 'O-CMD.EDIT': 'toEditMode', - 'O-CMD.CANCEL': 'discardChange', - 'O-CMD.SAVE': function () { return this.saveRecord({reload: true}); }, - // 'O-CMD.PAGER-PREV': - // 'O-CMD.PAGER-NEXT': - } - } + 'O-CMD.EDIT': this._onEdit.bind(this), + 'O-CMD.CANCEL': this._onDiscard.bind(this), + 'O-CMD.SAVE': this._onCommandSave.bind(this), + 'O-CMD.PAGER-PREV': this._onCommandPrevious.bind(this), + 'O-CMD.PAGER-NEXT': this._onCommandNext.bind(this), + }, + }, }; this.barcodeMutex = new concurrency.Mutex(); this._barcodeStartListening(); }, + /** + * @override + */ destroy: function () { this._barcodeStopListening(); this._super(); @@ -70,6 +72,8 @@ FormController.include({ } }, /** + * Returns true iff the given barcode matches the given record (candidate). + * * @private * @param {Object} candidate: record in the x2m * @param {string} barcode sent by the scanner (string generate from keypress series) @@ -128,7 +132,7 @@ FormController.include({ */ _getBarCodeRecord: function (record, barcode, activeBarcode) { var self = this; - if (!activeBarcode.fieldName) { + if (!activeBarcode.fieldName || !record.data[activeBarcode.fieldName]) { return; } return _.find(record.data[activeBarcode.fieldName].data, function (record) { @@ -237,6 +241,24 @@ FormController.include({ }); }); }, + /** + * @private + */ + _onCommandNext: function () { + return this.mutex.exec(function () {}).then(this.pager.next.bind(this.pager)); + }, + /** + * @private + */ + _onCommandPrevious: function () { + return this.mutex.exec(function () {}).then(this.pager.previous.bind(this.pager)); + }, + /** + * @private + */ + _onCommandSave: function () { + return this.saveRecord(); + }, /** * @private * @param {KeyEvent} event @@ -359,6 +381,21 @@ FormRenderer.include({ } return $button; }, + /** + * Add barcode event handler + * + * @override + * @private + * @param {Object} node + * @returns {jQueryElement} + */ + _renderTagButton: function (node) { + var $button = this._super.apply(this, arguments); + if (node.attrs.barcode_trigger) { + this._barcodeButtonHandler($button, node); + } + return $button; + } }); BarcodeEvents.ReservedBarcodePrefixes.push('O-BTN'); -- cgit v1.2.3 From 5b968ad37108015805cfebd6b1ab0ea97e595361 Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 2 Aug 2017 17:24:46 +0200 Subject: [FIX] web, barcodes: restore `disableAutofocus` option This option has been removed with the new views but was used in barcode. The form renderer was also dealing with some part of the autofocus but this should only be handled by the controller. The logic is now only in the controller and a test case has been added. --- addons/barcodes/static/src/js/barcode_form_view.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 37fa34dcf95..7b82e68405c 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -171,6 +171,10 @@ FormController.include({ candidate: this.activeBarcode[name] && this.activeBarcode[name].handle === this.handle ? this.activeBarcode[name].candidate : null, }; + + // we want to disable autofocus when activating the barcode to avoid + // putting the scanned value in the focused field + this.disableAutofocus = true; }, /** * @private -- cgit v1.2.3 From dec1bdb44d2ee2bf1874f0a5051bdd7720cbb4d7 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 24 Aug 2017 08:40:10 +0200 Subject: [FIX] barcodes: don't redraw form twice When scanning a command barcode, the view shouldn't be redrawn after the operation corresponding to the command is executed, for two reasons: first, it is useless as the view has already been reloaded, and second, it may cause issues if it has been destroyed meanwhile. For example, when scanning barcode O-CMD.MAIN-MENU from a picking form view: a do_action is performed to go back to the main barcode client action (so the current form view is destroyed), but an handler tried to redrawn the destroyed form view, which produced a crash because the session was required by Date fields, and it couldn't be provided (there were no more parent listening to provide it). --- addons/barcodes/static/src/js/barcode_form_view.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 7b82e68405c..08872f8084a 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -240,8 +240,13 @@ FormController.include({ if (prefixed && !hasCommand) { self.do_warn(_t('Error : Barcode command is undefined'), barcode); } - return $.when.apply($, defs).then(function () { - self.update({}, {reload: false}); + return self.alive($.when.apply($, defs)).then(function () { + if (!prefixed) { + // redraw the view if we scanned a real barcode (required if + // we manually apply the change in JS, e.g. incrementing the + // quantity) + self.update({}, {reload: false}); + } }); }); }, -- cgit v1.2.3 From c73233d2489e3d074d4d13d1c782a97734e29e32 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 24 Aug 2017 14:37:13 +0200 Subject: [FIX] barcodes: don't warn if action unavailable Before this rev. a warning was displayed when a barcode mapping an invisible button was scanned. It shouldn't, for the following reason. In the stock.picking form view, two buttons with barcode_trigger='validate' are in the page ('Start Inventory' and 'Validate'), and there is always at least one of them that is invisible. So when scanning the 'validate' barcode, we don't want a warning to be displayed, as there is potentially another button that is able to handle the request. --- addons/barcodes/static/src/js/barcode_form_view.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 08872f8084a..c238df4da59 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -350,8 +350,6 @@ FormRenderer.include({ commands['O-BTN.' + node.attrs.barcode_trigger] = function () { if (!$button.hasClass('o_invisible_modifier')) { $button.click(); - } else { - this.do_warn(_t('Action currently unavailable')); } return $.when(); }; -- cgit v1.2.3 From 8019eacb4716befcc0affa4f8c9d554be2ec6655 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 24 Aug 2017 14:36:53 +0200 Subject: [FIX] barcodes: generate correct update command The barcode mechanism is optimized to (in certain conditions), directly increment the quantity of a numerical field inside an x2many, without performing any RPC, and then re-rendering the x2many to display the new value. Before this rev., it seemed to work fine, but when the view was saved, those changes of quantity were lost. This was because the changes weren't correctly notified to the model (the quantity field being inside an x2many, the correct update command for the x2many should be generated). --- addons/barcodes/static/src/js/barcode_form_view.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index c238df4da59..8f6a3b0a3e2 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -93,8 +93,14 @@ FormController.include({ */ _barcodeSelectedCandidate: function (candidate, record, barcode, activeBarcode) { var changes = {}; - changes[activeBarcode.quantity] = candidate.data[activeBarcode.quantity] + 1; - return this.model.notifyChanges(candidate.id, changes); + var candidateChanges = {}; + candidateChanges[activeBarcode.quantity] = candidate.data[activeBarcode.quantity] + 1; + changes[activeBarcode.fieldName] = { + operation: 'UPDATE', + id: candidate.id, + data: candidateChanges, + }; + return this.model.notifyChanges(this.handle, changes); }, /** * @private -- cgit v1.2.3 From 802dde31f3b58b8f26d6f6c584e5e4f8f392a482 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 25 Aug 2017 14:29:11 +0200 Subject: [FIX] web,barcodes: pager: FIRST and LAST actions The barcode mechanism is supposed to handle commands PAGER-FIRST (go to first record referenced in the pager) and PAGER-LAST (go to last record). Before this rev., it wasn't the case. We needed to add an option 'notifyChange' to the updateState function of the pager, to ensure that the form is correctly reloaded when the pager cursor is manually set (by the barcode handler) on the first or last element. --- addons/barcodes/static/src/js/barcode_form_view.js | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 8f6a3b0a3e2..5b042999bf6 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -31,6 +31,8 @@ FormController.include({ 'O-CMD.SAVE': this._onCommandSave.bind(this), 'O-CMD.PAGER-PREV': this._onCommandPrevious.bind(this), 'O-CMD.PAGER-NEXT': this._onCommandNext.bind(this), + 'O-CMD.PAGER-FIRST': this._onCommandFirst.bind(this), + 'O-CMD.PAGER-LAST': this._onCommandLast.bind(this), }, }, }; @@ -256,6 +258,29 @@ FormController.include({ }); }); }, + /** + * @private + */ + _onCommandFirst: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + self.pager.updateState({ + current_min: 1, + }, {notifyChange: true}); + }); + }, + /** + * @private + */ + _onCommandLast: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + var state = self.model.get(self.handle, {raw: true}); + self.pager.updateState({ + current_min: state.count, + }, {notifyChange: true}); + }); + }, /** * @private */ -- cgit v1.2.3 From 6df482eb99e83dbe3f6b51d950e70d8d7c5e4015 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 25 Aug 2017 16:13:57 +0200 Subject: [FIX] barcodes: barcode handlers should return a deferred This was the case about 15minutes ago. But before pushing we decided to change the Barcode override of FormController to directly use handlers of the FormController when it could (e.g. to handle 'Edit', 'Discard' actions). This wasn't a good idea as those handlers don't return anything, and the barcode mechanism requires its handlers to return a deferred. So basically, it crashed when such a barcode was scanned. Having a test suite is nice, but you should not forget to relaunch it before pushing... --- addons/barcodes/static/src/js/barcode_form_view.js | 109 ++++++++++++--------- 1 file changed, 60 insertions(+), 49 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_form_view.js') diff --git a/addons/barcodes/static/src/js/barcode_form_view.js b/addons/barcodes/static/src/js/barcode_form_view.js index 5b042999bf6..3f1c5ef1a86 100644 --- a/addons/barcodes/static/src/js/barcode_form_view.js +++ b/addons/barcodes/static/src/js/barcode_form_view.js @@ -26,13 +26,13 @@ FormController.include({ this.activeBarcode = { form_view: { commands: { - 'O-CMD.EDIT': this._onEdit.bind(this), - 'O-CMD.CANCEL': this._onDiscard.bind(this), - 'O-CMD.SAVE': this._onCommandSave.bind(this), - 'O-CMD.PAGER-PREV': this._onCommandPrevious.bind(this), - 'O-CMD.PAGER-NEXT': this._onCommandNext.bind(this), - 'O-CMD.PAGER-FIRST': this._onCommandFirst.bind(this), - 'O-CMD.PAGER-LAST': this._onCommandLast.bind(this), + 'O-CMD.EDIT': this._barcodeEdit.bind(this), + 'O-CMD.CANCEL': this._barcodeDiscard.bind(this), + 'O-CMD.SAVE': this._barcodeSave.bind(this), + 'O-CMD.PAGER-PREV': this._barcodePagerPrevious.bind(this), + 'O-CMD.PAGER-NEXT': this._barcodePagerNext.bind(this), + 'O-CMD.PAGER-FIRST': this._barcodePagerFirst.bind(this), + 'O-CMD.PAGER-LAST': this._barcodePagerLast.bind(this), }, }, }; @@ -73,6 +73,53 @@ FormController.include({ return this._barcodeWithoutCandidate(record, barcode, activeBarcode); } }, + /** + * @private + */ + _barcodeDiscard: function () { + return this.discardChanges(); + }, + /** + * @private + */ + _barcodeEdit: function () { + return this._setMode('edit'); + }, + /** + * @private + */ + _barcodePagerFirst: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + self.pager.updateState({ + current_min: 1, + }, {notifyChange: true}); + }); + }, + /** + * @private + */ + _barcodePagerLast: function () { + var self = this; + return this.mutex.exec(function () {}).then(function () { + var state = self.model.get(self.handle, {raw: true}); + self.pager.updateState({ + current_min: state.count, + }, {notifyChange: true}); + }); + }, + /** + * @private + */ + _barcodePagerNext: function () { + return this.mutex.exec(function () {}).then(this.pager.next.bind(this.pager)); + }, + /** + * @private + */ + _barcodePagerPrevious: function () { + return this.mutex.exec(function () {}).then(this.pager.previous.bind(this.pager)); + }, /** * Returns true iff the given barcode matches the given record (candidate). * @@ -85,6 +132,12 @@ FormController.include({ _barcodeRecordFilter: function (candidate, barcode, activeBarcode) { return candidate.data.product_barcode === barcode; }, + /** + * @private + */ + _barcodeSave: function () { + return this.saveRecord(); + }, /** * @private * @param {Object} candidate: record in the x2m @@ -111,7 +164,6 @@ FormController.include({ core.bus.on('barcode_scanned', this, this._barcodeScanned); core.bus.on('keypress', this, this._quantityListener); }, - /** * @private */ @@ -258,47 +310,6 @@ FormController.include({ }); }); }, - /** - * @private - */ - _onCommandFirst: function () { - var self = this; - return this.mutex.exec(function () {}).then(function () { - self.pager.updateState({ - current_min: 1, - }, {notifyChange: true}); - }); - }, - /** - * @private - */ - _onCommandLast: function () { - var self = this; - return this.mutex.exec(function () {}).then(function () { - var state = self.model.get(self.handle, {raw: true}); - self.pager.updateState({ - current_min: state.count, - }, {notifyChange: true}); - }); - }, - /** - * @private - */ - _onCommandNext: function () { - return this.mutex.exec(function () {}).then(this.pager.next.bind(this.pager)); - }, - /** - * @private - */ - _onCommandPrevious: function () { - return this.mutex.exec(function () {}).then(this.pager.previous.bind(this.pager)); - }, - /** - * @private - */ - _onCommandSave: function () { - return this.saveRecord(); - }, /** * @private * @param {KeyEvent} event -- cgit v1.2.3