cordis/client/webapp/elements.ts
2021-10-30 12:26:41 -05:00

209 lines
9.8 KiB
TypeScript

// Creates elements for use in the DOM
import { Channel, ConnectionInfo, IDummyTextMessage, Member, Message, ShouldNeverHappenError } from "./data-types";
import IState from "./elements/require/elements-state";
import ClientController from "./client-controller";
import createImageContextMenu from "./elements/context-menu-image";
import createConnectionContextMenu from "./elements/context-menu-connection";
import createServerListServer from "./elements/server-list-server";
import createChannel from "./elements/channel";
import createMember from "./elements/member";
import createTextMessage from "./elements/message-text";
import createTextMessageContinued from "./elements/message-text-continued";
import createImageResourceMessage from "./elements/message-image-resource";
import createImageResourceMessageContinued from "./elements/message-image-resource-continued";
import createResourceMessage from "./elements/message-resource";
import createResourceMessageContinued from "./elements/message-resource-continued";
import createMessage from "./elements/message";
import createImageOverlay from "./elements/overlay-image";
import createUploadOverlayFromPath from "./elements/overlay-upload-path";
import createUploadOverlayFromDataTransferItem from "./elements/overlay-upload-datatransfer";
import createUploadDropTarget from "./elements/overlay-upload-drop-target";
import createPersonalizeOverlay from "./elements/overlay-personalize";
import createAddServerOverlay, { IAddServerData } from "./elements/overlay-add-server";
import createErrorMessageOverlay from "./elements/overlay-error-message";
import createErrorIndicator, { CreateErrorIndicatorProps } from "./elements/error-indicator";
import createServerContextMenu from "./elements/context-menu-server";
import createModifyChannelOverlay from "./elements/overlay-modify-channel";
import createServerTitleContextMenu from "./elements/context-menu-server-title";
import createServerSettingsOverlay from "./elements/overlay-server-settings";
import createCreateChannelOverlay from "./elements/overlay-create-channel";
import createCreateInviteTokenOverlay from "./elements/overlay-create-invite-token";
import createTokenLogOverlay from "./elements/overlay-token-log";
import bindWindowButtonEvents from "./elements/events-window-buttons";
import bindTextInputEvents from "./elements/events-text-input";
import bindInfiniteScrollEvents from "./elements/events-infinite-scroll";
import bindConnectionEvents from "./elements/events-connection";
import bindAddServerTitleEvents from "./elements/events-server-title";
import bindAddServerEvents from "./elements/events-add-server";
let state: IState | null = null;
// TODO: move to using state instead of state.document for all of these.
export default class Elements {
static init({ window, document, controller, ui, actions }) {
if (!(window instanceof Window)) {
throw new Error('window is not a Window: ' + document);
}
if (!(document instanceof Document)) {
throw new Error('document is not a Document: ' + document);
}
state = {
window: window,
document: document,
controller: controller,
ui: ui,
actions: actions
};
}
static createImageContextMenu(resourceName: string, server: ClientController, buffer: Buffer, mime: string, ext: string, isPreview: boolean): HTMLElement {
if (!state) throw new ShouldNeverHappenError('state is null');
return createImageContextMenu(state, server, resourceName, buffer, mime, ext, isPreview);
}
static createConnectionContextMenu(server: ClientController): HTMLElement {
if (!state) throw new ShouldNeverHappenError('state is null');
return createConnectionContextMenu(state, server);
}
static createServerListServer(server: ClientController): HTMLElement {
if (!state) throw new ShouldNeverHappenError('state is null');
return createServerListServer(state, server);
}
static createChannel(server: ClientController, channel: Channel): HTMLElement {
if (!state) throw new ShouldNeverHappenError('state is null');
return createChannel(state, server, channel);
}
static createMember(server: ClientController, member: Member) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createMember(state, server, member);
}
static createTextMessage(server: ClientController, message: Message | IDummyTextMessage) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createTextMessage(state, server, message);
}
static createTextMessageContinued(server: ClientController, message: Message) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createTextMessageContinued(state, server, message);
}
static createImageResourceMessage(server: ClientController, message: Message) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createImageResourceMessage(state, server, message);
}
static createImageResourceMessageContinued(server: ClientController, message: Message) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createImageResourceMessageContinued(state, server, message);
}
static createResourceMessage(server: ClientController, message: Message) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createResourceMessage(state, server, message);
}
static createResourceMessageContinued(server: ClientController, message: Message) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createResourceMessageContinued(state, server, message);
}
static createMessage(server: ClientController, message: Message, lastMessage: Message | null) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createMessage(server, message, lastMessage);
}
static createImageOverlay(server: ClientController, resourceId: string, resourceName: string) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createImageOverlay(state, server, resourceId, resourceName);
}
static createUploadOverlayFromPath(server: ClientController, channel: Channel, resourcePath: string) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createUploadOverlayFromPath(state, server, channel, resourcePath);
}
static createUploadOverlayFromDataTransferItem(server: ClientController, channel: Channel, dataTransferItem: DataTransferItem) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createUploadOverlayFromDataTransferItem(state, server, channel, dataTransferItem);
}
static createUploadDropTarget(server?: ClientController, channel?: Channel) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createUploadDropTarget(state, server, channel);
}
static createPersonalizeOverlay(server: ClientController, connection: ConnectionInfo) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createPersonalizeOverlay(state, server, connection);
}
static createAddServerOverlay(addServerData: IAddServerData) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createAddServerOverlay(state, addServerData);
}
static createErrorMessageOverlay(title: string, message: string) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createErrorMessageOverlay(state, title, message);
}
static createErrorIndicator(config: CreateErrorIndicatorProps) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createErrorIndicator(state, config);
}
static createServerContextMenu(server: ClientController) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createServerContextMenu(state, server);
}
static createModifyChannelOverlay(server: ClientController, channel: Channel) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createModifyChannelOverlay(state, server, channel);
}
static createServerTitleContextMenu(server: ClientController) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createServerTitleContextMenu(state, server);
}
static createServerSettingsOverlay(server: ClientController, serverMeta: any) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createServerSettingsOverlay(state, server, serverMeta);
}
static createCreateChannelOverlay(server: ClientController) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createCreateChannelOverlay(state, server);
}
static createCreateInviteTokenOverlay(server: ClientController) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createCreateInviteTokenOverlay(state, server);
}
static createTokenLogOverlay(server: ClientController) {
if (!state) throw new ShouldNeverHappenError('state is null');
return createTokenLogOverlay(state, server);
}
static bindBaseEvents() {
if (!state) throw new ShouldNeverHappenError('state is null');
bindWindowButtonEvents(state);
bindTextInputEvents(state);
bindInfiniteScrollEvents(state);
bindConnectionEvents(state);
bindAddServerTitleEvents(state);
bindAddServerEvents(state);
}
}