cordis/client/webapp/elements/context-menu-srv-title.ts
2021-11-07 15:57:09 -06:00

112 lines
4.1 KiB
TypeScript

import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../../logger/logger';
const LOG = Logger.create(__filename, electronConsole);
import ElementsUtil from './require/elements-util.js';
import BaseElements from './require/base-elements.js';
import ClientController from '../client-controller';
import { CacheServerData, ServerMetaData } from '../data-types';
import Q from '../q-module';
import UI from '../ui';
import createErrorMessageOverlay from './overlay-error-message';
import createServerSettingsOverlay from './overlay-server-settings';
import createCreateInviteTokenOverlay from './overlay-create-invite-token';
import createCreateChannelOverlay from './overlay-create-channel';
import createTokenLogOverlay from './overlay-token-log';
export default function createServerTitleContextMenu(document: Document, q: Q, ui: UI, server: ClientController): HTMLElement {
if (ui.activeConnection === null) {
LOG.warn('no active connection when creating server title context menu');
return q.create({}) as HTMLElement;
}
let menuItems: any[] = [];
if (ui.activeConnection.privileges.includes('modify_profile')) {
menuItems.push({ class: 'item server-settings', content: [
{ class: 'icon', content: BaseElements.COG },
'Server Settings'
] });
}
if (ui.activeConnection.privileges.includes('modify_channels')) {
if (ui.activeConnection.privileges.includes('modify_profile')) {
menuItems.push({ class: 'item-spacer' });
}
menuItems.push({ class: 'item create-channel', content: [
{ class: 'icon', content: BaseElements.CREATE },
'Create Channel'
] });
}
if (ui.activeConnection.privileges.includes('modify_members')) {
if (
ui.activeConnection.privileges.includes('modify_profile') ||
ui.activeConnection.privileges.includes('modify_channels')
) {
menuItems.push({ class: 'item-spacer' });
}
menuItems.push({ class: 'item create-invite-token', content: [
{ class: 'icon', content: BaseElements.TOKEN },
'Create Invite Token'
] });
menuItems.push({ class: 'item token-log', content: [
{ class: 'icon', content: BaseElements.TOKEN },
'Token Log'
] });
}
let element = BaseElements.createContextMenu(document, {
class: 'server-title-context', content: menuItems
});
if (ui.activeConnection.privileges.includes('modify_profile')) {
q.$$$(element, '.item.server-settings').addEventListener('click', async () => {
element.removeSelf();
let serverMeta: ServerMetaData | CacheServerData | null = null;
try {
serverMeta = await server.grabMetadata();
} catch (e) {
LOG.error('error fetching server info', e);
}
if (serverMeta === null) {
let overlay = createErrorMessageOverlay(document, 'Error Opening Settings', 'Could not load server information');
document.body.appendChild(overlay);
} else {
let overlay = createServerSettingsOverlay(document, q, server, serverMeta);
document.body.appendChild(overlay);
q.$$$(overlay, '.text-input').focus();
ElementsUtil.setCursorToEnd(q.$$$(overlay, '.text-input'));
}
});
}
if (ui.activeConnection.privileges.includes('modify_channels')) {
q.$$$(element, '.item.create-channel').addEventListener('click', () => {
element.removeSelf();
let overlay = createCreateChannelOverlay(document, q, server);
document.body.appendChild(overlay);
q.$$$(overlay, '.text-input.channel-name').focus();
ElementsUtil.setCursorToEnd(q.$$$(overlay, '.text-input.channel-name'));
});
}
if (ui.activeConnection.privileges.includes('modify_members')) {
q.$$$(element, '.item.create-invite-token').addEventListener('click', () => {
element.removeSelf();
let overlay = createCreateInviteTokenOverlay(document, server);
document.body.appendChild(overlay);
//LOG.info('create invite token clicked');
});
q.$$$(element, '.item.token-log').addEventListener('click', () => {
element.removeSelf();
let overlay = createTokenLogOverlay(document, q, server);
document.body.appendChild(overlay);
});
}
return element;
}