2020-03-27 22:36:57 +00:00
|
|
|
import {profiles} from "./profiles/ConnectionProfile";
|
2018-12-18 19:00:29 +00:00
|
|
|
|
2020-03-27 22:36:57 +00:00
|
|
|
export namespace bookmarks {
|
2019-08-21 08:00:01 +00:00
|
|
|
export const boorkmak_connect = (mark: Bookmark, new_tab?: boolean) => {
|
|
|
|
const profile = profiles.find_profile(mark.connect_profile) || profiles.default_profile();
|
|
|
|
if(profile.valid()) {
|
|
|
|
const connection = (typeof(new_tab) !== "boolean" || !new_tab) ? server_connections.active_connection_handler() : server_connections.spawn_server_connection_handler();
|
|
|
|
server_connections.set_active_connection_handler(connection);
|
|
|
|
connection.startConnection(
|
|
|
|
mark.server_properties.server_address + ":" + mark.server_properties.server_port,
|
|
|
|
profile,
|
|
|
|
true,
|
|
|
|
{
|
2019-10-19 18:52:41 +00:00
|
|
|
nickname: mark.nickname === "Another TeaSpeak user" || !mark.nickname ? profile.connect_username() : mark.nickname,
|
2019-08-21 08:00:01 +00:00
|
|
|
password: mark.server_properties.server_password_hash ? {
|
|
|
|
password: mark.server_properties.server_password_hash,
|
|
|
|
hashed: true
|
|
|
|
} : mark.server_properties.server_password ? {
|
|
|
|
hashed: false,
|
|
|
|
password: mark.server_properties.server_password
|
|
|
|
} : undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Modals.spawnConnectModal({}, {
|
|
|
|
url: mark.server_properties.server_address + ":" + mark.server_properties.server_port,
|
|
|
|
enforce: true
|
|
|
|
}, {
|
|
|
|
profile: profile,
|
|
|
|
enforce: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-18 19:00:29 +00:00
|
|
|
export interface ServerProperties {
|
|
|
|
server_address: string;
|
|
|
|
server_port: number;
|
|
|
|
server_password_hash?: string;
|
|
|
|
server_password?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum BookmarkType {
|
|
|
|
ENTRY,
|
|
|
|
DIRECTORY
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Bookmark {
|
2018-12-23 21:56:04 +00:00
|
|
|
type: /* BookmarkType.ENTRY */ BookmarkType;
|
2019-08-31 16:31:01 +00:00
|
|
|
/* readonly */ parent: DirectoryBookmark;
|
2018-12-18 19:00:29 +00:00
|
|
|
|
|
|
|
server_properties: ServerProperties;
|
|
|
|
display_name: string;
|
|
|
|
unique_id: string;
|
|
|
|
|
|
|
|
nickname: string;
|
|
|
|
default_channel?: number | string;
|
|
|
|
default_channel_password_hash?: string;
|
|
|
|
default_channel_password?: string;
|
|
|
|
|
2018-12-28 14:39:23 +00:00
|
|
|
connect_profile: string;
|
2019-08-21 08:00:01 +00:00
|
|
|
|
|
|
|
last_icon_id?: number;
|
2018-12-18 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface DirectoryBookmark {
|
2018-12-23 21:56:04 +00:00
|
|
|
type: /* BookmarkType.DIRECTORY */ BookmarkType;
|
2019-09-01 15:24:06 +00:00
|
|
|
/* readonly */ parent: DirectoryBookmark;
|
2018-12-18 19:00:29 +00:00
|
|
|
|
|
|
|
readonly content: (Bookmark | DirectoryBookmark)[];
|
|
|
|
unique_id: string;
|
|
|
|
display_name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BookmarkConfig {
|
|
|
|
root_bookmark?: DirectoryBookmark;
|
|
|
|
default_added?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
let _bookmark_config: BookmarkConfig;
|
|
|
|
|
|
|
|
function bookmark_config() : BookmarkConfig {
|
|
|
|
if(_bookmark_config)
|
|
|
|
return _bookmark_config;
|
|
|
|
|
|
|
|
let bookmark_json = localStorage.getItem("bookmarks");
|
2019-06-01 14:15:42 +00:00
|
|
|
let bookmarks;
|
|
|
|
try {
|
|
|
|
bookmarks = JSON.parse(bookmark_json) || {} as BookmarkConfig;
|
|
|
|
} catch(error) {
|
2019-08-30 21:06:39 +00:00
|
|
|
log.error(LogCategory.BOOKMARKS, tr("Failed to load bookmarks: %o"), error);
|
2019-06-01 14:15:42 +00:00
|
|
|
bookmarks = {} as any;
|
|
|
|
}
|
2018-12-18 19:00:29 +00:00
|
|
|
|
|
|
|
_bookmark_config = bookmarks;
|
|
|
|
_bookmark_config.root_bookmark = _bookmark_config.root_bookmark || { content: [], display_name: "root", type: BookmarkType.DIRECTORY} as DirectoryBookmark;
|
|
|
|
|
|
|
|
if(!_bookmark_config.default_added) {
|
|
|
|
_bookmark_config.default_added = true;
|
|
|
|
create_bookmark("TeaSpeak official Test-Server", _bookmark_config.root_bookmark, {
|
|
|
|
server_address: "ts.teaspeak.de",
|
|
|
|
server_port: 9987
|
2019-10-19 18:52:41 +00:00
|
|
|
}, undefined);
|
2018-12-18 19:00:29 +00:00
|
|
|
|
|
|
|
save_config();
|
|
|
|
}
|
2019-08-31 16:31:01 +00:00
|
|
|
|
|
|
|
const fix_parent = (parent: DirectoryBookmark, entry: Bookmark | DirectoryBookmark) => {
|
|
|
|
entry.parent = parent;
|
|
|
|
if(entry.type === BookmarkType.DIRECTORY)
|
|
|
|
for(const child of (entry as DirectoryBookmark).content)
|
|
|
|
fix_parent(entry as DirectoryBookmark, child);
|
|
|
|
};
|
|
|
|
for(const entry of _bookmark_config.root_bookmark.content)
|
|
|
|
fix_parent(_bookmark_config.root_bookmark, entry);
|
|
|
|
|
2018-12-18 19:00:29 +00:00
|
|
|
return _bookmark_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function save_config() {
|
2019-08-31 16:31:01 +00:00
|
|
|
localStorage.setItem("bookmarks", JSON.stringify(bookmark_config(), (key, value) => {
|
|
|
|
if(key === "parent")
|
|
|
|
return undefined;
|
|
|
|
return value;
|
|
|
|
}));
|
2018-12-18 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function bookmarks() : DirectoryBookmark {
|
|
|
|
return bookmark_config().root_bookmark;
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:00:01 +00:00
|
|
|
export function bookmarks_flat() : Bookmark[] {
|
|
|
|
const result: Bookmark[] = [];
|
|
|
|
const _flat = (bookmark: Bookmark | DirectoryBookmark) => {
|
|
|
|
if(bookmark.type == BookmarkType.DIRECTORY)
|
|
|
|
for(const book of (bookmark as DirectoryBookmark).content)
|
|
|
|
_flat(book);
|
|
|
|
else
|
|
|
|
result.push(bookmark as Bookmark);
|
|
|
|
};
|
|
|
|
_flat(bookmark_config().root_bookmark);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:00:29 +00:00
|
|
|
function find_bookmark_recursive(parent: DirectoryBookmark, uuid: string) : Bookmark | DirectoryBookmark {
|
|
|
|
for(const entry of parent.content) {
|
|
|
|
if(entry.unique_id == uuid)
|
|
|
|
return entry;
|
|
|
|
if(entry.type == BookmarkType.DIRECTORY) {
|
2018-12-23 21:56:04 +00:00
|
|
|
const result = find_bookmark_recursive(entry as DirectoryBookmark, uuid);
|
2018-12-18 19:00:29 +00:00
|
|
|
if(result) return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function find_bookmark(uuid: string) : Bookmark | DirectoryBookmark | undefined {
|
|
|
|
return find_bookmark_recursive(bookmarks(), uuid);
|
|
|
|
}
|
|
|
|
|
2018-12-28 14:39:23 +00:00
|
|
|
export function parent_bookmark(bookmark: Bookmark) : DirectoryBookmark {
|
|
|
|
const books: (DirectoryBookmark | Bookmark)[] = [bookmarks()];
|
|
|
|
while(!books.length) {
|
|
|
|
const directory = books.pop_front();
|
|
|
|
if(directory.type == BookmarkType.DIRECTORY) {
|
|
|
|
const cast = <DirectoryBookmark>directory;
|
|
|
|
|
|
|
|
if(cast.content.indexOf(bookmark) != -1)
|
|
|
|
return cast;
|
|
|
|
books.push(...cast.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bookmarks();
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:00:29 +00:00
|
|
|
export function create_bookmark(display_name: string, directory: DirectoryBookmark, server_properties: ServerProperties, nickname: string) : Bookmark {
|
|
|
|
const bookmark = {
|
|
|
|
display_name: display_name,
|
|
|
|
server_properties: server_properties,
|
|
|
|
nickname: nickname,
|
|
|
|
type: BookmarkType.ENTRY,
|
2018-12-28 14:39:23 +00:00
|
|
|
connect_profile: "default",
|
2019-08-31 16:31:01 +00:00
|
|
|
unique_id: guid(),
|
|
|
|
parent: directory
|
2018-12-18 19:00:29 +00:00
|
|
|
} as Bookmark;
|
|
|
|
|
|
|
|
directory.content.push(bookmark);
|
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create_bookmark_directory(parent: DirectoryBookmark, name: string) : DirectoryBookmark {
|
|
|
|
const bookmark = {
|
|
|
|
type: BookmarkType.DIRECTORY,
|
|
|
|
|
|
|
|
display_name: name,
|
|
|
|
content: [],
|
2019-08-31 16:31:01 +00:00
|
|
|
unique_id: guid(),
|
|
|
|
parent: parent
|
2018-12-18 19:00:29 +00:00
|
|
|
} as DirectoryBookmark;
|
|
|
|
|
|
|
|
parent.content.push(bookmark);
|
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO test if the new parent is within the old bookmark
|
|
|
|
export function change_directory(parent: DirectoryBookmark, bookmark: Bookmark | DirectoryBookmark) {
|
2018-12-28 14:39:23 +00:00
|
|
|
delete_bookmark(bookmark);
|
2018-12-18 19:00:29 +00:00
|
|
|
parent.content.push(bookmark)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function save_bookmark(bookmark?: Bookmark | DirectoryBookmark) {
|
|
|
|
save_config(); /* nvm we dont give a fuck... saving everything */
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete_bookmark_recursive(parent: DirectoryBookmark, bookmark: Bookmark | DirectoryBookmark) {
|
|
|
|
const index = parent.content.indexOf(bookmark);
|
|
|
|
if(index != -1)
|
|
|
|
parent.content.remove(bookmark);
|
|
|
|
else
|
|
|
|
for(const entry of parent.content)
|
|
|
|
if(entry.type == BookmarkType.DIRECTORY)
|
2018-12-23 21:56:04 +00:00
|
|
|
delete_bookmark_recursive(entry as DirectoryBookmark, bookmark)
|
2018-12-18 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function delete_bookmark(bookmark: Bookmark | DirectoryBookmark) {
|
|
|
|
delete_bookmark_recursive(bookmarks(), bookmark)
|
|
|
|
}
|
2019-08-21 08:00:01 +00:00
|
|
|
|
|
|
|
export function add_current_server() {
|
|
|
|
const ch = server_connections.active_connection_handler();
|
|
|
|
if(ch && ch.connected) {
|
2019-10-13 19:33:07 +00:00
|
|
|
const ce = ch.getClient();
|
|
|
|
const name = ce ? ce.clientNickName() : undefined;
|
|
|
|
createInputModal(tr("Enter bookmarks name"), tr("Please enter the bookmarks name:<br>"), text => text.length > 0, result => {
|
2019-08-21 08:00:01 +00:00
|
|
|
if(result) {
|
|
|
|
const bookmark = create_bookmark(result as string, bookmarks(), {
|
|
|
|
server_port: ch.serverConnection.remote_address().port,
|
|
|
|
server_address: ch.serverConnection.remote_address().host,
|
|
|
|
|
|
|
|
server_password: "",
|
|
|
|
server_password_hash: ""
|
2019-10-13 19:33:07 +00:00
|
|
|
}, name);
|
2019-08-21 08:00:01 +00:00
|
|
|
save_bookmark(bookmark);
|
|
|
|
|
|
|
|
control_bar.update_bookmarks();
|
|
|
|
top_menu.rebuild_bookmarks();
|
2019-10-13 19:33:07 +00:00
|
|
|
|
|
|
|
createInfoModal(tr("Server added"), tr("Server has been successfully added to your bookmarks.")).open();
|
2019-08-21 08:00:01 +00:00
|
|
|
}
|
|
|
|
}).open();
|
|
|
|
} else {
|
|
|
|
createErrorModal(tr("You have to be connected"), tr("You have to be connected!")).open();
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 19:00:29 +00:00
|
|
|
}
|