PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.78
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.78
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / Image_Accordion / script.js

script.js in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.78, at includes/widgets/Image_Accordion/script.js

303 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2
3 (function ($) {
4 $(window).on("elementor/frontend/init", function () {
5 elementorFrontend.hooks.addAction(
6 "frontend/element_ready/king-addons-image-accordion.default",
7 function ($scope) {
8 elementorFrontend.elementsHandler.addHandler(
9 elementorModules.frontend.handlers.Base.extend({
10 onInit() {
11 const normalizeSafeUrl = (rawUrl) => {
12 if (!rawUrl || typeof rawUrl !== "string") return null;
13 try {
14 const url = new URL(rawUrl, window.location.href);
15 const allowedProtocols = ["http:", "https:", "mailto:", "tel:"];
16 if (allowedProtocols.includes(url.protocol)) {
17 return url.href;
18 }
19 } catch (e) {
20 // ignore
21 }
22 return null;
23 };
24
25 const $elem = this.$element,
26 $wrap = $elem.find(".king-addons-image-accordion"),
27 $wrapContainer = $elem.find(".king-addons-image-accordion-wrap"),
28 settings = JSON.parse(
29 $elem
30 .find(".king-addons-img-accordion-media-hover")
31 .attr("data-settings")
32 ),
33 lightboxAttr = $wrap.attr("lightbox"),
34 lightboxSettings = lightboxAttr ? JSON.parse(lightboxAttr) : "",
35 $accordionItems = $elem.find(".king-addons-image-accordion-item");
36
37 // Adjust row/column layout if necessary
38 if ($wrapContainer.hasClass("king-addons-acc-no-column")) {
39 if (!$elem.hasClass("king-addons-image-accordion-row")) {
40 $elem
41 .removeClass("king-addons-image-accordion-column")
42 .addClass("king-addons-image-accordion-row");
43 $wrap.css("flex-direction", "row");
44 }
45 }
46
47 // Initialize lightbox if settings exist
48 if (lightboxSettings) {
49 $wrap.lightGallery(lightboxSettings);
50
51 // Adjust thumbnails in lightbox
52 $wrap.on("onAfterOpen.lg", function () {
53 const $lgOuter = $(".lg-outer");
54 if ($lgOuter.find(".lg-thumb-item").length) {
55 $lgOuter.find(".lg-thumb-item").each(function () {
56 const $img = $(this).find("img");
57 const imgSrc = $img.attr("src");
58 const extIndex = imgSrc.lastIndexOf(".");
59 const ext = imgSrc.slice(extIndex);
60 const cropIndex = imgSrc.lastIndexOf("-");
61 let cropSize = /\d{3,}x\d{3,}/.test(
62 imgSrc.substring(extIndex, cropIndex)
63 )
64 ? imgSrc.substring(extIndex, cropIndex)
65 : false;
66 let newImgSrc = imgSrc;
67
68 if (cropSize && cropSize.length >= 42) {
69 cropSize = "";
70 }
71
72 if (cropSize !== "") {
73 if (cropSize !== false) {
74 newImgSrc = imgSrc.replace(cropSize, "-150x150");
75 } else {
76 newImgSrc = [
77 imgSrc.slice(0, extIndex),
78 "-150x150",
79 ext
80 ].join("");
81 }
82 }
83
84 $img.attr("src", newImgSrc);
85
86 // Restore original if invalid crop size
87 if (cropSize === false || cropSize === "-450x450") {
88 $img.attr("src", imgSrc);
89 }
90 });
91 }
92 });
93
94 // Control certain lightbox behaviors
95 $wrap.on("onAferAppendSlide.lg onAfterSlide.lg", function () {
96 const $lightboxDownload = $("#lg-download"),
97 lightboxControls = $(
98 "#lg-actual-size, #lg-zoom-in, #lg-zoom-out, #lg-download"
99 ),
100 downloadHref = $lightboxDownload.attr("href");
101
102 if ($lightboxDownload.length) {
103 if (downloadHref.indexOf("wp-content") === -1) {
104 lightboxControls.addClass("king-addons-hidden-element");
105 } else {
106 lightboxControls.removeClass("king-addons-hidden-element");
107 }
108 }
109
110 // Hide autoplay if not enabled
111 if (!lightboxSettings.autoplay) {
112 $(".lg-autoplay-button").css({
113 width: "0",
114 height: "0",
115 overflow: "hidden"
116 });
117 }
118 });
119 }
120
121 // Make the entire accordion item clickable
122 $wrap.css("cursor", "pointer");
123
124 // Handle hover interactions for clickable links
125 function mediaHoverLink() {
126 // Avoid linking in the Elementor editor
127 if (!$("body").hasClass("elementor-editor-active")) {
128 $elem
129 .find(".king-addons-img-accordion-media-hover")
130 .on("click", function (event) {
131 const isTarget = event.target.className.includes(
132 "king-addons-img-accordion-media-hover"
133 );
134 const thisSettings = JSON.parse(
135 isTarget
136 ? $(this).attr("data-settings")
137 : $(this)
138 .closest(".king-addons-img-accordion-media-hover")
139 .attr("data-settings")
140 );
141 if (
142 !$(event.target).hasClass(
143 "king-addons-img-accordion-item-lightbox"
144 ) &&
145 !$(event.target).closest(
146 ".king-addons-img-accordion-item-lightbox"
147 ).length
148 ) {
149 const itemUrl = thisSettings.activeItem.overlayLink;
150 const safeUrl = normalizeSafeUrl(itemUrl);
151 if (safeUrl) {
152 if (
153 thisSettings.activeItem.overlayLinkTarget ===
154 "_blank"
155 ) {
156 const w = window.open(safeUrl, "_blank");
157 if (w && typeof w.focus === "function") {
158 w.focus();
159 }
160 } else {
161 window.location.href = safeUrl;
162 }
163 }
164 }
165 });
166 }
167 }
168
169 // Interaction type: "hover"
170 if (settings.activeItem.interaction === "hover") {
171 mediaHoverLink();
172
173 $accordionItems
174 .on("mouseenter", function () {
175 $accordionItems.removeClass(
176 "king-addons-image-accordion-item-grow"
177 );
178 $accordionItems
179 .find(".king-addons-animation-wrap")
180 .removeClass("king-addons-animation-wrap-active");
181
182 $(this).addClass("king-addons-image-accordion-item-grow");
183 $(this)
184 .find(".king-addons-animation-wrap")
185 .addClass("king-addons-animation-wrap-active");
186 })
187 .on("mouseleave", function () {
188 $(this).removeClass("king-addons-image-accordion-item-grow");
189 $(this)
190 .find(".king-addons-animation-wrap")
191 .removeClass("king-addons-animation-wrap-active");
192 });
193
194 // Interaction type: "click"
195 } else if (settings.activeItem.interaction === "click") {
196 $elem
197 .find(".king-addons-img-accordion-media-hover")
198 .removeClass("king-addons-animation-wrap");
199
200 $accordionItems.on(
201 "click",
202 ".king-addons-img-accordion-media-hover",
203 function (event) {
204 const isTarget = event.target.className.includes(
205 "king-addons-img-accordion-media-hover"
206 );
207 const hasActiveClass = isTarget
208 ? event.target.className.includes(
209 "king-addons-animation-wrap-active"
210 )
211 : $(this)
212 .closest(".king-addons-img-accordion-media-hover")
213 .hasClass("king-addons-animation-wrap-active");
214
215 // If this item is already active, redirect if a link is set
216 if (
217 hasActiveClass &&
218 !$("body").hasClass("elementor-editor-active")
219 ) {
220 const thisSettings = JSON.parse(
221 isTarget
222 ? $(this).attr("data-settings")
223 : $(this)
224 .closest(".king-addons-img-accordion-media-hover")
225 .attr("data-settings")
226 );
227
228 if (
229 !$(event.target).hasClass(
230 "king-addons-img-accordion-item-lightbox"
231 ) &&
232 !$(event.target).closest(
233 ".king-addons-img-accordion-item-lightbox"
234 ).length
235 ) {
236 const itemUrl = thisSettings.activeItem.overlayLink;
237 const safeUrl = normalizeSafeUrl(itemUrl);
238 if (safeUrl) {
239 if (
240 thisSettings.activeItem.overlayLinkTarget ===
241 "_blank"
242 ) {
243 const w = window.open(safeUrl, "_blank");
244 if (w && typeof w.focus === "function") {
245 w.focus();
246 }
247 } else {
248 window.location.href = safeUrl;
249 }
250 }
251 }
252 } else {
253 // Activate this item
254 $elem
255 .find(".king-addons-img-accordion-media-hover")
256 .removeClass("king-addons-animation-wrap king-addons-animation-wrap-active");
257 $accordionItems.removeClass(
258 "king-addons-image-accordion-item-grow"
259 );
260
261 $(this)
262 .closest(".king-addons-image-accordion-item")
263 .addClass("king-addons-image-accordion-item-grow");
264 $(this)
265 .closest(".king-addons-img-accordion-media-hover")
266 .addClass("king-addons-animation-wrap-active");
267 }
268 }
269 );
270 } else {
271 // If interaction is neither hover nor click
272 $elem
273 .find(".king-addons-img-accordion-media-hover")
274 .removeClass("king-addons-animation-wrap");
275 }
276
277 // Set default active item
278 $accordionItems.each(function () {
279 if ($(this).index() === settings.activeItem.defaultActive - 1) {
280 setTimeout(() => {
281 const action =
282 settings.activeItem.interaction === "click"
283 ? "click"
284 : "mouseenter";
285 $(this)
286 .find(".king-addons-img-accordion-media-hover")
287 .trigger(action);
288 }, 400);
289 }
290 });
291
292 // Show the accordion after initialization
293 $wrapContainer.css("opacity", 1);
294 }
295 }),
296 {
297 $element: $scope
298 }
299 );
300 }
301 );
302 });
303 })(jQuery);