(function (){
'use strict';
var cfg=window.ufaqmPublic||{};
var viewed={};
function restUrl(path){
var base=(cfg.rest||'').replace(/\/?$/, '/');
return base + path.replace(/^\//, '');
}
function post(path, body){
return fetch(restUrl(path), {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
}).then(function (res){
return res.json().then(function (data){
return { ok: res.ok, status: res.status, data: data };});
}).catch(function (){
return { ok: false, status: 0, data: {}};});
}
function canTrack(){
if(!cfg.consentWanted){
return false;
}
if(cfg.consentRequired&&cfg.consent!=='granted'){
return false;
}
return true;
}
function recordView(faqId, context){
if(!cfg.views||!canTrack()||!faqId){
return;
}
var key=String(faqId) + ':' + context;
if(viewed[key]){
return;
}
viewed[key]=true;
try {
var stored=sessionStorage.getItem('ufaqm_v_' + key);
if(stored){
return;
}
sessionStorage.setItem('ufaqm_v_' + key, '1');
}
catch (e){
}
post('views', { faq_id: parseInt(faqId, 10), context: context });
}
function setExpanded(item, expanded){
var trigger=item.querySelector('.ufaqm-item__trigger');
var panel=item.querySelector('.ufaqm-item__panel');
if(!trigger||!panel){
return;
}
trigger.setAttribute('aria-expanded', expanded ? 'true':'false');
if(expanded){
panel.removeAttribute('hidden');
recordView(item.getAttribute('data-faq-id'), 'accordion');
}else{
panel.setAttribute('hidden', '');
}}
function bindList(list){
var exclusive=list.getAttribute('data-exclusive')==='1';
var items=Array.prototype.slice.call(list.querySelectorAll('.ufaqm-item'));
items.forEach(function (item){
var trigger=item.querySelector('.ufaqm-item__trigger');
if(!trigger){
if(item.getAttribute('data-view-on-load')==='1'){
recordView(item.getAttribute('data-faq-id'), 'detail');
}
return;
}
if(trigger.getAttribute('aria-expanded')==='true'){
recordView(item.getAttribute('data-faq-id'), 'accordion');
}
trigger.addEventListener('click', function (){
var open=trigger.getAttribute('aria-expanded')==='true';
if(exclusive&&!open){
items.forEach(function (other){
setExpanded(other, other===item);
});
return;
}
setExpanded(item, !open);
});
trigger.addEventListener('keydown', function (event){
if(event.key!=='Home'&&event.key!=='End'){
return;
}
event.preventDefault();
var index=event.key==='Home' ? 0:items.length - 1;
var next=items[index]&&items[index].querySelector('.ufaqm-item__trigger');
if(next){
next.focus();
}});
});
}
function findHashTarget(hash){
if(!hash){
return null;
}
var byId=document.getElementById(hash);
if(byId){
return byId;
}
var safe=hash.replace(/[^a-zA-Z0-9_\-]/g, '');
if(!safe){
return null;
}
var byHeading=document.querySelector('[data-ufaqm-heading="' + safe + '"]');
if(byHeading){
return byHeading;
}
return document.querySelector('[data-faq-anchor="' + safe + '"]');
}
function faqRoots(){
var pages=Array.prototype.slice.call(document.querySelectorAll('.ufaqm-page'));
var embeds=Array.prototype.slice.call(document.querySelectorAll('.ufaqm')).filter(function (el){
return !el.closest('.ufaqm-page');
});
return pages.concat(embeds);
}
function isFaqChrome(el){
return !!(el.closest&&(el.closest('.ufaqm-page')||el.closest('.ufaqm')||el.closest('#wpadminbar')));
}
function isPinnedTop(el){
var pos=window.getComputedStyle(el).position;
if(pos==='fixed'||pos==='sticky'||pos==='-webkit-sticky'){
return true;
}
return pos==='absolute'&&isHeaderLike(el);
}
function isHeaderLike(el){
if(!el||!el.matches){
return false;
}
return el.matches('header, [role="banner"], .site-header, .yop-vs-header, .elementor-location-header, [data-elementor-type="header"], .elementor-sticky, .elementor-sticky--active, .ekit-template-content-header, #header-top');
}
function isVisibleChrome(el){
var style=window.getComputedStyle(el);
if(style.display==='none'||style.visibility==='hidden'){
return false;
}
var opacity=parseFloat(style.opacity);
return !(opacity < 0.05);
}
function probeHeading(root){
if(!root||!root.querySelector){
return null;
}
return root.querySelector('.ufaqm-hero-band')||root.querySelector('.ufaqm-hero')||root.querySelector('#ufaqm-hero-title')||root.querySelector('.ufaqm-page__header h1')||root.querySelector('h1');
}
function hitsAt(x, y){
if(x < 0||x > window.innerWidth||y < 0||y > window.innerHeight){
return [];
}
if(typeof document.elementsFromPoint==='function'){
return document.elementsFromPoint(x, y)||[];
}
if(typeof document.elementFromPoint==='function'){
var one=document.elementFromPoint(x, y);
return one ? [one]:[];
}
return [];
}
function bandBottom(el){
if(!el||el===document.body||el===document.documentElement||isFaqChrome(el)||!isVisibleChrome(el)){
return 0;
}
if(!isPinnedTop(el)&&!isHeaderLike(el)){
return 0;
}
if(!isPinnedTop(el)){
return 0;
}
var box=el.getBoundingClientRect();
var maxH=Math.min(240, window.innerHeight * 0.4);
if(box.top > 120){
return 0;
}
if(box.height >=8&&box.height <=maxH){
return box.bottom;
}
if(box.height > maxH||box.height < 8){
var bottom=0;
var i;
for (i=0; i < el.children.length; i++){
var cb=el.children[i].getBoundingClientRect();
if(cb.top > 120||cb.height < 8||cb.height > maxH){
continue;
}
if(cb.bottom > bottom){
bottom=cb.bottom;
}}
return bottom;
}
return 0;
}
function walkBottom(el){
var bottom=0;
var n=0;
while (el&&n < 8){
var b=bandBottom(el);
if(b > bottom){
bottom=b;
}
el=el.parentElement;
n +=1;
}
return bottom;
}
function raiseFromHits(bottom, x, y){
var stack=hitsAt(x, y);
var i;
for (i=0; i < stack.length; i++){
var b=walkBottom(stack[i]);
if(b > bottom){
bottom=b;
}}
return bottom;
}
function topChromeBottom(root){
var bottom=0;
var listed=document.querySelectorAll('header, [role="banner"], .site-header, .yop-vs-header, .elementor-location-header, [data-elementor-type="header"], .elementor-sticky--active, .ekit-template-content-header, #header-top');
Array.prototype.forEach.call(listed, function (el){
var b=walkBottom(el);
if(b > bottom){
bottom=b;
}});
var y=6;
var bar=document.getElementById('wpadminbar');
if(bar){
y=Math.min(window.innerHeight - 2, Math.ceil(bar.getBoundingClientRect().bottom) + 2);
}
bottom=raiseFromHits(bottom, 48, y);
bottom=raiseFromHits(bottom, window.innerWidth / 2, y);
bottom=raiseFromHits(bottom, window.innerWidth - 48, y);
return Math.ceil(bottom);
}
function stickyOffset(){
if(typeof cfg.headerOffset==='number'){
return Math.max(0, Math.min(240, cfg.headerOffset));
}
var chrome=topChromeBottom(null);
var bar=document.getElementById('wpadminbar');
var floor=bar ? Math.ceil(bar.getBoundingClientRect().bottom):0;
return Math.max(0, Math.min(240, chrome - floor));
}
function appliedOffset(root){
var raw=root.style.getPropertyValue('--ufaqm-header-offset')||'';
var n=parseFloat(raw);
return isFinite(n) ? n:0;
}
function surfaceDocTop(el, root){
return el.getBoundingClientRect().top + window.pageYOffset - appliedOffset(root);
}
function applyHeaderClearance(){
faqRoots().forEach(function (root){
var px=0;
if(typeof cfg.headerOffset==='number'){
px=Math.max(0, Math.min(240, cfg.headerOffset));
}else{
var probe=probeHeading(root)||root;
var top=surfaceDocTop(probe, root);
var cover=topChromeBottom(root);
if(cover > top + 1){
px=Math.max(0, Math.min(240, Math.ceil(cover - top)));
}}
root.style.setProperty('--ufaqm-header-offset', px + 'px');
});
}
function scrollToTarget(el){
var reduce=window.matchMedia&&window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var top=el.getBoundingClientRect().top + window.pageYOffset - stickyOffset() - 8;
window.scrollTo({ top: Math.max(0, top), behavior: reduce ? 'auto':'smooth' });
}
function openFromHash(){
var hash=window.location.hash.replace('#', '');
var target=findHashTarget(hash);
if(!target){
return;
}
var item=target.closest('.ufaqm-item');
if(item){
setExpanded(item, true);
var trigger=item.querySelector('.ufaqm-item__trigger');
if(trigger){
trigger.focus();
}else{
item.focus();
}}
window.requestAnimationFrame(function (){
scrollToTarget(target);
});
}
function bindFeedback(root){
var status=root.querySelector('.ufaqm-helpfulness__status');
var reasons=root.querySelector('.ufaqm-helpfulness__reasons');
var faqId=parseInt(root.getAttribute('data-faq-id'), 10);
function send(vote, reason){
if(!cfg.feedback){
return;
}
if(!canTrack()){
if(status){
status.textContent=(cfg.i18n&&cfg.i18n.needConsent) ? cfg.i18n.needConsent:'';
}
return;
}
post('feedback', { faq_id: faqId, vote: vote, reason: reason||'' }).then(function (res){
if(!status){
return;
}
var i18n=cfg.i18n||{};
if(res.data&&res.data.duplicate){
status.textContent=i18n.duplicate||'';
return;
}
status.textContent=res.ok ? (i18n.thanks||''):(i18n.fail||'');
});
}
root.querySelectorAll('[data-vote]').forEach(function (btn){
btn.addEventListener('click', function (){
var vote=btn.getAttribute('data-vote');
if(vote==='no'&&reasons){
reasons.removeAttribute('hidden');
return;
}
send(vote, '');
});
});
if(reasons){
reasons.querySelectorAll('[data-reason]').forEach(function (btn){
btn.addEventListener('click', function (){
send('no', btn.getAttribute('data-reason'));
reasons.setAttribute('hidden', '');
});
});
}}
function bindConsent(){
if(!cfg.consentWanted||!cfg.consentRequired||cfg.consent!=='unknown'){
return;
}
if(document.querySelector('.ufaqm-consent')){
return;
}
var i18n=cfg.i18n||{};
var bar=document.createElement('div');
bar.className='ufaqm-consent';
var text=document.createElement('p');
text.textContent=i18n.consent||'';
var actions=document.createElement('p');
var grant=document.createElement('button');
grant.type='button';
grant.className='ufaqm-consent__grant';
grant.textContent=i18n.grant||'';
var deny=document.createElement('button');
deny.type='button';
deny.className='ufaqm-consent__deny';
deny.textContent=i18n.deny||'';
actions.appendChild(grant);
actions.appendChild(document.createTextNode(' '));
actions.appendChild(deny);
bar.appendChild(text);
bar.appendChild(actions);
document.body.appendChild(bar);
grant.addEventListener('click', function (){
post('consent', { state: 'granted' }).then(function (res){
if(res.ok){
cfg.consent='granted';
bar.remove();
}});
});
deny.addEventListener('click', function (){
post('consent', { state: 'denied' }).then(function (){
cfg.consent='denied';
bar.remove();
});
});
}
applyHeaderClearance();
window.addEventListener('load', function (){
applyHeaderClearance();
window.requestAnimationFrame(applyHeaderClearance);
});
window.addEventListener('resize', applyHeaderClearance);
window.setTimeout(applyHeaderClearance, 50);
window.setTimeout(applyHeaderClearance, 400);
window.setTimeout(applyHeaderClearance, 1200);
document.addEventListener('elementor/sticky/effect', applyHeaderClearance);
window.addEventListener('pageshow', applyHeaderClearance);
document.querySelectorAll('.ufaqm-list--accordion').forEach(bindList);
document.querySelectorAll('.ufaqm-list--list .ufaqm-item, .ufaqm-list--cards .ufaqm-item').forEach(function (item){
recordView(item.getAttribute('data-faq-id'), 'detail');
});
document.querySelectorAll('[data-view-on-load="1"]').forEach(function (el){
recordView(el.getAttribute('data-faq-id'), 'detail');
});
document.querySelectorAll('.ufaqm-helpfulness').forEach(bindFeedback);
bindConsent();
bindLiveSearch();
document.querySelectorAll('.ufaqm-share').forEach(bindShare);
openFromHash();
window.addEventListener('hashchange', openFromHash);
document.querySelectorAll('.ufaqm-toc a[href^="#"]').forEach(function (link){
link.addEventListener('click', function (){
window.setTimeout(openFromHash, 0);
});
});
function bindLiveSearch(){
var cfg=window.ufaqmPublic||{};
var i18n=cfg.i18n||{};
document.querySelectorAll('.ufaqm-search').forEach(function (form){
var input=form.querySelector('input[type="search"]');
if(!input){
return;
}
var listId=(input.id||'ufaqm-q') + '-suggest';
var status=document.createElement('p');
status.className='ufaqm-search__live';
status.setAttribute('role', 'status');
status.setAttribute('aria-live', 'polite');
var box=document.createElement('ul');
box.className='ufaqm-search__suggest';
box.id=listId;
box.hidden=true;
box.setAttribute('role', 'listbox');
form.appendChild(status);
form.appendChild(box);
input.setAttribute('role', 'combobox');
input.setAttribute('aria-autocomplete', 'list');
input.setAttribute('aria-expanded', 'false');
input.setAttribute('aria-controls', listId);
var timer=0;
var controller=null;
var active=-1;
function closeList(){
box.hidden=true;
box.textContent='';
input.setAttribute('aria-expanded', 'false');
input.removeAttribute('aria-activedescendant');
active=-1;
}
input.addEventListener('input', function (){
window.clearTimeout(timer);
var value=input.value.trim();
if(controller){
controller.abort();
controller=null;
}
if(value.length < 2){
closeList();
status.textContent='';
return;
}
timer=window.setTimeout(function (){
controller=typeof AbortController==='function' ? new AbortController():null;
var rest=String(cfg.rest||'').replace(/\/$/, '');
var url=rest + '/search';
var headers={};
headers['X-WP-Nonce']=String(cfg.restNonce||'');
var opts={
credentials: 'same-origin',
headers: headers,
signal: controller ? controller.signal:undefined
};
if(cfg.searchPost){
opts.method='POST';
headers['Content-Type']='application/json';
opts.body=JSON.stringify({ query: value, per_page: 10 });
}else{
url +='?query=' + encodeURIComponent(value) + '&per_page=10';
}
window.fetch(url, opts).then(function (res){
return res.json();
}).then(function (data){
var hits=data&&data.hits ? data.hits.slice(0, 10):[];
box.textContent='';
hits.forEach(function (hit, index){
var item=document.createElement('li');
item.id=listId + '-' + String(index);
item.setAttribute('role', 'option');
var link=document.createElement('a');
link.href=hit.url||'#';
link.textContent=hit.title||'';
item.appendChild(link);
box.appendChild(item);
});
box.hidden=hits.length===0;
input.setAttribute('aria-expanded', hits.length ? 'true':'false');
status.textContent=hits.length
? String(hits.length) + ' ' + (i18n.suggestions||'')
: (i18n.noSuggestions||'');
active=-1;
}).catch(function (err){
if(err&&err.name==='AbortError'){
return;
}
status.textContent=i18n.searchFail||'';
});
}, 250);
});
input.addEventListener('keydown', function (event){
var options=box.querySelectorAll('[role="option"]');
if(event.key==='Escape'){
closeList();
status.textContent='';
return;
}
if((event.key==='ArrowDown'||event.key==='ArrowUp')&&options.length){
event.preventDefault();
active=event.key==='ArrowDown'
? Math.min(options.length - 1, active + 1)
: Math.max(0, active - 1);
options.forEach(function (opt, index){
opt.setAttribute('aria-selected', index===active ? 'true':'false');
});
input.setAttribute('aria-activedescendant', options[active].id);
}
if(event.key==='Enter'&&active >=0&&options[active]){
var chosen=options[active].querySelector('a');
if(chosen){
event.preventDefault();
window.location.href=chosen.href;
}}
});
});
}
function bindShare(box){
var url=box.getAttribute('data-url')||'';
var title=box.getAttribute('data-title')||'';
var copied=box.getAttribute('data-copied')||'';
var status=box.querySelector('.ufaqm-share__status');
var copyBtn=box.querySelector('.ufaqm-share__copy');
var nativeBtn=box.querySelector('.ufaqm-share__native');
if(nativeBtn&&typeof navigator.share==='function'){
nativeBtn.hidden=false;
nativeBtn.addEventListener('click', function (){
navigator.share({ title: title, url: url }).catch(function (){ });
});
}
if(copyBtn){
copyBtn.addEventListener('click', function (){
var done=function (){
if(status){
status.textContent=copied;
}};
if(navigator.clipboard&&navigator.clipboard.writeText){
navigator.clipboard.writeText(url).then(done).catch(done);
return;
}
done();
});
}}
})();