HEX
Server: Apache/2.4.57 (Debian)
System: Linux web-server-k8s-e92jnr3j-6f99bff6b6-rp2wg 6.1.0-22-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.94-1 (2024-06-21) x86_64
User: apache (48)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /var/www/sites/1250.info/wp-content/themes/savoy/assets/js/plugins/snapback-cache.js
/* Snapback Cache: https://github.com/highrisehq/snapback_cache */
var SnapbackCache = (function(options) {
    var options = options || {};

    var SessionStorageHash = (function() {
        var set = function(namespace, key, item) {
            var storageHash = sessionStorage.getItem(namespace);
            
            if (! storageHash) {
                storageHash = {}
            } else {
                storageHash = JSON.parse(storageHash)
            }

            if (item) {
                storageHash[key] = JSON.stringify(item)
            } else {
                delete storageHash[key]
            }

            sessionStorage.setItem(namespace, JSON.stringify(storageHash))
        };

        var get = function(namespace, key, item) {
            var storageHash = sessionStorage.getItem(namespace)

            if (storageHash) {
                storageHash = JSON.parse(storageHash)
                if (storageHash[key]) {
                    return JSON.parse(storageHash[key])
                }
            }

            return null
        }

        return {
            set: set,
            get: get
        }
    })();

    var enabled = true;

    var supported = function() {
        return !!(sessionStorage && history && enabled)
    };

    var setItem = function(url, value){
        if (value) {
            // Only keep 10 things cached
            trimStorage()
        }
        SessionStorageHash.set('pageCache', url, value)
    };

    var getItem = function(url) {
        return SessionStorageHash.get('pageCache', url)
    };

    var removeItem = function(url) {
        setItem(url, null)
    };

    var disableAutofocusIfReplacingCachedPage = function() {
        if (typeof options.removeAutofocus === 'function') {
            if (willUseCacheOnThisPage()) {
                options.removeAutofocus()
            }
        }
    };

    var cachePage = function() {
        //console.log('NM Snapback: CACHING START');

        if (! supported()) {
            return;
        }
        
        //console.log('NM Snapback: CACHING SUPPORTED');
        
        // Get jQuery animations to finish
        /*jQuery(document).finish();
        if (typeof options.wait === 'function') {
            options.finish()
        }*/

        // Give transitions/animations a chance to finish
        //setTimeout(function() {
        if (typeof options.removeAutofocus === 'function') {
            options.removeAutofocus();
        }

        var $cachedBody = jQuery(options.bodySelector);
        
        //console.log('NM Snapback: CACHED BODY BELOW:');
        //console.log($cachedBody);
        
        // NM: Remove "loader" class from pagination
        $cachedBody.children('.nm-infload-controls').removeClass('nm-loader');
        // /NM
        
        var cachedPage = {
            body: $cachedBody.html(),
            // NM
            wrapperClass: jQuery(options.bodySelector).attr('class'),
            // /NM
            // NM: title: document.title,
            positionY: window.pageYOffset,
            positionX: window.pageXOffset,
            cachedAt: new Date().getTime()
        }

        // Help to setup the next page of infinite scrolling
        /*if (typeof options.nextPageOffset === 'function') {
            cachedPage.nextPageOffset = options.nextPageOffset()
        }*/
        
        setItem(document.location.href, cachedPage);
        
        jQuery(options.bodySelector).trigger('snapback-cache:cached', cachedPage);
        //}, 500)
    };

    var loadFromCache = function(/*noCacheCallback*/){
        // Check if there is a cache and if its less than 15 minutes old
        if (willUseCacheOnThisPage()) {
            var cachedPage = getItem(document.location.href);

            //console.log('NM Snapback: LOADING CACHE');

            // NM: Add previous classes to wrapper container
            var $bodySelector = jQuery(options.bodySelector);
            $bodySelector.removeClass().addClass(cachedPage.wrapperClass);
            // /NM

            // Replace the content and scroll
            // NM
            //jQuery(options.bodySelector).html(cachedPage.body)
            $bodySelector.html(cachedPage.body);
            // NM
            
            // NM: Show hidden products (in case cache was saved when products were just added)
            setTimeout(function() {
                var $hiddenProducts = $bodySelector.children('.nm-products').children('.hide');
                if ($hiddenProducts.length) {
                    $hiddenProducts.find('.lazyloading').removeClass('lazyloading').addClass('lazyloaded');
                    $hiddenProducts.removeClass('hide');
                }
            }, 300);
            // /NM
            
            // Try to make sure autofocus events don't run. 
            if (typeof options.removeAutofocus === 'function') {
                options.removeAutofocus();
            }

            // IE 10+ needs a delay to stop the autofocus during dom load
            setTimeout(function() {
                window.scrollTo(cachedPage.positionX, cachedPage.positionY)
            }, 1);

            // Pop the cache
            removeItem(document.location.href);

            // NM: Reset page cache views
            sessionStorage.setItem('pageCacheViews', 1);
            // /NM

            jQuery(options.bodySelector).trigger('snapback-cache:loaded', cachedPage);

            return false;
        } else {
            return
        }
    };

    var trimStorage = function() {
        var storageHash = sessionStorage.getItem('pageCache');
        
        if (storageHash) {
            storageHash = JSON.parse(storageHash);

            var tuples = [];

            for (var key in storageHash) {
                tuples.push([key, storageHash[key]])
            }
            
            // If storage is bigger than size, sort them, and remove oldest
            if (tuples.length >= 10) {
                tuples.sort(function(a, b) {
                    a = a[1].cachedAt;
                    b = b[1].cachedAt;
                    return b < a ? -1 : (b > a ? 1 : 0);
                });

                for (var i = 0; i < (tuples.length + 1 - 10); i++) {
                    var key = tuples[i][0];
                    delete storageHash[key];
                }

                sessionStorage.setItem(namespace, JSON.stringify(storageHash));
            }
        }
    };

    var willUseCacheOnThisPage = function() {
        if (! supported()) {
            return false;
        }

        var cachedPage = getItem(document.location.href);

        // Check if there is a cache and if its less than 15 minutes old
        if (cachedPage && cachedPage.cachedAt > (new Date().getTime()-900000)) {
            // NM: Remove cache after 2 page loads
            var pageViews = sessionStorage.getItem('pageCacheViews');
            
            //console.log('NM Snapback: PAGE VIEWS: '+pageViews);
            
            if (parseInt(pageViews) > 3) {
                //console.log('NM Snapback: PAGE VIEWS - REMOVING CACHE');

                removeItem(document.location.href);
                sessionStorage.setItem('pageCacheViews', 1);
                return false;
            }
            // /NM
            
            //console.log('NM Snapback: CACHE FOUND');

            return true; 
        } else {
            return false;
        }
    };
    
    jQuery(function() {
        disableAutofocusIfReplacingCachedPage()
    });

    jQuery(window).on('load', function() {
        loadFromCache()
    });

    return {
        remove: removeItem,
        loadFromCache: loadFromCache,
        cachePage: cachePage,
        willUseCacheOnThisPage: willUseCacheOnThisPage
    }
});