(function(root, factory) { "use strict"; if (typeof define === "function" && define.amd) { define(["jquery"], factory); } else if (typeof exports === "object") { factory(require("jquery")); } else { factory(root.jQuery); } })(this, function($) { "use strict"; $.fn.cleverInfiniteScroll = function(options) { var windowHeight = (typeof window.outerHeight !== "undefined") ? Math.max(window.outerHeight, $(window).height()) : $(window).height(), defaults = { offset: windowHeight, }, settings = $.extend(defaults, options); var generateHiddenSpans = function(_title, _path) { return "<span class='hidden-title' style='display:none'>" + _title + "</span><span class='hidden-url' style='display:none'>" + _path + "</span>"; }, setTitleAndHistory = function(_title, _path) { history.replaceState(null, _title, _path); $("title").html(_title); }, changeTitleAndURL = function(_value) { var title = $(_value).children(".hidden-title:first").text(), path = $(_value).children(".hidden-url:first").text(); if ($("title").text() !== title) { $(settings.contentSelector).removeClass("active"); $(_value).addClass("active"); setTitleAndHistory(title, path); $(document).trigger('clever-infinite-scroll-url-change', [title, path]); } }; var title = $("title").text(), path = $(location).attr("href"), documentHeight = $(document).height(), threshold = settings.offset, $contents = $(settings.contentSelector); $(settings.contentSelector + ":last").append(generateHiddenSpans(title, path)); $(settings.contentSelector).addClass("active"); setTitleAndHistory(title, path); var lastScroll = 0, currentScroll; $(window).scroll(function() { window.clearTimeout($.data("this", "scrollTimer")); $.data(this, "scrollTimer", window.setTimeout(function() { currentScroll = $(window).scrollTop(); if (currentScroll > lastScroll) { $contents.each(function(key, value) { if ($(value).offset().top + $(value).height() - 20 > currentScroll) { changeTitleAndURL(value); return false; } }); } else if (currentScroll < lastScroll) { $contents.each(function(key, value) { if ($(value).offset().top + $(value).height() - windowHeight / 2 > currentScroll) { changeTitleAndURL(value); return false; } }); } else { } lastScroll = currentScroll; }, 200)); if ($(window).scrollTop() + windowHeight + threshold >= documentHeight) { var $url = [$(settings.nextSelector).attr("href")]; $(settings.nextSelector).remove(); if ($url[0] !== undefined) { $(settings.contentsWrapperSelector).append('<div id="infiniteImage"></div>'); $.ajax({ url: $url[0], dataType: "html", success: function(res) { title = $(res).filter("title").text(); path = $url[0]; $(settings.contentsWrapperSelector).append($(res).find(settings.contentSelector).append(generateHiddenSpans(title, path))); if ($(res).find(settings.contentSelector).find(settings.nextSelector).length === 0) { $(settings.contentsWrapperSelector).append($(res).find(settings.nextSelector)); } documentHeight = $(document).height(); $contents = $(settings.contentSelector); $("#infiniteImage").remove(); $(document).trigger('clever-infinite-scroll-content-loaded'); // Hits let numberWithDots = function(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); } let safirGetHits = function(ids) { jQuery.ajax({ type: "post", dataType: "json", url: safirAjax.ajaxurl, data: { action: "safirGetHits", ids: ids, nonce: jQuery("body").attr("data-nonce"), }, success: function (response) { for (const [key, value] of Object.entries(response.data)) { let count = numberWithDots(Number(value)) jQuery(".meta.hit[data-id="+key+"] .count").html(count).hide().css("opacity", "1").fadeIn() } }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.responseText); } }); } let infiniteContentIDs = [] $(res).find(settings.contentSelector).find(".meta.hit").each(function () { let id = $(this).data("id") infiniteContentIDs.push(id) }) if (infiniteContentIDs.length) safirGetHits(infiniteContentIDs) $('#archiveListing img.lazy').unveil(0, function () { $(this).load(function() { this.style.opacity = 1; }); }); $(".safirHover").each(function() { var icon = $(this).data("hovericon"); $(this).find(".postthumb a").append('<div class="hover safiricon icon"><svg class="safiricon-'+icon+'"><use href="#safiricon-'+icon+'"></use></svg></div>'); }); } }); } } }); return (this); }; }); jQuery(function($) { $('#archiveListing .widgetContent').cleverInfiniteScroll({ contentsWrapperSelector: '#archiveListing .widgetContent', contentSelector: '#archiveListing .items', nextSelector: '.infiniteLink a', }); });