cordis/client/webapp/elements/server-list-server.ts

114 lines
4.7 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 BaseElements from './require/base-elements';
import ElementsUtil from './require/elements-util';
import ClientController from '../client-controller';
import { CacheServerData, ServerMetaData } from '../data-types';
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';
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) {
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 () => {
let serverData: ServerMetaData | CacheServerData;
2021-10-30 17:26:41 +00:00
try {
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);
(q.$$$(element, 'img') as HTMLImageElement).src = serverIconSrc;
2021-10-30 17:26:41 +00:00
} catch (e) {
LOG.error('Error fetching server icon', e);
(q.$$$(element, 'img') as HTMLImageElement).src = './img/error.png';
return;
2021-10-30 17:26:41 +00:00
}
element.setAttribute('meta-name', serverData.name);
let contextElement = q.create({ class: 'context', content: {
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 () => {
Q.clearChildren(q.$$$(contextElement, '.content'));
q.$$$(contextElement, '.content').appendChild(q.create({ class: 'name', content: element.getAttribute('meta-name') }));
document.body.appendChild(contextElement);
ElementsUtil.alignContextElement(contextElement, element, { left: 'right', centerY: 'centerY' });
(async () => {
let connection = await server.fetchConnectionInfo();
let connectionElement = q.create({ class: 'connection ' + connection.status, content: [
{ class: 'status-circle' },
{ class: 'display-name', content: connection.displayName }
] });
q.$$$(contextElement, '.content').appendChild(connectionElement);
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) => {
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;
}