2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const render = require("../render");
|
2017-12-06 12:07:43 +00:00
|
|
|
const condensed = require("../condensed");
|
2017-05-18 20:08:54 +00:00
|
|
|
const chat = $("#chat");
|
2018-07-08 12:18:17 +00:00
|
|
|
const {Vue, vueApp, findChannel} = require("../vue");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
const channel = findChannel(data.chan);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
if (!channel) {
|
|
|
|
return;
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
channel.channel.messages.unshift(...data.messages);
|
2017-09-12 12:40:26 +00:00
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
Vue.nextTick(() => {
|
|
|
|
// restore scroll position
|
|
|
|
const position = chan.height() - heightOld;
|
|
|
|
scrollable.finish().scrollTop(position);
|
|
|
|
});
|
2017-09-12 12:40:26 +00:00
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
if (data.messages.length !== 100) {
|
|
|
|
scrollable.find(".show-more").removeClass("show");
|
|
|
|
}
|
2017-09-12 12:40:26 +00:00
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
// Swap button text back from its alternative label
|
|
|
|
const showMoreBtn = scrollable.find(".show-more button");
|
|
|
|
swapText(showMoreBtn);
|
|
|
|
showMoreBtn.prop("disabled", false);
|
2017-09-12 12:40:26 +00:00
|
|
|
|
2018-07-08 12:18:17 +00:00
|
|
|
return;
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-12-06 12:07:43 +00:00
|
|
|
// Join duplicate condensed messages together
|
|
|
|
const condensedDuplicate = chan.find(".msg.condensed + .msg.condensed");
|
|
|
|
|
|
|
|
if (condensedDuplicate) {
|
|
|
|
const condensedCorrect = condensedDuplicate.prev();
|
|
|
|
|
|
|
|
condensed.updateText(condensedCorrect, condensed.getStoredTypes(condensedDuplicate));
|
|
|
|
|
|
|
|
condensedCorrect
|
|
|
|
.append(condensedDuplicate.find(".msg"))
|
|
|
|
.toggleClass("closed", condensedDuplicate.hasClass("closed"));
|
|
|
|
|
|
|
|
condensedDuplicate.remove();
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|
2017-09-05 16:10:48 +00:00
|
|
|
|
2018-04-15 03:02:35 +00:00
|
|
|
chat.on("click", ".show-more button", function() {
|
2017-09-12 13:05:40 +00:00
|
|
|
const self = $(this);
|
2017-09-19 15:01:02 +00:00
|
|
|
const lastMessage = self.closest(".chat").find(".msg:not(.condensed)").first();
|
2017-09-12 13:05:40 +00:00
|
|
|
let lastMessageId = -1;
|
|
|
|
|
|
|
|
if (lastMessage.length > 0) {
|
2018-01-30 09:38:33 +00:00
|
|
|
lastMessageId = parseInt(lastMessage.prop("id").replace("msg-", ""), 10);
|
2017-09-05 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 03:02:35 +00:00
|
|
|
// Swap button text with its alternative label
|
|
|
|
swapText(self);
|
|
|
|
self.prop("disabled", true);
|
2017-09-05 16:10:48 +00:00
|
|
|
|
|
|
|
socket.emit("more", {
|
|
|
|
target: self.data("id"),
|
2017-11-15 06:35:15 +00:00
|
|
|
lastId: lastMessageId,
|
2017-09-05 16:10:48 +00:00
|
|
|
});
|
|
|
|
});
|
2018-04-15 03:02:35 +00:00
|
|
|
|
|
|
|
// Given a button, swap its text with the content of `data-alt-text`
|
|
|
|
function swapText(btn) {
|
|
|
|
const altText = btn.data("alt-text");
|
|
|
|
btn.data("alt-text", btn.text()).text(altText);
|
|
|
|
}
|