improve notification system

This commit is contained in:
Géry Debongnie
2019-01-29 16:13:05 +01:00
parent d029f87a0f
commit 0bbeabd55c
6 changed files with 62 additions and 11 deletions
+9
View File
@@ -68,6 +68,15 @@ body {
margin: 5px 0 0 0; margin: 5px 0 0 0;
background-color: $o-notification-info-bg-color; background-color: $o-notification-info-bg-color;
box-shadow: 0px 0px 5px 1px $o-main-text-color; box-shadow: 0px 0px 5px 1px $o-main-text-color;
position: relative;
.o_close {
position: absolute;
top: 5px;
right: 5px;
color: rgba(0, 0, 0, 0.3);
text-decoration: none;
}
.o_notification_title { .o_notification_title {
display: flex; display: flex;
+9 -3
View File
@@ -8,7 +8,7 @@ import { Env } from "./env";
const template = ` const template = `
<div class="o_web_client"> <div class="o_web_client">
<t t-widget="Navbar"/> <t t-widget="Navbar"/>
<div class="o_content" t-ref="content"></div> <div class="o_content" t-ref="content"/>
<div class="o_notification_container"> <div class="o_notification_container">
<t t-foreach="state.notifications" t-as="notif"> <t t-foreach="state.notifications" t-as="notif">
<t t-widget="Notification" t-props="notif"/> <t t-widget="Notification" t-props="notif"/>
@@ -27,7 +27,9 @@ export class Root extends Widget<Env, {}> {
mounted() { mounted() {
this.env.actionManager.on("action_ready", this, this.setContentWidget); this.env.actionManager.on("action_ready", this, this.setContentWidget);
this.env.notifications.on("notification_added", this, this.addNotification); this.env.notifications.on("notification_added", this, this.addNotif);
this.env.notifications.on("notification_removed", this, this.removeNotif);
const actionWidget = this.env.actionManager.getCurrentAction(); const actionWidget = this.env.actionManager.getCurrentAction();
if (actionWidget) { if (actionWidget) {
this.setContentWidget(actionWidget); this.setContentWidget(actionWidget);
@@ -44,8 +46,12 @@ export class Root extends Widget<Env, {}> {
this.content = newWidget; this.content = newWidget;
} }
addNotification(notif: INotification) { addNotif(notif: INotification) {
const notifications = this.state.notifications.concat(notif); const notifications = this.state.notifications.concat(notif);
this.updateState({ notifications }); this.updateState({ notifications });
} }
removeNotif(notif: INotification) {
const notifs = this.state.notifications.filter(f => f.id !== notif.id);
this.updateState({ notifications: notifs });
}
} }
+5 -2
View File
@@ -12,7 +12,7 @@ export interface INotification {
sticky: boolean; sticky: boolean;
} }
export type NotificationEvent = "notification_added" | "notification_closed"; export type NotificationEvent = "notification_added" | "notification_removed";
export type Callback = (notif: INotification) => void; export type Callback = (notif: INotification) => void;
@@ -40,13 +40,16 @@ export class NotificationManager extends Bus implements INotificationManager {
const notification = Object.assign(defaultVals, notif, { id }); const notification = Object.assign(defaultVals, notif, { id });
this.notifications[id] = notification; this.notifications[id] = notification;
this.trigger("notification_added", notification); this.trigger("notification_added", notification);
if (!notification.sticky) {
setTimeout(() => this.close(id), 2500);
}
return id; return id;
} }
close(id: number) { close(id: number) {
let notification = this.notifications[id]; let notification = this.notifications[id];
if (notification) { if (notification) {
delete this.notifications[id]; delete this.notifications[id];
this.trigger("notification_closed", notification); this.trigger("notification_removed", notification);
} }
} }
} }
+4 -3
View File
@@ -20,7 +20,8 @@ const template = `
<t t-widget="Clock"/> <t t-widget="Clock"/>
</t> </t>
<t t-widget="ColorWidget" t-props="{color: state.color}"/> <t t-widget="ColorWidget" t-props="{color: state.color}"/>
<button t-on-click="addNotif">Add Notification</button> <button t-on-click="addNotif(false)">Add notif</button>
<button t-on-click="addNotif(true)">Add sticky notif</button>
</div> </div>
`; `;
@@ -54,10 +55,10 @@ export class Discuss extends Widget<Env, {}> {
this.updateState({ color: newColor }); this.updateState({ color: newColor });
} }
addNotif() { addNotif(sticky: boolean) {
const text = (<any>this.refs.textinput).value; const text = (<any>this.refs.textinput).value;
const message = `It is now ${new Date().toLocaleTimeString()}.<br/> Msg: ${text}`; const message = `It is now ${new Date().toLocaleTimeString()}.<br/> Msg: ${text}`;
this.env.notifications.add({ title: "hey", message: message }); this.env.notifications.add({ title: "hey", message: message, sticky });
} }
} }
+6 -2
View File
@@ -2,7 +2,9 @@ import { QWeb } from "../src/ts/core/qweb_vdom";
import { idGenerator } from "../src/ts/core/utils"; import { idGenerator } from "../src/ts/core/utils";
import { WEnv } from "../src/ts/core/widget"; import { WEnv } from "../src/ts/core/widget";
import { Env } from "../src/ts/env"; import { Env } from "../src/ts/env";
import { IAjax, RPC } from "../src/ts/services/ajax"; import { IAjax, RPCQuery } from "../src/ts/services/ajax";
import { NotificationManager } from "../src/ts/services/notifications";
import { import {
IActionManager, IActionManager,
ActionEvent, ActionEvent,
@@ -28,12 +30,14 @@ export function makeTestEnv(): Env {
const ajax = new MockAjax(); const ajax = new MockAjax();
const actionManager = new MockActionManager(); const actionManager = new MockActionManager();
const router = new MockRouter(); const router = new MockRouter();
const notifications = new NotificationManager();
let { qweb, getID } = makeTestWEnv(); let { qweb, getID } = makeTestWEnv();
return { return {
qweb, qweb,
getID, getID,
ajax, ajax,
actionManager, actionManager,
notifications,
router, router,
rpc: ajax.rpc, rpc: ajax.rpc,
debug: false, debug: false,
@@ -42,7 +46,7 @@ export function makeTestEnv(): Env {
} }
class MockAjax implements IAjax { class MockAjax implements IAjax {
async rpc(rpc: RPC) { async rpc(rpc: RPCQuery) {
return true; return true;
} }
} }
@@ -36,7 +36,7 @@ test("can subscribe and add notification", () => {
test("can close a notification", () => { test("can close a notification", () => {
const notifications: INotificationManager = new NotificationManager(); const notifications: INotificationManager = new NotificationManager();
let notified = false; let notified = false;
notifications.on("notification_closed", {}, () => (notified = true)); notifications.on("notification_removed", {}, () => (notified = true));
const id = notifications.add(makeNotification()); const id = notifications.add(makeNotification());
expect(notified).toBe(false); expect(notified).toBe(false);
@@ -44,3 +44,31 @@ test("can close a notification", () => {
notifications.close(id); notifications.close(id);
expect(notified).toBe(true); expect(notified).toBe(true);
}); });
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
const notifications: INotificationManager = new NotificationManager();
let removed = false;
notifications.on("notification_removed", {}, () => (removed = true));
notifications.add(makeNotification());
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(removed).toBe(false);
jest.runAllTimers();
expect(removed).toBe(true);
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
const notifications: INotificationManager = new NotificationManager();
let removed = false;
notifications.on("notification_removed", {}, () => (removed = true));
notifications.add(makeNotification({ sticky: true }));
expect(setTimeout).toHaveBeenCalledTimes(0);
expect(removed).toBe(false);
jest.runAllTimers();
expect(removed).toBe(false);
});