diff --git a/README.md b/README.md index e664a772..bd7b1703 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ summary: all planned features work! now please enjoy the bloatening * ☑ images using Pillow * ☑ videos using FFmpeg * ☑ cache eviction (max-age; maybe max-size eventually) + * ☑ image gallery * ☑ SPA (browse while uploading) * if you use the file-tree on the left only, not folders in the file list * server indexing diff --git a/copyparty/web/baguettebox.js b/copyparty/web/baguettebox.js new file mode 100644 index 00000000..907daff2 --- /dev/null +++ b/copyparty/web/baguettebox.js @@ -0,0 +1,583 @@ +/*! + * baguetteBox.js + * @author feimosi + * @version 1.11.1-mod + * @url https://github.com/feimosi/baguetteBox.js + */ + +window.baguetteBox = (function () { + 'use strict'; + + var options = {}, + defaults = { + captions: true, + buttons: 'auto', + noScrollbars: false, + bodyClass: 'baguetteBox-open', + titleTag: false, + async: false, + preload: 2, + animation: 'slideIn', + afterShow: null, + afterHide: null, + onChange: null, + }, + overlay, slider, previousButton, nextButton, closeButton, + currentGallery = [], + currentIndex = 0, + isOverlayVisible = false, + touch = {}, // start-pos + touchFlag = false, // busy + regex = /.+\.(gif|jpe?g|png|webp)/i, + data = {}, // all galleries + imagesElements = [], + documentLastFocus = null; + + var overlayClickHandler = function (event) { + if (event.target.id.indexOf('baguette-img') !== -1) { + hideOverlay(); + } + }; + + var touchstartHandler = function (event) { + touch.count++; + if (touch.count > 1) { + touch.multitouch = true; + } + touch.startX = event.changedTouches[0].pageX; + touch.startY = event.changedTouches[0].pageY; + }; + var touchmoveHandler = function (event) { + if (touchFlag || touch.multitouch) { + return; + } + event.preventDefault ? event.preventDefault() : event.returnValue = false; + var touchEvent = event.touches[0] || event.changedTouches[0]; + if (touchEvent.pageX - touch.startX > 40) { + touchFlag = true; + showPreviousImage(); + } else if (touchEvent.pageX - touch.startX < -40) { + touchFlag = true; + showNextImage(); + } else if (touch.startY - touchEvent.pageY > 100) { + hideOverlay(); + } + }; + var touchendHandler = function () { + touch.count--; + if (touch.count <= 0) { + touch.multitouch = false; + } + touchFlag = false; + }; + var contextmenuHandler = function () { + touchendHandler(); + }; + + var trapFocusInsideOverlay = function (event) { + if (overlay.style.display === 'block' && (overlay.contains && !overlay.contains(event.target))) { + event.stopPropagation(); + initFocus(); + } + }; + + function run(selector, userOptions) { + buildOverlay(); + removeFromCache(selector); + return bindImageClickListeners(selector, userOptions); + } + + function bindImageClickListeners(selector, userOptions) { + var galleryNodeList = document.querySelectorAll(selector); + var selectorData = { + galleries: [], + nodeList: galleryNodeList + }; + data[selector] = selectorData; + + [].forEach.call(galleryNodeList, function (galleryElement) { + if (userOptions && userOptions.filter) { + regex = userOptions.filter; + } + + var tagsNodeList = []; + if (galleryElement.tagName === 'A') { + tagsNodeList = [galleryElement]; + } else { + tagsNodeList = galleryElement.getElementsByTagName('a'); + } + + tagsNodeList = [].filter.call(tagsNodeList, function (element) { + if (element.className.indexOf(userOptions && userOptions.ignoreClass) === -1) { + return regex.test(element.href); + } + }); + if (tagsNodeList.length === 0) { + return; + } + + var gallery = []; + [].forEach.call(tagsNodeList, function (imageElement, imageIndex) { + var imageElementClickHandler = function (event) { + if (event && event.ctrlKey) + return true; + + event.preventDefault ? event.preventDefault() : event.returnValue = false; + prepareOverlay(gallery, userOptions); + showOverlay(imageIndex); + }; + var imageItem = { + eventHandler: imageElementClickHandler, + imageElement: imageElement + }; + bind(imageElement, 'click', imageElementClickHandler); + gallery.push(imageItem); + }); + selectorData.galleries.push(gallery); + }); + + return selectorData.galleries; + } + + function clearCachedData() { + for (var selector in data) { + if (data.hasOwnProperty(selector)) { + removeFromCache(selector); + } + } + } + + function removeFromCache(selector) { + if (!data.hasOwnProperty(selector)) { + return; + } + var galleries = data[selector].galleries; + [].forEach.call(galleries, function (gallery) { + [].forEach.call(gallery, function (imageItem) { + unbind(imageItem.imageElement, 'click', imageItem.eventHandler); + }); + + if (currentGallery === gallery) { + currentGallery = []; + } + }); + + delete data[selector]; + } + + function buildOverlay() { + overlay = ebi('baguetteBox-overlay'); + if (overlay) { + slider = ebi('baguetteBox-slider'); + previousButton = ebi('previous-button'); + nextButton = ebi('next-button'); + closeButton = ebi('close-button'); + return; + } + overlay = mknod('div'); + overlay.setAttribute('role', 'dialog'); + overlay.id = 'baguetteBox-overlay'; + document.getElementsByTagName('body')[0].appendChild(overlay); + + slider = mknod('div'); + slider.id = 'baguetteBox-slider'; + overlay.appendChild(slider); + + previousButton = mknod('button'); + previousButton.setAttribute('type', 'button'); + previousButton.id = 'previous-button'; + previousButton.setAttribute('aria-label', 'Previous'); + previousButton.innerHTML = '<'; + overlay.appendChild(previousButton); + + nextButton = mknod('button'); + nextButton.setAttribute('type', 'button'); + nextButton.id = 'next-button'; + nextButton.setAttribute('aria-label', 'Next'); + nextButton.innerHTML = '>'; + overlay.appendChild(nextButton); + + closeButton = mknod('button'); + closeButton.setAttribute('type', 'button'); + closeButton.id = 'close-button'; + closeButton.setAttribute('aria-label', 'Close'); + closeButton.innerHTML = '×'; + overlay.appendChild(closeButton); + + previousButton.className = nextButton.className = closeButton.className = 'baguetteBox-button'; + + bindEvents(); + } + + function keyDownHandler(event) { + switch (event.keyCode) { + case 37: // Left + showPreviousImage(); + break; + case 39: // Right + showNextImage(); + break; + case 27: // Esc + hideOverlay(); + break; + case 36: // Home + showFirstImage(event); + break; + case 35: // End + showLastImage(event); + break; + } + } + + var passiveSupp = false; + try { + var opts = { + get passive() { + passiveSupp = true; + return false; + } + }; + window.addEventListener('test', null, opts); + window.removeEventListener('test', null, opts); + } + catch (ex) { + passiveSupp = false; + } + var passiveEvent = passiveSupp ? { passive: false } : null; + var nonPassiveEvent = passiveSupp ? { passive: true } : null; + + function bindEvents() { + bind(overlay, 'click', overlayClickHandler); + bind(previousButton, 'click', showPreviousImage); + bind(nextButton, 'click', showNextImage); + bind(closeButton, 'click', hideOverlay); + bind(slider, 'contextmenu', contextmenuHandler); + bind(overlay, 'touchstart', touchstartHandler, nonPassiveEvent); + bind(overlay, 'touchmove', touchmoveHandler, passiveEvent); + bind(overlay, 'touchend', touchendHandler); + bind(document, 'focus', trapFocusInsideOverlay, true); + } + + function unbindEvents() { + unbind(overlay, 'click', overlayClickHandler); + unbind(previousButton, 'click', showPreviousImage); + unbind(nextButton, 'click', showNextImage); + unbind(closeButton, 'click', hideOverlay); + unbind(slider, 'contextmenu', contextmenuHandler); + unbind(overlay, 'touchstart', touchstartHandler, nonPassiveEvent); + unbind(overlay, 'touchmove', touchmoveHandler, passiveEvent); + unbind(overlay, 'touchend', touchendHandler); + unbind(document, 'focus', trapFocusInsideOverlay, true); + } + + function prepareOverlay(gallery, userOptions) { + if (currentGallery === gallery) { + return; + } + currentGallery = gallery; + setOptions(userOptions); + slider.innerHTML = ''; + imagesElements.length = 0; + + var imagesFiguresIds = []; + var imagesCaptionsIds = []; + for (var i = 0, fullImage; i < gallery.length; i++) { + fullImage = mknod('div'); + fullImage.className = 'full-image'; + fullImage.id = 'baguette-img-' + i; + imagesElements.push(fullImage); + + imagesFiguresIds.push('baguetteBox-figure-' + i); + imagesCaptionsIds.push('baguetteBox-figcaption-' + i); + slider.appendChild(imagesElements[i]); + } + overlay.setAttribute('aria-labelledby', imagesFiguresIds.join(' ')); + overlay.setAttribute('aria-describedby', imagesCaptionsIds.join(' ')); + } + + function setOptions(newOptions) { + if (!newOptions) { + newOptions = {}; + } + for (var item in defaults) { + options[item] = defaults[item]; + if (typeof newOptions[item] !== 'undefined') { + options[item] = newOptions[item]; + } + } + slider.style.transition = (options.animation === 'fadeIn' ? 'opacity .4s ease' : + options.animation === 'slideIn' ? '' : 'none'); + + if (options.buttons === 'auto' && ('ontouchstart' in window || currentGallery.length === 1)) { + options.buttons = false; + } + + previousButton.style.display = nextButton.style.display = (options.buttons ? '' : 'none'); + } + + function showOverlay(chosenImageIndex) { + if (options.noScrollbars) { + document.documentElement.style.overflowY = 'hidden'; + document.body.style.overflowY = 'scroll'; + } + if (overlay.style.display === 'block') { + return; + } + + bind(document, 'keydown', keyDownHandler); + currentIndex = chosenImageIndex; + touch = { + count: 0, + startX: null, + startY: null + }; + loadImage(currentIndex, function () { + preloadNext(currentIndex); + preloadPrev(currentIndex); + }); + + updateOffset(); + overlay.style.display = 'block'; + // Fade in overlay + setTimeout(function () { + overlay.className = 'visible'; + if (options.bodyClass && document.body.classList) { + document.body.classList.add(options.bodyClass); + } + if (options.afterShow) { + options.afterShow(); + } + }, 50); + if (options.onChange) { + options.onChange(currentIndex, imagesElements.length); + } + documentLastFocus = document.activeElement; + initFocus(); + isOverlayVisible = true; + } + + function initFocus() { + if (options.buttons) { + previousButton.focus(); + } else { + closeButton.focus(); + } + } + + function hideOverlay(e) { + ev(e); + if (options.noScrollbars) { + document.documentElement.style.overflowY = 'auto'; + document.body.style.overflowY = 'auto'; + } + if (overlay.style.display === 'none') { + return; + } + + unbind(document, 'keydown', keyDownHandler); + // Fade out and hide the overlay + overlay.className = ''; + setTimeout(function () { + overlay.style.display = 'none'; + if (options.bodyClass && document.body.classList) { + document.body.classList.remove(options.bodyClass); + } + if (options.afterHide) { + options.afterHide(); + } + documentLastFocus && documentLastFocus.focus(); + isOverlayVisible = false; + }, 500); + } + + function loadImage(index, callback) { + var imageContainer = imagesElements[index]; + var galleryItem = currentGallery[index]; + + if (typeof imageContainer === 'undefined' || typeof galleryItem === 'undefined') { + return; // out-of-bounds or gallery dirty + } + + if (imageContainer.getElementsByTagName('img')[0]) { + // image is loaded, cb and bail + if (callback) { + callback(); + } + return; + } + + var imageElement = galleryItem.imageElement, + imageSrc = imageElement.href, + thumbnailElement = imageElement.getElementsByTagName('img')[0], + imageCaption = typeof options.captions === 'function' ? + options.captions.call(currentGallery, imageElement) : + imageElement.getAttribute('data-caption') || imageElement.title; + + var figure = mknod('figure'); + figure.id = 'baguetteBox-figure-' + index; + figure.innerHTML = '