2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const render = require("../render");
|
|
|
|
const chat = $("#chat");
|
|
|
|
|
|
|
|
socket.on("more", function(data) {
|
2017-08-24 13:50:47 +00:00
|
|
|
let chan = chat.find("#chan-" + data.chan);
|
|
|
|
const type = chan.data("type");
|
|
|
|
chan = chan.find(".messages");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
// get the scrollable wrapper around messages
|
|
|
|
const scrollable = chan.closest(".chat");
|
2017-06-29 09:15:06 +00:00
|
|
|
const heightOld = chan.height() - scrollable.scrollTop();
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-08-23 09:32:59 +00:00
|
|
|
// If there are no more messages to show, just hide the button and do nothing else
|
|
|
|
if (!data.messages.length) {
|
|
|
|
scrollable.find(".show-more").removeClass("show");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
// Remove the date-change marker we put at the top, because it may
|
|
|
|
// not actually be a date change now
|
|
|
|
const children = $(chan).children();
|
|
|
|
if (children.eq(0).hasClass("date-marker-container")) { // Check top most child
|
|
|
|
children.eq(0).remove();
|
|
|
|
} else if (children.eq(1).hasClass("date-marker-container")) {
|
|
|
|
// The unread-marker could be at index 0, which will cause the date-marker to become "stuck"
|
|
|
|
children.eq(1).remove();
|
2017-06-22 20:08:36 +00:00
|
|
|
} else if (children.eq(0).hasClass("condensed") && children.eq(0).children(".date-marker-container").eq(0).hasClass("date-marker-container")) {
|
|
|
|
children.eq(0).children(".date-marker-container").eq(0).remove();
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the older messages
|
2017-08-28 20:06:28 +00:00
|
|
|
const documentFragment = render.buildChannelMessages($(document.createDocumentFragment()), data.chan, type, data.messages);
|
2017-09-12 12:40:26 +00:00
|
|
|
chan.prepend(documentFragment);
|
|
|
|
|
|
|
|
// Move unread marker to correct spot if needed
|
|
|
|
const unreadMarker = chan.find(".unread-marker");
|
|
|
|
const firstUnread = unreadMarker.data("unread-id");
|
|
|
|
|
|
|
|
if (firstUnread > 0) {
|
|
|
|
let first = chan.find("#msg-" + firstUnread);
|
|
|
|
|
|
|
|
if (!first.length) {
|
|
|
|
chan.prepend(unreadMarker);
|
|
|
|
} else {
|
|
|
|
const parent = first.parent();
|
|
|
|
|
|
|
|
if (parent.hasClass("condensed")) {
|
|
|
|
first = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
unreadMarker.data("unread-id", 0);
|
|
|
|
|
|
|
|
first.before(unreadMarker);
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
// restore scroll position
|
|
|
|
const position = chan.height() - heightOld;
|
2017-07-10 16:16:20 +00:00
|
|
|
scrollable.finish().scrollTop(position);
|
|
|
|
|
|
|
|
// We have to do this hack due to smooth scrolling in browsers,
|
|
|
|
// as scrollTop does not apply correctly
|
|
|
|
if (window.requestAnimationFrame) {
|
|
|
|
window.requestAnimationFrame(() => scrollable.scrollTop(position));
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (data.messages.length !== 100) {
|
|
|
|
scrollable.find(".show-more").removeClass("show");
|
|
|
|
}
|
|
|
|
|
2017-08-09 11:18:37 +00:00
|
|
|
scrollable.find(".show-more-button")
|
|
|
|
.text("Show older messages")
|
|
|
|
.prop("disabled", false);
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|
2017-09-05 16:10:48 +00:00
|
|
|
|
|
|
|
chat.on("click", ".show-more-button", function() {
|
2017-09-12 13:05:40 +00:00
|
|
|
const self = $(this);
|
|
|
|
const lastMessage = self.closest(".chat").find(".msg").first();
|
|
|
|
let lastMessageId = -1;
|
|
|
|
|
|
|
|
if (lastMessage.length > 0) {
|
|
|
|
lastMessageId = parseInt(lastMessage[0].id.replace("msg-", ""), 10);
|
2017-09-05 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
.text("Loading older messages…")
|
|
|
|
.prop("disabled", true);
|
|
|
|
|
|
|
|
socket.emit("more", {
|
|
|
|
target: self.data("id"),
|
|
|
|
lastId: lastMessageId
|
|
|
|
});
|
|
|
|
});
|