From 5b968ad37108015805cfebd6b1ab0ea97e595361 Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 2 Aug 2017 17:24:46 +0200 Subject: [PATCH] [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. --- .../static/src/js/barcode_form_view.js | 4 +++ .../src/js/views/form/form_controller.js | 7 +++-- .../static/src/js/views/form/form_renderer.js | 3 --- .../web/static/src/js/views/form/form_view.js | 1 + addons/web/static/tests/views/form_tests.js | 26 +++++++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) 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 diff --git a/addons/web/static/src/js/views/form/form_controller.js b/addons/web/static/src/js/views/form/form_controller.js index 7ddd3d16587..611847ac3f4 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -28,6 +28,7 @@ var FormController = BasicController.extend({ this._super.apply(this, arguments); this.actionButtons = params.actionButtons; + this.disableAutofocus = params.disableAutofocus; this.footerToButtons = params.footerToButtons; this.defaultButtons = params.defaultButtons; this.hasSidebar = params.hasSidebar; @@ -42,7 +43,9 @@ var FormController = BasicController.extend({ * Calls autofocus on the renderer */ autofocus: function () { - this.renderer.autofocus(); + if (!this.disableAutofocus) { + this.renderer.autofocus(); + } }, /** * This method switches the form view in edit mode, with a new record. @@ -305,7 +308,7 @@ var FormController = BasicController.extend({ this.set('title', title); this._updateButtons(); this._updateSidebar(); - return this._super.apply(this, arguments); + return this._super.apply(this, arguments).then(this.autofocus.bind(this)); }, /** * @private diff --git a/addons/web/static/src/js/views/form/form_renderer.js b/addons/web/static/src/js/views/form/form_renderer.js index 999006e54c2..ac53168df6e 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -815,9 +815,6 @@ var FormRenderer = BasicRenderer.extend({ self._addFieldTooltip(widget, $label); } }); - - // Focus the default field (in edit mode) - self.autofocus(); }, /** * Sets id attribute of given widget to idForLabel diff --git a/addons/web/static/src/js/views/form/form_view.js b/addons/web/static/src/js/views/form/form_view.js index f0264b67307..b48a5488848 100644 --- a/addons/web/static/src/js/views/form/form_view.js +++ b/addons/web/static/src/js/views/form/form_view.js @@ -29,6 +29,7 @@ var FormView = BasicView.extend({ this.loadParams.type = 'record'; this.loadParams.parentID = params.parentID; + this.controllerParams.disableAutofocus = params.disable_autofocus; this.controllerParams.hasSidebar = params.sidebar; this.controllerParams.toolbarActions = viewInfo.toolbar; this.controllerParams.footerToButtons = params.footer_to_buttons; diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index 3ceb2046756..15147c22d1d 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -4197,6 +4197,32 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('no autofocus with disable_autofocus option [REQUIRE FOCUS]', function (assert) { + assert.expect(2); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '', + viewOptions: { + disable_autofocus: true, + }, + }); + assert.notStrictEqual(document.activeElement, form.$('input[name="int_field"]')[0], + "int_field field should not have focus"); + + form.update({}); + + assert.notStrictEqual(document.activeElement, form.$('input[name="int_field"]')[0], + "int_field field should not have focus"); + + form.destroy(); + }); + QUnit.test('open one2many form containing many2many_tags', function (assert) { assert.expect(4);