mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
add notification service
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
export interface RPC {
|
export interface RPCQuery {
|
||||||
model: string;
|
model: string;
|
||||||
method: string;
|
method: string;
|
||||||
args: any;
|
args: any;
|
||||||
}
|
}
|
||||||
export interface IAjax {
|
export interface IAjax {
|
||||||
rpc(rpc: RPC): Promise<any>;
|
rpc(rpc: RPCQuery): Promise<any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Ajax implements IAjax {
|
export class Ajax implements IAjax {
|
||||||
rpc(rpc: RPC): Promise<any> {
|
rpc(rpc: RPCQuery): Promise<any> {
|
||||||
return Promise.resolve(1);
|
return Promise.resolve(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { EventBus as Bus } from "../core/event_bus";
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Types
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
export interface Notification {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
message: string;
|
||||||
|
type: "notification" | "warning";
|
||||||
|
sticky: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type NotificationEvent = "notification_added" | "notification_closed";
|
||||||
|
|
||||||
|
export type Callback = (notif: Notification) => void;
|
||||||
|
|
||||||
|
export interface INotificationManager {
|
||||||
|
add(notif: Partial<Notification>): number;
|
||||||
|
close(id: number): void;
|
||||||
|
on(event: NotificationEvent, owner: any, callback: Callback): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Notification Manager
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
export class NotificationManager extends Bus implements INotificationManager {
|
||||||
|
nextID = 0;
|
||||||
|
notifications: { [key: number]: Notification } = {};
|
||||||
|
|
||||||
|
add(notif: Partial<Notification>): number {
|
||||||
|
const id = this.nextID++;
|
||||||
|
const defaultVals = {
|
||||||
|
title: "",
|
||||||
|
message: "",
|
||||||
|
type: "notification",
|
||||||
|
sticky: false
|
||||||
|
};
|
||||||
|
const notification = Object.assign(defaultVals, notif, { id });
|
||||||
|
this.notifications[id] = notification;
|
||||||
|
this.trigger("notification_added", notification);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
close(id: number) {
|
||||||
|
let notification = this.notifications[id];
|
||||||
|
if (notification) {
|
||||||
|
delete this.notifications[id];
|
||||||
|
this.trigger("notification_closed", notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import {
|
||||||
|
NotificationManager,
|
||||||
|
Notification,
|
||||||
|
INotificationManager
|
||||||
|
} from "../../src/ts/services/notifications";
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Setup and helpers
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function makeNotification(notif: Partial<Notification> = {}): Notification {
|
||||||
|
const defaultNotif = {
|
||||||
|
id: 1,
|
||||||
|
title: "title",
|
||||||
|
message: "message",
|
||||||
|
type: "notification",
|
||||||
|
sticky: false
|
||||||
|
};
|
||||||
|
return Object.assign(defaultNotif, notif);
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Tests
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
test("can subscribe and add notification", () => {
|
||||||
|
let notified = false;
|
||||||
|
const notifications = new NotificationManager();
|
||||||
|
notifications.on("notification_added", {}, () => (notified = true));
|
||||||
|
expect(notified).toBe(false);
|
||||||
|
const id = notifications.add(makeNotification());
|
||||||
|
expect(notified).toBe(true);
|
||||||
|
expect(id).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("can close a notification", () => {
|
||||||
|
const notifications: INotificationManager = new NotificationManager();
|
||||||
|
let notified = false;
|
||||||
|
notifications.on("notification_closed", {}, () => (notified = true));
|
||||||
|
|
||||||
|
const id = notifications.add(makeNotification());
|
||||||
|
expect(notified).toBe(false);
|
||||||
|
|
||||||
|
notifications.close(id);
|
||||||
|
expect(notified).toBe(true);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user