2017-07-06 06:16:01 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
2018-04-06 04:40:27 +00:00
|
|
|
const Mousetrap = require("mousetrap");
|
2017-07-06 06:16:01 +00:00
|
|
|
const templates = require("../views");
|
|
|
|
|
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
|
2019-07-17 09:33:59 +00:00
|
|
|
let previousImage = link
|
|
|
|
.closest(".preview")
|
|
|
|
.prev(".preview")
|
|
|
|
.find(".toggle-content .toggle-thumbnail")
|
|
|
|
.last();
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-07-24 06:14:14 +00:00
|
|
|
if (!previousImage.length) {
|
2019-07-17 09:33:59 +00:00
|
|
|
previousImage = link
|
|
|
|
.closest(".msg")
|
|
|
|
.prevAll()
|
|
|
|
.find(".toggle-content .toggle-thumbnail")
|
|
|
|
.last();
|
2017-07-24 06:14:14 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-07-23 17:14:27 +00:00
|
|
|
previousImage.addClass("previous-image");
|
|
|
|
|
|
|
|
// Next image
|
2019-07-17 09:33:59 +00:00
|
|
|
let nextImage = link
|
|
|
|
.closest(".preview")
|
|
|
|
.next(".preview")
|
|
|
|
.find(".toggle-content .toggle-thumbnail")
|
|
|
|
.first();
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-07-24 06:14:14 +00:00
|
|
|
if (!nextImage.length) {
|
2019-07-17 09:33:59 +00:00
|
|
|
nextImage = link
|
|
|
|
.closest(".msg")
|
|
|
|
.nextAll()
|
|
|
|
.find(".toggle-content .toggle-thumbnail")
|
|
|
|
.first();
|
2017-07-24 06:14:14 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-07-23 17:14:27 +00:00
|
|
|
nextImage.addClass("next-image");
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
imageViewer.html(
|
|
|
|
templates.image_viewer({
|
|
|
|
image: link.find("img").prop("src"),
|
|
|
|
link: link.prop("href"),
|
|
|
|
type: link.parent().hasClass("toggle-type-link") ? "link" : "image",
|
|
|
|
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
|
2019-07-17 09:33:59 +00:00
|
|
|
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 = "";
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-24 23:58:01 +00:00
|
|
|
// 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-02-20 07:28:04 +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} = {}) {
|
2019-07-17 09:33:59 +00:00
|
|
|
imageViewer.removeClass("opened").one("transitionend", function() {
|
|
|
|
imageViewer.empty();
|
|
|
|
});
|
2017-07-25 14:54:35 +00:00
|
|
|
|
2017-09-06 04:56:11 +00:00
|
|
|
// History management
|
|
|
|
if (pushState) {
|
2017-09-10 19:00:27 +00:00
|
|
|
const clickTarget =
|
2019-07-17 09:33:59 +00:00
|
|
|
"#sidebar " + `.chan[data-id="${$("#sidebar .chan.active").attr("data-id")}"]`;
|
2017-09-10 19:00:27 +00:00
|
|
|
history.pushState({clickTarget}, null, null);
|
2017-09-06 04:56:11 +00:00
|
|
|
}
|
2017-07-12 06:55:41 +00:00
|
|
|
}
|