2017-07-06 06:16:01 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
|
|
|
|
const options = require("./options");
|
|
|
|
const templates = require("../views");
|
|
|
|
|
|
|
|
module.exports = renderPreview;
|
|
|
|
|
|
|
|
function renderPreview(preview, msg) {
|
2017-07-21 05:28:51 +00:00
|
|
|
if (preview.type === "loading") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-07-06 06:16:01 +00:00
|
|
|
preview.shown = options.shouldOpenMessagePreview(preview.type);
|
|
|
|
|
|
|
|
const container = msg.closest(".chat");
|
|
|
|
let bottom = false;
|
|
|
|
if (container.length) {
|
|
|
|
bottom = container.isScrollBottom();
|
|
|
|
}
|
|
|
|
|
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-07-21 10:22:59 +00:00
|
|
|
previewContainer
|
2017-07-06 06:16:01 +00:00
|
|
|
.append(templates.msg_preview({preview: preview}));
|
|
|
|
|
|
|
|
if (preview.shown && bottom) {
|
|
|
|
handleImageInPreview(msg.find(".toggle-content"), container);
|
|
|
|
}
|
|
|
|
|
|
|
|
container.trigger("keepToBottom");
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#chat").on("click", ".toggle-button", function() {
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (bottom && !content.hasClass("show")) {
|
|
|
|
handleImageInPreview(content, container);
|
|
|
|
}
|
|
|
|
|
2017-07-06 07:35:54 +00:00
|
|
|
self.toggleClass("opened");
|
2017-07-06 06:16:01 +00:00
|
|
|
content.toggleClass("show");
|
|
|
|
|
|
|
|
// If scrollbar was at the bottom before toggling the preview, keep it at the bottom
|
|
|
|
if (bottom) {
|
|
|
|
container.scrollBottom();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function handleImageInPreview(content, container) {
|
|
|
|
const img = content.find("img");
|
|
|
|
|
|
|
|
// Trigger scroll logic after the image loads
|
|
|
|
if (img.length && !img.width()) {
|
|
|
|
img.on("load", function() {
|
|
|
|
container.trigger("keepToBottom");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 06:55:41 +00:00
|
|
|
|
|
|
|
/* Image viewer */
|
|
|
|
|
|
|
|
// FIXME Remove #input focus when this is open
|
|
|
|
// See https://github.com/thelounge/lounge/issues/1342
|
|
|
|
$("#viewport").on("click", ".toggle-thumbnail", function() {
|
|
|
|
const link = $(this);
|
|
|
|
|
|
|
|
openImageViewer(link);
|
|
|
|
|
|
|
|
// Prevent the link to open a new page since we're opening the image viewer,
|
|
|
|
// but keep it a link to allow for Ctrl/Cmd+click
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#image-viewer").on("click", function() {
|
|
|
|
closeImageViewer();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).keydown(function(e) {
|
|
|
|
switch (e.keyCode ? e.keyCode : e.which) {
|
|
|
|
case 27: // Escape
|
|
|
|
closeImageViewer();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function openImageViewer(link) {
|
|
|
|
$("#image-viewer").html(templates.image_viewer({
|
|
|
|
image: link.find("img").attr("src"),
|
|
|
|
link: link.attr("href"),
|
|
|
|
type: link.parent().hasClass("toggle-type-image") ? "image" : "link"
|
|
|
|
}));
|
|
|
|
|
|
|
|
$(document.body).addClass("image-viewer-opened");
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeImageViewer() {
|
|
|
|
$(document.body)
|
|
|
|
.removeClass("image-viewer-opened")
|
|
|
|
.one("transitionend", function() {
|
|
|
|
$("#image-viewer").empty();
|
|
|
|
});
|
|
|
|
}
|