[IMP] web: do not set focus on form when lastActivatedFieldIndex is not set

When opening a dialog using mouse and closing it, form_dialog_discarded is
triggered, which tries to set focus on form while lastActivatedFieldIndex
is -1, as dialog is opened using mouse directly

do not set focus back to form widget if lastActivatedFieldIndex is -1

task-2031706

closes odoo/odoo#34684

Signed-off-by: VincentSchippefilt <VincentSchippefilt@users.noreply.github.com>
This commit is contained in:
Mohammed Shekha
2019-07-22 10:23:29 +00:00
committed by Vincent Schippefilt
parent 324d3fc93a
commit 0ac4055504
5 changed files with 95 additions and 15 deletions
@@ -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
@@ -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
+14 -4
View File
@@ -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
@@ -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');
@@ -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: '<form string="Partners">' +
'<field name="display_name"/>' +
'<field name="foo"/>' +
'<field name="bar"/>' +
'<field name="p"/>' +
'<field name="timmy"/>' +
'<field name="product_ids"/>' +
'<field name="trululu"/>' +
'</form>',
res_id: 2,
archs: {
'partner,false,list': '<tree><field name="display_name"/></tree>',
'partner_type,false,list': '<tree><field name="name"/></tree>',
'partner,false,form': '<form><field name="trululu"/></form>',
'product,false,list': '<tree><field name="name"/></tree>',
},
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);