Commit 22573cf7 authored by JC Brand's avatar JC Brand

Fixes #1637 aned #2130

parent 1269cb72
...@@ -240,11 +240,7 @@ converse.plugins.add('converse-chatview', { ...@@ -240,11 +240,7 @@ converse.plugins.add('converse-chatview', {
render () { render () {
const result = tpl_chatbox( const result = tpl_chatbox(
Object.assign( Object.assign(this.model.toJSON(), {'markScrolled': () => this.markScrolled()})
this.model.toJSON(), {
'markScrolled': () => this.markScrolled()
}
)
); );
render(result, this.el); render(result, this.el);
this.content = this.el.querySelector('.chat-content'); this.content = this.el.querySelector('.chat-content');
...@@ -1099,6 +1095,14 @@ converse.plugins.add('converse-chatview', { ...@@ -1099,6 +1095,14 @@ converse.plugins.add('converse-chatview', {
if (is_at_bottom) { if (is_at_bottom) {
scrolled = false; scrolled = false;
this.onScrolledDown(); this.onScrolledDown();
} else if (this.content.scrollTop === 0) {
/**
* Triggered once the chat's message area has been scrolled to the top
* @event _converse#chatBoxScrolledUp
* @property { _converse.ChatBoxView | _converse.ChatRoomView } view
* @example _converse.api.listen.on('chatBoxScrolledUp', obj => { ... });
*/
api.trigger('chatBoxScrolledUp', this);
} }
u.safeSave(this.model, { u.safeSave(this.model, {
'scrolled': scrolled, 'scrolled': scrolled,
......
...@@ -5,60 +5,29 @@ ...@@ -5,60 +5,29 @@
* @copyright 2020, the Converse.js contributors * @copyright 2020, the Converse.js contributors
* @license Mozilla Public License (MPLv2) * @license Mozilla Public License (MPLv2)
*/ */
import { converse } from "@converse/headless/converse-core"; import { api, converse } from "@converse/headless/converse-core";
import { debounce } from 'lodash-es'
converse.plugins.add('converse-mam-views', { converse.plugins.add('converse-mam-views', {
dependencies: ['converse-mam', 'converse-chatview', 'converse-muc-views'], dependencies: ['converse-mam', 'converse-chatview', 'converse-muc-views'],
overrides: { initialize () {
// Overrides mentioned here will be picked up by converse.js's api.listen.on('chatBoxScrolledUp', async view => {
// plugin architecture they will replace existing methods on the if (view.model.messages.length) {
// relevant objects or classes. const oldest_message = view.model.getOldestMessage();
//
// New functions which don't exist yet can also be added.
ChatBoxView: {
render () {
const result = this.__super__.render.apply(this, arguments);
if (!this.disable_mam) {
this.content.addEventListener('scroll', debounce(this.onScroll.bind(this), 100));
}
return result;
},
async onScroll () {
if (this.content.scrollTop === 0 && this.model.messages.length) {
const oldest_message = this.model.getOldestMessage();
if (oldest_message) { if (oldest_message) {
const by_jid = this.model.get('jid'); const by_jid = view.model.get('jid');
const stanza_id = oldest_message && oldest_message.get(`stanza_id ${by_jid}`); const stanza_id = oldest_message && oldest_message.get(`stanza_id ${by_jid}`);
this.addSpinner(); view.addSpinner();
if (stanza_id) { if (stanza_id) {
await this.model.fetchArchivedMessages({ await view.model.fetchArchivedMessages({'before': stanza_id});
'before': stanza_id
});
} else { } else {
await this.model.fetchArchivedMessages({ await view.model.fetchArchivedMessages({'end': oldest_message.get('time')});
'end': oldest_message.get('time')
});
}
this.clearSpinner();
} }
view.clearSpinner();
} }
} }
}, });
ChatRoomView: {
renderChatArea () {
const result = this.__super__.renderChatArea.apply(this, arguments);
if (!this.disable_mam) {
this.content.addEventListener('scroll', debounce(this.onScroll.bind(this), 100));
}
return result;
}
}
} }
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment