2020-09-16 18:38:50 +02:00
|
|
|
import {HtmlUtils} from "./HtmlUtils";
|
|
|
|
import {Connection, GlobalMessageInterface} from "../Connection";
|
|
|
|
|
|
|
|
export class GlobalMessageManager {
|
|
|
|
|
|
|
|
private Connection: Connection;
|
|
|
|
|
|
|
|
constructor(Connection: Connection) {
|
|
|
|
this.Connection = Connection;
|
|
|
|
this.initialise();
|
|
|
|
}
|
|
|
|
|
|
|
|
initialise(){
|
|
|
|
//receive signal to show message
|
|
|
|
this.Connection.receivePlayGlobalMessage((message: GlobalMessageInterface) => {
|
|
|
|
this.playMessage(message.id, message.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
//receive signal to close message
|
|
|
|
this.Connection.receiveStopGlobalMessage((message: GlobalMessageInterface) => {
|
|
|
|
this.stopMessage(message.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:50:04 +02:00
|
|
|
private playMessage(messageId : number, htmlMessage: string){
|
2020-09-16 18:38:50 +02:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = htmlMessage;
|
|
|
|
div.id = this.getHtmlMessageId(messageId);
|
|
|
|
div.className = "message-container";
|
|
|
|
|
|
|
|
const mainSectionDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>('main-container');
|
|
|
|
mainSectionDiv.appendChild(div);
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:50:04 +02:00
|
|
|
private stopMessage(messageId: number){
|
2020-09-16 18:38:50 +02:00
|
|
|
HtmlUtils.removeElementByIdOrFail<HTMLDivElement>(this.getHtmlMessageId(messageId));
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:50:04 +02:00
|
|
|
private getHtmlMessageId(messageId: number) : string{
|
2020-09-16 18:38:50 +02:00
|
|
|
return `message-${messageId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|