Use AJAX for selected-photo downloads

This commit is contained in:
Terranom674
2026-08-16 14:21:55 +02:00
parent dddd1f2133
commit 93d9598fbb

View File

@@ -3,6 +3,7 @@
var active = false; var active = false;
var selected = {}; var selected = {};
var downloading = false;
function candidateLinks() { function candidateLinks() {
return $('#thumbnails a[href], .thumbnails a[href]'); return $('#thumbnails a[href], .thumbnails a[href]');
@@ -45,8 +46,6 @@
} }
function itemFromLink($link) { function itemFromLink($link) {
// Prefer semantic list/card wrappers used by Piwigo themes and thumbnail
// plugins. gdThumb is only one supported markup, not a dependency.
var $item = $link.closest('li, .thumbnail, .thumbnailCategory, .gdthumb, .card'); var $item = $link.closest('li, .thumbnail, .thumbnailCategory, .gdthumb, .card');
return $item.length ? $item.first() : $link; return $item.length ? $item.first() : $link;
} }
@@ -63,35 +62,43 @@
var imageId = imageIdFromLink($link); var imageId = imageIdFromLink($link);
var $item = itemFromLink($link); var $item = itemFromLink($link);
$item.addClass('bratonien-selectable').attr('data-bratonien-image-id', imageId); $item.addClass('bratonien-selectable').attr('data-bratonien-image-id', imageId);
if (selected[imageId]) { $item.toggleClass('bratonien-selected', !!selected[imageId]);
$item.addClass('bratonien-selected');
}
}); });
} }
function showError(message) {
var $error = $('#bratonien-selection-error');
if (!message) {
$error.prop('hidden', true).text('');
return;
}
$error.text(message).prop('hidden', false);
}
function updateBar() { function updateBar() {
var ids = Object.keys(selected); var ids = Object.keys(selected);
$('#bratonien-selection-count').text(ids.length); $('#bratonien-selection-count').text(ids.length);
$('#bratonien-selection-download').prop('disabled', ids.length === 0); $('#bratonien-selection-download').prop('disabled', ids.length === 0 || downloading);
}
function clearSelection() {
selected = {};
$('.bratonien-selectable').removeClass('bratonien-selected');
showError('');
updateBar();
} }
function setActive(value) { function setActive(value) {
active = value; active = value;
$('body').toggleClass('bratonien-selection-active', active); $('body').toggleClass('bratonien-selection-active', active);
$('#bratonien-selection-bar').prop('hidden', !active); $('#bratonien-selection-bar').prop('hidden', !active);
$('#bratonien-selection-toggle').toggleClass('active', active); $('#bratonien-selection-toggle').toggleClass('active', active).attr('aria-pressed', active ? 'true' : 'false');
refreshSelectableItems(); refreshSelectableItems();
if (!active) { if (!active) {
clearSelection(); clearSelection();
} }
} }
function clearSelection() {
selected = {};
$('.bratonien-selectable').removeClass('bratonien-selected');
updateBar();
}
$(document).on('click', '#bratonien-selection-toggle', function (e) { $(document).on('click', '#bratonien-selection-toggle', function (e) {
e.preventDefault(); e.preventDefault();
setActive(!active); setActive(!active);
@@ -119,6 +126,20 @@
selected[imageId] = true; selected[imageId] = true;
$item.addClass('bratonien-selected'); $item.addClass('bratonien-selected');
} }
showError('');
updateBar();
});
$(document).on('click', '#bratonien-selection-all', function () {
selectableLinks().each(function () {
var $link = $(this);
var imageId = imageIdFromLink($link);
if (imageId) {
selected[imageId] = true;
itemFromLink($link).addClass('bratonien-selected');
}
});
showError('');
updateBar(); updateBar();
}); });
@@ -128,16 +149,53 @@
$(document).on('click', '#bratonien-selection-download', function () { $(document).on('click', '#bratonien-selection-download', function () {
var ids = Object.keys(selected); var ids = Object.keys(selected);
if (!ids.length || !window.BratonienSelectionConfig) { var config = window.BratonienSelectionConfig || {};
if (!ids.length || !config.downloadUrl || downloading) {
return; return;
} }
var separator = window.BratonienSelectionConfig.downloadUrl.indexOf('?') === -1 ? '?' : '&'; downloading = true;
window.location.href = window.BratonienSelectionConfig.downloadUrl + separator + 'bratonien_selection=' + encodeURIComponent(ids.join(',')); showError('');
$('#bratonien-selection-download').text('Wird vorbereitet …');
updateBar();
$.ajax({
url: config.downloadUrl,
method: 'POST',
dataType: 'json',
data: {
bratonien_selection_download: 1,
image_ids: ids.join(','),
pwg_token: config.token || ''
}
}).done(function (response) {
if (!response || !response.ok || !response.download_url) {
showError(response && response.error ? response.error : 'Der Download konnte nicht vorbereitet werden.');
return;
}
window.location.href = response.download_url;
}).fail(function (xhr) {
var message = 'Der Download konnte nicht vorbereitet werden.';
if (xhr.responseJSON && xhr.responseJSON.error) {
message = xhr.responseJSON.error;
}
showError(message);
}).always(function () {
downloading = false;
$('#bratonien-selection-download').text('Herunterladen');
updateBar();
});
});
// The native Batch Downloader button downloads the whole current set and
// performs a page round-trip first. When Bratonien selection is available,
// the selection mode replaces that control; "Alle auswählen" covers the
// complete visible album without the confusing duplicate button.
$(function () {
$('#batchDownloadLink, #batchDownloadRequest').closest('a').hide();
$('#bratonien-selection-toggle').attr('aria-pressed', 'false');
}); });
// Some plugins append thumbnails after the initial page render. Refresh the
// markers without depending on any particular thumbnail implementation.
$(document).ajaxComplete(function () { $(document).ajaxComplete(function () {
if (active) { if (active) {
refreshSelectableItems(); refreshSelectableItems();