(function ($){
function initializeBHSections(){
document
.querySelectorAll(".directorist-bh-dayzone__single")
.forEach((dayZone)=> {
const switchCheckbox=dayZone.querySelector('.directorist-bh-dayzone__single__switch input[type="checkbox"]'
);
const hourSelectionCheckbox=dayZone.querySelector('.directorist-bh-dayzone__single__hour-selection input[type="checkbox"]'
);
if(switchCheckbox)
dayZone.classList.toggle("directorist-enable-hour-time-selection",
switchCheckbox.checked
);
if(switchCheckbox.checked){
switchCheckbox.dispatchEvent(new Event("change", { bubbles: true }));
}
if(hourSelectionCheckbox)
dayZone.classList.toggle("directorist-full-time",
hourSelectionCheckbox.checked
);
});
}
function toggleBusinessHourSections(){
const isEnabled=$("#enable_directorist_bh_listing").is(":checked");
const isCustomHour=$("input[name='directorist_bh_option']").is(":checked"
);
$(".directorist-bh-section--type")
.toggleClass("directorist-bh-show", isEnabled)
.toggleClass("directorist-bh-hide", !isEnabled);
$(".directorist-bh-section--timezone")
.toggleClass("directorist-bh-show", isEnabled)
.toggleClass("directorist-bh-hide", !isEnabled);
$(".directorist-bh-section--selection")
.toggleClass("directorist-bh-show", isEnabled&&isCustomHour)
.toggleClass("directorist-bh-hide", !(isEnabled&&isCustomHour));
}
let isUpdatingNextDay=false;
function handleBHNextDayLabel($startSelect, $endSelect){
if(isUpdatingNextDay) return;
$selected=$endSelect.find("option:selected");
const startTime=$startSelect.val();
const endTime=$endSelect.val();
const timeToMinutes=(t)=> {
if(!t) return 0;
const [h, m]=t.split(":").map(Number);
return h * 60 + m;
};
const startTimeCount=timeToMinutes(startTime);
const endTimeCount=timeToMinutes(endTime);
if(!startTime||!endTime) return;
const isCrossingMidnight=startTimeCount >=720&&endTimeCount < 720;
const isCrossingToNextDay=endTimeCount < startTimeCount;
const dayMatch=$endSelect.attr("name")?.match(/\[([a-z]+)\]/i);
const day=dayMatch?.[1]||"";
const days=[
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
];
const nextDay=days[(days.indexOf(day) + 1) % 7];
const suffix=nextDay;
if((isCrossingMidnight||isCrossingToNextDay)&&nextDay){
if(!$selected.data("original-text")){
$selected.data("original-text", $selected.text());
}
const originalText=$selected.data("original-text");
$selected.attr("data-next-day", "true");
$selected.attr("data-next-day-suffix", suffix);
isUpdatingNextDay=true;
$endSelect.trigger("change");
isUpdatingNextDay=false;
setTimeout(()=> {
const rendered=$endSelect
.next(".select2")
.find(".select2-selection__rendered");
const mainText=originalText;
const spanHtml=`<span class="next-day-suffix">${suffix}</span>`;
rendered.html(`${mainText} ${spanHtml}`);
}, 0);
}else{
const originalText=$selected.data("original-text")||$selected.text();
$selected.text(originalText);
$selected.removeAttr("data-next-day data-next-day-suffix");
setTimeout(()=> {
const rendered=$endSelect
.next(".select2")
.find(".select2-selection__rendered");
rendered.html(originalText);
}, 0);
}}
function initializeBHLabelUpdate(){
$(".directorist-bh-dayzone__single__choice__item").each(function (){
const $container=$(this);
const $startSelect=$container.find(".directorist-selects--start select"
);
const $endSelect=$container.find(".directorist-selects--close select");
handleBHNextDayLabel($startSelect, $endSelect);
});
}
function handleStartTimeChange(e){
const $startSelect=$(e.currentTarget);
const $endSelect=$startSelect
.closest(".directorist-bh-dayzone__single__choice__item")
.find(".directorist-selects--close select");
handleBHNextDayLabel($startSelect, $endSelect);
}
function handleEndTimeChange(e){
const $endSelect=$(e.currentTarget);
const $startSelect=$endSelect
.closest(".directorist-bh-dayzone__single__choice__item")
.find(".directorist-selects--start select");
handleBHNextDayLabel($startSelect, $endSelect);
}
$("body").on("change",
".directorist-bh-dayzone__single__hour-selection .directorist-checkbox input",
function (){
if($(this).prop("checked")){
$(this)
.closest(".directorist-bh-dayzone__single__hour-selection")
.siblings(".directorist-bh-dayzone__single__choice")
.parent(".directorist-bh-dayzone__single")
.addClass("directorist-full-time");
}else{
$(this)
.closest(".directorist-bh-dayzone__single__hour-selection")
.siblings(".directorist-bh-dayzone__single__choice")
.parent(".directorist-bh-dayzone__single")
.removeClass("directorist-full-time");
}}
);
$("body").on("change", ".directorist-switch-input", function (){
const $wrapper=$(this).closest(".directorist-bh-dayzone__single");
if(this.checked){
$wrapper.addClass("directorist-enable-hour-time-selection");
$($wrapper)
.find("select")
.each(function (){
const $select=$(this);
if(!$select.hasClass("select2-hidden-accessible")){
const placeholder =
$select.closest(".directorist-selects--start").length > 0
? atbdp_business_hours.select_start_placeholder
: $select.closest(".directorist-selects--close").length > 0
? atbdp_business_hours.select_close_placeholder
: atbdp_business_hours.select_placeholder;
$select.select2({
placeholder,
allowClear: true,
width: "100%",
dropdownCssClass: "dbh-select2-dropdown",
containerCssClass: "dbh-select2-container",
});
}});
}else{
$wrapper.removeClass("directorist-enable-hour-time-selection");
if($wrapper.hasClass("directorist-full-time")){
$wrapper.removeClass("directorist-full-time");
$wrapper
.find('.directorist-bh-dayzone__single__hour-selection input[type="checkbox"]'
)
.prop("checked", false);
}}
});
$("body").on("click",
".directorist-bh-dayzone__single__choice__add",
function (e){
e.preventDefault();
const $choiceWrapper=$(this).closest(".directorist-bh-dayzone__single__choice"
);
const $originalItem=$choiceWrapper
.find(".directorist-bh-dayzone__single__choice__item")
.last();
const $originalClose=$originalItem.find(".directorist-selects--close select"
);
const $clonedItem=$originalItem
.clone()
.removeClass("directorist-not-removable");
$clonedItem.find("select").each(function (){
const $select=$(this);
if($select.hasClass("select2-hidden-accessible")){
$select.select2("destroy");
}
$select
.removeClass("select2-hidden-accessible")
.removeAttr("data-select2-id tabindex aria-hidden")
.siblings(".select2")
.remove();
$select.closest(".directorist-selects").removeAttr("data-select2-id");
$select
.closest(".directorist-bh-dayzone__single__choice__slot")
.removeAttr("data-select2-id");
$select.val(null);
});
$clonedItem.removeAttr("data-select2-id");
$choiceWrapper.removeAttr("data-select2-id");
$originalItem.after($clonedItem);
$choiceWrapper
.find(".directorist-bh-dayzone__single__choice__item")
.each(function (index){
const $startSelect=$(this).find(".directorist-selects--start select"
);
const $closeSelect=$(this).find(".directorist-selects--close select"
);
if($startSelect.length){
$startSelect.attr("name",
$startSelect
.attr("name")
.replace(/\[start]\[\d+]/, `[start][${index}]`)
);
}
if($closeSelect.length){
$closeSelect.attr("name",
$closeSelect
.attr("name")
.replace(/\[close]\[\d+]/, `[close][${index}]`)
);
}});
$choiceWrapper
.find(".directorist-bh-dayzone__single__choice__item")
.removeClass("directorist-not-removable");
setTimeout(()=> {
$originalClose.trigger("change").trigger("select2:select");
}, 0);
}
);
$("body").on("click",
".directorist-bh-dayzone__single__choice__remove",
function (e){
e.preventDefault();
const $choiceWrapper=$(this).closest(".directorist-bh-dayzone__single__choice"
);
const $items=$choiceWrapper.find(".directorist-bh-dayzone__single__choice__item"
);
if($items.length > 1){
$(this)
.closest(".directorist-bh-dayzone__single__choice__item")
.remove();
}
const $remainingItems=$choiceWrapper.find(".directorist-bh-dayzone__single__choice__item"
);
$remainingItems.toggleClass("directorist-not-removable",
$remainingItems.length <=1
);
}
);
$("body").on("change",
"#enable_directorist_bh_listing",
toggleBusinessHourSections
);
$("body").on("change",
'input[type="checkbox"].directorist-247-alternative',
function (){
$('input[type="checkbox"].directorist-247-alternative')
.not(this)
.prop("checked", false);
switch (this.value){
case "always":
$(this)
.closest(".directorist-bh-wrap")
.find(".directorist-bh-section--selection")
.removeClass("directorist-bh-show")
.addClass("directorist-bh-hide");
$(this)
.closest(".directorist-bh-section")
.addClass("bh-always-active");
$('input[name="directorist_bh_option"]').prop("checked", false);
break;
case "custom":
$(this)
.closest(".directorist-bh-wrap")
.find(".directorist-bh-section--selection")
.removeClass("directorist-bh-hide")
.addClass("directorist-bh-show");
$(this)
.closest(".directorist-bh-section")
.removeClass("bh-always-active");
$('input[name="enable247hour"]').prop("checked", false);
break;
}}
);
$("body")
.on("change", ".directorist-selects--start select", handleStartTimeChange)
.on("select2:select",
".directorist-selects--start select",
handleStartTimeChange
);
$("body")
.on("change", ".directorist-selects--close select", handleEndTimeChange)
.on("select2:select",
".directorist-selects--close select",
handleEndTimeChange
);
$(window).on("load", function (){
const observer=new MutationObserver(initializeBHSections);
observer.observe(document.body, { childList: true, subtree: true });
initializeBHSections();
setTimeout(()=> {
initializeBHLabelUpdate();
if($("#dbh-select-timezone").length){
$("#dbh-select-timezone").select2({
placeholder: "Select Timezone",
dropdownCssClass: "dbh-select2-dropdown",
containerCssClass: "dbh-select2-container",
});
}}, 1000);
});
function initializeCopyDropdown(){
$("body").on("click",
".directorist-bh-dayzone__single__action__btn--copy",
function (e){
e.preventDefault();
e.stopPropagation();
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
const isVisible=$dropdown.hasClass("directorist-bh-dayzone__copy-dropdown--show"
);
if(!isVisible){
this.classList.add("active");
$(".directorist-bh-dayzone__copy-dropdown").removeClass("directorist-bh-dayzone__copy-dropdown--show"
);
currentButton=$(this);
currentDay=$(this).closest(".directorist-bh-dayzone__single");
const currentDayValue=currentDay.data("day");
disableCurrentDayInDropdown($dropdown, currentDayValue);
setTimeout(()=> {
positionDropdownRelativeToButton($dropdown, $(this));
}, 10);
$dropdown.addClass("directorist-bh-dayzone__copy-dropdown--show");
updateButtonStates($dropdown);
}else{
closeDropdown($dropdown);
}}
);
$(document).on("click", function (e){
if(!$(e.target).closest(".directorist-bh-dayzone__copy-dropdown, .directorist-bh-dayzone__single__action__btn--copy"
).length
){
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
closeDropdown($dropdown);
}});
let currentButton=null;
$(window).on("scroll resize", function (){
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
if($dropdown.hasClass("directorist-bh-dayzone__copy-dropdown--show") &&
currentButton
){
positionDropdownRelativeToButton($dropdown, currentButton);
}});
$("body").on("change", "#directorist-bh-copy-select-all", function (){
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
const $enabledCheckboxes=$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__checkbox")
.not(this)
.filter(function (){
const $item=$(this).closest(".directorist-bh-dayzone__copy-dropdown__item"
);
return !$item.hasClass("disabled");
});
const checkedCount=$enabledCheckboxes.filter(":checked").length;
const totalCount=$enabledCheckboxes.length;
const allSelected=checkedCount===totalCount;
const shouldSelect = !allSelected;
$enabledCheckboxes.each(function (){
$(this).prop("checked", shouldSelect);
});
$(this).prop("checked", shouldSelect);
updateButtonStates($dropdown);
updateSelectAllState($dropdown);
});
$("body").on("change",
".directorist-bh-dayzone__copy-dropdown__checkbox:not(#directorist-bh-copy-select-all)",
function (){
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
updateSelectAllState($dropdown);
updateButtonStates($dropdown);
}
);
$("body").on("click",
".directorist-bh-dayzone__copy-dropdown__btn--reset",
function (e){
e.preventDefault();
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__checkbox")
.each(function (){
const $item=$(this).closest(".directorist-bh-dayzone__copy-dropdown__item"
);
if(!$item.hasClass("disabled")){
$(this).prop("checked", false);
}});
updateButtonStates($dropdown);
}
);
$("body").on("click",
".directorist-bh-dayzone__copy-dropdown__btn--apply",
function (e){
e.preventDefault();
const $dropdown=$(".directorist-bh-dayzone__copy-dropdown");
const selectedDays=[];
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__checkbox:checked:not(#directorist-bh-copy-select-all)"
)
.each(function (){
selectedDays.push($(this).val());
});
copyBusinessHoursToSelectedDays(selectedDays);
closeDropdown($dropdown);
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__checkbox")
.prop("checked", false);
}
);
}
function updateSelectAllState($dropdown){
const $allCheckboxes=$dropdown.find(".directorist-bh-dayzone__copy-dropdown__checkbox:not(#directorist-bh-copy-select-all)"
);
const $selectAll=$dropdown.find("#directorist-bh-copy-select-all");
const $selectAllLabel=$dropdown.find('label[for="directorist-bh-copy-select-all"]'
);
const $enabledCheckboxes=$allCheckboxes.filter(function (){
const $item=$(this).closest(".directorist-bh-dayzone__copy-dropdown__item"
);
return !$item.hasClass("disabled");
});
const checkedCount=$enabledCheckboxes.filter(":checked").length;
const totalCount=$enabledCheckboxes.length;
if(checkedCount===0){
$selectAll.prop("indeterminate", false).prop("checked", false);
$selectAllLabel.text("Select All");
}else if(checkedCount===totalCount){
$selectAll.prop("indeterminate", false).prop("checked", true);
$selectAllLabel.text("Unselect All");
}else{
$selectAll.prop("indeterminate", true);
$selectAllLabel.text("Select All");
}}
function updateButtonStates($dropdown){
const $allCheckboxes=$dropdown.find(".directorist-bh-dayzone__copy-dropdown__checkbox:not(#directorist-bh-copy-select-all)"
);
const $resetBtn=$dropdown.find(".directorist-bh-dayzone__copy-dropdown__btn--reset"
);
const $applyBtn=$dropdown.find(".directorist-bh-dayzone__copy-dropdown__btn--apply"
);
const $enabledCheckboxes=$allCheckboxes.filter(function (){
const $item=$(this).closest(".directorist-bh-dayzone__copy-dropdown__item"
);
return !$item.hasClass("disabled");
});
const checkedCount=$enabledCheckboxes.filter(":checked").length;
const totalCount=$enabledCheckboxes.length;
$resetBtn.prop("disabled", checkedCount===0);
$applyBtn.prop("disabled", checkedCount===0);
const $selectAllLabel=$dropdown.find('label[for="directorist-bh-copy-select-all"]'
);
if(checkedCount===totalCount&&totalCount > 0){
$selectAllLabel.text("Unselect All");
}else{
$selectAllLabel.text("Select All");
}}
function closeDropdown($dropdown){
$dropdown.removeClass("directorist-bh-dayzone__copy-dropdown--show");
$dropdown.css({
display: "",
position: "",
top: "",
left: "",
"z-index": "",
});
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__item")
.removeClass("disabled");
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__checkbox")
.prop("checked", false);
$(".directorist-bh-dayzone__single__action__btn").removeClass("active");
currentButton=null;
}
function copyBusinessHoursToSelectedDays(selectedDays){
if(!currentDay||selectedDays.length===0){
console.warn("No source day or selected days found");
return;
}
const sourceDayValue=currentDay.data("day");
const sourceDayName=sourceDayValue.replace("bdbh[", "").replace("]", "");
selectedDays.forEach(function (targetDayValue){
const $targetDay=$(
`.directorist-bh-dayzone__single[data-day="${targetDayValue}"]`
);
if($targetDay.length){
const targetDayName=targetDayValue
.replace("bdbh[", "")
.replace("]", "");
copyDayData(currentDay, $targetDay, sourceDayName, targetDayName);
}else{
console.warn(`Target day not found: ${targetDayValue}`);
}});
}
function copyDayData($sourceDay, $targetDay, sourceDayName, targetDayName){
const $sourceEnable=$sourceDay.find(`input[name="bdbh[${sourceDayName}][enable]"]`
);
const $targetEnable=$targetDay.find(`input[name="bdbh[${targetDayName}][enable]"]`
);
if($sourceEnable.length&&$targetEnable.length){
$targetEnable.prop("checked", $sourceEnable.prop("checked"));
$targetEnable.trigger("change");
}
const $source247=$sourceDay.find(`input[name="bdbh[${sourceDayName}][remain_close]"]`
);
const $target247=$targetDay.find(`input[name="bdbh[${targetDayName}][remain_close]"]`
);
if($source247.length&&$target247.length){
$target247.prop("checked", $source247.prop("checked"));
$target247.trigger("change");
}
copyTimeSlots($sourceDay, $targetDay, sourceDayName, targetDayName);
updateDayUIState($targetDay, $sourceDay);
}
function copyTimeSlots($sourceDay, $targetDay, sourceDayName, targetDayName){
const $sourceChoice=$sourceDay.find(".directorist-bh-dayzone__single__choice"
);
const $targetChoice=$targetDay.find(".directorist-bh-dayzone__single__choice"
);
if(!$sourceChoice.length||!$targetChoice.length){
console.warn("Choice elements not found");
return;
}
const $sourceWrapper=$sourceChoice.find(".directorist-bh-dayzone__single__choice__wrapper"
);
const $targetWrapper=$targetChoice.find(".directorist-bh-dayzone__single__choice__wrapper"
);
if(!$sourceWrapper.length||!$targetWrapper.length){
console.warn("Choice wrappers not found", {
sourceWrapper: $sourceWrapper.length,
targetWrapper: $targetWrapper.length,
});
return;
}
const $sourceItems=$sourceWrapper.find(".directorist-bh-dayzone__single__choice__item"
);
const $targetItems=$targetWrapper.find(".directorist-bh-dayzone__single__choice__item"
);
if($targetItems.length > 1){
$targetItems.slice(1).remove();
}
const $firstItem=$targetItems.first();
if($firstItem.length){
$firstItem.find("select").val(null);
$firstItem.find('input[type="checkbox"]').prop("checked", false);
}
$sourceItems.each(function (index){
const $sourceItem=$(this);
let $targetItem;
if(index===0){
$targetItem=$targetWrapper
.find(".directorist-bh-dayzone__single__choice__item")
.first();
}else{
$targetItem=$targetWrapper
.find(".directorist-bh-dayzone__single__choice__item")
.first()
.clone();
$targetItem.removeClass("directorist-not-removable");
$targetItem.find("select").each(function (){
const $select=$(this);
if($select.hasClass("select2-hidden-accessible")){
$select.select2("destroy");
}
$select
.removeClass("select2-hidden-accessible")
.removeAttr("data-select2-id tabindex aria-hidden")
.siblings(".select2")
.remove();
$select.closest(".directorist-selects").removeAttr("data-select2-id");
$select
.closest(".directorist-bh-dayzone__single__choice__slot")
.removeAttr("data-select2-id");
});
$targetWrapper.append($targetItem);
}
$targetItem.find("select").each(function (){
const $select=$(this);
const currentName=$select.attr("name");
if(currentName){
const newName=currentName.replace(/\[\d+\]/, `[${index}]`);
$select.attr("name", newName);
}});
const $sourceStart=$sourceItem.find(`select[name="bdbh[${sourceDayName}][start][${index}]"]`
);
const $targetStart=$targetItem.find(`select[name="bdbh[${targetDayName}][start][${index}]"]`
);
if($sourceStart.length&&$targetStart.length){
$targetStart.val($sourceStart.val());
if($targetStart.hasClass("select2-hidden-accessible")){
$targetStart.select2("destroy");
}
$targetStart.select2({
placeholder: "Select Starting Time",
allowClear: true,
width: "100%",
dropdownCssClass: "dbh-select2-dropdown",
containerCssClass: "dbh-select2-container",
});
}
const $sourceEnd=$sourceItem.find(`select[name="bdbh[${sourceDayName}][close][${index}]"]`
);
const $targetEnd=$targetItem.find(`select[name="bdbh[${targetDayName}][close][${index}]"]`
);
if($sourceEnd.length&&$targetEnd.length){
$targetEnd.val($sourceEnd.val());
if($targetEnd.hasClass("select2-hidden-accessible")){
$targetEnd.select2("destroy");
}
$targetEnd.select2({
placeholder: "Select Closing Time",
allowClear: true,
width: "100%",
dropdownCssClass: "dbh-select2-dropdown",
containerCssClass: "dbh-select2-container",
});
}});
const $finalTargetItems=$targetWrapper.find(".directorist-bh-dayzone__single__choice__item"
);
$finalTargetItems.toggleClass("directorist-not-removable",
$finalTargetItems.length <=1
);
}
function updateDayUIState($targetDay, $sourceDay){
const sourceClasses=$sourceDay.attr("class").split(" ");
const relevantClasses=sourceClasses.filter((cls)=>
cls.includes("directorist-enable-hour-time-selection") ||
cls.includes("directorist-full-time")
);
$targetDay.removeClass("directorist-enable-hour-time-selection directorist-full-time"
);
relevantClasses.forEach((cls)=> {
$targetDay.addClass(cls);
});
$targetDay.find('input[type="checkbox"]').each(function (){
$(this).trigger("change");
});
setTimeout(()=> {
$targetDay
.find(".directorist-bh-dayzone__single__choice__item")
.each(function (){
const $item=$(this);
const $startSelect=$item.find(".directorist-selects--start select");
const $endSelect=$item.find(".directorist-selects--close select");
if($startSelect.length&&$endSelect.length){
handleBHNextDayLabel($startSelect, $endSelect);
}});
}, 100);
}
function disableCurrentDayInDropdown($dropdown, currentDayValue){
$dropdown
.find(".directorist-bh-dayzone__copy-dropdown__item")
.removeClass("disabled");
const $matchingCheckbox=$dropdown.find(`input[value="${currentDayValue}"]`
);
if($matchingCheckbox.length){
const $parentItem=$matchingCheckbox.closest(".directorist-bh-dayzone__copy-dropdown__item"
);
$parentItem.addClass("disabled");
}else{
console.warn(`Checkbox not found for current day: ${currentDayValue}`);
}}
function positionDropdownRelativeToButton($dropdown, $button){
const buttonRect=$button[0].getBoundingClientRect();
const dropdownWidth=172;
const dropdownHeight=200;
let left=buttonRect.right - dropdownWidth;
let top=buttonRect.bottom + 4;
if(left < 10){
left=10;
}else if(left + dropdownWidth > window.innerWidth - 10){
left=window.innerWidth - dropdownWidth - 10;
}
if(top + dropdownHeight > window.innerHeight - 10){
top=buttonRect.top - dropdownHeight - 4;
}
$dropdown.css({
display: "block",
position: "fixed",
top: top + "px",
left: left + "px",
"z-index": 1000,
});
}
$(document).ready(function (){
initializeCopyDropdown();
});
})(jQuery);