[REF] web: small refactoring of dialog service

- move dialog state handling in dialog service
- remove use of __destroy calls in dialog container

closes odoo-dev/odoo#964

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Géry Debongnie
2021-06-18 21:31:34 +02:00
committed by Aaron Bohy
parent 8e384a7569
commit 8d8201a8e0
3 changed files with 20 additions and 40 deletions
@@ -1,6 +1,5 @@
/** @odoo-modules */
import session from "web.session";
import { registry } from "@web/core/registry";
import { errorService } from "@web/core/errors/error_service";
import { dialogService } from "@web/core/dialog/dialog_service";
@@ -70,17 +69,11 @@ QUnit.module("base_automation", {}, function () {
exceptionName: errorContext.exception_class,
});
patchWithCleanup(DialogContainer.prototype, {
patchWithCleanup(BaseAutomationErrorDialog.prototype, {
setup() {
this.props.bus.on("ADD", this, ({ class: dialogClass, props }) => {
assert.equal(props.data.context, errorContext, "Received the correct error context");
});
assert.equal(this.props.data.context, errorContext, "Received the correct error context");
this._super();
},
mounted() {
this._super();
this.el.classList.add("o_dialog_container");
}
});
const env = await makeTestEnv();
@@ -1,8 +1,5 @@
/** @odoo-module **/
import { registry } from "../registry";
import { useService } from "../service_hook";
const { Component, tags } = owl;
class ErrorHandler extends Component {
@@ -15,39 +12,24 @@ ErrorHandler.template = tags.xml`<t t-component="props.dialog.class" t-props="pr
export class DialogContainer extends Component {
setup() {
this.dialogs = {};
this.props.bus.on("ADD", this, this.add);
this.props.bus.on("REMOVE", this, this.remove);
}
__destroy() {
this.props.bus.off("ADD", this);
this.props.bus.off("REMOVE", this);
super.__destroy();
this.props.bus.on("UPDATE", this, this.render);
}
add(params) {
this.dialogs[params.id] = params;
this.render();
}
remove(id) {
if (this.dialogs[id]) {
delete this.dialogs[id];
this.render();
close(id) {
if (this.props.dialogs[id]) {
this.props.dialogs[id].props.close();
}
}
onDialogClosed(id) {
this.remove(id);
}
errorCallBack(id) {
return () => this.remove(id);
return () => this.close(id);
}
}
DialogContainer.components = { ErrorHandler };
DialogContainer.template = tags.xml`
<div class="o_dialog_container" t-att-class="{'modal-open': Object.keys(dialogs).length > 0}">
<t t-foreach="Object.values(dialogs)" t-as="dialog" t-key="dialog.id">
<ErrorHandler dialog="dialog" t-on-dialog-closed="onDialogClosed(dialog.id)" callback="errorCallBack(dialog.id)"
<div class="o_dialog_container" t-att-class="{'modal-open': Object.keys(props.dialogs).length > 0}">
<t t-foreach="Object.values(props.dialogs)" t-as="dialog" t-key="dialog.id">
<ErrorHandler dialog="dialog" t-on-dialog-closed="dialog.props.close()" callback="errorCallBack(dialog.id)"
t-att-class="{o_inactive_modal: !dialog_last}"/>
</t>
</div>
@@ -25,12 +25,13 @@ const { EventBus } = core;
export const dialogService = {
/** @returns {DialogServiceInterface} */
start() {
const dialogs = {};
const bus = new EventBus();
let dialogId = 0;
registry.category("main_components").add("DialogContainer", {
Component: DialogContainer,
props: { bus },
props: { bus, dialogs },
});
function add(dialogClass, props, options = {}) {
@@ -40,10 +41,13 @@ export const dialogService = {
const id = ++dialogId;
function close() {
if (options.onClose) {
options.onClose();
if (dialogs[id]) {
delete dialogs[id];
if (options.onClose) {
options.onClose();
}
bus.trigger("UPDATE");
}
bus.trigger("REMOVE", id);
}
const dialog = {
@@ -51,7 +55,8 @@ export const dialogService = {
class: dialogClass,
props: { ...props, close },
};
bus.trigger("ADD", dialog);
dialogs[id] = dialog;
bus.trigger("UPDATE");
return close;
}