2020-03-27 23:36:57 +01:00
|
|
|
import {ConnectionHandler, DisconnectReason} from "../../ConnectionHandler";
|
|
|
|
import {Settings, settings} from "../../settings";
|
|
|
|
import {control_bar} from "./ControlBar";
|
|
|
|
import {top_menu} from "./MenuBar";
|
2019-04-04 21:47:52 +02:00
|
|
|
|
2020-03-27 23:36:57 +01:00
|
|
|
export let server_connections: ServerConnectionManager;
|
2019-04-04 21:47:52 +02:00
|
|
|
|
2020-03-27 23:36:57 +01:00
|
|
|
export class ServerConnectionManager {
|
2019-04-04 21:47:52 +02:00
|
|
|
private connection_handlers: ConnectionHandler[] = [];
|
|
|
|
private active_handler: ConnectionHandler | undefined;
|
|
|
|
|
2019-07-10 00:52:08 +02:00
|
|
|
private _container_log_server: JQuery;
|
2019-04-04 21:47:52 +02:00
|
|
|
private _container_channel_tree: JQuery;
|
2019-08-21 10:00:01 +02:00
|
|
|
private _container_hostbanner: JQuery;
|
2019-07-10 00:52:08 +02:00
|
|
|
private _container_chat: JQuery;
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
private _tag: JQuery;
|
|
|
|
private _tag_connection_entries: JQuery;
|
|
|
|
private _tag_buttons_scoll: JQuery;
|
|
|
|
private _tag_button_scoll_right: JQuery;
|
|
|
|
private _tag_button_scoll_left: JQuery;
|
|
|
|
|
|
|
|
constructor(tag: JQuery) {
|
|
|
|
this._tag = tag;
|
|
|
|
|
|
|
|
if(settings.static_global(Settings.KEY_DISABLE_MULTI_SESSION, false))
|
|
|
|
this._tag.hide();
|
|
|
|
|
|
|
|
this._tag_connection_entries = this._tag.find(".connection-handlers");
|
|
|
|
this._tag_buttons_scoll = this._tag.find(".container-scroll");
|
|
|
|
this._tag_button_scoll_left = this._tag_buttons_scoll.find(".button-scroll-left");
|
|
|
|
this._tag_button_scoll_right = this._tag_buttons_scoll.find(".button-scroll-right");
|
|
|
|
|
|
|
|
this._tag_button_scoll_left.on('click', this._button_scroll_left_clicked.bind(this));
|
|
|
|
this._tag_button_scoll_right.on('click', this._button_scroll_right_clicked.bind(this));
|
|
|
|
|
2019-07-10 00:52:08 +02:00
|
|
|
this._container_log_server = $("#server-log");
|
2019-04-04 21:47:52 +02:00
|
|
|
this._container_channel_tree = $("#channelTree");
|
2019-08-21 10:00:01 +02:00
|
|
|
this._container_hostbanner = $("#hostbanner");
|
2019-07-10 00:52:08 +02:00
|
|
|
this._container_chat = $("#chat");
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
this.set_active_connection_handler(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn_server_connection_handler() : ConnectionHandler {
|
|
|
|
const handler = new ConnectionHandler();
|
|
|
|
this.connection_handlers.push(handler);
|
|
|
|
control_bar.update_button_away();
|
|
|
|
control_bar.initialize_connection_handler_state(handler);
|
|
|
|
|
|
|
|
handler.tag_connection_handler.appendTo(this._tag_connection_entries);
|
2019-07-10 00:52:08 +02:00
|
|
|
this._tag.toggleClass("shown", this.connection_handlers.length > 1);
|
2019-04-04 21:47:52 +02:00
|
|
|
this._update_scroll();
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_server_connection_handler(handler: ConnectionHandler) {
|
|
|
|
this.connection_handlers.remove(handler);
|
2019-08-21 10:00:01 +02:00
|
|
|
handler.tag_connection_handler.remove();
|
2019-04-04 21:47:52 +02:00
|
|
|
this._update_scroll();
|
2019-07-10 00:52:08 +02:00
|
|
|
this._tag.toggleClass("shown", this.connection_handlers.length > 1);
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
if(handler.serverConnection) {
|
|
|
|
const connected = handler.connected;
|
|
|
|
handler.serverConnection.disconnect("handler destroyed");
|
|
|
|
handler.handleDisconnect(DisconnectReason.HANDLER_DESTROYED, connected);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(handler === this.active_handler)
|
|
|
|
this.set_active_connection_handler(this.connection_handlers[0]);
|
2019-08-21 10:00:01 +02:00
|
|
|
|
|
|
|
/* destroy all elements */
|
|
|
|
handler.destroy();
|
2019-04-04 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set_active_connection_handler(handler: ConnectionHandler) {
|
|
|
|
if(handler && this.connection_handlers.indexOf(handler) == -1)
|
2019-08-21 10:00:01 +02:00
|
|
|
throw "Handler hasn't been registered or is already obsolete!";
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
this._tag_connection_entries.find(".active").removeClass("active");
|
|
|
|
this._container_channel_tree.children().detach();
|
2019-07-10 00:52:08 +02:00
|
|
|
this._container_chat.children().detach();
|
|
|
|
this._container_log_server.children().detach();
|
2019-08-21 10:00:01 +02:00
|
|
|
this._container_hostbanner.children().detach();
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
if(handler) {
|
|
|
|
handler.tag_connection_handler.addClass("active");
|
|
|
|
|
2019-08-21 10:00:01 +02:00
|
|
|
this._container_hostbanner.append(handler.hostbanner.html_tag);
|
2019-04-04 21:47:52 +02:00
|
|
|
this._container_channel_tree.append(handler.channelTree.tag_tree());
|
2019-08-21 10:00:01 +02:00
|
|
|
this._container_chat.append(handler.side_bar.html_tag());
|
2019-07-10 00:52:08 +02:00
|
|
|
this._container_log_server.append(handler.log.html_tag());
|
2019-04-04 21:47:52 +02:00
|
|
|
|
|
|
|
if(handler.invoke_resized_on_activate)
|
|
|
|
handler.resize_elements();
|
|
|
|
}
|
|
|
|
this.active_handler = handler;
|
2019-10-23 13:14:18 +02:00
|
|
|
control_bar.set_connection_handler(handler);
|
|
|
|
top_menu.update_state();
|
2019-04-04 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
active_connection_handler() : ConnectionHandler | undefined {
|
|
|
|
return this.active_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_connection_handlers() : ConnectionHandler[] {
|
|
|
|
return this.connection_handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_ui() {
|
|
|
|
this._update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _update_scroll() {
|
|
|
|
const has_scroll = this._tag_connection_entries.hasScrollBar("width")
|
|
|
|
&& this._tag_connection_entries.width() + 10 >= this._tag_connection_entries.parent().width();
|
|
|
|
|
|
|
|
this._tag_buttons_scoll.toggleClass("enabled", has_scroll);
|
|
|
|
this._tag.toggleClass("scrollbar", has_scroll);
|
|
|
|
|
|
|
|
if(has_scroll)
|
|
|
|
this._update_scroll_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _button_scroll_right_clicked() {
|
|
|
|
this._tag_connection_entries.scrollLeft((this._tag_connection_entries.scrollLeft() || 0) + 50);
|
|
|
|
this._update_scroll_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _button_scroll_left_clicked() {
|
|
|
|
this._tag_connection_entries.scrollLeft((this._tag_connection_entries.scrollLeft() || 0) - 50);
|
|
|
|
this._update_scroll_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _update_scroll_buttons() {
|
|
|
|
const scroll = this._tag_connection_entries.scrollLeft() || 0;
|
|
|
|
this._tag_button_scoll_left.toggleClass("disabled", scroll <= 0);
|
|
|
|
this._tag_button_scoll_right.toggleClass("disabled", scroll + this._tag_connection_entries.width() + 2 >= this._tag_connection_entries[0].scrollWidth);
|
|
|
|
}
|
|
|
|
}
|