2017-03-18 00:50:39 +01:00
|
|
|
var lightbox = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
2017-03-19 01:51:20 +01:00
|
|
|
var is_open = false;
|
2017-03-20 19:37:23 +01:00
|
|
|
// the asset map is a map of all retrieved images and YouTube videos that are
|
|
|
|
// memoized instead of being looked up multiple times.
|
|
|
|
var asset_map = {
|
2017-03-19 01:51:20 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
function render_lightbox_list_images(preview_source) {
|
2017-03-19 01:51:20 +01:00
|
|
|
if (!is_open) {
|
2017-03-25 03:59:52 +01:00
|
|
|
var images = Array.prototype.slice.call($(".focused_table .message_inline_image img"));
|
2017-03-19 01:51:20 +01:00
|
|
|
var $image_list = $("#lightbox_overlay .image-list").html("");
|
|
|
|
|
|
|
|
images.forEach(function (img) {
|
|
|
|
var src = img.getAttribute("src");
|
2018-07-16 20:19:21 +02:00
|
|
|
var className = preview_source === src ? "image selected" : "image";
|
2017-03-19 01:51:20 +01:00
|
|
|
|
|
|
|
var node = $("<div></div>", {
|
|
|
|
class: className,
|
|
|
|
"data-src": src,
|
|
|
|
}).css({ backgroundImage: "url(" + src + ")"});
|
|
|
|
|
|
|
|
$image_list.append(node);
|
|
|
|
}, "");
|
|
|
|
}
|
2017-03-20 19:37:23 +01:00
|
|
|
}
|
2017-03-19 01:51:20 +01:00
|
|
|
|
2017-06-08 22:58:15 +02:00
|
|
|
function display_image(payload, options) {
|
2017-03-20 19:37:23 +01:00
|
|
|
render_lightbox_list_images(payload.preview);
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
$(".player-container").hide();
|
2017-06-08 22:58:15 +02:00
|
|
|
$(".image-actions, .image-description, .download, .lightbox-canvas-trigger").show();
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-06-08 22:58:15 +02:00
|
|
|
if (options.lightbox_canvas === true) {
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.setAttribute("data-src", payload.source);
|
|
|
|
|
|
|
|
$("#lightbox_overlay .image-preview").html(canvas).show();
|
|
|
|
var photo = new LightboxCanvas(canvas);
|
|
|
|
photo.speed(2.3);
|
|
|
|
} else {
|
|
|
|
var img = new Image();
|
|
|
|
img.src = payload.source;
|
|
|
|
|
|
|
|
$("#lightbox_overlay .image-preview").html(img).show();
|
|
|
|
}
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
$(".image-description .title").text(payload.title || "N/A");
|
|
|
|
$(".image-description .user").text(payload.user);
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
$(".image-actions .open, .image-actions .download").attr("href", payload.source);
|
2017-03-18 00:50:39 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:17:00 +01:00
|
|
|
function display_video(payload) {
|
2017-03-20 19:37:23 +01:00
|
|
|
render_lightbox_list_images(payload.preview);
|
|
|
|
|
2017-06-08 22:58:15 +02:00
|
|
|
$("#lightbox_overlay .image-preview, .image-description, .download, .lightbox-canvas-trigger").hide();
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-12-14 22:17:00 +01:00
|
|
|
var source;
|
|
|
|
if (payload.type === "youtube-video") {
|
|
|
|
source = "https://www.youtube.com/embed/" + payload.source;
|
|
|
|
} else if (payload.type === "vimeo-video") {
|
|
|
|
source = "https://player.vimeo.com/video/" + payload.source;
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:16:46 +02:00
|
|
|
var iframe = $("<iframe></iframe>");
|
|
|
|
iframe.attr("src", source);
|
|
|
|
iframe.attr("frameborder", 0);
|
|
|
|
iframe.attr("allowfullscreen", true);
|
2017-03-18 00:50:39 +01:00
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
$("#lightbox_overlay .player-container").html(iframe).show();
|
2017-12-14 22:17:00 +01:00
|
|
|
|
|
|
|
var url = (payload.type === "youtube-video" ? "https://youtu.be/" : "https://vimeo.com/") + payload.source;
|
|
|
|
$(".image-actions .open").attr("href", url);
|
2017-03-18 00:50:39 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
// the image param is optional, but required on the first preview of an image.
|
|
|
|
// this will likely be passed in every time but just ignored if the result is already
|
|
|
|
// stored in the `asset_map`.
|
2017-06-08 22:58:15 +02:00
|
|
|
exports.open = function (image, options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {
|
|
|
|
// default to showing standard images.
|
|
|
|
lightbox_canvas: $(".lightbox-canvas-trigger").hasClass("enabled"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
var $image = $(image);
|
|
|
|
|
|
|
|
// if wrapped in the .youtube-video class, it will be length = 1, and therefore
|
|
|
|
// cast to true.
|
|
|
|
var is_youtube_video = !!$image.closest(".youtube-video").length;
|
2017-12-14 22:17:00 +01:00
|
|
|
var is_vimeo_video = !!$image.closest(".vimeo-video").length;
|
2017-03-20 19:37:23 +01:00
|
|
|
|
2019-02-02 14:26:34 +01:00
|
|
|
// check if image is descendent of #preview_content
|
|
|
|
var is_compose_preview_image = $image.closest("#preview_content").length === 1;
|
2017-03-20 19:37:23 +01:00
|
|
|
var payload;
|
|
|
|
// if the asset_map already contains the metadata required to display the
|
|
|
|
// asset, just recall that metadata.
|
2018-07-30 21:10:33 +02:00
|
|
|
var $image_source = $image.attr("data-src-fullsize") || $image.attr("src");
|
2018-07-16 20:50:57 +02:00
|
|
|
if (asset_map[$image_source]) {
|
|
|
|
payload = asset_map[$image_source];
|
2017-03-20 19:37:23 +01:00
|
|
|
// otherwise retrieve the metadata from the DOM and store into the asset_map.
|
|
|
|
} else {
|
|
|
|
var $parent = $image.parent();
|
2017-12-14 22:17:00 +01:00
|
|
|
var $type;
|
|
|
|
var $source;
|
|
|
|
if (is_youtube_video) {
|
|
|
|
$type = "youtube-video";
|
|
|
|
$source = $parent.attr("data-id");
|
|
|
|
} else if (is_vimeo_video) {
|
|
|
|
$type = "vimeo-video";
|
|
|
|
$source = $parent.attr("data-id");
|
|
|
|
} else {
|
|
|
|
$type = "image";
|
2018-07-30 21:10:33 +02:00
|
|
|
// thumbor supplies the src as thumbnail, data-src-fullsize as full-sized.
|
|
|
|
if ($image.attr("data-src-fullsize")) {
|
|
|
|
$source = $image.attr("data-src-fullsize");
|
2018-03-08 09:37:09 +01:00
|
|
|
} else {
|
|
|
|
$source = $image.attr("src");
|
|
|
|
}
|
2017-12-14 22:17:00 +01:00
|
|
|
}
|
2019-02-02 00:05:03 +01:00
|
|
|
var sender_full_name;
|
2019-02-02 14:26:34 +01:00
|
|
|
if (is_compose_preview_image) {
|
|
|
|
sender_full_name = people.my_full_name();
|
|
|
|
} else {
|
|
|
|
var $message = $parent.closest("[zid]");
|
|
|
|
var message = message_store.get($message.attr("zid"));
|
|
|
|
if (message === undefined) {
|
|
|
|
blueslip.error("Lightbox for unknown message " + $message.attr("zid"));
|
|
|
|
}
|
|
|
|
sender_full_name = message.sender_full_name;
|
2018-03-23 23:16:39 +01:00
|
|
|
}
|
2017-03-20 19:37:23 +01:00
|
|
|
payload = {
|
2019-02-02 00:05:03 +01:00
|
|
|
user: sender_full_name,
|
|
|
|
title: $parent.attr("title"),
|
2017-12-14 22:17:00 +01:00
|
|
|
type: $type,
|
2017-03-20 19:37:23 +01:00
|
|
|
preview: $image.attr("src"),
|
2017-12-14 22:17:00 +01:00
|
|
|
source: $source,
|
2017-03-20 19:37:23 +01:00
|
|
|
};
|
|
|
|
|
2018-07-16 20:50:57 +02:00
|
|
|
asset_map[$source] = payload;
|
2017-09-10 08:56:29 +02:00
|
|
|
}
|
2017-03-20 19:37:23 +01:00
|
|
|
|
2017-12-14 22:17:00 +01:00
|
|
|
if (payload.type.match("-video")) {
|
|
|
|
display_video(payload);
|
2017-09-10 08:56:29 +02:00
|
|
|
} else if (payload.type === "image") {
|
|
|
|
display_image(payload, options);
|
2017-03-18 00:50:39 +01:00
|
|
|
}
|
|
|
|
|
2017-05-10 15:27:31 +02:00
|
|
|
if (is_open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
function lightbox_close_overlay() {
|
2017-05-06 00:57:14 +02:00
|
|
|
$(".player-container iframe").remove();
|
2017-05-10 15:43:36 +02:00
|
|
|
is_open = false;
|
2017-05-06 00:57:14 +02:00
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.open_overlay({
|
2017-05-06 00:57:14 +02:00
|
|
|
name: 'lightbox',
|
|
|
|
overlay: $("#lightbox_overlay"),
|
2017-05-27 15:40:54 +02:00
|
|
|
on_close: lightbox_close_overlay,
|
2017-05-06 00:57:14 +02:00
|
|
|
});
|
|
|
|
|
2017-03-18 00:50:39 +01:00
|
|
|
popovers.hide_all();
|
2017-05-10 15:43:36 +02:00
|
|
|
is_open = true;
|
2017-03-18 00:50:39 +01:00
|
|
|
};
|
|
|
|
|
2017-03-18 21:01:16 +01:00
|
|
|
exports.show_from_selected_message = function () {
|
2017-12-10 20:47:38 +01:00
|
|
|
var $message_selected = $(".selected_message");
|
|
|
|
var $message = $message_selected;
|
|
|
|
var $image = $message.find(".message_inline_image img");
|
|
|
|
var $prev_traverse = false;
|
2017-05-22 19:44:53 +02:00
|
|
|
|
|
|
|
while ($image.length === 0) {
|
2017-12-10 20:47:38 +01:00
|
|
|
if ($message.prev().length === 0) {
|
|
|
|
$message = $message.parent().prev();
|
|
|
|
if ($message.length === 0) {
|
|
|
|
$prev_traverse = true;
|
|
|
|
$message = $message_selected;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$message = $message.find(".last_message");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 19:44:53 +02:00
|
|
|
$message = $message.prev();
|
2017-12-10 20:47:38 +01:00
|
|
|
$image = $message.find(".message_inline_image img");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prev_traverse) {
|
|
|
|
while ($image.length === 0) {
|
|
|
|
if ($message.next().length === 0) {
|
|
|
|
$message = $message.parent().next();
|
|
|
|
if ($message.length === 0) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$message = $message.children().first();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message = $message.next();
|
|
|
|
$image = $message.find(".message_inline_image img");
|
2017-05-22 19:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($image.length !== 0) {
|
|
|
|
exports.open($image);
|
2017-03-18 21:01:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-19 01:51:20 +01:00
|
|
|
exports.prev = function () {
|
|
|
|
$(".image-list .image.selected").prev().click();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.next = function () {
|
|
|
|
$(".image-list .image.selected").next().click();
|
|
|
|
};
|
|
|
|
|
2017-03-20 19:36:32 +01:00
|
|
|
// this is a block of events that are required for the lightbox to work.
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2019-02-02 14:26:34 +01:00
|
|
|
$("#main_div, #preview_content").on("click", ".message_inline_image a", function (e) {
|
2017-03-20 19:36:32 +01:00
|
|
|
// prevent the link from opening in a new page.
|
|
|
|
e.preventDefault();
|
|
|
|
// prevent the message compose dialog from happening.
|
|
|
|
e.stopPropagation();
|
2019-02-02 00:05:03 +01:00
|
|
|
var $img = $(this).find("img");
|
2017-03-20 19:37:23 +01:00
|
|
|
exports.open($img);
|
2017-03-20 19:36:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#lightbox_overlay .download").click(function () {
|
2018-05-06 21:43:17 +02:00
|
|
|
this.blur();
|
2017-03-20 19:36:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#lightbox_overlay").on("click", ".image-list .image", function () {
|
|
|
|
var $image_list = $(this).parent();
|
2017-03-20 19:37:23 +01:00
|
|
|
var original_image = $(".message_row img[src='" + $(this).attr('data-src') + "']");
|
2017-03-20 19:36:32 +01:00
|
|
|
|
2017-03-20 19:37:23 +01:00
|
|
|
exports.open(original_image);
|
2017-03-20 19:36:32 +01:00
|
|
|
|
|
|
|
$(".image-list .image.selected").removeClass("selected");
|
|
|
|
$(this).addClass("selected");
|
|
|
|
|
|
|
|
var parentOffset = this.parentNode.clientWidth + this.parentNode.scrollLeft;
|
|
|
|
// this is the left and right of the image compared to its parent.
|
|
|
|
var coords = {
|
|
|
|
left: this.offsetLeft,
|
|
|
|
right: this.offsetLeft + this.clientWidth,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (coords.right > parentOffset) {
|
|
|
|
// add 2px margin
|
|
|
|
$image_list.animate({
|
|
|
|
scrollLeft: coords.right - this.parentNode.clientWidth + 2,
|
|
|
|
}, 100);
|
|
|
|
} else if (coords.left < this.parentNode.scrollLeft) {
|
|
|
|
// subtract 2px margin
|
|
|
|
$image_list.animate({ scrollLeft: coords.left - 2 }, 100);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#lightbox_overlay").on("click", ".center .arrow", function () {
|
|
|
|
var direction = $(this).attr("data-direction");
|
|
|
|
|
|
|
|
if (/^(next|prev)$/.test(direction)) {
|
|
|
|
lightbox[direction]();
|
|
|
|
}
|
|
|
|
});
|
2017-03-18 22:47:13 +01:00
|
|
|
|
2017-06-08 22:58:15 +02:00
|
|
|
$("#lightbox_overlay").on("click", ".lightbox-canvas-trigger", function () {
|
|
|
|
var $img = $("#lightbox_overlay").find(".image-preview img");
|
|
|
|
|
|
|
|
if ($img.length) {
|
|
|
|
$(this).addClass("enabled");
|
|
|
|
// the `lightbox.open` function will see the enabled class and
|
|
|
|
// enable the `LightboxCanvas` class.
|
|
|
|
exports.open($img);
|
|
|
|
} else {
|
|
|
|
$img = $("#lightbox_overlay").find(".image-preview canvas")[0].image;
|
|
|
|
|
|
|
|
$(this).removeClass("enabled");
|
|
|
|
exports.open($img);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#lightbox_overlay .image-preview").on("dblclick", "img, canvas", function (e) {
|
|
|
|
$("#lightbox_overlay .lightbox-canvas-trigger").click();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-08-01 18:21:55 +02:00
|
|
|
|
|
|
|
$("#lightbox_overlay .player-container").on("click", function () {
|
|
|
|
if ($(this).is(".player-container")) {
|
|
|
|
overlays.close_active();
|
|
|
|
}
|
|
|
|
});
|
2017-11-29 19:19:44 +01:00
|
|
|
|
|
|
|
$("#lightbox_overlay").on("click", ".image-info-wrapper, .center", function (e) {
|
|
|
|
if ($(e.target).is(".image-info-wrapper, .center")) {
|
|
|
|
overlays.close_overlay("lightbox");
|
|
|
|
}
|
|
|
|
});
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2017-03-20 19:36:32 +01:00
|
|
|
|
2017-03-18 00:50:39 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = lightbox;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.lightbox = lightbox;
|