2018-07-08 17:22:01 +00:00
|
|
|
<template>
|
2018-07-10 20:29:53 +00:00
|
|
|
<aside
|
|
|
|
ref="userlist"
|
|
|
|
class="userlist"
|
|
|
|
>
|
2018-07-08 17:22:01 +00:00
|
|
|
<div class="count">
|
|
|
|
<input
|
2018-07-10 20:29:53 +00:00
|
|
|
ref="input"
|
2018-07-08 17:22:01 +00:00
|
|
|
:placeholder="channel.users.length + ' user' + (channel.users.length === 1 ? '' : 's')"
|
2018-07-08 20:08:08 +00:00
|
|
|
v-model="userSearchInput"
|
2018-07-08 17:22:01 +00:00
|
|
|
type="search"
|
|
|
|
class="search"
|
|
|
|
aria-label="Search among the user list"
|
2018-07-08 19:02:36 +00:00
|
|
|
tabindex="-1"
|
2018-07-10 20:29:53 +00:00
|
|
|
@keydown.up="navigateUserList(-1)"
|
|
|
|
@keydown.down="navigateUserList(1)"
|
|
|
|
@keydown.page-up="navigateUserList(-10)"
|
|
|
|
@keydown.page-down="navigateUserList(10)"
|
|
|
|
@keydown.enter="selectUser"
|
2018-07-08 19:02:36 +00:00
|
|
|
>
|
2018-07-08 17:22:01 +00:00
|
|
|
</div>
|
|
|
|
<div class="names">
|
|
|
|
<div
|
|
|
|
v-for="(users, mode) in groupedUsers"
|
|
|
|
:key="mode"
|
2018-07-08 19:02:36 +00:00
|
|
|
:class="['user-mode', getModeClass(mode)]"
|
|
|
|
>
|
|
|
|
<template v-if="userSearchInput.length > 0">
|
|
|
|
<UsernameFiltered
|
|
|
|
v-for="user in users"
|
2018-07-11 07:33:11 +00:00
|
|
|
:on-hover="hoverUser"
|
2018-07-08 19:02:36 +00:00
|
|
|
:key="user.original.nick"
|
2018-07-10 20:29:53 +00:00
|
|
|
:active="user.original === activeUser"
|
2018-07-08 19:02:36 +00:00
|
|
|
:user="user"/>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<Username
|
|
|
|
v-for="user in users"
|
2018-07-11 07:33:11 +00:00
|
|
|
:on-hover="hoverUser"
|
2018-07-08 19:02:36 +00:00
|
|
|
:key="user.nick"
|
2018-07-10 20:29:53 +00:00
|
|
|
:active="user === activeUser"
|
2018-07-08 19:02:36 +00:00
|
|
|
:user="user"/>
|
|
|
|
</template>
|
2018-07-08 17:22:01 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</aside>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-07-08 19:02:36 +00:00
|
|
|
const fuzzy = require("fuzzy");
|
2018-07-08 17:22:01 +00:00
|
|
|
import Username from "./Username.vue";
|
2018-07-08 19:02:36 +00:00
|
|
|
import UsernameFiltered from "./UsernameFiltered.vue";
|
2018-07-08 17:22:01 +00:00
|
|
|
|
|
|
|
const modes = {
|
|
|
|
"~": "owner",
|
|
|
|
"&": "admin",
|
|
|
|
"!": "admin",
|
|
|
|
"@": "op",
|
|
|
|
"%": "half-op",
|
|
|
|
"+": "voice",
|
|
|
|
"": "normal",
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "ChatUserList",
|
|
|
|
components: {
|
|
|
|
Username,
|
2018-07-08 19:02:36 +00:00
|
|
|
UsernameFiltered,
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
props: {
|
|
|
|
channel: Object,
|
|
|
|
},
|
2018-07-08 20:08:08 +00:00
|
|
|
data() {
|
2018-07-08 19:02:36 +00:00
|
|
|
return {
|
|
|
|
userSearchInput: "",
|
2018-07-10 20:29:53 +00:00
|
|
|
activeUser: null,
|
2018-07-08 19:02:36 +00:00
|
|
|
};
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
computed: {
|
2018-07-10 23:25:21 +00:00
|
|
|
// filteredUsers is computed, to avoid unnecessary filtering
|
|
|
|
// as it is shared between filtering and keybindings.
|
|
|
|
filteredUsers() {
|
|
|
|
return fuzzy.filter(
|
|
|
|
this.userSearchInput,
|
|
|
|
this.channel.users,
|
|
|
|
{
|
|
|
|
pre: "<b>",
|
|
|
|
post: "</b>",
|
|
|
|
extract: (u) => u.nick,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
groupedUsers() {
|
|
|
|
const groups = {};
|
|
|
|
|
2018-07-08 19:02:36 +00:00
|
|
|
if (this.userSearchInput) {
|
2018-07-10 23:25:21 +00:00
|
|
|
const result = this.filteredUsers;
|
2018-07-08 19:02:36 +00:00
|
|
|
|
|
|
|
for (const user of result) {
|
|
|
|
if (!groups[user.original.mode]) {
|
|
|
|
groups[user.original.mode] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
groups[user.original.mode].push(user);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const user of this.channel.users) {
|
|
|
|
if (!groups[user.mode]) {
|
|
|
|
groups[user.mode] = [user];
|
|
|
|
} else {
|
|
|
|
groups[user.mode].push(user);
|
|
|
|
}
|
2018-07-08 17:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getModeClass(mode) {
|
|
|
|
return modes[mode];
|
|
|
|
},
|
2018-07-10 20:29:53 +00:00
|
|
|
selectUser() {
|
|
|
|
// Simulate a click on the active user to open the context menu.
|
|
|
|
// Coordinates are provided to position the menu correctly.
|
|
|
|
if (!this.activeUser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const el = this.$refs.userlist.querySelector(".active");
|
|
|
|
const rect = el.getBoundingClientRect();
|
|
|
|
const ev = new MouseEvent("click", {
|
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
clientX: rect.x,
|
|
|
|
clientY: rect.y + rect.height,
|
|
|
|
});
|
|
|
|
el.dispatchEvent(ev);
|
|
|
|
},
|
2018-07-10 23:25:21 +00:00
|
|
|
hoverUser(user) {
|
|
|
|
this.activeUser = user;
|
|
|
|
},
|
2018-07-10 20:29:53 +00:00
|
|
|
navigateUserList(direction) {
|
|
|
|
let users = this.channel.users;
|
|
|
|
|
2018-07-10 23:25:21 +00:00
|
|
|
// Only using filteredUsers when we have to avoids filtering when it's not needed
|
2018-07-10 20:29:53 +00:00
|
|
|
if (this.userSearchInput) {
|
2018-07-10 23:25:21 +00:00
|
|
|
users = this.filteredUsers.map((result) => result.original);
|
2018-07-10 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out if there's no users to select
|
|
|
|
if (!users.length) {
|
|
|
|
this.activeUser = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let currentIndex = users.indexOf(this.activeUser);
|
|
|
|
|
|
|
|
// If there's no active user select the first or last one depending on direction
|
|
|
|
if (!this.activeUser || currentIndex === -1) {
|
|
|
|
this.activeUser = direction ? users[0] : users[users.length - 1];
|
|
|
|
this.scrollToActiveUser();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.activeUser = users[currentIndex];
|
|
|
|
this.scrollToActiveUser();
|
|
|
|
},
|
|
|
|
scrollToActiveUser() {
|
|
|
|
// Scroll the list if needed after the active class is applied
|
|
|
|
this.$nextTick(() => {
|
|
|
|
const el = this.$refs.userlist.querySelector(".active");
|
|
|
|
el.scrollIntoView({block: "nearest", inline: "nearest"});
|
|
|
|
});
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|