TeaWeb/shared/js/main.ts

240 lines
8.1 KiB
TypeScript
Raw Normal View History

2018-02-27 16:20:49 +00:00
/// <reference path="chat.ts" />
/// <reference path="client.ts" />
2018-04-11 15:56:09 +00:00
/// <reference path="Identity.ts" />
2018-02-27 16:20:49 +00:00
/// <reference path="utils/modal.ts" />
2018-03-07 18:06:52 +00:00
/// <reference path="ui/modal/ModalConnect.ts" />
2018-04-16 18:38:35 +00:00
/// <reference path="ui/modal/ModalCreateChannel.ts" />
/// <reference path="ui/modal/ModalBanCreate.ts" />
2018-04-30 21:57:21 +00:00
/// <reference path="ui/modal/ModalBanClient.ts" />
/// <reference path="ui/modal/ModalYesNo.ts" />
/// <reference path="ui/modal/ModalBanList.ts" />
2018-04-11 15:56:09 +00:00
/// <reference path="settings.ts" />
2018-04-16 18:38:35 +00:00
/// <reference path="log.ts" />
2018-02-27 16:20:49 +00:00
2018-04-16 18:38:35 +00:00
let settings: Settings;
2018-04-11 15:56:09 +00:00
let globalClient: TSClient;
let chat: ChatBox;
let forumIdentity: TeaForumIdentity;
2018-02-27 16:20:49 +00:00
const js_render = window.jsrender || $;
2018-10-06 13:13:45 +00:00
const native_client = window.require !== undefined;
2018-10-28 22:01:09 +00:00
function getUserMediaFunction() {
2018-12-23 21:56:04 +00:00
if((navigator as any).mediaDevices && (navigator as any).mediaDevices.getUserMedia)
return (settings, success, fail) => { (navigator as any).mediaDevices.getUserMedia(settings).then(success).catch(fail); };
return (navigator as any).getUserMedia || (navigator as any).webkitGetUserMedia || (navigator as any).mozGetUserMedia;
2018-10-28 22:01:09 +00:00
}
2018-10-06 13:13:45 +00:00
function setup_close() {
if(settings.static(Settings.KEY_DISABLE_UNLOAD_DIALOG, false)) return;
window.onbeforeunload = event => {
if(!globalClient.serverConnection || !globalClient.serverConnection.connected) return;
if(!native_client) {
event.returnValue = "Are you really sure?<br>You're still connected!";
} else {
event.preventDefault();
event.returnValue = "question";
const {remote} = require('electron');
const dialog = remote.dialog;
dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you really sure?\nYou\'re still connected!'
}, choice => {
if(choice === 0) {
window.onbeforeunload = undefined;
remote.getCurrentWindow().close();
}
});
}
};
}
declare function moment(...arguments) : any;
function setup_jsrender() : boolean {
if(!js_render) {
displayCriticalError("Missing jsrender extension!");
return false;
}
2018-10-07 16:21:28 +00:00
if(!js_render.views) {
displayCriticalError("Missing jsrender viewer extension!");
return false;
2018-10-07 16:21:28 +00:00
}
js_render.views.settings.allowCode(true);
js_render.views.tags("rnd", (argument) => {
2018-06-24 11:38:53 +00:00
let min = parseInt(argument.substr(0, argument.indexOf('~')));
let max = parseInt(argument.substr(argument.indexOf('~') + 1));
return (Math.round(Math.random() * (min + max + 1) - min)).toString();
});
js_render.views.tags("fmt_date", (...arguments) => {
return moment(arguments[0]).format(arguments[1]);
});
js_render.views.tags("tr", (...arguments) => {
return tr(arguments[0]);
});
$(".jsrender-template").each((idx, _entry) => {
if(!js_render.templates(_entry.id, _entry.innerHTML)) { //, _entry.innerHTML
console.error("Failed to cache template " + _entry.id + " for js render!");
} else
console.debug("Successfully loaded jsrender template " + _entry.id);
});
return true;
}
async function initialize() {
const display_load_error = message => {
if(typeof(display_critical_load) !== "undefined")
display_critical_load(message);
else
displayCriticalError(message);
};
try {
await i18n.initialize();
} catch(error) {
console.error(tr("Failed to initialized the translation system!\nError: %o"), error);
displayCriticalError("Failed to setup the translation system");
return;
}
try {
if(!setup_jsrender())
throw "invalid load";
} catch (error) {
display_load_error(tr("Failed to setup jsrender"));
console.error(tr("Failed to load jsrender! %o"), error);
return;
}
try { //Initialize main template
const main = $("#tmpl_main").renderTag();
$("body").append(main);
} catch(error) {
display_load_error(tr("Failed to setup main page!"));
return;
}
2018-04-11 15:56:09 +00:00
AudioController.initializeAudioController();
if(!TSIdentityHelper.setup()) {
console.error(tr("Could not setup the TeamSpeak identity parser!"));
return;
}
2018-04-11 15:56:09 +00:00
try {
await ppt.initialize();
} catch(error) {
console.error(tr("Failed to initialize ppt!\nError: %o"), error);
displayCriticalError(tr("Failed to initialize ppt!"));
return;
}
}
function main() {
//http://localhost:63343/Web-Client/index.php?_ijt=omcpmt8b9hnjlfguh8ajgrgolr&default_connect_url=true&default_connect_type=teamspeak&default_connect_url=localhost%3A9987&disableUnloadDialog=1&loader_ignore_age=1
2018-04-16 18:38:35 +00:00
settings = new Settings();
2018-03-24 22:38:01 +00:00
globalClient = new TSClient();
2018-04-11 15:56:09 +00:00
/** Setup the XF forum identity **/
2018-04-16 18:38:35 +00:00
if(settings.static("forum_user_data")) {
forumIdentity = new TeaForumIdentity(settings.static("forum_user_data"), settings.static("forum_user_sign"));
2018-04-11 15:56:09 +00:00
}
2018-03-24 22:38:01 +00:00
chat = new ChatBox($("#chat"));
globalClient.setup();
2018-03-10 08:03:29 +00:00
2018-04-16 18:38:35 +00:00
2018-10-06 13:13:45 +00:00
if(!settings.static(Settings.KEY_DISABLE_UNLOAD_DIALOG, false) && !native_client) {
2018-04-16 18:38:35 +00:00
}
2018-03-24 22:38:01 +00:00
//Modals.spawnConnectModal();
//Modals.spawnSettingsModal();
2018-04-16 18:38:35 +00:00
//Modals.createChannelModal(undefined);
2018-03-24 22:38:01 +00:00
2018-04-16 18:38:35 +00:00
if(settings.static("default_connect_url")) {
switch (settings.static("default_connect_type")) {
case "teaforo":
2018-12-03 17:56:36 +00:00
if(forumIdentity && forumIdentity.valid())
globalClient.startConnection(settings.static("default_connect_url"), forumIdentity);
else
2018-12-03 17:56:36 +00:00
Modals.spawnConnectModal({
url: settings.static<string>("default_connect_url"),
enforce: true
}, { identity: IdentitifyType.TEAFORO, enforce: true});
break;
case "teamspeak":
let connectIdentity = TSIdentityHelper.loadIdentity(settings.global("connect_identity_teamspeak_identity", ""));
if(!connectIdentity || !connectIdentity.valid())
2018-12-03 17:56:36 +00:00
Modals.spawnConnectModal({
url: settings.static<string>("default_connect_url"),
enforce: true
}, { identity: IdentitifyType.TEAMSPEAK, enforce: true});
else
globalClient.startConnection(settings.static("default_connect_url"), connectIdentity);
break;
default:
2018-12-03 17:56:36 +00:00
Modals.spawnConnectModal({
url: settings.static<string>("default_connect_url"),
enforce: true
});
}
2018-04-16 18:38:35 +00:00
}
2018-04-30 21:57:21 +00:00
/*
2018-06-24 11:38:53 +00:00
let tag = $("#tmpl_music_frame").renderTag({
//thumbnail: "img/loading_image.svg"
});
$("#music-test").replaceWith(tag);
2018-04-30 21:57:21 +00:00
Modals.spawnSettingsModal();
/*
Modals.spawnYesNo("Are your sure?", "Do you really want to exit?", flag => {
console.log("Response: " + flag);
})
*/
2018-09-30 19:50:59 +00:00
2018-10-06 13:13:45 +00:00
setup_close();
2018-11-03 23:39:29 +00:00
let _resize_timeout: NodeJS.Timer;
2018-10-06 13:13:45 +00:00
$(window).on('resize', () => {
2018-11-03 23:39:29 +00:00
if(_resize_timeout)
clearTimeout(_resize_timeout);
_resize_timeout = setTimeout(() => {
globalClient.channelTree.handle_resized();
}, 1000);
2018-10-06 13:13:45 +00:00
});
2018-04-19 16:42:34 +00:00
}
app.loadedListener.push(async () => {
2018-09-25 15:39:38 +00:00
try {
await initialize();
2018-09-25 15:39:38 +00:00
main();
if(!audio.player.initialized()) {
log.info(LogCategory.VOICE, tr("Initialize audio controller later!"));
if(!audio.player.initializeFromGesture) {
console.error(tr("Missing audio.player.initializeFromGesture"));
} else
$(document).one('click', event => audio.player.initializeFromGesture());
2018-09-25 15:39:38 +00:00
}
} catch (ex) {
console.error(ex.stack);
if(ex instanceof ReferenceError || ex instanceof TypeError)
ex = ex.name + ": " + ex.message;
displayCriticalError("Failed to invoke main function:<br>" + ex);
2018-09-25 15:39:38 +00:00
}
2018-10-06 13:13:45 +00:00
});