cordis/client/webapp/elements/context-menu-guild-title.ts

112 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-10-30 17:26:41 +00:00
import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../../logger/logger';
2021-11-02 04:29:24 +00:00
const LOG = Logger.create(__filename, electronConsole);
2021-10-30 17:26:41 +00:00
import ElementsUtil from './require/elements-util.js';
import BaseElements from './require/base-elements.js';
2021-11-22 02:47:29 +00:00
import { GuildMetadata } from '../data-types';
import Q from '../q-module';
import UI from '../ui';
2021-10-30 17:26:41 +00:00
import createErrorMessageOverlay from './overlay-error-message';
2021-11-22 02:47:29 +00:00
import createGuildSettingsOverlay from './overlay-guild-settings';
import createCreateInviteTokenOverlay from './overlay-create-invite-token';
import createCreateChannelOverlay from './overlay-create-channel';
import createTokenLogOverlay from './overlay-token-log';
2021-11-22 02:47:29 +00:00
import CombinedGuild from '../guild-combined';
2021-10-30 17:26:41 +00:00
2021-11-22 02:47:29 +00:00
export default function createGuildTitleContextMenu(document: Document, q: Q, ui: UI, guild: CombinedGuild): HTMLElement {
if (ui.activeConnection === null) {
2021-11-22 02:47:29 +00:00
LOG.warn('no active connection when creating guild title context menu');
return q.create({}) as HTMLElement;
}
2021-10-30 17:26:41 +00:00
let menuItems: any[] = [];
2021-10-30 17:26:41 +00:00
if (ui.activeConnection.privileges.includes('modify_profile')) {
2021-11-22 02:47:29 +00:00
menuItems.push({ class: 'item guild-settings', content: [
{ class: 'icon', content: BaseElements.COG },
2021-11-22 02:47:29 +00:00
'Guild Settings'
] });
}
2021-10-30 17:26:41 +00:00
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'
] });
}
2021-10-30 17:26:41 +00:00
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'
] });
}
2021-10-30 17:26:41 +00:00
let element = BaseElements.createContextMenu(document, {
2021-11-22 02:47:29 +00:00
class: 'guild-title-context', content: menuItems
});
2021-10-30 17:26:41 +00:00
if (ui.activeConnection.privileges.includes('modify_profile')) {
2021-11-22 02:47:29 +00:00
q.$$$(element, '.item.guild-settings').addEventListener('click', async () => {
element.removeSelf();
2021-11-22 02:47:29 +00:00
let guildMeta: GuildMetadata | null = null;
try {
2021-11-22 02:47:29 +00:00
guildMeta = await guild.fetchMetadata();
} catch (e) {
2021-11-22 02:47:29 +00:00
LOG.error('error fetching guild info', e);
}
2021-11-22 02:47:29 +00:00
if (guildMeta === null) {
let overlay = createErrorMessageOverlay(document, 'Error Opening Settings', 'Could not load guild information');
document.body.appendChild(overlay);
} else {
2021-11-22 02:47:29 +00:00
let overlay = createGuildSettingsOverlay(document, q, guild, guildMeta);
document.body.appendChild(overlay);
q.$$$(overlay, '.text-input').focus();
ElementsUtil.setCursorToEnd(q.$$$(overlay, '.text-input'));
}
});
}
2021-10-30 17:26:41 +00:00
if (ui.activeConnection.privileges.includes('modify_channels')) {
q.$$$(element, '.item.create-channel').addEventListener('click', () => {
element.removeSelf();
2021-11-22 02:47:29 +00:00
let overlay = createCreateChannelOverlay(document, q, guild);
document.body.appendChild(overlay);
q.$$$(overlay, '.text-input.channel-name').focus();
ElementsUtil.setCursorToEnd(q.$$$(overlay, '.text-input.channel-name'));
});
}
2021-10-30 17:26:41 +00:00
if (ui.activeConnection.privileges.includes('modify_members')) {
q.$$$(element, '.item.create-invite-token').addEventListener('click', () => {
element.removeSelf();
2021-11-22 02:47:29 +00:00
let overlay = createCreateInviteTokenOverlay(document, guild);
document.body.appendChild(overlay);
//LOG.info('create invite token clicked');
});
q.$$$(element, '.item.token-log').addEventListener('click', () => {
element.removeSelf();
2021-11-22 02:47:29 +00:00
let overlay = createTokenLogOverlay(document, q, guild);
document.body.appendChild(overlay);
});
}
2021-10-30 17:26:41 +00:00
return element;
2021-10-30 17:26:41 +00:00
}