Add public photo selection behavior

This commit is contained in:
Terranom674
2026-08-16 12:51:36 +02:00
parent 4f7d451388
commit 88bb14b339

87
js/public_selection.js Normal file
View File

@@ -0,0 +1,87 @@
(function ($) {
'use strict';
var active = false;
var selected = {};
function getThumbs() {
return $('#thumbnails .gdthumb');
}
function imageIdFromThumb($thumb) {
var $link = $thumb.find('a[href]').first();
var href = $link.attr('href') || '';
var matches = href.match(/(?:image|picture)\/(\d+)|[?&](?:image_id|id)=(\d+)/i);
if (matches) {
return parseInt(matches[1] || matches[2], 10);
}
var id = $thumb.data('image-id') || $thumb.attr('data-image-id');
return id ? parseInt(id, 10) : 0;
}
function updateBar() {
var ids = Object.keys(selected);
$('#bratonien-selection-count').text(ids.length);
$('#bratonien-selection-download').prop('disabled', ids.length === 0);
}
function setActive(value) {
active = value;
$('body').toggleClass('bratonien-selection-active', active);
$('#bratonien-selection-bar').prop('hidden', !active);
$('#bratonien-selection-toggle').toggleClass('active', active);
if (!active) {
clearSelection();
}
}
function clearSelection() {
selected = {};
getThumbs().removeClass('bratonien-selected');
updateBar();
}
$(document).on('click', '#bratonien-selection-toggle', function (e) {
e.preventDefault();
setActive(!active);
});
$(document).on('click', '#thumbnails .gdthumb a', function (e) {
if (!active) {
return;
}
e.preventDefault();
e.stopPropagation();
var $thumb = $(this).closest('.gdthumb');
var imageId = imageIdFromThumb($thumb);
if (!imageId) {
return;
}
if (selected[imageId]) {
delete selected[imageId];
$thumb.removeClass('bratonien-selected');
} else {
selected[imageId] = true;
$thumb.addClass('bratonien-selected');
}
updateBar();
});
$(document).on('click', '#bratonien-selection-clear', function () {
clearSelection();
});
$(document).on('click', '#bratonien-selection-download', function () {
var ids = Object.keys(selected);
if (!ids.length || !window.BratonienSelectionConfig) {
return;
}
var separator = window.BratonienSelectionConfig.downloadUrl.indexOf('?') === -1 ? '?' : '&';
window.location.href = window.BratonienSelectionConfig.downloadUrl + separator + 'bratonien_selection=' + encodeURIComponent(ids.join(','));
});
})(jQuery);