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 434e2095a8e..e3085b1379d 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -596,9 +596,12 @@ var FormController = BasicController.extend({ * @private * @param {OdooEvent} event */ - _onFormDialogDiscarded: function(e) { - e.stopPropagation(); - this.renderer.focusLastActivatedWidget(); + _onFormDialogDiscarded: function(ev) { + ev.stopPropagation(); + var isFocused = this.renderer.focusLastActivatedWidget(); + if (ev.data.callback) { + ev.data.callback(_.str.toBool(isFocused)); + } }, /** * Opens a one2many record (potentially new) in a dialog. This handler is 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 2bf3c29197c..518791a6be9 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -176,8 +176,11 @@ var FormRenderer = BasicRenderer.extend({ * field. */ focusLastActivatedWidget: function () { - this._activateNextFieldWidget(this.state, this.lastActivatedFieldIndex - 1, - { noAutomaticCreate: true }); + if (this.lastActivatedFieldIndex !== -1) { + return this._activateNextFieldWidget(this.state, this.lastActivatedFieldIndex - 1, + { noAutomaticCreate: true }); + } + return false; }, /** * returns the active tab pages for each notebook diff --git a/addons/web/static/src/js/views/view_dialogs.js b/addons/web/static/src/js/views/view_dialogs.js index 3df17bbf155..c7360c81d02 100644 --- a/addons/web/static/src/js/views/view_dialogs.js +++ b/addons/web/static/src/js/views/view_dialogs.js @@ -238,8 +238,13 @@ var FormViewDialog = ViewDialog.extend({ * @override */ _focusOnClose: function() { - this.trigger_up('form_dialog_discarded'); - return true; + var isFocusSet = false; + this.trigger_up('form_dialog_discarded', { + callback: function (isFocused) { + isFocusSet = isFocused; + }, + }); + return isFocusSet; }, /** @@ -466,8 +471,13 @@ var SelectCreateDialog = ViewDialog.extend({ * @override */ _focusOnClose: function() { - this.trigger_up('form_dialog_discarded'); - return true; + var isFocusSet = false; + this.trigger_up('form_dialog_discarded', { + callback: function (isFocused) { + isFocusSet = isFocused; + }, + }); + return isFocusSet; }, //-------------------------------------------------------------------------- // Handlers diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 52fc313e452..459c51df441 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -2980,8 +2980,8 @@ QUnit.module('relational_fields', { // Open many2one modal form.$buttons.find('.btn.o_form_button_edit').click(); form.$('.o_external_button').click(); - var $originalModal = $('.modal-dialog'); - var $focusedModal = $(document.activeElement).closest('.modal-dialog'); + var $originalModal = $('.modal'); + var $focusedModal = $(document.activeElement).closest('.modal'); assert.equal($originalModal.length, 1, 'There should be one modal'); assert.equal($originalModal[0], $focusedModal[0], 'Modal is focused'); @@ -2989,8 +2989,8 @@ QUnit.module('relational_fields', { // Open many2one modal of field in many2one modal $originalModal.find('.o_external_button').click(); - var $modals = $('.modal-dialog'); - $focusedModal = $(document.activeElement).closest('.modal-dialog'); + var $modals = $('.modal'); + $focusedModal = $(document.activeElement).closest('.modal'); assert.equal($modals.length, 2, 'There should be two modals'); assert.equal($modals[1], $focusedModal[0], 'Last modal is focused'); @@ -2998,8 +2998,8 @@ QUnit.module('relational_fields', { // Close second modal $modals.last().find('button[class="close"]').click(); - var $modal = $('.modal-dialog'); - $focusedModal = $(document.activeElement).closest('.modal-dialog'); + var $modal = $('.modal'); + $focusedModal = $(document.activeElement).closest('.modal'); assert.equal($modal.length, 1, 'There should be one modal'); assert.equal($modal[0], $originalModal[0], 'First modal is still opened'); diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index 31b0e333887..eee1093624f 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -4823,6 +4823,70 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.module('focus and scroll test', { + after: function () { + var content = document.getElementsByClassName('o_content')[0]; + content.style = null; + } + }, function(){ + QUnit.test('no focus set on form when closing many2one modal if lastActivatedFieldIndex is not set', function (assert) { + assert.expect(8); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '', + res_id: 2, + archs: { + 'partner,false,list': '', + 'partner_type,false,list': '', + 'partner,false,form': '
', + 'product,false,list': '', + }, + mockRPC: function (route, args) { + if (args.method === 'get_formview_id') { + return $.when(false); + } + return this._super(route, args); + }, + }); + + // set max-height to have scroll forcefully so that we can test scroll position after modal close + $('.o_content').css({'overflow': 'auto', 'max-height': '300px'}); + // Open many2one modal, lastActivatedFieldIndex will not set as we directly click on external button + testUtils.form.clickEdit(form); + assert.strictEqual($(".o_content").scrollTop(), 0, "scroll position should be 0"); + + form.$(".o_field_many2one[name='trululu'] .o_input").focus(); + assert.notStrictEqual($(".o_content").scrollTop(), 0, "scroll position should not be 0"); + + testUtils.dom.click(form.$('.o_external_button')); + // Close modal + testUtils.dom.click($('.modal').last().find('button[class="close"]')); + assert.notStrictEqual($(".o_content").scrollTop(), 0, + "scroll position should not be 0 after closing modal"); + assert.containsNone(document.body, '.modal', 'There should be no modal'); + assert.doesNotHaveClass($('body'), 'modal-open', 'Modal is not said opened'); + assert.strictEqual(form.renderer.lastActivatedFieldIndex, -1, + "lastActivatedFieldIndex is -1"); + assert.equal(document.activeElement, $('body')[0], + 'body is focused, should not set focus on form widget'); + assert.notStrictEqual(document.activeElement, form.$('.o_field_many2one[name="trululu"] .o_input'), + 'field widget should not be focused when lastActivatedFieldIndex is -1'); + + form.destroy(); + }); + }); + QUnit.test('in create mode, autofocus fields are focused', function (assert) { assert.expect(1);