| @@ -1,0 +1,1288 @@ | ||
| 1 | +// noinspection JSUnresolvedReference,DuplicatedCode | |
| 2 | + | |
| 3 | +"use strict"; | |
| 4 | +(function ($) { | |
| 5 | + $(window).on("elementor/frontend/init", () => { | |
| 6 | + | |
| 7 | + const gridHooks = [ | |
| 8 | + "frontend/element_ready/king-addons-grid.default" | |
| 9 | + ]; | |
| 10 | + | |
| 11 | + const gridHandler = ($scope) => { | |
| 12 | + elementorFrontend.elementsHandler.addHandler( | |
| 13 | + elementorModules.frontend.handlers.Base.extend({ | |
| 14 | + onInit() { | |
| 15 | + const $body = $("body"); | |
| 16 | + const isEditor = $body.hasClass("elementor-editor-active"); | |
| 17 | + const $grid = this.$element.find(".king-addons-grid"); | |
| 18 | + if (!$grid.length) return; | |
| 19 | + | |
| 20 | + /* ────────────────────────────────────── | |
| 21 | + * UTILITY FUNCTIONS | |
| 22 | + * ────────────────────────────────────── */ | |
| 23 | + | |
| 24 | + // Simple debounce helper. | |
| 25 | + const debounce = (func, threshold = 100, execAsap) => { | |
| 26 | + let timeout; | |
| 27 | + return function (...args) { | |
| 28 | + const context = this; | |
| 29 | + const delayed = () => { | |
| 30 | + if (!execAsap) func.apply(context, args); | |
| 31 | + timeout = null; | |
| 32 | + }; | |
| 33 | + if (timeout) clearTimeout(timeout); | |
| 34 | + else if (execAsap) func.apply(context, args); | |
| 35 | + timeout = setTimeout(delayed, threshold); | |
| 36 | + }; | |
| 37 | + }; | |
| 38 | + | |
| 39 | + // Attach "smartresize" to jQuery. | |
| 40 | + $.fn.smartresize = function (fn) { | |
| 41 | + return fn ? this.on("resize", debounce(fn)) : this.trigger("smartresize"); | |
| 42 | + }; | |
| 43 | + | |
| 44 | + // Parse grid settings. | |
| 45 | + const settingsData = $grid.attr("data-settings"); | |
| 46 | + const settings = settingsData ? JSON.parse(settingsData) : false; | |
| 47 | + let pagesLoaded = 0; | |
| 48 | + | |
| 49 | + /* ────────────────────────────────────── | |
| 50 | + * INITIAL EVENT HANDLERS | |
| 51 | + * ────────────────────────────────────── */ | |
| 52 | + | |
| 53 | + // Handle WooCommerce orderby form. | |
| 54 | + if ($scope.find(".king-addons-grid-orderby form").length) { | |
| 55 | + const $orderbyForm = $scope.find(".king-addons-grid-orderby form"); | |
| 56 | + $scope.find(".orderby").on("change", () => $orderbyForm.trigger("submit")); | |
| 57 | + } | |
| 58 | + | |
| 59 | + // Adjust result count text if WooCommerce count element exists. | |
| 60 | + if (settings && $scope.find(".woocommerce-result-count").length) { | |
| 61 | + adjustResultCount($scope, $grid, settings, isEditor); | |
| 62 | + } | |
| 63 | + | |
| 64 | + // Helper to schedule a delayed isotope layout. | |
| 65 | + const scheduleIsotopeLayout = (delay) => | |
| 66 | + setTimeout(() => isotopeLayout(settings), delay); | |
| 67 | + | |
| 68 | + if (settings) { | |
| 69 | + // Perform initial layout. | |
| 70 | + isotopeLayout(settings); | |
| 71 | + scheduleIsotopeLayout(100); | |
| 72 | + if (isEditor) { | |
| 73 | + scheduleIsotopeLayout(500); | |
| 74 | + scheduleIsotopeLayout(1000); | |
| 75 | + } | |
| 76 | + $(window).on("load", () => scheduleIsotopeLayout(100)); | |
| 77 | + $(document).ready(() => scheduleIsotopeLayout(100)); | |
| 78 | + $(window).smartresize(() => scheduleIsotopeLayout(200)); | |
| 79 | + | |
| 80 | + // If dynamic grid settings exist, set up filtering and “load more” experiments. | |
| 81 | + if (settings.grid_settings) { | |
| 82 | + loadMoreExperiment(); | |
| 83 | + filtersExperiment(settings); | |
| 84 | + } | |
| 85 | + isotopeFilters(settings); | |
| 86 | + | |
| 87 | + let initialItems = 0; | |
| 88 | + $grid.on("arrangeComplete", (e, filteredItems) => { | |
| 89 | + animateGridItems(filteredItems, settings, $grid); | |
| 90 | + initialItems = filteredItems.length; | |
| 91 | + }); | |
| 92 | + | |
| 93 | + // Once images load, set grid opacity and equalize item heights. | |
| 94 | + $grid.imagesLoaded(() => { | |
| 95 | + if ($grid.css("opacity") !== "1") $grid.css("opacity", "1"); | |
| 96 | + setTimeout(() => $grid.addClass("grid-images-loaded"), 500); | |
| 97 | + setEqualHeight(settings); | |
| 98 | + }); | |
| 99 | + | |
| 100 | + // Pagination: load-more or infinite-scroll. | |
| 101 | + if (["load-more", "infinite-scroll"].includes(settings.pagination_type)) { | |
| 102 | + if ($scope.find(".king-addons-grid-pagination").length && !isEditor) { | |
| 103 | + setupInfiniteScroll(settings); | |
| 104 | + } else { | |
| 105 | + $scope.find(".king-addons-load-more-btn").on("click", () => | |
| 106 | + alert( | |
| 107 | + "Load More is Disabled in the Editor! Please Preview this Page to see it in action" | |
| 108 | + ) | |
| 109 | + ); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } else { | |
| 113 | + // Fallback: initialize Slick slider. | |
| 114 | + $grid.animate({ opacity: "1" }, 1000); | |
| 115 | + initSlickSlider(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Additional features. | |
| 119 | + if ($grid.find(".king-addons-grid-item-add-to-cart").length) { | |
| 120 | + setupAddToCart(); | |
| 121 | + } | |
| 122 | + postSharing(); | |
| 123 | + mediaHoverLink(); | |
| 124 | + if ( | |
| 125 | + !$scope.hasClass("elementor-widget-king-addons-woocommerce-category-grid-pro") && | |
| 126 | + !$scope.hasClass("elementor-widget-king-addons-category-grid-pro") | |
| 127 | + ) { | |
| 128 | + // lightboxPopup(settings); | |
| 129 | + } | |
| 130 | + postLikes(settings); | |
| 131 | + | |
| 132 | + /* ────────────────────────────────────── | |
| 133 | + * FUNCTION DEFINITIONS | |
| 134 | + * ────────────────────────────────────── */ | |
| 135 | + | |
| 136 | + // Adjust WooCommerce result count. | |
| 137 | + function adjustResultCount($scope, $grid, s, isEditor) { | |
| 138 | + const $resultCount = $scope.find(".woocommerce-result-count"); | |
| 139 | + let text = $resultCount.text(); | |
| 140 | + const scopeId = $scope.data("id"); | |
| 141 | + const storageKey = `king-addons-cached-items-length-${scopeId}`; | |
| 142 | + let cachedLength = localStorage.getItem(storageKey); | |
| 143 | + if (!cachedLength || isEditor) { | |
| 144 | + cachedLength = $scope.find(".king-addons-grid-item").length; | |
| 145 | + localStorage.setItem(storageKey, cachedLength); | |
| 146 | + } | |
| 147 | + const itemsPerPage = s.query_posts_per_page || parseInt(cachedLength, 10); | |
| 148 | + if (isNaN(itemsPerPage) || itemsPerPage <= 0) return; | |
| 149 | + let currentPage = 1; | |
| 150 | + const $currentPage = $scope.find(".king-addons-grid-current-page"); | |
| 151 | + if ($currentPage.length) { | |
| 152 | + currentPage = parseInt($currentPage.text().trim(), 10) || 1; | |
| 153 | + } | |
| 154 | + const totalMatch = text.match(/of (\d+) results/); | |
| 155 | + const totalItems = totalMatch ? parseInt(totalMatch[1].trim(), 10) : itemsPerPage; | |
| 156 | + if (isNaN(totalItems) || totalItems <= 0) return; | |
| 157 | + const startItem = (currentPage - 1) * itemsPerPage + 1; | |
| 158 | + const endItem = Math.min(startItem + itemsPerPage - 1, totalItems); | |
| 159 | + text = text.replace(/\d+\u2013\d+/, `${startItem}\u2013${endItem}`); | |
| 160 | + $resultCount.text(text); | |
| 161 | + } | |
| 162 | + | |
| 163 | + // Layout the grid using Isotope (or its custom wrapper). | |
| 164 | + function isotopeLayout(s, $response = "") { | |
| 165 | + const layout = s.layout; | |
| 166 | + const columnsDesktop = parseInt(s.columns_desktop, 10); | |
| 167 | + let items = $response ? $response : $grid.find(".king-addons-grid-item"), | |
| 168 | + contWidth = $grid.width() + s.gutter_hr - 0.3, | |
| 169 | + viewportWidth = $(window).outerWidth(), | |
| 170 | + columns, | |
| 171 | + gutterHr, | |
| 172 | + gutterVr; | |
| 173 | + | |
| 174 | + // Breakpoints. | |
| 175 | + const { mobile, mobile_extra, tablet, tablet_extra, laptop, widescreen } = | |
| 176 | + elementorFrontend.config.responsive.breakpoints; | |
| 177 | + const activeBps = elementorFrontend.config.responsive.activeBreakpoints; | |
| 178 | + let cMobile = 1, | |
| 179 | + cMobileExtra, | |
| 180 | + cTablet = 2, | |
| 181 | + cTabletExtra, | |
| 182 | + cLaptop, | |
| 183 | + cWideScreen; | |
| 184 | + $scope | |
| 185 | + .attr("class") | |
| 186 | + .split(" ") | |
| 187 | + .forEach((cl) => { | |
| 188 | + if (/mobile\d/.test(cl)) cMobile = parseInt(cl.slice(-1), 10); | |
| 189 | + if (/mobile_extra\d/.test(cl)) cMobileExtra = parseInt(cl.slice(-1), 10); | |
| 190 | + if (/tablet\d/.test(cl)) cTablet = parseInt(cl.slice(-1), 10); | |
| 191 | + if (/tablet_extra\d/.test(cl)) cTabletExtra = parseInt(cl.slice(-1), 10); | |
| 192 | + if (/widescreen\d/.test(cl)) cWideScreen = parseInt(cl.slice(-1), 10); | |
| 193 | + if (/laptop\d/.test(cl)) cLaptop = parseInt(cl.slice(-1), 10); | |
| 194 | + }); | |
| 195 | + | |
| 196 | + if (viewportWidth <= mobile.value && activeBps.mobile) { | |
| 197 | + columns = cMobile; | |
| 198 | + gutterHr = s.gutter_hr_mobile; | |
| 199 | + gutterVr = s.gutter_vr_mobile; | |
| 200 | + } else if (viewportWidth <= mobile_extra.value && activeBps.mobile_extra) { | |
| 201 | + columns = cMobileExtra || cTablet; | |
| 202 | + gutterHr = s.gutter_hr_mobile_extra; | |
| 203 | + gutterVr = s.gutter_vr_mobile_extra; | |
| 204 | + } else if (viewportWidth <= tablet.value && activeBps.tablet) { | |
| 205 | + columns = cTablet; | |
| 206 | + gutterHr = s.gutter_hr_tablet; | |
| 207 | + gutterVr = s.gutter_vr_tablet; | |
| 208 | + } else if (viewportWidth <= tablet_extra.value && activeBps.tablet_extra) { | |
| 209 | + columns = cTabletExtra || cTablet; | |
| 210 | + gutterHr = s.gutter_hr_tablet_extra; | |
| 211 | + gutterVr = s.gutter_vr_tablet_extra; | |
| 212 | + } else if (viewportWidth <= laptop.value && activeBps.laptop) { | |
| 213 | + columns = cLaptop || columnsDesktop; | |
| 214 | + gutterHr = s.gutter_hr_laptop; | |
| 215 | + gutterVr = s.gutter_vr_laptop; | |
| 216 | + } else if (viewportWidth <= widescreen.value) { | |
| 217 | + columns = columnsDesktop; | |
| 218 | + gutterHr = s.gutter_hr; | |
| 219 | + gutterVr = s.gutter_vr; | |
| 220 | + } else { | |
| 221 | + columns = cWideScreen || columnsDesktop; | |
| 222 | + gutterHr = s.gutter_hr_widescreen; | |
| 223 | + gutterVr = s.gutter_vr_widescreen; | |
| 224 | + } | |
| 225 | + if (columns > 8) columns = 8; | |
| 226 | + items.outerWidth(Math.floor(contWidth / columns - gutterHr)); | |
| 227 | + items.css("margin-bottom", `${gutterVr}px`); | |
| 228 | + | |
| 229 | + if (layout === "list") { | |
| 230 | + handleListLayout(s, items); | |
| 231 | + } | |
| 232 | + | |
| 233 | + const isoLayoutMode = layout === "list" ? "fitRows" : layout; | |
| 234 | + const transDuration = s.filters_animation === "default" ? 400 : 0; | |
| 235 | + | |
| 236 | + $grid.isotopekng({ | |
| 237 | + layoutMode: isoLayoutMode, | |
| 238 | + masonry: { gutter: gutterHr }, | |
| 239 | + fitRows: { gutter: gutterHr }, | |
| 240 | + transitionDuration: transDuration, | |
| 241 | + percentPosition: true, | |
| 242 | + }); | |
| 243 | + } | |
| 244 | + | |
| 245 | + // Handle "list" layout adjustments. | |
| 246 | + function handleListLayout(s, items) { | |
| 247 | + const imageHeight = items.find(".king-addons-grid-image-wrap").outerHeight(); | |
| 248 | + items.find(".king-addons-grid-item-below-content").css("min-height", `${imageHeight}px`); | |
| 249 | + | |
| 250 | + if ($body.width() < 480) { | |
| 251 | + items.find(".king-addons-grid-media-wrap").css({ float: "none", width: "100%" }); | |
| 252 | + items.find(".king-addons-grid-item-below-content").css({ | |
| 253 | + float: "none", | |
| 254 | + width: "100%", | |
| 255 | + "min-height": "0", | |
| 256 | + }); | |
| 257 | + } else { | |
| 258 | + const { media_align: align, media_width: mWidth, media_distance: mDistance } = s; | |
| 259 | + if (align === "zigzag") { | |
| 260 | + handleZigzag(items, mWidth, mDistance); | |
| 261 | + } else { | |
| 262 | + items.find(".king-addons-grid-media-wrap").css({ | |
| 263 | + float: align, | |
| 264 | + width: `${mWidth}%`, | |
| 265 | + [`margin-${align === "left" ? "right" : "left"}`]: `${mDistance}px`, | |
| 266 | + }); | |
| 267 | + items.find(".king-addons-grid-item-below-content").css({ | |
| 268 | + float: align, | |
| 269 | + width: `calc((100% - ${mWidth}%) - ${mDistance}px)`, | |
| 270 | + }); | |
| 271 | + } | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + // Alternate ("zigzag") layout for list items. | |
| 276 | + function handleZigzag(items, mWidth, mDistance) { | |
| 277 | + items.filter(":even").each(function () { | |
| 278 | + $(this).find(".king-addons-grid-media-wrap").css({ | |
| 279 | + float: "left", | |
| 280 | + width: `${mWidth}%`, | |
| 281 | + "margin-right": `${mDistance}px`, | |
| 282 | + }); | |
| 283 | + $(this).find(".king-addons-grid-item-below-content").css({ | |
| 284 | + float: "left", | |
| 285 | + width: `calc((100% - ${mWidth}%) - ${mDistance}px)`, | |
| 286 | + }); | |
| 287 | + }); | |
| 288 | + items.filter(":odd").each(function () { | |
| 289 | + $(this).find(".king-addons-grid-media-wrap").css({ | |
| 290 | + float: "right", | |
| 291 | + width: `${mWidth}%`, | |
| 292 | + "margin-left": `${mDistance}px`, | |
| 293 | + }); | |
| 294 | + $(this).find(".king-addons-grid-item-below-content").css({ | |
| 295 | + float: "right", | |
| 296 | + width: `calc((100% - ${mWidth}%) - ${mDistance}px)`, | |
| 297 | + }); | |
| 298 | + }); | |
| 299 | + } | |
| 300 | + | |
| 301 | + // Equalize grid item heights for "fitRows" layout. | |
| 302 | + function setEqualHeight(s) { | |
| 303 | + if (s.layout === "fitRows") { | |
| 304 | + const $items = $grid.children("article"); | |
| 305 | + const columns = Math.floor($grid.outerWidth() / $items.outerWidth()); | |
| 306 | + if (columns > 1) { | |
| 307 | + const maxH = Math.max(...$items.map((_, el) => $(el).outerHeight()).get()); | |
| 308 | + $items.css("height", `${maxH}px`); | |
| 309 | + if (s.stick_last_element_to_bottom === "yes") { | |
| 310 | + $scope.addClass("king-addons-grid-last-element-yes"); | |
| 311 | + } | |
| 312 | + } | |
| 313 | + } | |
| 314 | + } | |
| 315 | + | |
| 316 | + // Update filter counts and set up filtering behavior. | |
| 317 | + function isotopeFilters(s, ev = "load") { | |
| 318 | + | |
| 319 | + // console.log("isotopeFilters"); | |
| 320 | + | |
| 321 | + if (s.filters_count === "yes") { | |
| 322 | + $scope.find(".king-addons-grid-filters a, .king-addons-grid-filters span").each(function () { | |
| 323 | + const $el = $(this); | |
| 324 | + if (s.grid_settings && ev === "load") { | |
| 325 | + const thisTaxonomy = | |
| 326 | + $el.attr("data-filter") !== "*" ? $el.data("ajax-filter")[0] : "*"; | |
| 327 | + const thisFilter = | |
| 328 | + $el.attr("data-filter") !== "*" ? $el.data("ajax-filter")[1] : "*"; | |
| 329 | + // console.log('CURRENT OFFSET = ' + (+s.grid_settings.query_offset + $grid.find(".king-addons-grid-item").length)) | |
| 330 | + $.ajax({ | |
| 331 | + type: "POST", | |
| 332 | + url: KingAddonsGridData.ajaxUrl, | |
| 333 | + data: { | |
| 334 | + action: s.grid_settings | |
| 335 | + ? "king_addons_get_filtered_count" | |
| 336 | + : "king_addons_get_filtered_count", | |
| 337 | + // king_addons_offset: 0, | |
| 338 | + king_addons_offset: +s.grid_settings.query_offset + $grid.find(".king-addons-grid-item").length, | |
| 339 | + king_addons_filter: thisFilter, | |
| 340 | + king_addons_taxonomy: thisTaxonomy, | |
| 341 | + grid_settings: s.grid_settings, | |
| 342 | + }, | |
| 343 | + success: (response) => { | |
| 344 | + // console.log('ISOTOPE GRID'); | |
| 345 | + $el.find("sup").text(response.data.query_found); | |
| 346 | + }, | |
| 347 | + }); | |
| 348 | + } else { | |
| 349 | + if ($el.attr("data-filter") === "*") { | |
| 350 | + $el.find("sup").text( | |
| 351 | + $scope.find(".king-addons-grid-filters").next().find("article").length | |
| 352 | + ); | |
| 353 | + } else { | |
| 354 | + $el.find("sup").text($scope.find($el.attr("data-filter")).length); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + }); | |
| 358 | + } | |
| 359 | + | |
| 360 | + if (s.filters_linkable === "yes") return; | |
| 361 | + | |
| 362 | + // Deeplinking support. | |
| 363 | + if (s.deeplinking === "yes") { | |
| 364 | + let deepLink = window.location.hash.replace("#filter:", "."); | |
| 365 | + if (window.location.hash.match("#filter:all")) deepLink = "*"; | |
| 366 | + const activeFilter = $scope | |
| 367 | + .find(`.king-addons-grid-filters span[data-filter="${deepLink}"]`) | |
| 368 | + .not(".king-addons-back-filter"); | |
| 369 | + $scope.find(".king-addons-grid-filters span").removeClass("king-addons-active-filter"); | |
| 370 | + activeFilter.addClass("king-addons-active-filter"); | |
| 371 | + $grid.isotopekng({ filter: deepLink }); | |
| 372 | + s.lightbox.selector = | |
| 373 | + deepLink === "*" ? ".king-addons-grid-image-wrap" : `${deepLink} .king-addons-grid-image-wrap`; | |
| 374 | + // lightboxPopup(s); | |
| 375 | + } | |
| 376 | + | |
| 377 | + // Hide empty filters. | |
| 378 | + if (s.filters_hide_empty === "yes" && !s.grid_settings) { | |
| 379 | + $scope.find(".king-addons-grid-filters span").each(function () { | |
| 380 | + const filterClass = $(this).attr("data-filter"); | |
| 381 | + if (filterClass !== "*" && $grid.find(filterClass).length === 0) { | |
| 382 | + $(this).parent("li").addClass("king-addons-hidden-element"); | |
| 383 | + } else { | |
| 384 | + $(this).parent("li").removeClass("king-addons-hidden-element"); | |
| 385 | + } | |
| 386 | + }); | |
| 387 | + } | |
| 388 | + | |
| 389 | + // Default filter. | |
| 390 | + if ( | |
| 391 | + !$scope.hasClass("elementor-widget-king-addons-woocommerce-category-grid-pro") && | |
| 392 | + !$scope.hasClass("elementor-widget-king-addons-category-grid-pro") && | |
| 393 | + s.filters_default_filter | |
| 394 | + ) { | |
| 395 | + setTimeout(() => { | |
| 396 | + const filterEl = $scope | |
| 397 | + .find(".king-addons-grid-filters") | |
| 398 | + .find(`span[data-filter*="-${s.filters_default_filter}"]`); | |
| 399 | + if (filterEl.length) filterEl[0].click(); | |
| 400 | + }, 100); | |
| 401 | + } | |
| 402 | + | |
| 403 | + // Filter click behavior. | |
| 404 | + if (!s.grid_settings) { | |
| 405 | + $scope.find(".king-addons-grid-filters span").on("click", function () { | |
| 406 | + const filterClass = $(this).data("filter"); | |
| 407 | + $scope.find(".king-addons-grid-filters span").removeClass("king-addons-active-filter"); | |
| 408 | + $(this).addClass("king-addons-active-filter"); | |
| 409 | + if (s.deeplinking === "yes") { | |
| 410 | + const filterHash = | |
| 411 | + filterClass === "*" ? "#filter:all" : `#filter:${filterClass.replace(".", "")}`; | |
| 412 | + window.location.href = | |
| 413 | + window.location.pathname + window.location.search + filterHash; | |
| 414 | + } | |
| 415 | + if (["infinite-scroll", "load-more"].includes(s.pagination_type)) { | |
| 416 | + if ($grid.find($(this).attr("data-filter")).length === 0) { | |
| 417 | + $grid.infiniteScroll("loadNextPage"); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + if (s.filters_animation !== "default") { | |
| 421 | + $scope.find(".king-addons-grid-item-inner").css({ | |
| 422 | + opacity: "0", | |
| 423 | + transition: "none", | |
| 424 | + }); | |
| 425 | + if (s.filters_animation === "fade-slide") { | |
| 426 | + $scope.find(".king-addons-grid-item-inner").css("top", "20px"); | |
| 427 | + } else if (s.filters_animation === "zoom") { | |
| 428 | + $scope.find(".king-addons-grid-item-inner").css("transform", "scale(0.01)"); | |
| 429 | + } | |
| 430 | + } | |
| 431 | + $grid.isotopekng({ filter: filterClass }); | |
| 432 | + s.lightbox.selector = | |
| 433 | + filterClass === "*" ? ".king-addons-grid-image-wrap" : `${filterClass} .king-addons-grid-image-wrap`; | |
| 434 | + $grid.data("lightGallery").destroy(true); | |
| 435 | + $grid.lightGallery(s.lightbox); | |
| 436 | + }); | |
| 437 | + } | |
| 438 | + } | |
| 439 | + | |
| 440 | + // Experiment: dynamic filtering via AJAX. | |
| 441 | + function filtersExperiment(settings) { | |
| 442 | + const countAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 443 | + ? "king_addons_get_woocommerce_filtered_count" | |
| 444 | + : "king_addons_get_filtered_count"; | |
| 445 | + const contentAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 446 | + ? "king_addons_filter_woocommerce_products" | |
| 447 | + : "king_addons_filter_grid_posts"; | |
| 448 | + const $pagination = $scope.find(".king-addons-grid-pagination"); | |
| 449 | + | |
| 450 | + let isLoading = false; | |
| 451 | + | |
| 452 | + $scope.find(".king-addons-grid-filters").on("click", "span", function (e) { | |
| 453 | + e.preventDefault(); | |
| 454 | + e.stopPropagation(); | |
| 455 | + e.stopImmediatePropagation(); | |
| 456 | + | |
| 457 | + if (isLoading) { | |
| 458 | + e.preventDefault(); | |
| 459 | + return; | |
| 460 | + } | |
| 461 | + | |
| 462 | + isLoading = true; | |
| 463 | + | |
| 464 | + const filterClass = $(this).data("filter"); | |
| 465 | + const thisTaxonomy = filterClass !== "*" ? $(this).data("ajax-filter")[0] : "*"; | |
| 466 | + const thisFilter = filterClass !== "*" ? $(this).data("ajax-filter")[1] : "*"; | |
| 467 | + const loader = `<div class="king-addons-grid-loader-wrap"><div class="king-addons-ring"><div></div><div></div><div></div><div></div></div></div>`; | |
| 468 | + | |
| 469 | + | |
| 470 | + // let thisTaxonomy = "*"; | |
| 471 | + // let thisFilter = "*"; | |
| 472 | + // const $activeFilter = $scope.find(".king-addons-active-filter"); | |
| 473 | + // if ($activeFilter.length && $activeFilter.data("filter") !== "*") { | |
| 474 | + // thisTaxonomy = $activeFilter.data("ajax-filter")[0]; | |
| 475 | + // thisFilter = $activeFilter.data("ajax-filter")[1]; | |
| 476 | + // } | |
| 477 | + | |
| 478 | + $scope.find(".king-addons-grid-filters span").removeClass("king-addons-active-filter"); | |
| 479 | + $(this).addClass("king-addons-active-filter"); | |
| 480 | + | |
| 481 | + $pagination.find(".king-addons-load-more-btn").hide(); | |
| 482 | + | |
| 483 | + $grid.infiniteScroll("destroy"); | |
| 484 | + // setupInfiniteScroll(settings); | |
| 485 | + // $grid.isotopekng("destroy"); | |
| 486 | + pagesLoaded = 0; | |
| 487 | + // $grid.isotopekng("destroy"); | |
| 488 | + | |
| 489 | + $grid.html(loader); | |
| 490 | + | |
| 491 | + // console.log('FILTER'); | |
| 492 | + | |
| 493 | + $.ajax({ | |
| 494 | + type: "POST", | |
| 495 | + url: KingAddonsGridData.ajaxUrl, | |
| 496 | + data: { | |
| 497 | + action: countAction, | |
| 498 | + king_addons_offset: | |
| 499 | + +settings.grid_settings.query_offset + | |
| 500 | + $scope.find(".king-addons-grid-item").length, | |
| 501 | + king_addons_filter: thisFilter, | |
| 502 | + king_addons_taxonomy: thisTaxonomy, | |
| 503 | + grid_settings: settings.grid_settings, | |
| 504 | + }, | |
| 505 | + success: function (res) { | |
| 506 | + // console.log("settings.grid_settings.query_offset = " + settings.grid_settings.query_offset); | |
| 507 | + $.ajax({ | |
| 508 | + type: "POST", | |
| 509 | + url: KingAddonsGridData.ajaxUrl, | |
| 510 | + data: { | |
| 511 | + action: contentAction, | |
| 512 | + king_addons_item_length: | |
| 513 | + +settings.grid_settings.query_offset + | |
| 514 | + $scope.find(".king-addons-grid-item").length, | |
| 515 | + king_addons_filter: thisFilter, | |
| 516 | + king_addons_taxonomy: thisTaxonomy, | |
| 517 | + grid_settings: settings.grid_settings, | |
| 518 | + }, | |
| 519 | + success: function (resp) { | |
| 520 | + // setTimeout(() => { | |
| 521 | + $grid.addClass("king-addons-zero-opacity"); | |
| 522 | + | |
| 523 | + // console.log("FILTER pagesLoaded = " + pagesLoaded); | |
| 524 | + // $grid.infiniteScroll("destroy"); | |
| 525 | + $grid.isotopekng("destroy"); | |
| 526 | + | |
| 527 | + $grid.html($(resp)); | |
| 528 | + | |
| 529 | + isotopeLayout(settings, $(resp)); | |
| 530 | + $grid.imagesLoaded().progress(() => { | |
| 531 | + isotopeLayout(settings); | |
| 532 | + window.dispatchEvent(new Event("resize")); | |
| 533 | + window.dispatchEvent(new Event("scroll")); | |
| 534 | + | |
| 535 | + $pagination.find(".king-addons-pagination-finish").hide(); | |
| 536 | + if (res.data.page_count > 1) { | |
| 537 | + $pagination.find(".king-addons-load-more-btn").show(); | |
| 538 | + $pagination.show(); | |
| 539 | + } else { | |
| 540 | + $pagination.find(".king-addons-pagination-finish").fadeIn(1000); | |
| 541 | + $pagination.delay(2000).fadeOut(1000); | |
| 542 | + setTimeout(() => $pagination.find(".king-addons-pagination-loading").hide(), 500); | |
| 543 | + } | |
| 544 | + | |
| 545 | + setupInfiniteScroll(settings); | |
| 546 | + loadMoreExperiment(); | |
| 547 | + | |
| 548 | + mediaHoverLink(); | |
| 549 | + $grid.removeClass("king-addons-zero-opacity"); | |
| 550 | + }); | |
| 551 | + // }, 800); | |
| 552 | + }, | |
| 553 | + }); | |
| 554 | + }, | |
| 555 | + complete: function () { | |
| 556 | + isLoading = false; | |
| 557 | + } | |
| 558 | + }); | |
| 559 | + }); | |
| 560 | + } | |
| 561 | + | |
| 562 | + // Experiment: load-more via AJAX. | |
| 563 | + function loadMoreExperiment() { | |
| 564 | + const countAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 565 | + ? "king_addons_get_woocommerce_filtered_count" | |
| 566 | + : "king_addons_get_filtered_count"; | |
| 567 | + const contentAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 568 | + ? "king_addons_filter_woocommerce_products" | |
| 569 | + : "king_addons_filter_grid_posts"; | |
| 570 | + const $pagination = $scope.find(".king-addons-grid-pagination"); | |
| 571 | + | |
| 572 | + let isLoading = false; | |
| 573 | + | |
| 574 | + $scope.find(".king-addons-load-more-btn").on("click", function (e) { | |
| 575 | + e.preventDefault(); | |
| 576 | + e.stopPropagation(); | |
| 577 | + e.stopImmediatePropagation(); | |
| 578 | + | |
| 579 | + if (isLoading) { | |
| 580 | + e.preventDefault(); | |
| 581 | + return; | |
| 582 | + } | |
| 583 | + | |
| 584 | + isLoading = true; | |
| 585 | + | |
| 586 | + let thisTaxonomy = "*"; | |
| 587 | + let thisFilter = "*"; | |
| 588 | + const $activeFilter = $scope.find(".king-addons-active-filter"); | |
| 589 | + if ($activeFilter.length && $activeFilter.data("filter") !== "*") { | |
| 590 | + thisTaxonomy = $activeFilter.data("ajax-filter")[0]; | |
| 591 | + thisFilter = $activeFilter.data("ajax-filter")[1]; | |
| 592 | + } | |
| 593 | + | |
| 594 | + $pagination.find(".king-addons-load-more-btn").hide(); | |
| 595 | + $pagination.find(".king-addons-pagination-loading").css("display", "inline-block"); | |
| 596 | + | |
| 597 | + $.ajax({ | |
| 598 | + type: "POST", | |
| 599 | + url: KingAddonsGridData.ajaxUrl, | |
| 600 | + data: { | |
| 601 | + action: countAction, | |
| 602 | + king_addons_offset: | |
| 603 | + +settings.grid_settings.query_offset + | |
| 604 | + $grid.find(".king-addons-grid-item").length, | |
| 605 | + king_addons_filter: thisFilter, | |
| 606 | + king_addons_taxonomy: thisTaxonomy, | |
| 607 | + grid_settings: settings.grid_settings, | |
| 608 | + }, | |
| 609 | + success: function (responseCountAction) { | |
| 610 | + $.ajax({ | |
| 611 | + type: "POST", | |
| 612 | + url: KingAddonsGridData.ajaxUrl, | |
| 613 | + data: { | |
| 614 | + action: contentAction, | |
| 615 | + king_addons_offset: | |
| 616 | + +settings.grid_settings.query_offset + | |
| 617 | + $grid.find(".king-addons-grid-item").length, | |
| 618 | + king_addons_filter: thisFilter, | |
| 619 | + king_addons_taxonomy: thisTaxonomy, | |
| 620 | + grid_settings: settings.grid_settings, | |
| 621 | + }, | |
| 622 | + success: function (responseContentAction) { | |
| 623 | + const $items = $(responseContentAction); | |
| 624 | + $grid.infiniteScroll("appendItems", $items); | |
| 625 | + $grid.isotopekng("appended", $items); | |
| 626 | + $items.imagesLoaded().progress(() => { | |
| 627 | + isotopeLayout(settings); | |
| 628 | + setTimeout(() => isotopeLayout(settings), 100); | |
| 629 | + setTimeout(() => $grid.addClass("grid-images-loaded"), 500); | |
| 630 | + }); | |
| 631 | + $pagination.find(".king-addons-pagination-loading").hide(); | |
| 632 | + if (responseCountAction.data.page_count > 1) { | |
| 633 | + $pagination.find(".king-addons-load-more-btn").fadeIn(); | |
| 634 | + } else { | |
| 635 | + $pagination.find(".king-addons-pagination-finish").fadeIn(1000); | |
| 636 | + $pagination.delay(2000).fadeOut(1000); | |
| 637 | + setTimeout(() => $pagination.find(".king-addons-pagination-loading").hide(), 500); | |
| 638 | + } | |
| 639 | + // lightboxPopup(settings); | |
| 640 | + $grid.data("lightGallery").destroy(true); | |
| 641 | + $grid.lightGallery(settings.lightbox); | |
| 642 | + mediaHoverLink(); | |
| 643 | + postSharing(); | |
| 644 | + window.dispatchEvent(new Event("resize")); | |
| 645 | + window.dispatchEvent(new Event("scroll")); | |
| 646 | + }, | |
| 647 | + }); | |
| 648 | + }, | |
| 649 | + complete: function () { | |
| 650 | + isLoading = false; | |
| 651 | + } | |
| 652 | + }); | |
| 653 | + | |
| 654 | + | |
| 655 | + }); | |
| 656 | + } | |
| 657 | + | |
| 658 | + // Setup Infinite Scroll. | |
| 659 | + function setupInfiniteScroll(s) { | |
| 660 | + | |
| 661 | + | |
| 662 | + const countAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 663 | + ? "king_addons_get_woocommerce_filtered_count" | |
| 664 | + : "king_addons_get_filtered_count"; | |
| 665 | + const contentAction = $scope.hasClass("elementor-widget-king-addons-woocommerce-grid") | |
| 666 | + ? "king_addons_filter_woocommerce_products" | |
| 667 | + : "king_addons_filter_grid_posts"; | |
| 668 | + | |
| 669 | + const $pagination = $scope.find(".king-addons-grid-pagination"); | |
| 670 | + | |
| 671 | + const scopeClass = `.elementor-element-${$scope.attr("data-id")}`; | |
| 672 | + | |
| 673 | + let navClass = false, | |
| 674 | + threshold = false; | |
| 675 | + if (s.pagination_type === "infinite-scroll") { | |
| 676 | + threshold = 300; | |
| 677 | + navClass = `${scopeClass} .king-addons-load-more-btn`; | |
| 678 | + } | |
| 679 | + // todo | |
| 680 | + $grid.infiniteScroll({ | |
| 681 | + path: `${scopeClass} .king-addons-grid-pagination a`, | |
| 682 | + hideNav: navClass, | |
| 683 | + append: false, | |
| 684 | + history: false, | |
| 685 | + scrollThreshold: threshold, | |
| 686 | + status: `${scopeClass} .page-load-status`, | |
| 687 | + onInit() { | |
| 688 | + this.on("load", () => $grid.removeClass("grid-images-loaded")); | |
| 689 | + }, | |
| 690 | + }); | |
| 691 | + $grid.on("request.infiniteScroll", () => { | |
| 692 | + $pagination.find(".king-addons-load-more-btn").hide(); | |
| 693 | + $pagination.find(".king-addons-pagination-loading").css("display", "inline-block"); | |
| 694 | + }); | |
| 695 | + | |
| 696 | + $grid.on("load.infiniteScroll", (event, response) => { | |
| 697 | + // console.log("pagesLoaded = " + pagesLoaded); | |
| 698 | + | |
| 699 | + pagesLoaded++; | |
| 700 | + | |
| 701 | + const $items = $(response).find(scopeClass).find(".king-addons-grid-item"); | |
| 702 | + | |
| 703 | + // console.log("$items = " + $items); | |
| 704 | + // console.log("$items.length = " + $items.length); | |
| 705 | + | |
| 706 | + if ($scope.find(".woocommerce-result-count").length) { | |
| 707 | + let updatedCount = $scope.find(".woocommerce-result-count").text(); | |
| 708 | + updatedCount = updatedCount.replace( | |
| 709 | + /\d\u2013\d+/, | |
| 710 | + `1–${$scope.find(".king-addons-grid-item").length + $items.length}` | |
| 711 | + ); | |
| 712 | + $scope.find(".woocommerce-result-count").text(updatedCount); | |
| 713 | + } | |
| 714 | + | |
| 715 | + | |
| 716 | + $grid.infiniteScroll("appendItems", $items); | |
| 717 | + $grid.isotopekng("appended", $items); | |
| 718 | + $items.imagesLoaded().progress(() => { | |
| 719 | + isotopeLayout(s); | |
| 720 | + setTimeout(() => { | |
| 721 | + // isotopeLayout(s); | |
| 722 | + // isotopeFilters(s); | |
| 723 | + // =============================== | |
| 724 | + | |
| 725 | + // ================================ | |
| 726 | + }, 10); | |
| 727 | + setTimeout(() => $grid.addClass("grid-images-loaded"), 500); | |
| 728 | + }); | |
| 729 | + | |
| 730 | + // ===================== | |
| 731 | + | |
| 732 | + | |
| 733 | + // let thisTaxonomy = "*"; | |
| 734 | + // let thisFilter = "*"; | |
| 735 | + // const $activeFilter = $scope.find(".king-addons-active-filter"); | |
| 736 | + // if ($activeFilter.length && $activeFilter.data("filter") !== "*") { | |
| 737 | + // thisTaxonomy = $activeFilter.data("ajax-filter")[0]; | |
| 738 | + // thisFilter = $activeFilter.data("ajax-filter")[1]; | |
| 739 | + // } | |
| 740 | + // | |
| 741 | + // $pagination.find(".king-addons-load-more-btn").hide(); | |
| 742 | + // $pagination.find(".king-addons-pagination-loading").css("display", "inline-block"); | |
| 743 | + // | |
| 744 | + // $.ajax({ | |
| 745 | + // type: "POST", | |
| 746 | + // url: KingAddonsGridData.ajaxUrl, | |
| 747 | + // data: { | |
| 748 | + // action: countAction, | |
| 749 | + // king_addons_offset: | |
| 750 | + // +settings.grid_settings.query_offset + | |
| 751 | + // $grid.find(".king-addons-grid-item").length, | |
| 752 | + // king_addons_filter: thisFilter, | |
| 753 | + // king_addons_taxonomy: thisTaxonomy, | |
| 754 | + // grid_settings: settings.grid_settings, | |
| 755 | + // }, | |
| 756 | + // success: function (responseCountAction) { | |
| 757 | + // console.log('responseCountAction = ' + responseCountAction.data.page_count); | |
| 758 | + // $.ajax({ | |
| 759 | + // type: "POST", | |
| 760 | + // url: KingAddonsGridData.ajaxUrl, | |
| 761 | + // data: { | |
| 762 | + // action: contentAction, | |
| 763 | + // king_addons_offset: | |
| 764 | + // +settings.grid_settings.query_offset + | |
| 765 | + // $grid.find(".king-addons-grid-item").length, | |
| 766 | + // king_addons_filter: thisFilter, | |
| 767 | + // king_addons_taxonomy: thisTaxonomy, | |
| 768 | + // grid_settings: settings.grid_settings, | |
| 769 | + // }, | |
| 770 | + // success: function (responseContentAction) { | |
| 771 | + // const $items = $(responseContentAction); | |
| 772 | + // $grid.infiniteScroll("appendItems", $items); | |
| 773 | + // $grid.isotopekng("appended", $items); | |
| 774 | + // $items.imagesLoaded().progress(() => { | |
| 775 | + // isotopeLayout(settings); | |
| 776 | + // setTimeout(() => isotopeLayout(settings), 100); | |
| 777 | + // setTimeout(() => $grid.addClass("grid-images-loaded"), 500); | |
| 778 | + // }); | |
| 779 | + // $pagination.find(".king-addons-pagination-loading").hide(); | |
| 780 | + // if (responseCountAction.data.page_count > 1) { | |
| 781 | + // $pagination.find(".king-addons-load-more-btn").fadeIn(); | |
| 782 | + // } else { | |
| 783 | + // $pagination.find(".king-addons-pagination-finish").fadeIn(1000); | |
| 784 | + // $pagination.delay(2000).fadeOut(1000); | |
| 785 | + // setTimeout(() => $pagination.find(".king-addons-pagination-loading").hide(), 500); | |
| 786 | + // } | |
| 787 | + // lightboxPopup(settings); | |
| 788 | + // $grid.data("lightGallery").destroy(true); | |
| 789 | + // $grid.lightGallery(settings.lightbox); | |
| 790 | + // mediaHoverLink(); | |
| 791 | + // postSharing(); | |
| 792 | + // window.dispatchEvent(new Event("resize")); | |
| 793 | + // window.dispatchEvent(new Event("scroll")); | |
| 794 | + // | |
| 795 | + // }, | |
| 796 | + // }); | |
| 797 | + // }, | |
| 798 | + // // complete: function () { | |
| 799 | + // // isLoading = false; | |
| 800 | + // // } | |
| 801 | + // }); | |
| 802 | + | |
| 803 | + // ===================== | |
| 804 | + $pagination.find(".king-addons-pagination-loading").hide(); | |
| 805 | + if (pagesLoaded < s.pagination_max_pages) { | |
| 806 | + if (s.pagination_type === "load-more") { | |
| 807 | + $pagination.find(".king-addons-load-more-btn").fadeIn(); | |
| 808 | + if ($scope.find(".king-addons-grid-filters").length) { | |
| 809 | + const activeF = $scope.find(".king-addons-active-filter"); | |
| 810 | + if (activeF.length && activeF.attr("data-filter") !== "*") { | |
| 811 | + const filterClass = activeF.attr("data-filter").slice(1); | |
| 812 | + let foundOne = false; | |
| 813 | + $items.each(function () { | |
| 814 | + if ($(this).hasClass(filterClass)) { | |
| 815 | + foundOne = true; | |
| 816 | + return false; | |
| 817 | + } | |
| 818 | + }); | |
| 819 | + if (!foundOne) $grid.infiniteScroll("loadNextPage"); | |
| 820 | + } | |
| 821 | + } | |
| 822 | + } | |
| 823 | + } else { | |
| 824 | + $pagination.find(".king-addons-pagination-finish").fadeIn(1000); | |
| 825 | + $pagination.delay(2000).fadeOut(1000); | |
| 826 | + setTimeout(() => $pagination.find(".king-addons-pagination-loading").hide(), 500); | |
| 827 | + } | |
| 828 | + // lightboxPopup(s); | |
| 829 | + $grid.data("lightGallery").destroy(true); | |
| 830 | + $grid.lightGallery(s.lightbox); | |
| 831 | + mediaHoverLink(); | |
| 832 | + postSharing(); | |
| 833 | + setTimeout(() => { | |
| 834 | + setEqualHeight(s); | |
| 835 | + window.dispatchEvent(new Event("resize")); | |
| 836 | + }, 500); | |
| 837 | + }); | |
| 838 | + $pagination.find(".king-addons-load-more-btn").on("click", () => { | |
| 839 | + $grid.infiniteScroll("loadNextPage"); | |
| 840 | + return false; | |
| 841 | + }); | |
| 842 | + } | |
| 843 | + | |
| 844 | + // Initialize the Slick slider. | |
| 845 | + function initSlickSlider() { | |
| 846 | + const settingsSlick = JSON.parse($grid.attr("data-slick") || "{}"); | |
| 847 | + $grid.slick({ | |
| 848 | + appendDots: $scope.find(".king-addons-grid-slider-dots"), | |
| 849 | + rows: settingsSlick.sliderRows, | |
| 850 | + customPaging: () => `<span class="king-addons-grid-slider-dot"></span>`, | |
| 851 | + slidesToShow: getSliderColumns("desktop"), | |
| 852 | + responsive: getSliderResponsiveOptions(settingsSlick), | |
| 853 | + }); | |
| 854 | + handleSlickArrows(); | |
| 855 | + handleSlickDots(); | |
| 856 | + } | |
| 857 | + | |
| 858 | + // Get the number of slider columns. | |
| 859 | + function getSliderColumns(type) { | |
| 860 | + const match = $scope.attr("class").match(/king-addons-grid-slider-columns-(\d)/); | |
| 861 | + return type === "desktop" ? (match ? parseInt(match[1], 10) : 2) : 2; | |
| 862 | + } | |
| 863 | + | |
| 864 | + // Get responsive slider options. | |
| 865 | + function getSliderResponsiveOptions(sl) { | |
| 866 | + const className = $scope.attr("class"); | |
| 867 | + const colDesktop = getSliderColumns("desktop"); | |
| 868 | + const colWide = extractColumns(className, "widescreen") || colDesktop; | |
| 869 | + const colLaptop = extractColumns(className, "laptop") || colDesktop; | |
| 870 | + const colTablet = extractColumns(className, "tablet") || 2; | |
| 871 | + const colTabletExtra = extractColumns(className, "tablet_extra") || colTablet; | |
| 872 | + const colMobileExtra = extractColumns(className, "mobile_extra") || colTablet; | |
| 873 | + const colMobile = extractColumns(className, "mobile") || 1; | |
| 874 | + const slidesToScroll = sl.sliderSlidesToScroll; | |
| 875 | + const adjust = (cols) => (slidesToScroll > cols ? 1 : slidesToScroll); | |
| 876 | + return [ | |
| 877 | + { breakpoint: 10000, settings: { slidesToShow: colWide, slidesToScroll: adjust(colWide) } }, | |
| 878 | + { breakpoint: 2399, settings: { slidesToShow: colDesktop, slidesToScroll: adjust(colDesktop) } }, | |
| 879 | + { breakpoint: 1221, settings: { slidesToShow: colLaptop, slidesToScroll: adjust(colLaptop) } }, | |
| 880 | + { breakpoint: 1200, settings: { slidesToShow: colTabletExtra, slidesToScroll: adjust(colTabletExtra) } }, | |
| 881 | + { breakpoint: 1024, settings: { slidesToShow: colTablet, slidesToScroll: adjust(colTablet) } }, | |
| 882 | + { breakpoint: 880, settings: { slidesToShow: colMobileExtra, slidesToScroll: adjust(colMobileExtra) } }, | |
| 883 | + { breakpoint: 768, settings: { slidesToShow: colMobile, slidesToScroll: adjust(colMobile) } }, | |
| 884 | + ]; | |
| 885 | + } | |
| 886 | + | |
| 887 | + // Extract a number of columns from a class name. | |
| 888 | + function extractColumns(className, type) { | |
| 889 | + const match = className.match(new RegExp(`columns--${type}(\\d)`)); | |
| 890 | + return match ? parseInt(match[1], 10) : false; | |
| 891 | + } | |
| 892 | + | |
| 893 | + // Adjust the slider arrows based on available space. | |
| 894 | + function handleSlickArrows() { | |
| 895 | + const $prevArrow = $scope.find(".king-addons-grid-slider-prev-arrow"); | |
| 896 | + const $nextArrow = $scope.find(".king-addons-grid-slider-next-arrow"); | |
| 897 | + if (!$prevArrow.length || !$nextArrow.length) return; | |
| 898 | + const positionSum = $prevArrow.position().left * -2; | |
| 899 | + $(window).on("load", checkArrows); | |
| 900 | + $(window).smartresize(checkArrows); | |
| 901 | + function checkArrows() { | |
| 902 | + if ( | |
| 903 | + $(window).width() <= | |
| 904 | + $scope.outerWidth() + | |
| 905 | + $prevArrow.outerWidth() + | |
| 906 | + $nextArrow.outerWidth() + | |
| 907 | + positionSum | |
| 908 | + ) { | |
| 909 | + $prevArrow.addClass("king-addons-adjust-slider-prev-arrow"); | |
| 910 | + $nextArrow.addClass("king-addons-adjust-slider-next-arrow"); | |
| 911 | + } else { | |
| 912 | + $prevArrow.removeClass("king-addons-adjust-slider-prev-arrow"); | |
| 913 | + $nextArrow.removeClass("king-addons-adjust-slider-next-arrow"); | |
| 914 | + } | |
| 915 | + } | |
| 916 | + } | |
| 917 | + | |
| 918 | + // Adjust slider dots if needed. | |
| 919 | + function handleSlickDots() { | |
| 920 | + if ( | |
| 921 | + $scope.find(".slick-dots").length && | |
| 922 | + $scope.hasClass("king-addons-grid-slider-dots-horizontal") | |
| 923 | + ) { | |
| 924 | + resizeDots(); | |
| 925 | + $(window).smartresize(() => setTimeout(resizeDots, 300)); | |
| 926 | + } | |
| 927 | + function resizeDots() { | |
| 928 | + const $dots = $scope.find(".slick-dots li"); | |
| 929 | + const marginRight = parseInt($dots.find("span").css("margin-right"), 10); | |
| 930 | + const width = $dots.outerWidth() * $dots.length - marginRight; | |
| 931 | + $scope.find(".slick-dots").css("width", width); | |
| 932 | + } | |
| 933 | + } | |
| 934 | + | |
| 935 | + // Setup "Add to Cart" behavior. | |
| 936 | + function setupAddToCart() { | |
| 937 | + const $addCartIcon = $grid.find(".king-addons-grid-item-add-to-cart i"); | |
| 938 | + let addCartIconClass = $addCartIcon.attr("class") || ""; | |
| 939 | + if (addCartIconClass) { | |
| 940 | + addCartIconClass = addCartIconClass.substring( | |
| 941 | + addCartIconClass.indexOf("fa-") | |
| 942 | + ); | |
| 943 | + } | |
| 944 | + $body.on("adding_to_cart", (ev, btn) => btn.fadeTo("slow", 0)); | |
| 945 | + $body.on("added_to_cart", (ev, fragments, hash, btn) => { | |
| 946 | + const productId = btn.data("product_id"); | |
| 947 | + btn.next().fadeTo(700, 1).css("display", "inline-block"); | |
| 948 | + btn.css("display", "none"); | |
| 949 | + if (btn.data("atc-popup") === "sidebar") { | |
| 950 | + $(".king-addons-mini-cart-toggle-wrap a").each(function () { | |
| 951 | + const $miniCart = $(this) | |
| 952 | + .closest(".king-addons-mini-cart-inner") | |
| 953 | + .find(".king-addons-mini-cart"); | |
| 954 | + if ($miniCart.css("display") === "none") $(this).trigger("click"); | |
| 955 | + }); | |
| 956 | + } else if (btn.data("atc-popup") === "popup") { | |
| 957 | + addToCartPopup(btn, productId); | |
| 958 | + } | |
| 959 | + if (addCartIconClass) { | |
| 960 | + btn.find("i").removeClass(addCartIconClass).addClass("fa-check"); | |
| 961 | + setTimeout(() => { | |
| 962 | + btn.find("i").removeClass("fa-check").addClass(addCartIconClass); | |
| 963 | + }, 3500); | |
| 964 | + } | |
| 965 | + }); | |
| 966 | + | |
| 967 | + function addToCartPopup(btn, productId) { | |
| 968 | + const $popupItem = btn.closest(".king-addons-grid-item"); | |
| 969 | + const popupText = $popupItem.find(".king-addons-grid-item-title").text(); | |
| 970 | + const popupLink = btn.next().attr("href"); | |
| 971 | + const popupImageSrc = $popupItem.find(".king-addons-grid-image-wrap").data("src"); | |
| 972 | + const popupAnimation = btn.data("atc-animation"); | |
| 973 | + const fadeOutIn = btn.data("atc-fade-out-in"); | |
| 974 | + const animTime = btn.data("atc-animation-time"); | |
| 975 | + let animationClass = "king-addons-added-to-cart-default", | |
| 976 | + removeAnimationClass = "king-addons-added-to-cart-popup-hide"; | |
| 977 | + const popupImage = popupImageSrc | |
| 978 | + ? `<div class="king-addons-added-tc-popup-img"><img src="${popupImageSrc}" alt="" /></div>` | |
| 979 | + : ""; | |
| 980 | + switch (popupAnimation) { | |
| 981 | + case "slide-left": | |
| 982 | + animationClass = "king-addons-added-to-cart-slide-in-left"; | |
| 983 | + removeAnimationClass = "king-addons-added-to-cart-slide-out-left"; | |
| 984 | + break; | |
| 985 | + case "scale-up": | |
| 986 | + animationClass = "king-addons-added-to-cart-scale-up"; | |
| 987 | + removeAnimationClass = "king-addons-added-to-cart-scale-down"; | |
| 988 | + break; | |
| 989 | + case "skew": | |
| 990 | + animationClass = "king-addons-added-to-cart-skew"; | |
| 991 | + removeAnimationClass = "king-addons-added-to-cart-skew-off"; | |
| 992 | + break; | |
| 993 | + case "fade": | |
| 994 | + animationClass = "king-addons-added-to-cart-fade"; | |
| 995 | + removeAnimationClass = "king-addons-added-to-cart-fade-out"; | |
| 996 | + break; | |
| 997 | + } | |
| 998 | + if (!$scope.find(`#king-addons-added-to-cart-${productId}`).length) { | |
| 999 | + $scope | |
| 1000 | + .find(".king-addons-grid") | |
| 1001 | + .append( | |
| 1002 | + `<div id="king-addons-added-to-cart-${productId}" class="king-addons-added-to-cart-popup ${animationClass}"> | |
| 1003 | + ${popupImage} | |
| 1004 | + <div class="king-addons-added-tc-title"> | |
| 1005 | + <p>${popupText} ${KingAddonsGridData.addedToCartText}</p> | |
| 1006 | + <p><a href="${popupLink}">${KingAddonsGridData.viewCart}</a></p> | |
| 1007 | + </div> | |
| 1008 | + </div>` | |
| 1009 | + ); | |
| 1010 | + setTimeout(() => { | |
| 1011 | + $scope | |
| 1012 | + .find(`#king-addons-added-to-cart-${productId}`) | |
| 1013 | + .addClass(removeAnimationClass); | |
| 1014 | + setTimeout(() => { | |
| 1015 | + $scope.find(`#king-addons-added-to-cart-${productId}`).remove(); | |
| 1016 | + }, animTime * 1000); | |
| 1017 | + }, fadeOutIn * 1000); | |
| 1018 | + } | |
| 1019 | + } | |
| 1020 | + } | |
| 1021 | + | |
| 1022 | + // Setup post sharing behavior. | |
| 1023 | + function postSharing() { | |
| 1024 | + if (!$scope.find(".king-addons-sharing-trigger").length) return; | |
| 1025 | + const $sharingTrigger = $scope.find(".king-addons-sharing-trigger"); | |
| 1026 | + const $sharingInner = $scope.find(".king-addons-post-sharing-inner"); | |
| 1027 | + let sharingWidth = 5; | |
| 1028 | + $sharingInner.first().find("a").each(function () { | |
| 1029 | + sharingWidth += $(this).outerWidth() + parseInt($(this).css("margin-right"), 10); | |
| 1030 | + }); | |
| 1031 | + const direction = $sharingTrigger.attr("data-direction"); | |
| 1032 | + if (direction === "left" || direction === "right") { | |
| 1033 | + $sharingInner.css("width", `${sharingWidth}px`); | |
| 1034 | + if (direction === "left") { | |
| 1035 | + $sharingInner.css( | |
| 1036 | + "left", | |
| 1037 | + -(parseInt($sharingInner.find("a").css("margin-right"), 10) + sharingWidth) + "px" | |
| 1038 | + ); | |
| 1039 | + } else { | |
| 1040 | + $sharingInner.css({ left: $sharingTrigger.css("margin-right") }); | |
| 1041 | + } | |
| 1042 | + } else if (direction === "top") { | |
| 1043 | + const margin = parseInt($sharingInner.find("a").css("margin-right"), 10); | |
| 1044 | + $sharingInner.find("a").css({ "margin-right": "0", "margin-top": `${margin}px` }); | |
| 1045 | + $sharingInner.css({ | |
| 1046 | + top: `-${margin}px`, | |
| 1047 | + left: "50%", | |
| 1048 | + transform: "translate(-50%, -100%)", | |
| 1049 | + }); | |
| 1050 | + } else if (direction === "bottom") { | |
| 1051 | + const margin = parseInt($sharingInner.find("a").css("margin-right"), 10); | |
| 1052 | + $sharingInner.find("a").css({ "margin-right": "0", "margin-bottom": `${margin}px` }); | |
| 1053 | + $sharingInner.css({ | |
| 1054 | + bottom: `-${margin}px`, | |
| 1055 | + left: "50%", | |
| 1056 | + transform: "translate(-50%, 100%)", | |
| 1057 | + }); | |
| 1058 | + } | |
| 1059 | + if ($sharingTrigger.attr("data-action") === "click") { | |
| 1060 | + $sharingTrigger.on("click", function () { | |
| 1061 | + const $inner = $(this).next(); | |
| 1062 | + if ($inner.css("visibility") === "hidden") { | |
| 1063 | + $inner.css("visibility", "visible").find("a").css({ opacity: "1", top: "0" }); | |
| 1064 | + setTimeout(() => $inner.find("a").addClass("king-addons-no-transition-delay"), $inner.find("a").length * 100); | |
| 1065 | + } else { | |
| 1066 | + $inner.find("a").removeClass("king-addons-no-transition-delay").css({ | |
| 1067 | + opacity: "0", | |
| 1068 | + top: "-5px", | |
| 1069 | + }); | |
| 1070 | + setTimeout(() => $inner.css("visibility", "hidden"), $inner.find("a").length * 100); | |
| 1071 | + } | |
| 1072 | + }); | |
| 1073 | + } else { | |
| 1074 | + $sharingTrigger.on("mouseenter", function () { | |
| 1075 | + const $inner = $(this).next(); | |
| 1076 | + $inner.css("visibility", "visible").find("a").css({ opacity: "1", top: "0" }); | |
| 1077 | + setTimeout(() => $inner.find("a").addClass("king-addons-no-transition-delay"), $inner.find("a").length * 100); | |
| 1078 | + }); | |
| 1079 | + $scope.find(".king-addons-grid-item-sharing").on("mouseleave", function () { | |
| 1080 | + const $inner = $(this).find(".king-addons-post-sharing-inner"); | |
| 1081 | + $inner.find("a").removeClass("king-addons-no-transition-delay").css({ | |
| 1082 | + opacity: "0", | |
| 1083 | + top: "-5px", | |
| 1084 | + }); | |
| 1085 | + setTimeout(() => $inner.css("visibility", "hidden"), $inner.find("a").length * 100); | |
| 1086 | + }); | |
| 1087 | + } | |
| 1088 | + } | |
| 1089 | + | |
| 1090 | + // Setup media hover behavior and overlay clickable links. | |
| 1091 | + function mediaHoverLink() { | |
| 1092 | + const $wrap = $grid.find(".king-addons-grid-image-wrap"); | |
| 1093 | + if ($wrap.data("img-on-hover") === "yes") { | |
| 1094 | + $grid.find(".king-addons-grid-media-wrap").hover( | |
| 1095 | + function () { | |
| 1096 | + const $secondImg = $(this).find("img:nth-of-type(2)"); | |
| 1097 | + if ($secondImg.attr("src")) { | |
| 1098 | + $(this).find("img:first-of-type").addClass("king-addons-hidden-img"); | |
| 1099 | + $secondImg.removeClass("king-addons-hidden-img"); | |
| 1100 | + } | |
| 1101 | + }, | |
| 1102 | + function () { | |
| 1103 | + const $secondImg = $(this).find("img:nth-of-type(2)"); | |
| 1104 | + if ($secondImg.attr("src")) { | |
| 1105 | + $secondImg.addClass("king-addons-hidden-img"); | |
| 1106 | + $(this).find("img:first-of-type").removeClass("king-addons-hidden-img"); | |
| 1107 | + } | |
| 1108 | + } | |
| 1109 | + ); | |
| 1110 | + } | |
| 1111 | + if ($wrap.attr("data-overlay-link") === "yes" && !isEditor) { | |
| 1112 | + $wrap.css("cursor", "pointer").on("click", function (e) { | |
| 1113 | + const cn = e.target.className; | |
| 1114 | + if ( | |
| 1115 | + cn.indexOf("inner-block") !== -1 || | |
| 1116 | + cn.indexOf("king-addons-cv-inner") !== -1 || | |
| 1117 | + cn.indexOf("king-addons-grid-media-hover") !== -1 | |
| 1118 | + ) { | |
| 1119 | + e.preventDefault(); | |
| 1120 | + let itemUrl = $(this) | |
| 1121 | + .find(".king-addons-grid-media-hover-bg") | |
| 1122 | + .attr("data-url") | |
| 1123 | + .replace("#new_tab", ""); | |
| 1124 | + if ($grid.find(".king-addons-grid-item-title a").attr("target") === "_blank") { | |
| 1125 | + window.open(itemUrl, "_blank").focus(); | |
| 1126 | + } else { | |
| 1127 | + window.location.href = itemUrl; | |
| 1128 | + } | |
| 1129 | + } | |
| 1130 | + }); | |
| 1131 | + } | |
| 1132 | + } | |
| 1133 | + | |
| 1134 | + // Initialize lightbox popups. | |
| 1135 | + function lightboxPopup(s) { | |
| 1136 | + if ($scope.find(".king-addons-grid-item-lightbox").length < 0) return; | |
| 1137 | + $grid.find(".king-addons-grid-item-lightbox").each(function () { | |
| 1138 | + const source = $(this).find(".inner-block > span").attr("data-src"); | |
| 1139 | + const $article = $(this).closest("article").not(".slick-cloned"); | |
| 1140 | + if (!$grid.hasClass("king-addons-media-grid")) { | |
| 1141 | + $article.find(".king-addons-grid-image-wrap").attr("data-src", source); | |
| 1142 | + } | |
| 1143 | + }); | |
| 1144 | + $grid.lightGallery(s.lightbox); | |
| 1145 | + $grid.on("onAfterOpen.lg", () => { | |
| 1146 | + $(".lg-outer") | |
| 1147 | + .find(".lg-thumb-item") | |
| 1148 | + .each(function () { | |
| 1149 | + const $img = $(this).find("img"); | |
| 1150 | + let src = $img.attr("src"); | |
| 1151 | + const extIndex = src.lastIndexOf("."); | |
| 1152 | + const ext = src.slice(extIndex); | |
| 1153 | + const cropIndex = src.lastIndexOf("-"); | |
| 1154 | + const cropCandidate = src.substring(cropIndex, extIndex); | |
| 1155 | + const isStandardCrop = /\d{3,}x\d{3,}/.test(cropCandidate); | |
| 1156 | + if (!isStandardCrop && cropCandidate.length > 0) { | |
| 1157 | + src = src.slice(0, extIndex) + "-150x150" + ext; | |
| 1158 | + } else { | |
| 1159 | + src = src.replace(cropCandidate, "-150x150"); | |
| 1160 | + } | |
| 1161 | + $img.attr("src", src); | |
| 1162 | + }); | |
| 1163 | + }); | |
| 1164 | + $grid.on("onAferAppendSlide.lg onAfterSlide.lg", () => { | |
| 1165 | + const download = $("#lg-download").attr("href"); | |
| 1166 | + const $controls = $("#lg-actual-size, #lg-zoom-in, #lg-zoom-out, #lg-download"); | |
| 1167 | + if (download && download.indexOf("wp-content") === -1) { | |
| 1168 | + $controls.addClass("king-addons-hidden-element"); | |
| 1169 | + } else { | |
| 1170 | + $controls.removeClass("king-addons-hidden-element"); | |
| 1171 | + } | |
| 1172 | + if (!s.lightbox.autoplay) { | |
| 1173 | + $(".lg-autoplay-button").css({ width: "0", height: "0", overflow: "hidden" }); | |
| 1174 | + } | |
| 1175 | + }); | |
| 1176 | + const $overlay = $scope.find(".king-addons-grid-lightbox-overlay"); | |
| 1177 | + if ($overlay.length) { | |
| 1178 | + $scope.find(".king-addons-grid-media-hover-bg").after($overlay.remove()); | |
| 1179 | + $scope.find(".king-addons-grid-lightbox-overlay").on("click", function () { | |
| 1180 | + if (!isEditor) { | |
| 1181 | + $(this).closest("article").find(".king-addons-grid-image-wrap").trigger("click"); | |
| 1182 | + } else { | |
| 1183 | + alert( | |
| 1184 | + "Lightbox is Disabled in the Editor! Please Preview this Page to see it in action." | |
| 1185 | + ); | |
| 1186 | + } | |
| 1187 | + }); | |
| 1188 | + } else { | |
| 1189 | + $scope.find(".king-addons-grid-item-lightbox .inner-block > span").on("click", function () { | |
| 1190 | + if (!isEditor) { | |
| 1191 | + $(this).closest("article").find(".king-addons-grid-image-wrap").trigger("click"); | |
| 1192 | + } else { | |
| 1193 | + alert( | |
| 1194 | + "Lightbox is Disabled in the Editor! Please Preview this Page to see it in action." | |
| 1195 | + ); | |
| 1196 | + } | |
| 1197 | + }); | |
| 1198 | + } | |
| 1199 | + } | |
| 1200 | + | |
| 1201 | + // Handle post likes. | |
| 1202 | + function postLikes() { | |
| 1203 | + if (!$scope.find(".king-addons-post-like-button").length) return; | |
| 1204 | + $scope.on("click", ".king-addons-post-like-button", function (e) { | |
| 1205 | + e.preventDefault(); | |
| 1206 | + const $btn = $(this); | |
| 1207 | + if (!$btn.attr("data-post-id")) return false; | |
| 1208 | + $.ajax({ | |
| 1209 | + type: "POST", | |
| 1210 | + url: $btn.attr("data-ajax"), | |
| 1211 | + data: { | |
| 1212 | + action: "king_addons_likes_init", | |
| 1213 | + post_id: $btn.attr("data-post-id"), | |
| 1214 | + nonce: $btn.attr("data-nonce"), | |
| 1215 | + }, | |
| 1216 | + beforeSend: () => $btn.fadeTo(500, 0.5), | |
| 1217 | + success: function (response) { | |
| 1218 | + let iconClass = $btn.attr("data-icon"); | |
| 1219 | + let countHTML = response.count; | |
| 1220 | + if (!countHTML.replace(/<\/?[^>]+(>|$)/g, "")) { | |
| 1221 | + countHTML = `<span class="king-addons-post-like-count">${$btn.attr("data-text")}</span>`; | |
| 1222 | + $btn.addClass("king-addons-likes-zero"); | |
| 1223 | + } else { | |
| 1224 | + $btn.removeClass("king-addons-likes-zero"); | |
| 1225 | + } | |
| 1226 | + if ($btn.hasClass("king-addons-already-liked")) { | |
| 1227 | + $btn.prop("title", "Like").removeClass("king-addons-already-liked"); | |
| 1228 | + $btn.html(`<i class="${iconClass.replace("fas", "far")}"></i>${countHTML}`); | |
| 1229 | + } else { | |
| 1230 | + $btn.prop("title", "Unlike").addClass("king-addons-already-liked"); | |
| 1231 | + $btn.html(`<i class="${iconClass.replace("far", "fas")}"></i>${countHTML}`); | |
| 1232 | + } | |
| 1233 | + $btn.fadeTo(500, 1); | |
| 1234 | + }, | |
| 1235 | + }); | |
| 1236 | + return false; | |
| 1237 | + }); | |
| 1238 | + } | |
| 1239 | + | |
| 1240 | + // Animate grid items after layout. | |
| 1241 | + function animateGridItems(filteredItems, s, $grid) { | |
| 1242 | + let initStager = 0, | |
| 1243 | + filterStager = 0, | |
| 1244 | + deepLinkStager = 0; | |
| 1245 | + if (!$grid.hasClass("grid-images-loaded")) $grid.css("opacity", "1"); | |
| 1246 | + filteredItems.forEach((item) => { | |
| 1247 | + initStager += s.animation_delay; | |
| 1248 | + $(item.element) | |
| 1249 | + .find(".king-addons-grid-item-inner") | |
| 1250 | + .css({ | |
| 1251 | + opacity: "1", | |
| 1252 | + top: "0", | |
| 1253 | + transform: "scale(1)", | |
| 1254 | + transition: `all ${s.animation_duration}s ease-in ${initStager}s`, | |
| 1255 | + }); | |
| 1256 | + filterStager += s.filters_animation_delay; | |
| 1257 | + if ($grid.hasClass("grid-images-loaded")) { | |
| 1258 | + $(item.element) | |
| 1259 | + .find(".king-addons-grid-item-inner") | |
| 1260 | + .css({ | |
| 1261 | + transition: `all ${s.filters_animation_duration}s ease-in ${filterStager}s`, | |
| 1262 | + }); | |
| 1263 | + } | |
| 1264 | + let deepLink = window.location.hash; | |
| 1265 | + if (deepLink.includes("#filter:") && !deepLink.includes("#filter:*")) { | |
| 1266 | + deepLink = deepLink.replace("#filter:", ""); | |
| 1267 | + if ($(item.element).hasClass(deepLink)) { | |
| 1268 | + deepLinkStager += s.filters_animation_delay; | |
| 1269 | + $(item.element) | |
| 1270 | + .find(".king-addons-grid-item-inner") | |
| 1271 | + .css({ "transition-delay": `${deepLinkStager}s` }); | |
| 1272 | + } | |
| 1273 | + } | |
| 1274 | + }); | |
| 1275 | + } | |
| 1276 | + }, | |
| 1277 | + }), | |
| 1278 | + { $element: $scope } | |
| 1279 | + ); | |
| 1280 | + } | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + gridHooks.forEach((hook) => { | |
| 1284 | + elementorFrontend.hooks.addAction(hook, gridHandler); | |
| 1285 | + }); | |
| 1286 | + | |
| 1287 | + }); | |
| 1288 | +})(jQuery); | |