(function (window, document, $){
'use strict';
const state={
loader: {
currentButton: null,
originalContent: null
}};
const Loader={
initStyles: function (){
if(!document.getElementById('dashicon-rotate-style')){
const style=document.createElement('style');
style.id='dashicon-rotate-style';
style.textContent=`
@keyframes rotate {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }}
.button-spinner.dashicons {
display: inline-block;
animation: rotate 1s linear infinite;
text-align: center;
flex-shrink: 0;
}
`;
document.head.appendChild(style);
}},
show: function (button){
if(state.loader.currentButton===button) return;
state.loader.currentButton=button;
state.loader.originalContent=button.innerHTML;
const rect=button.getBoundingClientRect();
button.style.height=rect.height + 'px';
button.style.minWidth=rect.width + 'px';
button.style.width='auto';
button.style.whiteSpace='nowrap';
const computedStyle=window.getComputedStyle(button);
if(computedStyle.display!=='flex'){
button.style.display='inline-flex';
button.style.justifyContent='center';
button.style.alignItems='center';
button.style.gap='8px';
}
const spinner=document.createElement('span');
spinner.className='button-spinner dashicons dashicons-update';
const existingIcon=button.querySelector('.dashicons');
if(existingIcon){
existingIcon.replaceWith(spinner);
}else{
button.prepend(spinner);
}},
restore: function (){
const btn=state.loader.currentButton;
if(btn&&state.loader.originalContent!==null){
btn.innerHTML=state.loader.originalContent;
btn.style.width='';
btn.style.minWidth='';
btn.style.height='';
btn.style.display='';
btn.style.justifyContent='';
btn.style.alignItems='';
btn.style.gap='';
btn.style.whiteSpace='';
}
state.loader.currentButton=null;
state.loader.originalContent=null;
}};
const Popup={
open: function (id){
const overlay=document.getElementById(id + '-overlay');
if(overlay) overlay.style.display='flex';
if(typeof Loader!=='undefined') Loader.restore();
},
close: function (overlayElement){
if(overlayElement) overlayElement.style.display='none';
if(typeof Loader!=='undefined') Loader.restore();
},
handleGlobalClick: function (e){
const target=e.target;
if(target.closest('.torlib-popup-close')||target.classList.contains('torlib-popup-overlay')||target.classList.contains('torlib-popup-close-footer')){
e.preventDefault();
e.stopPropagation();
const overlay=target.closest('.torlib-popup-overlay');
this.close(overlay);
return;
}
const button=target.closest('[data-popup-target], .torlib-action-loader');
if(button){
e.stopPropagation();
if(button.querySelector('.button-spinner')){
e.preventDefault();
return;
}
const isPopupTrigger=button.hasAttribute('data-popup-target');
if(button.classList.contains('tf-no-loader')){
e.preventDefault();
if(isPopupTrigger){
const targetId=button.getAttribute('data-popup-target');
this.open(targetId);
}
e.stopImmediatePropagation();
return;
}
const isLink=button.tagName==='A';
const href=button.getAttribute('href');
const hasRealHref=isLink&&href&&href!=='#'&&!href.startsWith('javascript');
if(isPopupTrigger||!hasRealHref){
e.preventDefault();
}
if(hasRealHref&&!isPopupTrigger){
Loader.show(button);
setTimeout(()=> Loader.restore(), 3000);
return;
}
requestAnimationFrame(()=> Loader.show(button));
if(isPopupTrigger){
const targetId=button.getAttribute('data-popup-target');
if(targetId){
setTimeout(()=> this.open(targetId), 150);
}}
}}
};
const Media={
init: function (){
if(typeof wp==='undefined'||!wp.media){
return;
}
document.addEventListener('click', this.handleClick.bind(this));
},
handleClick: function (e){
const uploadBtn=e.target.closest('.toret-media-upload-btn');
if(uploadBtn){
e.preventDefault();
this.openUploader(uploadBtn);
return;
}
const removeBtn=e.target.closest('.toret-media-remove-btn');
if(removeBtn){
e.preventDefault();
this.removeImage(removeBtn);
return;
}},
openUploader: function (button){
const container=button.closest('.toret-media-uploader');
const inputId=container.querySelector('.toret-media-id');
const previewImg=container.querySelector('.toret-media-preview');
const placeholderIcon=container.querySelector('.dashicons');
const removeBtn=container.querySelector('.toret-media-remove-btn');
const previewWrapper=container.querySelector('.toret-media-preview-wrapper');
let frame=button.frame;
if(!frame){
frame=wp.media({
title: 'Select Image',
button: { text: 'Use this image' },
multiple: false
});
button.frame=frame;
frame.on('select', function (){
const attachment=frame.state().get('selection').first().toJSON();
inputId.value=attachment.id;
previewImg.src=attachment.sizes&&attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url:attachment.url;
previewImg.classList.remove('hidden');
placeholderIcon.classList.add('hidden');
removeBtn.classList.remove('hidden');
previewWrapper.classList.remove('no-image');
});
}
frame.open();
},
removeImage: function (button){
const container=button.closest('.toret-media-uploader');
const inputId=container.querySelector('.toret-media-id');
const previewImg=container.querySelector('.toret-media-preview');
const placeholderIcon=container.querySelector('.dashicons');
const previewWrapper=container.querySelector('.toret-media-preview-wrapper');
inputId.value='';
previewImg.src='';
previewImg.classList.add('hidden');
placeholderIcon.classList.remove('hidden');
button.classList.add('hidden');
previewWrapper.classList.add('no-image');
}};
const UI={
initCopyLink: function (){
$(document).on("click", '.toret-copy-link', function (e){
const link=this.getAttribute('data-link');
if(link&&navigator.clipboard){
navigator.clipboard.writeText(link);
}});
}};
window.ToretLib={
showButtonLoader: Loader.show,
restoreButton: Loader.restore,
openPopup: Popup.open
};
$(function (){
Loader.initStyles();
UI.initCopyLink();
Media.init();
document.addEventListener('click', Popup.handleGlobalClick.bind(Popup), true);
});
})(window, document, jQuery);
document.addEventListener('DOMContentLoaded', function (){
const lightbox=document.getElementById('wp-lightbox');
const lightboxImg=lightbox.querySelector('img');
const leftArrow=lightbox.querySelector('.wp-lightbox-arrow.left');
const rightArrow=lightbox.querySelector('.wp-lightbox-arrow.right');
const galleryLinks=Array.from(document.querySelectorAll('.gallery .gallery-icon a'));
let currentIndex=-1;
galleryLinks.forEach((link, index)=> {
link.addEventListener('click', function (e){
const imgSrc=this.getAttribute('href');
if(/\.(jpe?g|png|gif|webp|bmp|svg)$/i.test(imgSrc)){
e.preventDefault();
currentIndex=index;
showLightbox(imgSrc);
}});
});
function showLightbox(src){
lightboxImg.src=src;
lightbox.classList.add('active');
}
function showNext(){
if(currentIndex < galleryLinks.length - 1){
currentIndex++;
showLightbox(galleryLinks[currentIndex].href);
}}
function showPrev(){
if(currentIndex > 0){
currentIndex--;
showLightbox(galleryLinks[currentIndex].href);
}}
rightArrow.addEventListener('click', function (e){
e.stopPropagation();
showNext();
});
leftArrow.addEventListener('click', function (e){
e.stopPropagation();
showPrev();
});
lightbox.addEventListener('click', function (){
lightbox.classList.remove('active');
lightboxImg.src='';
currentIndex=-1;
});
document.addEventListener('keydown', function (e){
if(!lightbox.classList.contains('active')) return;
if(e.key==='ArrowRight') showNext();
if(e.key==='ArrowLeft') showPrev();
if(e.key==='Escape'){
lightbox.classList.remove('active');
lightboxImg.src='';
currentIndex=-1;
}});
});