| 1 |
"use strict"; |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
const parseTime = (time) => { |
| 5 |
if (!time || typeof time !== "string") { |
| 6 |
return null; |
| 7 |
} |
| 8 |
const parts = time.split(":"); |
| 9 |
if (parts.length < 2) { |
| 10 |
return null; |
| 11 |
} |
| 12 |
const hours = parseInt(parts[0], 10); |
| 13 |
const minutes = parseInt(parts[1], 10); |
| 14 |
if (Number.isNaN(hours) || Number.isNaN(minutes)) { |
| 15 |
return null; |
| 16 |
} |
| 17 |
return hours * 60 + minutes; |
| 18 |
}; |
| 19 |
|
| 20 |
const isWithinSchedule = (start, end, offsetMinutes) => { |
| 21 |
const startMinutes = parseTime(start); |
| 22 |
const endMinutes = parseTime(end); |
| 23 |
if (startMinutes === null || endMinutes === null) { |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
const now = new Date(); |
| 28 |
let minutes = now.getUTCHours() * 60 + now.getUTCMinutes(); |
| 29 |
minutes += offsetMinutes; |
| 30 |
|
| 31 |
while (minutes < 0) { |
| 32 |
minutes += 1440; |
| 33 |
} |
| 34 |
minutes = minutes % 1440; |
| 35 |
|
| 36 |
if (startMinutes === endMinutes) { |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
if (startMinutes < endMinutes) { |
| 41 |
return minutes >= startMinutes && minutes <= endMinutes; |
| 42 |
} |
| 43 |
|
| 44 |
return minutes >= startMinutes || minutes <= endMinutes; |
| 45 |
}; |
| 46 |
|
| 47 |
const matchUtm = (key, value) => { |
| 48 |
if (!key || !value) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
const params = new URLSearchParams(window.location.search); |
| 52 |
return params.get(key) === value; |
| 53 |
}; |
| 54 |
|
| 55 |
const applyState = ($bar) => { |
| 56 |
const data = $bar.data(); |
| 57 |
let state = data.statusDefault === "offline" ? "offline" : "online"; |
| 58 |
|
| 59 |
if (matchUtm(data.utmKey, data.utmValue) && data.utmState) { |
| 60 |
state = data.utmState; |
| 61 |
} |
| 62 |
|
| 63 |
if (data.scheduleActive === "yes" && data.scheduleStart && data.scheduleEnd) { |
| 64 |
const offset = Number(data.timezoneOffset || 0) * 60; |
| 65 |
if (!isWithinSchedule(data.scheduleStart, data.scheduleEnd, offset)) { |
| 66 |
state = "offline"; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
const onlineLabel = data.onlineLabel || "Online"; |
| 71 |
const offlineLabel = data.offlineLabel || "Offline"; |
| 72 |
|
| 73 |
$bar.attr("data-state", state); |
| 74 |
$bar.toggleClass("is-offline", state === "offline"); |
| 75 |
|
| 76 |
const $status = $bar.find(".king-addons-sticky-contact-bar__status-text"); |
| 77 |
if ($status.length) { |
| 78 |
$status.text(state === "offline" ? offlineLabel : onlineLabel); |
| 79 |
} |
| 80 |
|
| 81 |
const $note = $bar.find(".king-addons-sticky-contact-bar__note"); |
| 82 |
if ($note.length) { |
| 83 |
const offlineNote = $bar.data("offlineNote"); |
| 84 |
if (offlineNote) { |
| 85 |
$note.text(offlineNote); |
| 86 |
} |
| 87 |
const showNote = state === "offline" && ($note.text().trim() !== ""); |
| 88 |
$note.toggleClass("is-visible", showNote); |
| 89 |
$note.attr("aria-hidden", showNote ? "false" : "true"); |
| 90 |
} |
| 91 |
|
| 92 |
const isMobile = window.matchMedia("(max-width: 767px)").matches; |
| 93 |
|
| 94 |
$bar.find(".king-addons-sticky-contact-bar__item").each(function () { |
| 95 |
const availability = $(this).data("availability") || "always"; |
| 96 |
const device = $(this).data("device") || "all"; |
| 97 |
let visible = true; |
| 98 |
|
| 99 |
if (availability === "online" && state !== "online") { |
| 100 |
visible = false; |
| 101 |
} |
| 102 |
|
| 103 |
if (availability === "offline" && state !== "offline") { |
| 104 |
visible = false; |
| 105 |
} |
| 106 |
|
| 107 |
if (device === "desktop" && isMobile) { |
| 108 |
visible = false; |
| 109 |
} |
| 110 |
|
| 111 |
if (device === "mobile" && !isMobile) { |
| 112 |
visible = false; |
| 113 |
} |
| 114 |
|
| 115 |
$(this).toggleClass("is-hidden", !visible); |
| 116 |
}); |
| 117 |
}; |
| 118 |
|
| 119 |
const applyPlacement = ($bar) => { |
| 120 |
const offset = parseInt($bar.data("offset"), 10); |
| 121 |
const align = $bar.data("align") || "middle"; |
| 122 |
const position = $bar.data("position") === "left" ? "left" : "right"; |
| 123 |
const safeOffset = Number.isNaN(offset) ? 120 : offset; |
| 124 |
|
| 125 |
const css = { |
| 126 |
left: "auto", |
| 127 |
right: "auto", |
| 128 |
top: "", |
| 129 |
bottom: "", |
| 130 |
transform: "", |
| 131 |
}; |
| 132 |
|
| 133 |
css[position] = `${safeOffset}px`; |
| 134 |
if (position === "left") { |
| 135 |
css.right = "auto"; |
| 136 |
} else { |
| 137 |
css.left = "auto"; |
| 138 |
} |
| 139 |
|
| 140 |
if (align === "top") { |
| 141 |
css.top = `${safeOffset}px`; |
| 142 |
css.transform = "translateY(0)"; |
| 143 |
} else if (align === "bottom") { |
| 144 |
css.bottom = `${safeOffset}px`; |
| 145 |
css.transform = "translateY(0)"; |
| 146 |
} else { |
| 147 |
css.top = "50%"; |
| 148 |
css.transform = "translateY(-50%)"; |
| 149 |
} |
| 150 |
|
| 151 |
$bar.css(css); |
| 152 |
|
| 153 |
if ($bar.data("showLabels") === "no") { |
| 154 |
$bar.addClass("no-labels"); |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
const initStickyBar = ($scope) => { |
| 159 |
const $bar = $scope.find(".king-addons-sticky-contact-bar"); |
| 160 |
if (!$bar.length) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
applyPlacement($bar); |
| 165 |
applyState($bar); |
| 166 |
|
| 167 |
window.setInterval(() => { |
| 168 |
applyState($bar); |
| 169 |
}, 60000); |
| 170 |
}; |
| 171 |
|
| 172 |
$(window).on("elementor/frontend/init", function () { |
| 173 |
elementorFrontend.hooks.addAction( |
| 174 |
"frontend/element_ready/king-addons-sticky-contact-bar.default", |
| 175 |
function ($scope) { |
| 176 |
initStickyBar($scope); |
| 177 |
} |
| 178 |
); |
| 179 |
}); |
| 180 |
})(jQuery); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|