From 802dde31f3b58b8f26d6f6c584e5e4f8f392a482 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 25 Aug 2017 14:29:11 +0200 Subject: [PATCH] [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. --- .../static/src/js/barcode_form_view.js | 25 +++++++++++++++++++ addons/barcodes/static/tests/barcode_tests.js | 8 +++++- addons/web/static/src/js/chrome/pager.js | 8 +++++- 3 files changed, 39 insertions(+), 2 deletions(-) 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 */ diff --git a/addons/barcodes/static/tests/barcode_tests.js b/addons/barcodes/static/tests/barcode_tests.js index 5f8af60911b..add55ed4d5d 100644 --- a/addons/barcodes/static/tests/barcode_tests.js +++ b/addons/barcodes/static/tests/barcode_tests.js @@ -128,7 +128,7 @@ QUnit.test('edit, save and cancel buttons', function (assert) { }); QUnit.test('pager buttons', function (assert) { - assert.expect(3); + assert.expect(5); var form = createView({ View: FormView, @@ -149,6 +149,12 @@ QUnit.test('pager buttons', function (assert) { // O-CMD.PAGER-PREV _.each(["O","-","C","M","D",".","P","A","G","E","R","-","P","R","E","V","Enter"], triggerKeypressEvent); assert.strictEqual(form.$('.o_field_widget').text(), 'iPad Mini'); + // O-CMD.PAGER-LAST + _.each(["O","-","C","M","D",".","P","A","G","E","R","-","L","A","S","T","Enter"], triggerKeypressEvent); + assert.strictEqual(form.$('.o_field_widget').text(), 'Mouse, Optical'); + // O-CMD.PAGER-FIRST + _.each(["O","-","C","M","D",".","P","A","G","E","R","-","F","I","R","S","T","Enter"], triggerKeypressEvent); + assert.strictEqual(form.$('.o_field_widget').text(), 'iPad Mini'); form.destroy(); }); diff --git a/addons/web/static/src/js/chrome/pager.js b/addons/web/static/src/js/chrome/pager.js index bbe69c6d86a..c7f22a6f2c9 100644 --- a/addons/web/static/src/js/chrome/pager.js +++ b/addons/web/static/src/js/chrome/pager.js @@ -97,10 +97,16 @@ var Pager = Widget.extend({ /** * Sets the state of the pager and renders it * @param {Object} [state] the values to update (size, current_min and limit) + * @param {Object} [options] + * @param {boolean} [options.notifyChange] set to true to make the pager + * notify the environment that its state changed */ - updateState: function (state) { + updateState: function (state, options) { _.extend(this.state, state); this._render(); + if (options && options.notifyChange) { + this.trigger('pager_changed', _.clone(this.state)); + } }, //--------------------------------------------------------------------------