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 { GuildMetadata } from '../data-types'; import Q from '../q-module'; import UI from '../ui'; import createErrorMessageOverlay from './overlay-error-message'; 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'; import CombinedGuild from '../guild-combined'; export default function createGuildTitleContextMenu(document: Document, q: Q, ui: UI, guild: CombinedGuild): HTMLElement { if (ui.activeConnection === null) { LOG.warn('no active connection when creating guild title context menu'); return q.create({}) as HTMLElement; } let menuItems: any[] = []; if (ui.activeConnection.privileges.includes('modify_profile')) { menuItems.push({ class: 'item guild-settings', content: [ { class: 'icon', content: BaseElements.COG }, 'Guild 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: 'guild-title-context', content: menuItems }); if (ui.activeConnection.privileges.includes('modify_profile')) { q.$$$(element, '.item.guild-settings').addEventListener('click', async () => { element.removeSelf(); let guildMeta: GuildMetadata | null = null; try { guildMeta = await guild.fetchMetadata(); } catch (e) { LOG.error('error fetching guild info', e); } if (guildMeta === null) { let overlay = createErrorMessageOverlay(document, 'Error Opening Settings', 'Could not load guild information'); document.body.appendChild(overlay); } else { let overlay = createGuildSettingsOverlay(document, q, guild, guildMeta); 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, guild); 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, guild); document.body.appendChild(overlay); //LOG.info('create invite token clicked'); }); q.$$$(element, '.item.token-log').addEventListener('click', () => { element.removeSelf(); let overlay = createTokenLogOverlay(document, q, guild); document.body.appendChild(overlay); }); } return element; }