Files
owl/web/static/src/ts/root.ts
T

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-01-29 10:00:33 +01:00
import { Widget } from "./core/widget";
import { Navbar } from "./widgets/navbar";
import { ActionWidget } from "./services/action_manager";
2019-01-29 15:19:02 +01:00
import { INotification } from "./services/notifications";
import { Notification } from "./widgets/notification";
2019-01-29 10:00:33 +01:00
import { Env } from "./env";
const template = `
<div class="o_web_client">
<t t-widget="Navbar"/>
2019-01-29 16:13:05 +01:00
<div class="o_content" t-ref="content"/>
2019-01-29 15:19:02 +01:00
<div class="o_notification_container">
<t t-foreach="state.notifications" t-as="notif">
<t t-widget="Notification" t-props="notif"/>
</t>
</div>
</div>
`;
2019-01-29 14:41:12 +01:00
export class Root extends Widget<Env, {}> {
name = "root";
template = template;
2019-01-29 15:19:02 +01:00
widgets = { Navbar, Notification };
2019-01-28 21:32:21 +01:00
content: Widget<Env, {}> | null = null;
2019-01-29 15:19:02 +01:00
state: { notifications: INotification[] } = { notifications: [] };
mounted() {
this.env.actionManager.on("action_ready", this, this.setContentWidget);
2019-01-29 16:13:05 +01:00
this.env.notifications.on("notification_added", this, this.addNotif);
this.env.notifications.on("notification_removed", this, this.removeNotif);
const actionWidget = this.env.actionManager.getCurrentAction();
if (actionWidget) {
this.setContentWidget(actionWidget);
}
}
async setContentWidget(actionWidget: ActionWidget) {
const currentWidget = this.content;
const newWidget = new actionWidget.Widget(this, actionWidget.props);
await newWidget.mount(<HTMLElement>this.refs.content);
if (currentWidget) {
currentWidget.destroy();
}
this.content = newWidget;
}
2019-01-29 15:19:02 +01:00
2019-01-29 16:13:05 +01:00
addNotif(notif: INotification) {
2019-01-29 15:19:02 +01:00
const notifications = this.state.notifications.concat(notif);
this.updateState({ notifications });
}
2019-01-29 16:13:05 +01:00
removeNotif(notif: INotification) {
const notifs = this.state.notifications.filter(f => f.id !== notif.id);
this.updateState({ notifications: notifs });
}
}