hardlounge/client/components/ChatUserList.vue

256 lines
5.9 KiB
Vue
Raw Permalink Normal View History

<template>
<aside
ref="userlist"
class="userlist"
2021-06-22 05:26:43 +00:00
:aria-label="'User list for ' + channel.name"
@mouseleave="removeHoverUser"
>
<div class="count">
<input
ref="input"
:value="userSearchInput"
2019-07-17 09:33:59 +00:00
:placeholder="
channel.users.length + ' user' + (channel.users.length === 1 ? '' : 's')
"
type="search"
class="search"
aria-label="Search among the user list"
2018-07-08 19:02:36 +00:00
tabindex="-1"
@input="setUserSearchInput"
@keydown.up="navigateUserList($event, -1)"
@keydown.down="navigateUserList($event, 1)"
@keydown.page-up="navigateUserList($event, -10)"
@keydown.page-down="navigateUserList($event, 10)"
@keydown.enter="selectUser"
2019-07-17 09:33:59 +00:00
/>
</div>
<div class="names">
<div
v-for="(users, mode) in groupedUsers"
:key="mode"
:class="['user-mode', getModeClass(String(mode))]"
>
2018-07-08 19:02:36 +00:00
<template v-if="userSearchInput.length > 0">
<!-- eslint-disable vue/no-v-text-v-html-on-component -->
<Username
2018-07-08 19:02:36 +00:00
v-for="user in users"
:key="user.original.nick + '-search'"
2018-07-29 17:57:14 +00:00
:on-hover="hoverUser"
:active="user.original === activeUser"
:user="user.original"
v-html="user.string"
/>
<!-- eslint-enable -->
2018-07-08 19:02:36 +00:00
</template>
<template v-else>
<Username
v-for="user in users"
:key="user.nick"
2018-07-29 17:57:14 +00:00
:on-hover="hoverUser"
:active="user === activeUser"
:user="user"
/>
2018-07-08 19:02:36 +00:00
</template>
</div>
</div>
</aside>
</template>
<script lang="ts">
2019-11-16 17:24:03 +00:00
import {filter as fuzzyFilter} from "fuzzy";
import {computed, defineComponent, nextTick, PropType, ref} from "vue";
import type {UserInMessage} from "../../server/models/msg";
import type {ClientChan, ClientUser} from "../js/types";
import Username from "./Username.vue";
const modes = {
"~": "owner",
"&": "admin",
"!": "admin",
"@": "op",
"%": "half-op",
"+": "voice",
"": "normal",
};
export default defineComponent({
name: "ChatUserList",
components: {
Username,
},
props: {
channel: {type: Object as PropType<ClientChan>, required: true},
},
setup(props) {
const userSearchInput = ref("");
const activeUser = ref<UserInMessage | null>();
const userlist = ref<HTMLDivElement>();
const filteredUsers = computed(() => {
if (!userSearchInput.value) {
return;
}
return fuzzyFilter(userSearchInput.value, props.channel.users, {
2019-07-17 09:33:59 +00:00
pre: "<b>",
post: "</b>",
extract: (u) => u.nick,
});
});
const groupedUsers = computed(() => {
const groups = {};
if (userSearchInput.value && filteredUsers.value) {
const result = filteredUsers.value;
2018-07-08 19:02:36 +00:00
for (const user of result) {
const mode = user.original.modes[0] || "";
if (!groups[mode]) {
groups[mode] = [];
2018-07-08 19:02:36 +00:00
}
// Prepend user mode to search result
user.string = mode + user.string;
groups[mode].push(user);
2018-07-08 19:02:36 +00:00
}
} else {
for (const user of props.channel.users) {
const mode = user.modes[0] || "";
if (!groups[mode]) {
groups[mode] = [user];
2018-07-08 19:02:36 +00:00
} else {
groups[mode].push(user);
2018-07-08 19:02:36 +00:00
}
}
}
return groups as {
[mode: string]: (ClientUser & {
original: UserInMessage;
string: string;
})[];
};
});
const setUserSearchInput = (e: Event) => {
userSearchInput.value = (e.target as HTMLInputElement).value;
};
const getModeClass = (mode: string) => {
return modes[mode] as typeof modes;
};
const selectUser = () => {
// Simulate a click on the active user to open the context menu.
// Coordinates are provided to position the menu correctly.
if (!activeUser.value || !userlist.value) {
return;
}
const el = userlist.value.querySelector(".active");
if (!el) {
return;
}
const rect = el.getBoundingClientRect();
const ev = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
2020-01-24 14:55:29 +00:00
clientX: rect.left,
clientY: rect.top + rect.height,
});
el.dispatchEvent(ev);
};
const hoverUser = (user: UserInMessage) => {
activeUser.value = user;
};
const removeHoverUser = () => {
activeUser.value = null;
};
const scrollToActiveUser = () => {
// Scroll the list if needed after the active class is applied
void nextTick(() => {
const el = userlist.value?.querySelector(".active");
el?.scrollIntoView({block: "nearest", inline: "nearest"});
});
};
const navigateUserList = (event: Event, direction: number) => {
// Prevent propagation to stop global keybind handler from capturing pagedown/pageup
// and redirecting it to the message list container for scrolling
event.stopImmediatePropagation();
event.preventDefault();
let users = props.channel.users;
2018-07-10 23:25:21 +00:00
// Only using filteredUsers when we have to avoids filtering when it's not needed
if (userSearchInput.value && filteredUsers.value) {
users = filteredUsers.value.map((result) => result.original);
}
// Bail out if there's no users to select
if (!users.length) {
activeUser.value = null;
return;
}
const abort = () => {
activeUser.value = direction ? users[0] : users[users.length - 1];
scrollToActiveUser();
};
// If there's no active user select the first or last one depending on direction
if (!activeUser.value) {
abort();
return;
}
let currentIndex = users.indexOf(activeUser.value as ClientUser);
if (currentIndex === -1) {
abort();
return;
}
currentIndex += direction;
// Wrap around the list if necessary. Normaly each loop iterates once at most,
// but might iterate more often if pgup or pgdown are used in a very short user list
while (currentIndex < 0) {
currentIndex += users.length;
}
while (currentIndex > users.length - 1) {
currentIndex -= users.length;
}
activeUser.value = users[currentIndex];
scrollToActiveUser();
};
return {
filteredUsers,
groupedUsers,
userSearchInput,
activeUser,
userlist,
setUserSearchInput,
getModeClass,
selectUser,
hoverUser,
removeHoverUser,
navigateUserList,
};
},
});
</script>