diff --git a/js/public_selection.js b/js/public_selection.js index b7045fd..b7c3b3c 100644 --- a/js/public_selection.js +++ b/js/public_selection.js @@ -3,6 +3,7 @@ var active = false; var selected = {}; + var downloading = false; function candidateLinks() { return $('#thumbnails a[href], .thumbnails a[href]'); @@ -45,8 +46,6 @@ } 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'); return $item.length ? $item.first() : $link; } @@ -63,35 +62,43 @@ var imageId = imageIdFromLink($link); var $item = itemFromLink($link); $item.addClass('bratonien-selectable').attr('data-bratonien-image-id', imageId); - if (selected[imageId]) { - $item.addClass('bratonien-selected'); - } + $item.toggleClass('bratonien-selected', !!selected[imageId]); }); } + function showError(message) { + var $error = $('#bratonien-selection-error'); + if (!message) { + $error.prop('hidden', true).text(''); + return; + } + $error.text(message).prop('hidden', false); + } + function updateBar() { var ids = Object.keys(selected); $('#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) { active = value; $('body').toggleClass('bratonien-selection-active', 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(); if (!active) { clearSelection(); } } - function clearSelection() { - selected = {}; - $('.bratonien-selectable').removeClass('bratonien-selected'); - updateBar(); - } - $(document).on('click', '#bratonien-selection-toggle', function (e) { e.preventDefault(); setActive(!active); @@ -119,6 +126,20 @@ selected[imageId] = true; $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(); }); @@ -128,16 +149,53 @@ $(document).on('click', '#bratonien-selection-download', function () { var ids = Object.keys(selected); - if (!ids.length || !window.BratonienSelectionConfig) { + var config = window.BratonienSelectionConfig || {}; + if (!ids.length || !config.downloadUrl || downloading) { return; } - var separator = window.BratonienSelectionConfig.downloadUrl.indexOf('?') === -1 ? '?' : '&'; - window.location.href = window.BratonienSelectionConfig.downloadUrl + separator + 'bratonien_selection=' + encodeURIComponent(ids.join(',')); + downloading = true; + 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 () { if (active) { refreshSelectableItems();