document.addEventListener('DOMContentLoaded', function (){
const bundleButton=document.getElementById('add-bundle-to-cart');
if(!bundleButton) return;
if(typeof pbb_ajax==='undefined'){
console.error('PBB: pbb_ajax není definovan!');
return;
}
if(typeof pbb_i18n==='undefined'){
console.error('PBB: pbb_i18n není definovan!');
return;
}
bundleButton.addEventListener('click', function (){
const selected=document.querySelector('input[name="selected_bundle"]:checked');
const bundleType=selected?.value||null;
const productId=bundleButton.dataset.product_id;
if(!bundleType||!productId){
alert(pbb_i18n.select_bundle);
return;
}
bundleButton.classList.add('loading');
bundleButton.disabled=true;
const formData=new URLSearchParams({
action: 'pbb_add_bundle_to_cart',
bundle_type: bundleType,
product_id: productId
});
fetch(pbb_ajax.ajax_url, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: formData
})
.then(response=> response.json())
.then(payload=> {
bundleButton.classList.remove('loading');
bundleButton.disabled=false;
if(payload.success){
const title=encodeURIComponent(payload.data.product_title||pbb_i18n.default_title);
const url=new URL(window.location.href);
url.searchParams.set('pbb_notice', 'success');
url.searchParams.set('title', payload.data.product_title||'');
window.location.href=url.toString();
}else{
const msg=typeof payload.data==='string'
? payload.data
: pbb_i18n.unknown_error;
alert(pbb_i18n.add_error + ' ' + msg);
}})
.catch(error=> {
bundleButton.classList.remove('loading');
bundleButton.disabled=false;
console.error('PBB: Chyba při AJAX požadavku:', error);
alert(pbb_i18n.unknown_error);
});
});
});
function parseI18Json(el){
const str=el?.dataset.i18n
try {
return JSON.parse(str||'{}')
} catch (e){
console.error(e)
return {}}
}
function initializeAlxAudio(el){
const i18n=parseI18Json(document.querySelector('.alx-audio [data-i18n]'))
let audio=el.querySelector('audio');
if(!audio){
audio=document.createElement('audio');
audio.preload='none';
const preScript=el.querySelector('.pre-script')
audio.src=preScript?.href
el.prepend(audio)
}
audio.controls=false;
if(!audio.src){
console.warn("No audio src on", el);
return;
}
let controls=el.querySelector('.controls');
if(controls){
controls.innerHTML='';
}else{
controls=document.createElement('div');
controls.className='controls';
}
const btnPlay=document.createElement('button');
btnPlay.type='button';
btnPlay.innerHTML=`<span role="presentation">▶︎</span><span class="caption">${i18n.play}</span>`;
btnPlay.dataset.action='play';
btnPlay.addEventListener('click', ()=> {
if(el.dataset.status==='idle'){
el.dataset.status='loading';
}
audio.play()
});
controls.appendChild(btnPlay);
const btnPause=document.createElement('button');
btnPause.type='button';
btnPause.innerHTML=`<span role="presentation">⏸︎</span><span class="caption">${i18n.pause}</span>`;
btnPause.dataset.action='pause';
btnPause.addEventListener('click', ()=> audio.pause());
controls.appendChild(btnPause);
const progress=document.createElement('progress');
progress.value=0;
progress.max=100;
controls.appendChild(progress);
const position=document.createElement('span');
position.classList.add('position');
position.innerText='-0:00';
controls.appendChild(position);
el.append(controls);
audio.addEventListener('playing', ()=> {
el.dataset.status='playing';
if(!positionUpdateInterval){
positionUpdateInterval=setInterval(updateProgress, 500);
}});
audio.addEventListener('pause', ()=> {
el.dataset.status='paused';
if(positionUpdateInterval){
clearInterval(positionUpdateInterval);
positionUpdateInterval=null;
}});
audio.addEventListener('durationchange', ()=> {
progress.max=audio.duration;
});
progress.addEventListener('click', e=> {
const rect=e.target.getBoundingClientRect();
const pct=(e.clientX - rect.left) / rect.width;
const targetTime=pct * audio.duration;
audio.currentTime=targetTime;
updateProgress()
});
let positionUpdateInterval;
function updateProgress(){
progress.value=audio.currentTime;
position.innerText='-' + formatTime(audio.duration - audio.currentTime);
}
function formatTime(seconds){
const minutes=Math.floor(seconds / 60);
seconds -=minutes * 60;
const secondsPadded=(Math.floor(seconds) + '').padStart(2, '0');
return `${minutes}:${secondsPadded}`;
}
el.dataset.status='idle';
}
document.querySelectorAll('.alx-audio').forEach(el=> initializeAlxAudio(el));
document.addEventListener('DOMContentLoaded', function (){
const ratingOverview=document.querySelector('.rating-overview');
if(!ratingOverview?.matches('.-filterable')){
return;
}
const commentsSelector=ratingOverview.dataset.ratingCommentsSelector;
if(!commentsSelector){
return console.warn('filterable rating does not have [data-comments-selector] set');
}
const searchParamName=ratingOverview.dataset.ratingSearchParamName
function getCurrentRating(){
return ratingOverview.querySelector('[data-rating][aria-current="true"]')?.dataset.rating;
}
function updateLocation(rating){
if(!searchParamName) return
const url=new URL(window.location);
rating ? url.searchParams.set('filtrHodnoceni', rating):url.searchParams.delete('filtrHodnoceni');
window.history.replaceState({}, '', url);
}
function updateOverviewActiveItem(rating){
ratingOverview.querySelectorAll('[data-rating]').forEach((ratingEl)=> {
ratingEl.removeAttribute('aria-current');
if(ratingEl.dataset.rating===rating){
ratingEl.setAttribute('aria-current', 'true');
}})
}
function updateRatingVisibility(rating){
document.querySelectorAll(commentsSelector + " [data-rating]").forEach((ratingEl)=> {
const reviewRating=ratingEl.dataset.rating;
const visible = !rating||!reviewRating||reviewRating==rating;
visible ? ratingEl.setAttribute('aria-current', 'true'):ratingEl.removeAttribute('aria-current');
})
}
ratingOverview.addEventListener('click', (event)=> {
const ratingEl=event.target.closest('[data-rating]');
if(!ratingEl) return;
let rating=ratingEl.getAttribute('data-rating');
if(rating===getCurrentRating()){
rating=null
}
event.preventDefault();
updateLocation(rating);
updateOverviewActiveItem(rating)
updateRatingVisibility(rating)
});
});