feathermost/js/view/sidebar.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

function populateServerSelectionList() {
const servers = localstorage_credentials.getServers();
let nodes = [];
for (let server of servers) {
const li = document.createElement("li");
const endpoint = humanReadableEndpoint(server.endpoint);
li.innerText = `${server.login_id}@${endpoint} `;
const logoutButton = document.createElement("button");
logoutButton.className = "logout";
logoutButton.innerText = "Log out";
logoutButton.addEventListener("click", e => logOut(server.endpoint, e.currentTarget));
li.appendChild(logoutButton);
nodes.push(li);
}
byId("server_selection_list").innerHTML = "";
byId("server_selection_list").append(...nodes);
}
function populateChannelList() {
async function addChannelItems(client) {
2022-06-17 23:04:16 +02:00
const {teams, channels, unread} = await client.channelStore.get();
const users = await client.getUsers();
2022-06-17 23:04:16 +02:00
let nodes = [];
2022-06-17 23:26:43 +02:00
let sortedChannels = Object.values(channels).sort((a, b) =>
b["last_post_at"] - a["last_post_at"]
);
for (const channel of sortedChannels) {
2022-06-17 23:04:16 +02:00
const team = teams[channel["team_id"]];
const chanUnread = unread[channel.id].unread;
const chanMentions = unread[channel.id].mentions;
const li = document.createElement("li");
const a = document.createElement("a");
a.href = "javascript:void(0)";
const titleAndElements = channelNameElements(team, channel, client.me["id"], users);
2022-06-17 23:04:16 +02:00
if (!titleAndElements) continue;
a.title = titleAndElements[0];
a.append(...titleAndElements[1]);
a.append(document.createTextNode(" "));
const msgCountGem = document.createElement("span");
msgCountGem.className = "msg_count_gem";
msgCountGem.innerText = chanMentions;
if (chanMentions > 0) msgCountGem.style.display = "inline-block";
a.append(msgCountGem);
a.addEventListener("click", () => switchToChannel(client, team, channel));
li.appendChild(a);
li.dataset["id"] = channel.id;
li.dataset["server"] = client.endpoint;
2022-06-17 23:26:43 +02:00
const curChan = currentChannel();
if (curChan.endpoint === client.endpoint && curChan.channel_id === channel.id) {
li.className = "active";
}
if (chanUnread > 0) {
li.className += " unread";
}
2022-06-17 23:04:16 +02:00
nodes.push(li);
}
2022-06-17 23:04:16 +02:00
byId("channel_list").append(...nodes);
}
byId("channel_list").innerHTML = "";
const endpoints = localstorage_credentials.getServers().map(server => server.endpoint);
for (let client of mm_client.getOrCreateMultiple(endpoints)) {
addChannelItems(client);
}
}
2022-06-17 23:04:16 +02:00
function updateUnreadCount(el, unread, mentions) {
2022-06-08 23:24:59 +02:00
let msgCountGem = el.querySelector('.msg_count_gem');
if (mentions > 0) {
msgCountGem.style.display = "inline-block";
msgCountGem.innerText = mentions;
} else {
msgCountGem.style.display = "none";
}
2022-06-17 23:04:16 +02:00
if (unread > 0) {
2022-06-08 23:24:59 +02:00
addClass(el, "unread");
} else {
removeClass(el, "unread");
}
}
2022-06-17 23:04:16 +02:00
pubsub.subscribe("CHANNEL_UNREAD_UPDATED", ({endpoint, channel_id, unread, mentions}) => {
for (let el of byId("channel_list").childNodes) {
if (el.dataset["server"] == endpoint && el.dataset["id"] == channel_id) {
updateUnreadCount(el, unread, mentions);
2022-06-08 23:24:59 +02:00
}
}
});
2022-06-17 23:04:16 +02:00
pubsub.subscribe("CHANNELS_RELOADED", () => {
populateChannelList();
});
2022-06-17 23:26:43 +02:00
pubsub.subscribe("CHANNEL_LAST_POST_TIME_UPDATED", () => {
populateChannelList();
});