gamja/components/buffer-list.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

import * as irc from "../lib/irc.js";
import { html, Component } from "../lib/index.js";
import { BufferType, Unread, getBufferURL, getServerName } from "../state.js";
2021-01-22 05:43:47 -05:00
function BufferItem(props) {
function handleClick(event) {
event.preventDefault();
props.onClick();
}
2021-10-17 13:33:02 -04:00
function handleMouseDown(event) {
if (event.button === 1) { // middle click
event.preventDefault();
props.onClose();
}
}
2021-06-10 12:11:11 -04:00
let name = props.buffer.name;
2020-06-26 04:35:38 -04:00
if (props.buffer.type == BufferType.SERVER) {
name = getServerName(props.server, props.bouncerNetwork, props.isBouncer);
}
2021-06-10 12:11:11 -04:00
let classes = ["type-" + props.buffer.type];
if (props.active) {
classes.push("active");
}
2020-06-24 10:56:28 -04:00
if (props.buffer.unread != Unread.NONE) {
classes.push("unread-" + props.buffer.unread);
2020-06-24 10:56:28 -04:00
}
return html`
<li class="${classes.join(" ")}">
2021-10-17 13:33:02 -04:00
<a
href=${getBufferURL(props.buffer)}
onClick=${handleClick}
onMouseDown=${handleMouseDown}
>${name}</a>
</li>
`;
}
2020-06-26 08:40:27 -04:00
export default function BufferList(props) {
2021-06-10 12:11:11 -04:00
let items = Array.from(props.buffers.values()).map((buf) => {
let server = props.servers.get(buf.server);
2021-06-10 12:11:11 -04:00
let bouncerNetwork = null;
let bouncerNetID = server.bouncerNetID;
if (bouncerNetID) {
bouncerNetwork = props.bouncerNetworks.get(bouncerNetID);
}
return html`
2021-05-31 11:34:36 -04:00
<${BufferItem}
key=${buf.id}
buffer=${buf}
server=${server}
2021-05-31 11:34:36 -04:00
isBouncer=${props.isBouncer}
bouncerNetwork=${bouncerNetwork}
onClick=${() => props.onBufferClick(buf)}
2021-10-17 13:33:02 -04:00
onClose=${() => props.onBufferClose(buf)}
2021-05-31 11:34:36 -04:00
active=${props.activeBuffer == buf.id}
/>
`;
});
return html`<ul>${items}</ul>`;
}