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 BaseElements from './require/base-elements';
|
|
|
|
import ElementsUtil from './require/elements-util';
|
|
|
|
|
|
|
|
import ClientController from '../client-controller';
|
2021-11-05 00:30:30 +00:00
|
|
|
import { CacheServerData, ServerMetaData } from '../data-types';
|
2021-11-07 16:50:30 +00:00
|
|
|
import Q from '../q-module';
|
|
|
|
import UI from '../ui';
|
|
|
|
import Actions from '../actions';
|
2021-11-07 21:57:09 +00:00
|
|
|
import createServerContextMenu from './context-menu-srv';
|
2021-11-07 16:50:30 +00:00
|
|
|
import Controller from '../controller';
|
|
|
|
|
2021-11-07 23:13:40 +00:00
|
|
|
export default function createServerListServer(document: Document, q: Q, ui: UI, controller: Controller, server: ClientController) {
|
2021-11-07 16:50:30 +00:00
|
|
|
let element = q.create({ class: 'server', 'meta-id': server.id, 'meta-name': server.id, content: [
|
2021-10-30 17:26:41 +00:00
|
|
|
{ class: 'pill' },
|
|
|
|
{ tag: 'img', src: './img/loading.svg', alt: 'server' }, // src is set later by script.js
|
|
|
|
] }) as HTMLElement;
|
|
|
|
|
|
|
|
// Hover over for name + connection info
|
|
|
|
(async () => {
|
2021-11-05 00:30:30 +00:00
|
|
|
let serverData: ServerMetaData | CacheServerData;
|
2021-10-30 17:26:41 +00:00
|
|
|
try {
|
2021-11-05 00:30:30 +00:00
|
|
|
serverData = await server.grabMetadata();
|
|
|
|
if (!serverData.iconResourceId) throw new Error('server icon not identified yet');
|
|
|
|
let serverIcon = await server.fetchResource(serverData.iconResourceId);
|
2021-10-30 17:26:41 +00:00
|
|
|
let serverIconSrc = await ElementsUtil.getImageBufferSrc(serverIcon);
|
2021-11-07 16:50:30 +00:00
|
|
|
(q.$$$(element, 'img') as HTMLImageElement).src = serverIconSrc;
|
2021-10-30 17:26:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
LOG.error('Error fetching server icon', e);
|
2021-11-07 16:50:30 +00:00
|
|
|
(q.$$$(element, 'img') as HTMLImageElement).src = './img/error.png';
|
2021-11-05 00:30:30 +00:00
|
|
|
return;
|
2021-10-30 17:26:41 +00:00
|
|
|
}
|
2021-11-05 00:30:30 +00:00
|
|
|
|
|
|
|
element.setAttribute('meta-name', serverData.name);
|
|
|
|
|
2021-11-07 16:50:30 +00:00
|
|
|
let contextElement = q.create({ class: 'context', content: {
|
2021-11-05 00:30:30 +00:00
|
|
|
class: 'info', content: [
|
|
|
|
BaseElements.TAB_LEFT,
|
|
|
|
{ class: 'content server' } // populated later
|
|
|
|
]
|
|
|
|
} }) as HTMLElement;
|
|
|
|
|
|
|
|
// TODO: future: update the status in real-time with the update-member event
|
|
|
|
|
|
|
|
element.addEventListener('mouseenter', async () => {
|
2021-11-07 16:50:30 +00:00
|
|
|
Q.clearChildren(q.$$$(contextElement, '.content'));
|
|
|
|
q.$$$(contextElement, '.content').appendChild(q.create({ class: 'name', content: element.getAttribute('meta-name') }));
|
2021-11-05 00:30:30 +00:00
|
|
|
document.body.appendChild(contextElement);
|
|
|
|
ElementsUtil.alignContextElement(contextElement, element, { left: 'right', centerY: 'centerY' });
|
|
|
|
(async () => {
|
|
|
|
let connection = await server.fetchConnectionInfo();
|
2021-11-07 16:50:30 +00:00
|
|
|
let connectionElement = q.create({ class: 'connection ' + connection.status, content: [
|
2021-11-05 00:30:30 +00:00
|
|
|
{ class: 'status-circle' },
|
|
|
|
{ class: 'display-name', content: connection.displayName }
|
|
|
|
] });
|
2021-11-07 16:50:30 +00:00
|
|
|
q.$$$(contextElement, '.content').appendChild(connectionElement);
|
2021-11-05 00:30:30 +00:00
|
|
|
ElementsUtil.alignContextElement(contextElement, element, { left: 'right', centerY: 'centerY' });
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener('mouseleave', () => {
|
|
|
|
if (contextElement.parentElement) {
|
|
|
|
contextElement.parentElement.removeChild(contextElement);
|
|
|
|
}
|
|
|
|
});
|
2021-10-30 17:26:41 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
element.addEventListener('click', async () => {
|
|
|
|
if (element.classList.contains('active')) return;
|
|
|
|
|
|
|
|
ui.setActiveServer(server);
|
|
|
|
|
|
|
|
// Connection information
|
|
|
|
(async () => {
|
2021-11-07 23:13:40 +00:00
|
|
|
await Actions.fetchAndUpdateConnection(ui, server);
|
2021-10-30 17:26:41 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Server Channel Name
|
|
|
|
(async () => {
|
|
|
|
// Explicitly not using a withPotentialError to make this simpler
|
|
|
|
try {
|
|
|
|
let serverData = await server.grabMetadata();
|
|
|
|
ui.updateServerName(server, serverData.name);
|
|
|
|
} catch (e) {
|
|
|
|
LOG.error('Error fetching server name', e);
|
|
|
|
ui.updateServerName(server, 'ERROR');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Server Channel List
|
|
|
|
(async () => {
|
2021-11-07 23:13:40 +00:00
|
|
|
await Actions.fetchAndUpdateChannels(q, ui, server);
|
2021-10-30 17:26:41 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Server Members
|
|
|
|
(async () => {
|
2021-11-07 23:13:40 +00:00
|
|
|
await Actions.fetchAndUpdateMembers(q, ui, server);
|
2021-10-30 17:26:41 +00:00
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener('contextmenu', (e) => {
|
2021-11-07 16:50:30 +00:00
|
|
|
let contextMenu = createServerContextMenu(document, q, ui, controller, server);
|
2021-10-30 17:26:41 +00:00
|
|
|
document.body.appendChild(contextMenu);
|
|
|
|
let relativeTo = { x: e.pageX, y: e.pageY };
|
|
|
|
ElementsUtil.alignContextElement(contextMenu, relativeTo, { top: 'centerY', left: 'centerX' });
|
|
|
|
});
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|