2021-06-03 12:10:30 +02:00
|
|
|
import './message-history';
|
2021-03-24 11:59:09 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2021-06-03 16:29:31 +02:00
|
|
|
import { _converse, api } from '@converse/headless/core';
|
2021-04-14 22:56:59 +02:00
|
|
|
import { html } from 'lit';
|
2021-06-17 10:43:08 +02:00
|
|
|
import { markScrolled } from './utils.js';
|
2020-05-15 14:33:31 +02:00
|
|
|
|
2021-06-07 19:26:16 +02:00
|
|
|
import './styles/chat-content.scss';
|
|
|
|
|
2021-06-03 15:32:39 +02:00
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
export default class ChatContent extends CustomElement {
|
2020-05-15 14:33:31 +02:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
2021-02-09 11:14:06 +01:00
|
|
|
jid: { type: String }
|
2021-02-08 11:27:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback () {
|
|
|
|
super.connectedCallback();
|
2021-06-17 10:43:08 +02:00
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback () {
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this.removeEventListener('scroll', markScrolled);
|
|
|
|
}
|
2021-06-03 12:10:30 +02:00
|
|
|
|
2021-06-17 10:43:08 +02:00
|
|
|
initialize () {
|
2021-02-09 11:14:06 +01:00
|
|
|
this.model = _converse.chatboxes.get(this.jid);
|
2021-06-03 12:10:30 +02:00
|
|
|
this.listenTo(this.model, 'change:hidden_occupants', this.requestUpdate);
|
2021-02-09 11:14:06 +01:00
|
|
|
this.listenTo(this.model.messages, 'add', this.requestUpdate);
|
|
|
|
this.listenTo(this.model.messages, 'change', this.requestUpdate);
|
|
|
|
this.listenTo(this.model.messages, 'remove', this.requestUpdate);
|
2021-06-03 12:10:30 +02:00
|
|
|
this.listenTo(this.model.messages, 'rendered', this.requestUpdate);
|
2021-02-09 11:14:06 +01:00
|
|
|
this.listenTo(this.model.messages, 'reset', this.requestUpdate);
|
|
|
|
this.listenTo(this.model.notifications, 'change', this.requestUpdate);
|
2021-06-02 23:52:16 +02:00
|
|
|
this.listenTo(this.model.ui, 'change', this.requestUpdate);
|
2021-06-17 10:43:08 +02:00
|
|
|
this.listenTo(this.model.ui, 'change:scrolled', this.scrollDown);
|
2021-06-03 12:10:30 +02:00
|
|
|
|
2021-02-09 11:14:06 +01:00
|
|
|
if (this.model.occupants) {
|
|
|
|
this.listenTo(this.model.occupants, 'change', this.requestUpdate);
|
2020-05-15 14:33:31 +02:00
|
|
|
}
|
2021-06-17 10:43:08 +02:00
|
|
|
this.addEventListener('scroll', markScrolled);
|
2020-05-15 14:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2021-06-07 19:26:16 +02:00
|
|
|
// This element has "flex-direction: reverse", so elements here are
|
|
|
|
// shown in reverse order.
|
2020-05-15 14:33:31 +02:00
|
|
|
return html`
|
2021-06-07 19:26:16 +02:00
|
|
|
<div class="chat-content__notifications">${this.model.getNotificationsText()}</div>
|
2020-05-15 14:33:31 +02:00
|
|
|
<converse-message-history
|
2021-02-09 11:14:06 +01:00
|
|
|
.model=${this.model}
|
|
|
|
.messages=${[...this.model.messages.models]}>
|
2020-05-15 14:33:31 +02:00
|
|
|
</converse-message-history>
|
2021-06-07 19:26:16 +02:00
|
|
|
${ this.model.ui?.get('chat-content-spinner-top') ? html`<span class="spinner fa fa-spinner centered"></span>` : '' }
|
2020-05-15 14:33:31 +02:00
|
|
|
`;
|
|
|
|
}
|
2021-02-09 11:14:06 +01:00
|
|
|
|
2021-06-07 19:26:16 +02:00
|
|
|
scrollDown () {
|
2021-06-17 10:43:08 +02:00
|
|
|
if (this.model.ui.get('scrolled')) {
|
2021-06-03 16:29:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-09 11:14:06 +01:00
|
|
|
if (this.scrollTo) {
|
|
|
|
const behavior = this.scrollTop ? 'smooth' : 'auto';
|
2021-06-07 19:26:16 +02:00
|
|
|
this.scrollTo({ 'top': 0, behavior });
|
2021-02-09 11:14:06 +01:00
|
|
|
} else {
|
2021-06-07 19:26:16 +02:00
|
|
|
this.scrollTop = 0;
|
2021-02-09 11:14:06 +01:00
|
|
|
}
|
2021-06-03 12:10:30 +02:00
|
|
|
/**
|
|
|
|
* Triggered once the converse-chat-content element has been scrolled down to the bottom.
|
|
|
|
* @event _converse#chatBoxScrolledDown
|
|
|
|
* @type {object}
|
|
|
|
* @property { _converse.ChatBox | _converse.ChatRoom } chatbox - The chat model
|
|
|
|
* @example _converse.api.listen.on('chatBoxScrolledDown', obj => { ... });
|
|
|
|
*/
|
|
|
|
api.trigger('chatBoxScrolledDown', { 'chatbox': this.model });
|
2021-02-09 11:14:06 +01:00
|
|
|
}
|
2020-05-15 14:33:31 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
api.elements.define('converse-chat-content', ChatContent);
|