From f2c7e44efe4e2bcdc9c77c66dbd56b8b4cc397b2 Mon Sep 17 00:00:00 2001 From: Khoi Nguyen Date: Thu, 13 Jul 2017 17:40:35 +0200 Subject: [PATCH] [FIX] web: no M2ODialog when pressing enter in a many2one Before this revision, pressing ENTER when inside a many2one field triggers a 'focus out' event. In particular, if one creates a many2one record and uses the ENTER key to click "Create and Edit", two dialog windows appear: one to actually create the associated record, and a second to alert the user that they are creating an associated record. The latter appears every time the many2one field loses focus. This commit changes the 'key up' event for many2one fields to ensure that the ENTER key does not trigger a 'focus out' event. --- .../static/src/js/fields/relational_fields.js | 10 +++- .../tests/fields/relational_fields_tests.js | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 822955fcf3c..14512c02912 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -485,8 +485,16 @@ var FieldMany2One = AbstractField.extend({ }, /** * @private + * + * @param {OdooEvent} ev */ - _onInputKeyup: function () { + _onInputKeyup: function (ev) { + if (ev.which === $.ui.keyCode.ENTER) { + // If we pressed enter, we want to prevent _onInputFocusout from + // executing since it would open a M2O dialog to request + // confirmation that the many2one is not properly set. + return; + } if (this.$input.val() === "") { this.reinitialize(false); } else if (this._getDisplayName(this.m2o_value) !== this.$input.val()) { diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 171b3e1f787..aacbb744252 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -1141,6 +1141,52 @@ QUnit.module('relational_fields', { }); }); + QUnit.test('pressing ENTER on a \'no_quick_create\' many2one should not trigger M2ODialog', function (assert) { + var done = assert.async(); + assert.expect(1); + + var M2O_DELAY = relationalFields.FieldMany2One.prototype.AUTOCOMPLETE_DELAY; + relationalFields.FieldMany2One.prototype.AUTOCOMPLETE_DELAY = 0; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '', + archs: { + 'partner,false,form': '
', + }, + }); + + var $input = form.$('.o_field_many2one input'); + $input.val("Something that does not exist").trigger('input'); + $('.ui-autocomplete .ui-menu-item a:contains(Create and)').trigger('mouseenter'); + concurrency.delay(0).then(function() { + $input.trigger($.Event('keydown', { + which: $.ui.keyCode.ENTER, + keyCode: $.ui.keyCode.ENTER, + })); + $input.trigger($.Event('keypress', { + which: $.ui.keyCode.ENTER, + keyCode: $.ui.keyCode.ENTER, + })); + $input.trigger($.Event('keyup', { + which: $.ui.keyCode.ENTER, + keyCode: $.ui.keyCode.ENTER, + })); + concurrency.delay(0).then(function() { + $input.blur(); + assert.strictEqual($('.modal').length, 1, + "should have one modal in body"); + form.destroy(); + done(); + }); + }); + }); + QUnit.test('many2one in editable list + onchange, with enter [REQUIRE FOCUS]', function (assert) { assert.expect(6); var done = assert.async();