2017-07-06 06:16:01 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const options = require("./options");
|
2017-07-24 06:01:25 +00:00
|
|
|
const socket = require("./socket");
|
2017-07-06 06:16:01 +00:00
|
|
|
const templates = require("../views");
|
2017-11-23 17:33:19 +00:00
|
|
|
const chat = $("#chat");
|
2017-07-25 14:54:35 +00:00
|
|
|
const input = $("#input");
|
2017-09-24 01:59:39 +00:00
|
|
|
const Mousetrap = require("mousetrap");
|
2017-07-06 06:16:01 +00:00
|
|
|
|
|
|
|
module.exports = renderPreview;
|
|
|
|
|
|
|
|
function renderPreview(preview, msg) {
|
2017-07-21 05:28:51 +00:00
|
|
|
if (preview.type === "loading") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:57:15 +00:00
|
|
|
preview.shown = preview.shown && options.shouldOpenMessagePreview(preview.type);
|
|
|
|
|
2017-12-29 10:30:15 +00:00
|
|
|
const template = $(templates.msg_preview({preview: preview}));
|
|
|
|
const image = template.find("img:first");
|
|
|
|
|
|
|
|
if (image.length === 0) {
|
|
|
|
return appendPreview(preview, msg, template);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is an image in preview, wait for it to load before appending it to DOM
|
|
|
|
// This is done to prevent problems keeping scroll to the bottom while images load
|
|
|
|
image.on("load", () => appendPreview(preview, msg, template));
|
2018-02-07 15:40:15 +00:00
|
|
|
|
|
|
|
// If the image fails to load, remove it from DOM and still render the preview
|
|
|
|
if (preview.type === "link") {
|
|
|
|
image.on("abort error", () => {
|
|
|
|
image.parent().remove();
|
|
|
|
|
|
|
|
appendPreview(preview, msg, template);
|
|
|
|
});
|
|
|
|
}
|
2017-12-29 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function appendPreview(preview, msg, template) {
|
2017-07-21 10:22:59 +00:00
|
|
|
const escapedLink = preview.link.replace(/["\\]/g, "\\$&");
|
|
|
|
const previewContainer = msg.find(`.preview[data-url="${escapedLink}"]`);
|
|
|
|
|
|
|
|
// This is to fix a very rare case of rendering a preview twice
|
|
|
|
// This happens when a very large amount of messages is being sent to the client
|
|
|
|
// and they get queued, so the `preview` object on the server has time to load before
|
|
|
|
// it actually gets sent to the server, which makes the loaded preview sent twice,
|
|
|
|
// once in `msg` and another in `msg:preview`
|
|
|
|
if (!previewContainer.is(":empty")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-26 13:49:31 +00:00
|
|
|
const container = msg.closest(".messages");
|
|
|
|
const channelId = container.closest(".chan").data("id") || -1;
|
|
|
|
const activeChannelId = chat.find(".chan.active").data("id") || -2;
|
2017-11-23 17:33:19 +00:00
|
|
|
|
2017-07-21 10:22:59 +00:00
|
|
|
msg.find(`.text a[href="${escapedLink}"]`)
|
2017-07-06 07:35:54 +00:00
|
|
|
.first()
|
2017-07-21 10:22:59 +00:00
|
|
|
.after(templates.msg_preview_toggle({preview: preview}).trim());
|
2017-07-06 07:35:54 +00:00
|
|
|
|
2017-12-29 10:30:15 +00:00
|
|
|
previewContainer.append(template);
|
2017-07-06 06:16:01 +00:00
|
|
|
|
2017-11-23 17:33:19 +00:00
|
|
|
if (activeChannelId === channelId) {
|
|
|
|
container.trigger("keepToBottom");
|
|
|
|
}
|
2017-07-06 06:16:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 16:35:01 +00:00
|
|
|
$("#chat").on("click", ".text .toggle-button", function() {
|
2017-07-06 06:16:01 +00:00
|
|
|
const self = $(this);
|
|
|
|
const container = self.closest(".chat");
|
2017-07-20 05:58:04 +00:00
|
|
|
const content = self.closest(".content")
|
2017-07-06 07:35:54 +00:00
|
|
|
.find(`.preview[data-url="${self.data("url")}"] .toggle-content`);
|
2017-07-06 06:16:01 +00:00
|
|
|
const bottom = container.isScrollBottom();
|
|
|
|
|
2017-07-06 07:35:54 +00:00
|
|
|
self.toggleClass("opened");
|
2017-07-06 06:16:01 +00:00
|
|
|
content.toggleClass("show");
|
|
|
|
|
2017-07-24 06:01:25 +00:00
|
|
|
// Tell the server we're toggling so it remembers at page reload
|
|
|
|
// TODO Avoid sending many single events when using `/collapse` or `/expand`
|
|
|
|
// See https://github.com/thelounge/lounge/issues/1377
|
|
|
|
socket.emit("msg:preview:toggle", {
|
|
|
|
target: parseInt(self.closest(".chan").data("id"), 10),
|
2018-01-30 09:38:33 +00:00
|
|
|
msgId: parseInt(self.closest(".msg").prop("id").replace("msg-", ""), 10),
|
2017-07-24 06:01:25 +00:00
|
|
|
link: self.data("url"),
|
|
|
|
shown: content.hasClass("show"),
|
|
|
|
});
|
|
|
|
|
2017-07-06 06:16:01 +00:00
|
|
|
// If scrollbar was at the bottom before toggling the preview, keep it at the bottom
|
|
|
|
if (bottom) {
|
|
|
|
container.scrollBottom();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-12 06:55:41 +00:00
|
|
|
/* Image viewer */
|
|
|
|
|
2017-07-24 06:31:12 +00:00
|
|
|
const imageViewer = $("#image-viewer");
|
|
|
|
|
2017-12-24 23:58:01 +00:00
|
|
|
$("#windows").on("click", ".toggle-thumbnail", function(event, data = {}) {
|
2017-07-12 06:55:41 +00:00
|
|
|
const link = $(this);
|
|
|
|
|
2017-09-09 20:36:06 +00:00
|
|
|
// Passing `data`, specifically `data.pushState`, to not add the action to the
|
|
|
|
// history state if back or forward buttons were pressed.
|
|
|
|
openImageViewer(link, data);
|
2017-07-12 06:55:41 +00:00
|
|
|
|
|
|
|
// Prevent the link to open a new page since we're opening the image viewer,
|
2017-07-25 14:54:35 +00:00
|
|
|
// but keep it a link to allow for Ctrl/Cmd+click.
|
|
|
|
// By binding this event on #chat we prevent input gaining focus after clicking.
|
2017-07-12 06:55:41 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-09-09 20:36:06 +00:00
|
|
|
imageViewer.on("click", function(event, data = {}) {
|
|
|
|
// Passing `data`, specifically `data.pushState`, to not add the action to the
|
|
|
|
// history state if back or forward buttons were pressed.
|
|
|
|
closeImageViewer(data);
|
2017-07-12 06:55:41 +00:00
|
|
|
});
|
|
|
|
|
2017-09-24 01:59:39 +00:00
|
|
|
Mousetrap.bind("esc", () => closeImageViewer());
|
|
|
|
|
|
|
|
Mousetrap.bind(["left", "right"], (e, key) => {
|
|
|
|
if (imageViewer.hasClass("opened")) {
|
|
|
|
const direction = key === "left" ? "previous" : "next";
|
2018-01-30 09:38:33 +00:00
|
|
|
imageViewer.find(`.${direction}-image-btn`).trigger("click");
|
2017-07-12 06:55:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-09 20:36:06 +00:00
|
|
|
function openImageViewer(link, {pushState = true} = {}) {
|
2017-07-23 17:14:27 +00:00
|
|
|
$(".previous-image").removeClass("previous-image");
|
|
|
|
$(".next-image").removeClass("next-image");
|
|
|
|
|
|
|
|
// The next two blocks figure out what are the previous/next images. We first
|
|
|
|
// look within the same message, as there can be multiple thumbnails per
|
|
|
|
// message, and if not, we look at previous/next messages and take the
|
|
|
|
// last/first thumbnail available.
|
|
|
|
// Only expanded thumbnails are being cycled through.
|
|
|
|
|
|
|
|
// Previous image
|
2017-07-24 06:14:14 +00:00
|
|
|
let previousImage = link.closest(".preview").prev(".preview")
|
2017-07-23 17:14:27 +00:00
|
|
|
.find(".toggle-content.show .toggle-thumbnail").last();
|
2017-07-24 06:14:14 +00:00
|
|
|
if (!previousImage.length) {
|
|
|
|
previousImage = link.closest(".msg").prevAll()
|
|
|
|
.find(".toggle-content.show .toggle-thumbnail").last();
|
|
|
|
}
|
2017-07-23 17:14:27 +00:00
|
|
|
previousImage.addClass("previous-image");
|
|
|
|
|
|
|
|
// Next image
|
2017-07-24 06:14:14 +00:00
|
|
|
let nextImage = link.closest(".preview").next(".preview")
|
2017-07-23 17:14:27 +00:00
|
|
|
.find(".toggle-content.show .toggle-thumbnail").first();
|
2017-07-24 06:14:14 +00:00
|
|
|
if (!nextImage.length) {
|
|
|
|
nextImage = link.closest(".msg").nextAll()
|
|
|
|
.find(".toggle-content.show .toggle-thumbnail").first();
|
|
|
|
}
|
2017-07-23 17:14:27 +00:00
|
|
|
nextImage.addClass("next-image");
|
|
|
|
|
2017-07-24 06:31:12 +00:00
|
|
|
imageViewer.html(templates.image_viewer({
|
2018-01-30 09:38:33 +00:00
|
|
|
image: link.find("img").prop("src"),
|
|
|
|
link: link.prop("href"),
|
2017-12-24 23:58:01 +00:00
|
|
|
type: link.parent().hasClass("toggle-type-link") ? "link" : "image",
|
2017-07-23 17:14:27 +00:00
|
|
|
hasPreviousImage: previousImage.length > 0,
|
|
|
|
hasNextImage: nextImage.length > 0,
|
2017-07-12 06:55:41 +00:00
|
|
|
}));
|
|
|
|
|
2017-09-09 10:22:49 +00:00
|
|
|
// Turn off transitionend listener before opening the viewer,
|
|
|
|
// which caused image viewer to become empty in rare cases
|
|
|
|
imageViewer
|
|
|
|
.off("transitionend")
|
|
|
|
.addClass("opened");
|
2017-09-06 04:56:11 +00:00
|
|
|
|
|
|
|
// History management
|
|
|
|
if (pushState) {
|
2017-12-24 23:58:01 +00:00
|
|
|
let clickTarget = "";
|
|
|
|
// Images can be in a message (channel URL previews) or not (window URL
|
|
|
|
// preview, e.g. changelog). This is sub-optimal and needs improvement to
|
|
|
|
// make image preview more generic and not specific for channel previews.
|
|
|
|
if (link.closest(".msg").length > 0) {
|
2018-01-30 09:38:33 +00:00
|
|
|
clickTarget = `#${link.closest(".msg").prop("id")} `;
|
2017-12-24 23:58:01 +00:00
|
|
|
}
|
2018-01-30 09:38:33 +00:00
|
|
|
clickTarget += `a.toggle-thumbnail[href="${link.prop("href")}"] img`;
|
2017-09-09 20:36:06 +00:00
|
|
|
history.pushState({clickTarget}, null, null);
|
2017-09-06 04:56:11 +00:00
|
|
|
}
|
2017-07-12 06:55:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 06:31:12 +00:00
|
|
|
imageViewer.on("click", ".previous-image-btn", function() {
|
2018-01-30 09:38:33 +00:00
|
|
|
$(".previous-image").trigger("click");
|
2017-07-23 17:14:27 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-07-24 06:31:12 +00:00
|
|
|
imageViewer.on("click", ".next-image-btn", function() {
|
2018-01-30 09:38:33 +00:00
|
|
|
$(".next-image").trigger("click");
|
2017-07-23 17:14:27 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-09-09 20:36:06 +00:00
|
|
|
function closeImageViewer({pushState = true} = {}) {
|
2017-07-24 06:31:12 +00:00
|
|
|
imageViewer
|
|
|
|
.removeClass("opened")
|
2017-07-12 06:55:41 +00:00
|
|
|
.one("transitionend", function() {
|
2017-07-24 06:31:12 +00:00
|
|
|
imageViewer.empty();
|
2017-07-12 06:55:41 +00:00
|
|
|
});
|
2017-07-25 14:54:35 +00:00
|
|
|
|
2018-01-30 09:38:33 +00:00
|
|
|
input.trigger("focus");
|
2017-09-06 04:56:11 +00:00
|
|
|
|
|
|
|
// History management
|
|
|
|
if (pushState) {
|
2017-09-10 19:00:27 +00:00
|
|
|
const clickTarget =
|
|
|
|
"#sidebar " +
|
|
|
|
`.chan[data-id="${$("#sidebar .chan.active").data("id")}"]`;
|
|
|
|
history.pushState({clickTarget}, null, null);
|
2017-09-06 04:56:11 +00:00
|
|
|
}
|
2017-07-12 06:55:41 +00:00
|
|
|
}
|