import {MessageProvider} from '../MessageProvider'; import {MessageCreationOptions} from '../MessageCreationOptions'; export class TestMessage { id: string; type: string; title: string; body: string; options: MessageCreationOptions; closeHandlers: Array; constructor(type: string, title: string, body: string, options: MessageCreationOptions) { this.type = type; this.title = title; this.body = body; this.options = options; this.id = 'test-message-' + (new Date()).getMilliseconds(); this.closeHandlers = new Array(); } getId(): string { return this.id; } registerCloseHandler(closeHandler: any): void { this.closeHandlers.push(closeHandler); } callbackCloseHandlers() { for (let closeHandler of this.closeHandlers) { closeHandler(); } } } /** * An implementation of MessageProvider. */ export class MessageProviderImpl implements MessageProvider { messageIdsToMessages: Map = new Map(); constructor() { } generic(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('generic', title, body, options); } hint(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('hint', title, body, options); } info(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('info', title, body, options); } success(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('success', title, body, options); } warning(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('warning', title, body, options); } error(title: string, body: string, options: MessageCreationOptions): void { this.buildMessage('error', title, body, options); } clear(id: string): void { const message = this.messageIdsToMessages.get(id); if (message) { this.messageIdsToMessages.delete(id); message.callbackCloseHandlers(); } } onClose(id: string, callback: any): void { const message = this.messageIdsToMessages.get(id); if (message) { message.registerCloseHandler(callback); } } buildMessage(type: string, title: string, body: string, options: MessageCreationOptions): string { const message = new TestMessage(type, title, body, options); const id = options.id; this.messageIdsToMessages.set(id, message); return id; } }