hesabixWpTheme/assets/js/bbpress-media.js

257 lines
7.4 KiB
JavaScript

(function () {
'use strict';
var VIDEO_EXT = /\.(mp4|webm|ogg|mov|m4v|avi|mkv)(\?.*)?$/i;
var IMAGE_EXT = /\.(jpe?g|png|gif|webp|bmp|svg)(\?.*)?$/i;
var FILE_EXT = /\.([a-z0-9]{2,5})(\?.*)?$/i;
function getContentRoots() {
return document.querySelectorAll(
'#bbpress-forums .bbp-reply-content, #bbpress-forums .bbp-topic-content'
);
}
function getFileName(url) {
try {
var path = new URL(url, window.location.href).pathname;
return decodeURIComponent(path.split('/').pop() || 'file');
} catch (e) {
return 'file';
}
}
function createLightbox() {
var root = document.getElementById('hesabix2-bbp-lightbox');
if (root) {
return root;
}
root = document.createElement('div');
root.id = 'hesabix2-bbp-lightbox';
root.className = 'hesabix2-bbp-lightbox';
root.hidden = true;
root.innerHTML =
'<div class="hesabix2-bbp-lightbox__backdrop" data-lightbox-close></div>' +
'<div class="hesabix2-bbp-lightbox__dialog" role="dialog" aria-modal="true" aria-label="نمایش تصویر">' +
'<button type="button" class="hesabix2-bbp-lightbox__close" data-lightbox-close aria-label="بستن">&times;</button>' +
'<img class="hesabix2-bbp-lightbox__image" alt="" />' +
'</div>';
document.body.appendChild(root);
root.addEventListener('click', function (event) {
if (event.target.matches('[data-lightbox-close]')) {
closeLightbox(root);
}
});
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape' && !root.hidden) {
closeLightbox(root);
}
});
return root;
}
function openLightbox(url, alt) {
var root = createLightbox();
var image = root.querySelector('.hesabix2-bbp-lightbox__image');
image.src = url;
image.alt = alt || '';
root.hidden = false;
document.body.classList.add('hesabix2-bbp-lightbox-open');
root.querySelector('.hesabix2-bbp-lightbox__close').focus();
}
function closeLightbox(root) {
var image = root.querySelector('.hesabix2-bbp-lightbox__image');
root.hidden = true;
image.removeAttribute('src');
document.body.classList.remove('hesabix2-bbp-lightbox-open');
}
function wrapImage(img) {
if (img.closest('.hesabix2-bbp-media--image, .hesabix2-bbp-lightbox, .bbp-attachments')) {
return;
}
var src = img.getAttribute('src');
if (!src || src.indexOf('data:') === 0) {
return;
}
var figure = document.createElement('figure');
figure.className = 'hesabix2-bbp-media hesabix2-bbp-media--image';
var button = document.createElement('button');
button.type = 'button';
button.className = 'hesabix2-bbp-media__trigger';
button.setAttribute('data-media-lightbox', src);
button.setAttribute('aria-label', 'بزرگ‌نمایی تصویر');
img.classList.add('hesabix2-bbp-media__image');
img.loading = img.loading || 'lazy';
img.decoding = img.decoding || 'async';
var parent = img.parentElement;
if (parent && parent.tagName === 'P' && parent.childNodes.length === 1) {
parent.replaceWith(figure);
} else {
img.replaceWith(figure);
}
button.appendChild(img);
figure.appendChild(button);
}
function upgradeFileLink(link) {
if (
link.closest('.hesabix2-bbp-media--file, .hesabix2-bbp-bale-meta, .bbp-reply-header, .bbp-topic-header') ||
link.classList.contains('hesabix2-bbp-media--file')
) {
return;
}
var href = link.getAttribute('href');
if (!href || href.indexOf('mailto:') === 0 || href.indexOf('#') === 0) {
return;
}
var name = (link.textContent || '').trim() || getFileName(href);
if (IMAGE_EXT.test(href) || VIDEO_EXT.test(href)) {
return;
}
if (!FILE_EXT.test(href) && !link.hasAttribute('download')) {
return;
}
link.classList.add('hesabix2-bbp-media', 'hesabix2-bbp-media--file');
if (!link.hasAttribute('download')) {
link.setAttribute('download', name);
}
if (!link.querySelector('.hesabix2-bbp-media__file-icon')) {
var icon = document.createElement('span');
icon.className = 'hesabix2-bbp-media__file-icon';
icon.setAttribute('aria-hidden', 'true');
link.insertBefore(icon, link.firstChild);
var body = document.createElement('span');
body.className = 'hesabix2-bbp-media__file-body';
var nameEl = document.createElement('span');
nameEl.className = 'hesabix2-bbp-media__file-name';
nameEl.textContent = name;
var action = document.createElement('span');
action.className = 'hesabix2-bbp-media__file-action';
action.textContent = 'دانلود فایل';
while (link.childNodes.length > 1) {
link.removeChild(link.lastChild);
}
body.appendChild(nameEl);
body.appendChild(action);
link.appendChild(body);
}
}
function upgradeVideoLink(link) {
if (link.closest('.hesabix2-bbp-media--video, .hesabix2-bbp-bale-meta')) {
return;
}
var href = link.getAttribute('href');
if (!href || !VIDEO_EXT.test(href)) {
return;
}
var figure = document.createElement('figure');
figure.className = 'hesabix2-bbp-media hesabix2-bbp-media--video';
var video = document.createElement('video');
video.className = 'hesabix2-bbp-media__player';
video.controls = true;
video.playsInline = true;
video.preload = 'metadata';
var source = document.createElement('source');
source.src = href;
video.appendChild(source);
figure.appendChild(video);
var parent = link.parentElement;
if (parent && parent.tagName === 'P' && parent.childNodes.length === 1) {
parent.replaceWith(figure);
} else {
link.replaceWith(figure);
}
}
function enhanceGdAttachments(root) {
root.querySelectorAll('.bbp-attachments .d4p-bba-thumbnails a, .bbp-attachments .d4p-bbp-listing a').forEach(function (link) {
var href = link.getAttribute('href') || '';
var img = link.querySelector('img');
if (img) {
img.classList.add('hesabix2-bbp-media__image');
link.setAttribute('data-media-lightbox', img.getAttribute('src') || href);
link.classList.add('hesabix2-bbp-media__trigger');
return;
}
if (VIDEO_EXT.test(href)) {
upgradeVideoLink(link);
} else if (!IMAGE_EXT.test(href)) {
upgradeFileLink(link);
}
});
}
function enhanceContent(root) {
root.querySelectorAll('img').forEach(wrapImage);
root.querySelectorAll('a[href]').forEach(function (link) {
var href = link.getAttribute('href') || '';
if (VIDEO_EXT.test(href)) {
upgradeVideoLink(link);
} else {
upgradeFileLink(link);
}
});
root.querySelectorAll('video').forEach(function (video) {
video.classList.add('hesabix2-bbp-media__player');
if (!video.closest('.hesabix2-bbp-media--video')) {
var figure = document.createElement('figure');
figure.className = 'hesabix2-bbp-media hesabix2-bbp-media--video';
video.parentNode.insertBefore(figure, video);
figure.appendChild(video);
}
video.controls = true;
video.playsInline = true;
});
enhanceGdAttachments(root);
}
function bindLightboxTriggers() {
document.addEventListener('click', function (event) {
var trigger = event.target.closest('[data-media-lightbox]');
if (!trigger || !trigger.closest('#bbpress-forums')) {
return;
}
event.preventDefault();
var url = trigger.getAttribute('data-media-lightbox');
var img = trigger.querySelector('img');
openLightbox(url, img ? img.getAttribute('alt') : '');
});
}
function init() {
getContentRoots().forEach(enhanceContent);
bindLightboxTriggers();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();