From ba05c66e092e0810679af33fe97270e6a46da8c6 Mon Sep 17 00:00:00 2001 From: jem-odoo Date: Mon, 2 Sep 2019 07:15:35 +0000 Subject: [PATCH] [IMP] web: formview auto close dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes when using a form view in a modal (e.i.: in a gantt view), we might want to have some button in the footer. We expect clicking on buttons to close the dialog but nothing happens, since the webclient does not know that this is a modal. To do so, we provide the `close='1'` attribute on the button, in form view. This will trigger the event closing the modal. This is done with the benediction of aab-odoo Task-2024216 closes odoo/odoo#36493 Signed-off-by: Jérome Maes (jem) --- addons/web/static/src/js/core/dialog.js | 8 ++++++++ addons/web/static/src/js/views/form/form_controller.js | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/core/dialog.js b/addons/web/static/src/js/core/dialog.js index 047df634ba2..cd76ec770bb 100644 --- a/addons/web/static/src/js/core/dialog.js +++ b/addons/web/static/src/js/core/dialog.js @@ -21,6 +21,7 @@ var Dialog = Widget.extend({ xmlDependencies: ['/web/static/src/xml/dialog.xml'], custom_events: _.extend({}, Widget.prototype.custom_events, { focus_control_button: '_onFocusControlButton', + close_dialog: '_onCloseDialog', }), events: _.extend({}, Widget.prototype.events, { 'keydown .modal-footer button': '_onFooterButtonKeyDown', @@ -316,6 +317,13 @@ var Dialog = Widget.extend({ //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- + /** + * @private + */ + _onCloseDialog: function (ev) { + ev.stopPropagation(); + this.close(); + }, /** * Moves the focus to the first button primary in the footer of the dialog * 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 e6c50848c4a..3ecd38cfe45 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -577,7 +577,14 @@ var FormController = BasicController.extend({ return; } - def.then(this._enableButtons.bind(this)).guardedCatch(this._enableButtons.bind(this)); + // Kind of hack for FormViewDialog: button on footer should trigger the dialog closing + // if the `close` attribute is set + def.then(function () { + self._enableButtons(); + if (attrs.close) { + self.trigger_up('close_dialog'); + } + }).guardedCatch(this._enableButtons.bind(this)); }, /** * Called when the user wants to create a new record -> @see createRecord