From 88bb14b3399a991616fbf454726d6cfa6a506392 Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Sun, 16 Aug 2026 12:51:36 +0200 Subject: [PATCH] Add public photo selection behavior --- js/public_selection.js | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 js/public_selection.js diff --git a/js/public_selection.js b/js/public_selection.js new file mode 100644 index 0000000..5d97332 --- /dev/null +++ b/js/public_selection.js @@ -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);