(function($){
'use strict';
const CekonayPostIt={
checkedKeys: [],
isOpen: false,
touchStartX: 0,
touchStartY: 0,
isTouchScrolling: false,
init: function(){
this.loadCheckedState();
this.bindEvents();
this.loadCheckedState();
this.syncCheckedStateWithDom();
this.updateCounter();
this.refreshScrollHints();
if(this.shouldRestoreOpenState()&&$('#cekonay-postit-checklist').length){
this.openPostit(false);
}},
loadCheckedState: function(){
const cookie=this.getCookie('cekonay_postit_checked');
if(cookie){
try {
const parsed=JSON.parse(decodeURIComponent(cookie));
if(Array.isArray(parsed)){
const unique=[];
parsed.forEach(function(key){
if(key===null||typeof key==='object') return;
const normalized=String(key);
if(normalized!==''&&!unique.includes(normalized)){
unique.push(normalized);
}});
this.checkedKeys=unique;
return;
}} catch(e){
}}
this.checkedKeys=[];
},
getDomKeys: function(){
const keys=[];
$('.cekonay-postit-item').each(function(){
const key=String($(this).data('key')||'');
if(key!==''&&!keys.includes(key)){
keys.push(key);
}});
return keys;
},
syncCheckedStateWithDom: function(){
const domKeys=this.getDomKeys();
const domSet=new Set(domKeys);
const previousLength=this.checkedKeys.length;
this.checkedKeys=this.checkedKeys.filter(function(key, idx, arr){
return domSet.has(key)&&arr.indexOf(key)===idx;
});
const checkedSet=new Set(this.checkedKeys);
$('.cekonay-postit-item').each(function(){
const key=String($(this).data('key')||'');
$(this).toggleClass('checked', checkedSet.has(key));
});
if(this.checkedKeys.length!==previousLength){
this.saveState(false);
}},
bindEvents: function(){
const self=this;
$(document).on('click', '.cekonay-postit-item', this.toggleItem.bind(this));
$(document).on('touchstart', '.cekonay-postit-item', this.handleTouchStart.bind(this));
$(document).on('touchmove', '.cekonay-postit-item', this.handleTouchMove.bind(this));
$(document).on('click', '#cekonay-postit-close', function(e){
e.stopPropagation();
self.closePostit();
});
$(document).on('click', '#cekonay-postit-mobile-trigger, .cekonay-postit-page-trigger', function(){
self.openPostit();
});
$(document.body).on('wc_fragment_refresh', function(){
if($('body').hasClass('cekonay-postit-open')){
self.rememberOpenState(true);
}else{
self.rememberOpenState(false);
}});
$(document.body).on('wc_fragments_refreshed wc_fragments_loaded', function(){
if(self.shouldRestoreOpenState()){
setTimeout(function(){
self.openPostit(false);
}, 50);
}
self.loadCheckedState();
self.syncCheckedStateWithDom();
self.updateCounter();
self.refreshScrollHints();
});
$(window).on('resize', function(){
self.refreshScrollHints();
});
},
bindScrollHintEvents: function(){
const self=this;
$('.cekonay-postit-items')
.off('scroll.cekonayPostitHint')
.on('scroll.cekonayPostitHint', function(){
self.updateScrollHint($(this));
});
},
refreshScrollHints: function(){
this.bindScrollHintEvents();
const self=this;
$('.cekonay-postit-items').each(function(){
self.updateScrollHint($(this));
});
},
updateScrollHint: function($items){
if(!$items.length){
return;
}
const element=$items.get(0);
if(!element){
return;
}
const $postit=$items.closest('.cekonay-postit-checklist');
const hasOverflow=(element.scrollHeight - element.clientHeight) > 6;
const hasMoreBelow=(element.scrollTop + element.clientHeight) < (element.scrollHeight - 8);
$postit.toggleClass('is-scrollable', hasOverflow);
$postit.toggleClass('has-more-items', hasOverflow&&hasMoreBelow);
},
handleTouchStart: function(e){
const touch=e.originalEvent.touches&&e.originalEvent.touches[0];
if(!touch){
return;
}
this.touchStartX=touch.clientX;
this.touchStartY=touch.clientY;
this.isTouchScrolling=false;
},
handleTouchMove: function(e){
const touch=e.originalEvent.touches&&e.originalEvent.touches[0];
if(!touch){
return;
}
const deltaX=Math.abs(touch.clientX - this.touchStartX);
const deltaY=Math.abs(touch.clientY - this.touchStartY);
if(deltaY > 8||deltaX > 8){
this.isTouchScrolling=true;
}},
openPostit: function(rememberState=true){
const $postit=$('#cekonay-postit-checklist');
if(!$postit.length){
return;
}
$('body').addClass('cekonay-postit-open');
this.isOpen=true;
$('.cekonay-postit-mobile-trigger, .cekonay-postit-page-trigger').attr('aria-expanded', 'true');
this.refreshScrollHints();
if(rememberState){
this.rememberOpenState(true);
}
if(!$postit.data('animated')){
this.animateCheckboxes();
$postit.data('animated', true);
}},
closePostit: function(){
$('body').removeClass('cekonay-postit-open');
this.isOpen=false;
$('.cekonay-postit-mobile-trigger, .cekonay-postit-page-trigger').attr('aria-expanded', 'false');
this.rememberOpenState(false);
},
togglePostit: function(){
if(this.isOpen){
this.closePostit();
}else{
this.openPostit();
}},
animateCheckboxes: function(){
const $items=$('.cekonay-postit-item');
$items.each(function(index){
const $item=$(this);
const $checkbox=$item.find('.cekonay-postit-checkbox');
const $text=$item.find('.cekonay-postit-text');
$checkbox.css({
opacity: 0,
transform: 'scale(0.8)'
});
$text.css({
opacity: 0
});
setTimeout(function(){
$checkbox.css({
opacity: 1,
transform: 'scale(1)',
transition: 'all 0.3s ease'
});
$text.css({
opacity: 1,
transition: 'opacity 0.3s ease'
});
}, index * 50);
});
},
toggleItem: function(e){
e.preventDefault();
if(this.isTouchScrolling){
this.isTouchScrolling=false;
return;
}
const $item=$(e.currentTarget);
const key=String($item.data('key')||'');
if(!key){
return;
}
$item.toggleClass('checked');
const index=this.checkedKeys.indexOf(key);
if(index > -1){
this.checkedKeys.splice(index, 1);
}else{
this.checkedKeys.push(key);
}
this.saveState();
this.updateCounter();
this.refreshScrollHints();
},
saveState: function(animate=true){
document.cookie='cekonay_postit_checked=' + encodeURIComponent(JSON.stringify(this.checkedKeys)) + '; path=/; max-age=' + (7 * 24 * 60 * 60);
if(animate){
$('.cekonay-postit-count').addClass('pulse');
setTimeout(function(){
$('.cekonay-postit-count').removeClass('pulse');
}, 300);
}},
rememberOpenState: function(isOpen){
try {
if(isOpen){
sessionStorage.setItem('cekonay_postit_was_open', 'true');
}else{
sessionStorage.removeItem('cekonay_postit_was_open');
}} catch (e){
}},
shouldRestoreOpenState: function(){
try {
return sessionStorage.getItem('cekonay_postit_was_open')==='true';
} catch (e){
return false;
}},
updateCounter: function(){
this.syncCheckedStateWithDom();
const checkedCount=this.checkedKeys.length;
$('.cekonay-postit-count .checked-count').text(checkedCount);
$('.cekonay-postit-mobile-trigger .badge').text(checkedCount + '/' + $('.cekonay-postit-item').length);
$('.cekonay-postit-page-trigger .badge').text(checkedCount + '/' + $('.cekonay-postit-item').length);
},
hasCartItems: function(){
const count=$('.cekonay-flycart-count').data('count')||0;
return count > 0;
},
getCookie: function(name){
const value=`; ${document.cookie}`;
const parts=value.split(`; ${name}=`);
if(parts.length===2) return parts.pop().split(';').shift();
return null;
}};
const style=document.createElement('style');
style.textContent=`
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }}
.cekonay-postit-count.pulse {
animation: pulse 0.3s ease;
}
`;
document.head.appendChild(style);
$(document).ready(function(){
if($('.cekonay-postit-checklist').length){
CekonayPostIt.init();
}});
window.CekonayPostIt=CekonayPostIt;
$(document.body).on('click', '.cekonay-open-postit-btn, .cekonay-flycart-postit-notice button', function(e){
e.preventDefault();
if(window.CekonayPostIt&&typeof window.CekonayPostIt.openPostit==='function'){
window.CekonayPostIt.openPostit();
}});
})(jQuery);