thelounge/client/components/Chat.vue

129 lines
2.7 KiB
Vue
Raw Normal View History

<template>
2018-07-08 15:42:54 +02:00
<div
id="chat-container"
class="window">
<div
id="chat"
ref="chat">
<div
:id="'chan-' + channel.id"
:class="[channel.type, 'chan', 'active']"
:data-id="channel.id"
:data-type="channel.type"
:aria-label="channel.name"
role="tabpanel"
>
<div class="header">
<button
class="lt"
aria-label="Toggle channel list"/>
<span class="title">{{ channel.name }}</span>
<span
:title="channel.topic"
class="topic"
v-html="$options.filters.parse(channel.topic)"/>
<button
class="menu"
aria-label="Open the context menu"
/>
<span
v-if="channel.type === 'channel'"
class="rt-tooltip tooltipped tooltipped-w"
aria-label="Toggle user list">
<button
class="rt"
aria-label="Toggle user list"/>
</span>
</div>
<div class="chat-content">
<div class="chat">
<div
v-if="channel.messages.length > 0"
2018-07-08 15:42:54 +02:00
ref="loadMoreButton"
:disabled="channel.historyLoading"
class="show-more show"
@click="onShowMoreClick"
>
<button
v-if="channel.historyLoading"
class="btn">Loading</button>
<button
2018-07-08 15:42:54 +02:00
v-else
class="btn">Show older messages</button>
</div>
<MessageList :channel="channel"/>
</div>
<ChatUserList
v-if="channel.type === 'channel'"
:channel="channel"/>
</div>
</div>
</div>
<div id="connection-error"/>
2018-07-08 15:42:54 +02:00
<ChatInput
:network="network"
:channel="channel"/>
</div>
</template>
<script>
2018-07-08 15:42:54 +02:00
require("intersection-observer");
const socket = require("../js/socket");
import MessageList from "./MessageList.vue";
2018-07-08 15:42:54 +02:00
import ChatInput from "./ChatInput.vue";
import ChatUserList from "./ChatUserList.vue";
export default {
name: "Chat",
components: {
MessageList,
2018-07-08 15:42:54 +02:00
ChatInput,
ChatUserList,
},
props: {
network: Object,
channel: Object,
},
2018-07-08 15:42:54 +02:00
created() {
if (window.IntersectionObserver) {
this.historyObserver = new window.IntersectionObserver(loadMoreHistory, {
root: this.$refs.chat,
});
}
},
mounted() {
if (this.historyObserver) {
this.historyObserver.observe(this.$refs.loadMoreButton);
}
},
destroyed() {
if (this.historyObserver) {
2018-07-08 16:57:02 +02:00
this.historyObserver.disconnect();
2018-07-08 15:42:54 +02:00
}
},
methods: {
2018-07-08 15:42:54 +02:00
onShowMoreClick() {
let lastMessage = this.channel.messages[0];
lastMessage = lastMessage ? lastMessage.id : -1;
this.$set(this.channel, "historyLoading", true);
socket.emit("more", {
target: this.channel.id,
lastId: lastMessage,
});
},
},
};
2018-07-08 15:42:54 +02:00
function loadMoreHistory(entries) {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
return;
}
entry.target.click();
});
}
</script>