TeaWeb/shared/js/ui/frames/SideBarController.ts

153 lines
5.6 KiB
TypeScript
Raw Normal View History

import {ConnectionHandler} from "../../ConnectionHandler";
import {ChannelConversationController} from "./side/ChannelConversationController";
import {PrivateConversationController} from "./side/PrivateConversationController";
import {ClientInfoController} from "tc-shared/ui/frames/side/ClientInfoController";
import {SideHeaderController} from "tc-shared/ui/frames/side/HeaderController";
import * as ReactDOM from "react-dom";
import {SideBarRenderer} from "tc-shared/ui/frames/SideBarRenderer";
import * as React from "react";
import {SideBarEvents, SideBarType} from "tc-shared/ui/frames/SideBarDefinitions";
import {Registry} from "tc-shared/events";
import {LogCategory, logWarn} from "tc-shared/log";
2020-03-30 13:44:18 +02:00
export class SideBarController {
private readonly uiEvents: Registry<SideBarEvents>;
2020-03-30 13:44:18 +02:00
private currentConnection: ConnectionHandler;
private listenerConnection: (() => void)[];
2020-03-30 13:44:18 +02:00
private header: SideHeaderController;
private clientInfo: ClientInfoController;
private channelConversations: ChannelConversationController;
private privateConversations: PrivateConversationController;
2020-03-30 13:44:18 +02:00
constructor() {
this.listenerConnection = [];
2020-03-30 13:44:18 +02:00
this.uiEvents = new Registry<SideBarEvents>();
this.uiEvents.on("query_content", () => this.sendContent());
this.uiEvents.on("query_content_data", event => this.sendContentData(event.content));
this.privateConversations = new PrivateConversationController();
this.channelConversations = new ChannelConversationController();
this.clientInfo = new ClientInfoController();
this.header = new SideHeaderController();
}
2020-03-30 13:44:18 +02:00
setConnection(connection: ConnectionHandler) {
if(this.currentConnection === connection) {
return;
}
this.listenerConnection.forEach(callback => callback());
this.listenerConnection = [];
this.currentConnection = connection;
this.header.setConnectionHandler(connection);
this.clientInfo.setConnectionHandler(connection);
this.channelConversations.setConnectionHandler(connection);
this.privateConversations.setConnectionHandler(connection);
if(connection) {
this.listenerConnection.push(connection.getSideBar().events.on("notify_content_type_changed", () => this.sendContent()));
}
this.sendContent();
}
2019-08-21 10:00:01 +02:00
2020-03-30 13:44:18 +02:00
destroy() {
this.header?.destroy();
this.header = undefined;
this.clientInfo?.destroy();
2020-12-07 15:07:47 +01:00
this.clientInfo = undefined;
this.privateConversations?.destroy();
this.privateConversations = undefined;
this.channelConversations?.destroy();
this.channelConversations = undefined;
}
2019-09-12 23:59:35 +02:00
renderInto(container: HTMLDivElement) {
ReactDOM.render(React.createElement(SideBarRenderer, {
events: this.uiEvents,
eventsHeader: this.header["uiEvents"],
}), container);
2020-03-30 13:44:18 +02:00
}
2019-09-12 23:59:35 +02:00
private sendContent() {
if(this.currentConnection) {
this.uiEvents.fire("notify_content", { content: this.currentConnection.getSideBar().getSideBarContent() });
} else {
this.uiEvents.fire("notify_content", { content: "none" });
}
2020-03-30 13:44:18 +02:00
}
private sendContentData(content: SideBarType) {
switch (content) {
case "none":
this.uiEvents.fire_react("notify_content_data", {
content: "none",
data: {}
});
break;
case "channel-chat":
if(!this.currentConnection) {
logWarn(LogCategory.GENERAL, tr("Received channel chat content data request without an active connection."));
return;
}
this.uiEvents.fire_react("notify_content_data", {
content: "channel-chat",
data: {
events: this.channelConversations["uiEvents"],
handlerId: this.currentConnection.handlerId
}
});
break;
case "private-chat":
if(!this.currentConnection) {
logWarn(LogCategory.GENERAL, tr("Received private chat content data request without an active connection."));
return;
}
this.uiEvents.fire_react("notify_content_data", {
content: "private-chat",
data: {
events: this.privateConversations["uiEvents"],
handlerId: this.currentConnection.handlerId
}
});
break;
case "client-info":
if(!this.currentConnection) {
logWarn(LogCategory.GENERAL, tr("Received client info content data request without an active connection."));
return;
}
this.uiEvents.fire_react("notify_content_data", {
content: "client-info",
data: {
events: this.clientInfo["uiEvents"],
}
});
break;
case "music-manage":
if(!this.currentConnection) {
logWarn(LogCategory.GENERAL, tr("Received music bot content data request without an active connection."));
return;
}
this.uiEvents.fire_react("notify_content_data", {
content: "music-manage",
data: { }
});
break;
}
}
}