philomena/assets/js/imagesclientside.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-16 23:45:58 +02:00
/**
* Simple image spoiler functionality.
*/
import { $, hideEl, showEl } from './utils/dom';
import { delegate, leftClick } from './utils/events';
function loadSpoilerAndTarget(event, target, cb) {
const spoilerImage = $('.js-spoiler-image', target);
const targetImage = $('.js-spoiler-target', target);
if (!spoilerImage || !targetImage) return;
event.preventDefault();
cb(spoilerImage, targetImage);
}
function unspoiler(event, target) {
loadSpoilerAndTarget(event, target, (spoilerImage, targetImage) => {
hideEl(spoilerImage);
showEl(targetImage);
});
}
function spoiler(event, target) {
loadSpoilerAndTarget(event, target, (spoilerImage, targetImage) => {
showEl(spoilerImage);
hideEl(targetImage);
});
}
export function configureSpoilers() {
switch (window.booru.spoilerType) {
case 'click':
delegate(document, 'click', {'.image-container': leftClick(unspoiler)});
delegate(document, 'mouseleave', {'.image-container': spoiler});
break;
case 'hover':
delegate(document, 'mouseenter', {'.image-container': unspoiler});
delegate(document, 'mouseleave', {'.image-container': spoiler});
break;
default:
break;
}
}