| 1 |
"use strict"; |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
const isDeviceAllowed = (device) => { |
| 5 |
if (device === "desktop") { |
| 6 |
return window.matchMedia("(min-width: 768px)").matches; |
| 7 |
} |
| 8 |
if (device === "mobile") { |
| 9 |
return window.matchMedia("(max-width: 767px)").matches; |
| 10 |
} |
| 11 |
return true; |
| 12 |
}; |
| 13 |
|
| 14 |
const getStoredState = (key) => { |
| 15 |
if (!key) { |
| 16 |
return null; |
| 17 |
} |
| 18 |
|
| 19 |
try { |
| 20 |
const raw = window.localStorage.getItem(key); |
| 21 |
if (!raw) { |
| 22 |
return null; |
| 23 |
} |
| 24 |
const parsed = JSON.parse(raw); |
| 25 |
if (parsed.expires && parsed.expires < Date.now()) { |
| 26 |
window.localStorage.removeItem(key); |
| 27 |
return null; |
| 28 |
} |
| 29 |
return parsed.state || null; |
| 30 |
} catch (e) { |
| 31 |
return null; |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
const setStoredState = (key, state, hours) => { |
| 36 |
if (!key) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
try { |
| 41 |
const expires = |
| 42 |
hours && hours > 0 |
| 43 |
? Date.now() + hours * 60 * 60 * 1000 |
| 44 |
: null; |
| 45 |
const payload = { |
| 46 |
state, |
| 47 |
expires, |
| 48 |
}; |
| 49 |
window.localStorage.setItem(key, JSON.stringify(payload)); |
| 50 |
} catch (e) { |
| 51 |
// Ignore storage errors (private mode). |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
const toggleVisibility = ($widget, visible) => { |
| 56 |
if (visible) { |
| 57 |
$widget.addClass("is-visible").attr("aria-hidden", "false"); |
| 58 |
} else { |
| 59 |
$widget.removeClass("is-visible").attr("aria-hidden", "true"); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
const bindClose = ($widget, data, state, evaluate) => { |
| 64 |
const $close = $widget.find(".king-addons-sticky-video__close"); |
| 65 |
if (!$close.length || data.showClose !== "yes") { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$close.on("click", (event) => { |
| 70 |
event.preventDefault(); |
| 71 |
state.dismissed = true; |
| 72 |
$widget.addClass("is-dismissed"); |
| 73 |
toggleVisibility($widget, false); |
| 74 |
if (data.persistClose === "yes" && data.persistKey) { |
| 75 |
setStoredState(data.persistKey, "closed", data.persistHours); |
| 76 |
} |
| 77 |
evaluate(); |
| 78 |
}); |
| 79 |
}; |
| 80 |
|
| 81 |
const initStickyVideo = ($scope) => { |
| 82 |
const $widget = $scope.find(".king-addons-sticky-video"); |
| 83 |
if (!$widget.length) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
const data = $widget.data(); |
| 88 |
data.triggerScroll = Number(data.triggerScroll || 0); |
| 89 |
data.delayMs = Number(data.delayMs || 0); |
| 90 |
data.persistHours = Number(data.persistHours || 0); |
| 91 |
data.persistKey = data.unique |
| 92 |
? `kingAddonsStickyVideo_${data.unique}` |
| 93 |
: ""; |
| 94 |
data.showClose = data.showClose || "yes"; |
| 95 |
data.persistClose = data.persistClose || "no"; |
| 96 |
data.device = data.device || "all"; |
| 97 |
|
| 98 |
if (!isDeviceAllowed(data.device)) { |
| 99 |
$widget.addClass("is-dismissed"); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
const savedState = getStoredState(data.persistKey); |
| 104 |
const state = { |
| 105 |
dismissed: savedState === "closed", |
| 106 |
delayPassed: data.delayMs <= 0, |
| 107 |
}; |
| 108 |
|
| 109 |
if (state.dismissed) { |
| 110 |
$widget.addClass("is-dismissed"); |
| 111 |
toggleVisibility($widget, false); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
const evaluateVisibility = () => { |
| 116 |
if (state.dismissed) { |
| 117 |
toggleVisibility($widget, false); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
const meetsScroll = |
| 122 |
data.triggerScroll <= 0 || |
| 123 |
window.scrollY >= data.triggerScroll || |
| 124 |
elementorFrontend.isEditMode(); |
| 125 |
const meetsDelay = state.delayPassed || elementorFrontend.isEditMode(); |
| 126 |
|
| 127 |
const shouldShow = meetsScroll && meetsDelay; |
| 128 |
toggleVisibility($widget, shouldShow); |
| 129 |
}; |
| 130 |
|
| 131 |
if (data.delayMs > 0) { |
| 132 |
window.setTimeout(() => { |
| 133 |
state.delayPassed = true; |
| 134 |
evaluateVisibility(); |
| 135 |
}, data.delayMs); |
| 136 |
} |
| 137 |
|
| 138 |
if (data.triggerScroll > 0 && !elementorFrontend.isEditMode()) { |
| 139 |
const namespace = `.kingAddonsStickyVideo.${data.unique || "default"}`; |
| 140 |
$(window).on( |
| 141 |
`scroll${namespace}`, |
| 142 |
() => window.requestAnimationFrame(evaluateVisibility) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
bindClose($widget, data, state, evaluateVisibility); |
| 147 |
evaluateVisibility(); |
| 148 |
}; |
| 149 |
|
| 150 |
$(window).on("elementor/frontend/init", function () { |
| 151 |
elementorFrontend.hooks.addAction( |
| 152 |
"frontend/element_ready/king-addons-sticky-video.default", |
| 153 |
function ($scope) { |
| 154 |
initStickyVideo($scope); |
| 155 |
} |
| 156 |
); |
| 157 |
}); |
| 158 |
})(jQuery); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|