<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">function OSearch(initialParams, initialData) {
    return {
        isOSearch: true,
        totalProductItemsPages: initialData.pages,
        initialProductPage : initialData.current_page,
        currentProductPage : initialData.current_page,
        defaultParams : initialParams.defaultParams,
        productItems: initialData.items,
        filters : initialData.filters,
        initialFilters : initialData.initial_filters ?? initialData.filters,
        isFiltersOpen : false,
        totalProductItemsCount : initialData.items_count,
        allowedParams : initialParams.allowedParams,
        schema : initialData.schema,
        pageSelect: false,
        isMobile: false,
        asGrid: true,
        glider: null,
        isLifestyle: false,
        endpoint: initialParams.endpoint,
        pageSize: initialData.params.pageSize,
        intersectPrevPageTriggered : false,
        intersectNextPageTriggered : false,
        disableIntersectionScroll: false,
        intersectPagesFetched : [initialData.current_page],
        refreshProductsController: false,

        init() {
            if(localStorage.getItem('oSearchAsGrid')) {
                this.asGrid = Boolean(parseInt(localStorage.getItem('oSearchAsGrid')));
            }
            if(localStorage.getItem('oSearchIsLifestyle')) {
                this.isLifestyle = Boolean(parseInt(localStorage.getItem('oSearchIsLifestyle')));
            }
            this.appendSchema();
            this.checkIsMobile();
            //this.dispatchTrackingEventData();
            //this.renderCurrentFilters();
            this.$nextTick(() =&gt; { this.nextTickFunctions(); });
        },
        nextTickFunctions() {
            this.initGlider();
            this.addAjaxCartEvents();
            this.observeSliderItems();
            if (this.asGrid &amp;&amp; localStorage.getItem('oSearchItemLimit') &amp;&amp; this.$refs.oSearchItemLimit.value != localStorage.getItem('oSearchItemLimit')) {
                this.$refs.oSearchItemLimit.value = localStorage.getItem('oSearchItemLimit');
                this.changeLimit();
            }
        },
        toggleGridView() {
            this.asGrid = !this.asGrid;
            localStorage.setItem("oSearchAsGrid", this.asGrid ? 1 : 0);
            this.$nextTick(() =&gt; { this.nextTickFunctions(); });
        },
        checkIsMobile() {
            this.isMobile = window.matchMedia('(max-width: 1023px)').matches;
        },
        setLifestyleView(value) {
            this.isLifestyle = value;
            localStorage.setItem("oSearchIsLifestyle", this.isLifestyle ? 1 : 0);
        },
        initGlider() {
            if(typeof window.SalesAppGlider == 'undefined') { return; }
            if(!this.asGrid) {
                if(this.$root) {
                    if(!this.glider) {
                        let salesAppGlider = window.SalesAppGlider();
                        this.glider = salesAppGlider.initGlider(this.$root);
                    }
                    let currentSlide = this.glider.getCurrentSlide();
                    if(this.intersectPrevPageTriggered) {
                        currentSlide += this.pageSize;
                    }
                    else if(this.disableIntersectionScroll) {
                        currentSlide = this.pageSize*this.currentProductPage-this.pageSize;
                    }
                    this.glider.refresh(true);
                    this.glider.scrollItem(parseInt(currentSlide));
                    let self = this;
                    setTimeout(function() {
                        self.intersectPrevPageTriggered = false;
                    },1000);
                    setTimeout(function() {
                        self.intersectNextPageTriggered = false;
                    },1000);
                }
            } else {
                if(this.glider) {
                    this.glider = false;
                }
            }
        },
        setFilters(filters) {
            var BreakException = {};
            var filtersState = [];
            this.initialFilters.forEach((inititalFilter) =&gt; {
                var filterState = Object.assign({},inititalFilter);
                filterState.options = [];
                try {
                    filters.forEach((filter) =&gt; {

                        if (filter.prop == inititalFilter.prop) {
                            let currentFilterOptions = [];
                            inititalFilter.options.forEach((initialOption) =&gt; {
                                try {
                                    filter.options.forEach((newFilterOption) =&gt; {
                                        if (newFilterOption.value == initialOption.value) {
                                            let tmpOption = Object.assign({},newFilterOption);
                                            currentFilterOptions.push(tmpOption);
                                            throw BreakException; // BREAK
                                        }
                                    });
                                } catch (e) {
                                    if (e !== BreakException) throw e;
                                }
                            });
                            filterState.options = currentFilterOptions;
                            throw BreakException; // BREAK
                        }
                    });
                } catch (e) {
                    if (e !== BreakException) throw e;
                }

                if(this.filters) {
                    try {
                        this.filters.forEach((currentFilter) =&gt; {
                            if (filterState.prop == currentFilter.prop &amp;&amp; currentFilter.open) {
                                filterState.open = true;
                            }
                        });
                    } catch (e) {
                        if (e !== BreakException) throw e;
                    }
                }
                filtersState.push(filterState);
            });
            this.filters = filtersState;
        },
        changeLimit() {
            localStorage.setItem('oSearchItemLimit', this.$refs.oSearchItemLimit.value);
            this.resetPager();
            this.refreshProducts(false);
        },
        resetPager() {
            this.currentProductPage = 1;
        },
        changeSortOrder() {
            this.resetPager();
            this.refreshProducts(true);
        },
        refreshProducts(scrollToTop = true,action = '', scrollItem = true) {
            if(action) {
                let foundPageItem = false;
                this.productItems.forEach((item,key) =&gt; {
                    if(!foundPageItem &amp;&amp; item.currentPage == this.currentProductPage) {
                        foundPageItem = true;
                        if(scrollItem) {
                            this.glider.scrollItem(key);
                        }
                    }
                });

                if(foundPageItem) {
                    return;
                }
            }
            if(scrollToTop) {
                this.scrollToTop();
            }
            if(this.refreshProductsController) {
                this.refreshProductsController.abort('new incomming request');
            }
            this.refreshProductsController = new AbortController();
            this.disableIntersectionScroll = true;
            const signal = this.refreshProductsController.signal
            fetch(this.getProductsUrl(action),{signal})
                .then((response) =&gt; {
                    this.refreshProductsController = false;
                    return response.json();
                })
                .then((responseJSON) =&gt; {
                    this.handleProductsResponse(responseJSON);
                })
                .finally(() =&gt; {
                    let self = this;
                    setTimeout(() =&gt; {
                        self.disableIntersectionScroll = false;
                    },1000);
                });
        },
        observeSliderItems() {
            const observer = new IntersectionObserver((slides) =&gt; {
                if(this.disableIntersectionScroll){return;}
                slides.forEach((slide) =&gt; {
                    if (slide.isIntersecting) {
                        const currentPage = slide.target.getAttribute("data-current-page");
                        if (currentPage &lt; this.currentProductPage &amp;&amp; !this.intersectNextPageTriggered) {
                            this.prevProductPage(false);
                        }
                        else if(currentPage &gt; this.currentProductPage &amp;&amp;  !this.intersectPrevPageTriggered) {
                            this.nextProductPage(false);
                        }
                    }
                });
            }, { threshold: 0.1 });
            document.querySelectorAll("[data-current-page]").forEach((item) =&gt; {
                observer.observe(item);
            });
        },

        handleProductsResponse(responseJSON) {

            this.initialProductPage = Math.min(responseJSON.current_page,this.initialProductPage);

            this.setFilters(responseJSON.filters);
            if(responseJSON.append) {
                this.productItems.push(...responseJSON.items);
            } else if(responseJSON.prepend) {
                responseJSON.items.push(...this.productItems);
                this.productItems = this.productItems = responseJSON.items;
            }else {
                this.productItems = responseJSON.items;
            }

            if(typeof responseJSON.items_count != 'undefined') {
                this.totalProductItemsCount = responseJSON.items_count;
            }
            if(typeof responseJSON.pages != 'undefined') {
                this.totalProductItemsPages = responseJSON.pages;
                this.currentProductPage = responseJSON.current_page;
            }
            if(typeof responseJSON.state != 'undefined') {
                this.pushCurrentStateToUrl(responseJSON.state);
            }

            if(typeof responseJSON.tracking != 'undefined') {
                this.trackingData = responseJSON.tracking;
            }
            if(typeof responseJSON.schema != 'undefined') {
                this.schema = responseJSON.schema;
                this.appendSchema();
            }

            this.$nextTick(() =&gt; { this.nextTickFunctions(); });
            //OSearch.triggerReRender();

            //OSearch.dispatchCategoryPageReload();
        },
        addAjaxCartEvents() {
            if(typeof window.setAjaxCart == 'function') {
                typeof window.setAjaxCart();
            }
        },
        getProductsUrl(action = '') {
            var params = Object.assign({}, this.defaultParams);
            if(this.currentProductPage &gt; 1) {
                params['currentPage'] = this.currentProductPage;
            }
            let sortSelect = document.querySelector("#osearch-items-sort")
            if(sortSelect) {
                let sortSelectOption = sortSelect.querySelector('option[value="'+sortSelect.value+'"]');
                params['sort'] = {};
                params['sort'][sortSelectOption.dataset.sortOrder] = sortSelectOption.dataset.sortDir;
            }
            let limitSelect = document.querySelector("#osearch-items-limit")
            if(limitSelect) {
                params['pageSize'] = limitSelect.value;
            }
            if(action == 'append') {
                params['append'] = 1;
            } else if(action == 'prepend') {
                params['prepend'] = 1;
            }

            params['filter'] = this.getCurrentFiltersSorted(); //OSearch.currentFilters;
            return this.endpoint+'?'+this.serializeObjectToParameterString(params).replace(/&amp;+$/, '');
        },


        scrollToTop() {
            let element = document.querySelector('.osearch-container');
            if(element) {
                const yOffset = this.isMobile ? -30 : -150;
                const y = element.getBoundingClientRect().top + window.scrollY + yOffset;
                window.scrollTo({
                    top: y,
                    left: 0,
                    behavior: 'smooth'
                });
            } else {
                window.scrollTo({
                    top: 0,
                    left: 0,
                    behavior: 'smooth'
                });
            }
        },
        serializeObjectToParameterString(obj, prefix) {
            var str = [];
            for (var p in obj) {
                if (obj.hasOwnProperty(p)) {
                    var k = prefix ? prefix + "[" + p + "]" : p,
                        v = obj[p];
                    str.push((v !== null &amp;&amp; typeof v === "object") ?
                        this.serializeObjectToParameterString(v, k) :
                        encodeURIComponent(k) + "=" + encodeURIComponent(v));
                }
            }
            return str.join("&amp;");
        },
        getCurrentFiltersSorted() {
            let sortedFilters = {};
            let activeFilters = this.getActiveFilters();
            activeFilters.forEach((filter) =&gt; {
                sortedFilters[filter.prop] = [];
                filter.options.forEach((option) =&gt; {
                    if (option.selected) {
                        sortedFilters[filter.prop].push(option.value);
                    }
                });
            });
            return sortedFilters;
        },
        pushCurrentStateToUrl(state) {
            try {
                if(state === false) {
                    state = {};
                }
                const url = new URL(window.location.href);
                let allowedParams = this.allowedParams;
                allowedParams.forEach(element =&gt; {
                    var value = url.searchParams.get(element);
                    if(value !== null &amp;&amp; value !== undefined) {
                        state[element] = value;
                    }
                });
            } catch (e) {
                console.error(e);
            }

            if(Object.keys(state).length !== 0) {
                history.replaceState(state,'',window.location.pathname+'?'+this.serializeObjectToParameterString(state));
            } else {
                history.replaceState({},'',window.location.pathname);
            }
        },
        appendSchema() {
            if (this.schema) {
                let element = document.createElement('script');
                element.setAttribute('id', 'schema');
                element.setAttribute('type', 'application/ld+json');
                element.innerText = JSON.stringify(this.schema);

                let body = document.querySelector('body');
                let schema = body.querySelector('#schema');
                if (schema) {
                    body.replaceChild(element, schema);
                } else {
                    body.appendChild(element);
                }
            }
        },
        getProductPages() {
            let pages = [];
            let i = Math.max(1,this.currentProductPage-2);
            if (this.totalProductItemsPages &gt; 5) {
                i = Math.min(i,this.totalProductItemsPages-4);
            }
            while(i &lt;= this.totalProductItemsPages) {
                if(pages.length &gt;= 5 &amp;&amp; i != this.totalProductItemsPages) {
                    pages.push(0);
                    pages.push(this.totalProductItemsPages);
                    break;
                }
                pages.push(i);
                i++;
            }
            return pages;
        },
        fetchProductPage(page) {
            if(page &amp;&amp; page != this.currentProductPage) {
                this.currentProductPage = page;
                this.refreshProducts();
            }
        },
        nextProductPage(scrollItem) {
            this.currentProductPage++;
            if(!this.asGrid) {
                this.refreshProducts(false,'append',scrollItem);
            } else {
                this.refreshProducts();
            }
        },
        prevProductPage(scrollItem) {
            this.currentProductPage--;
            if(!this.asGrid) {
                this.refreshProducts(false,'prepend',scrollItem);
            } else {
                this.refreshProducts();
            }
        },

        getTotalFilterCount() {
            var count = 0;
            this.filters.forEach((filter) =&gt; {
                count += this.getSelectedFilterCount(filter);
            });
            return count;
        },
        getSelectedFilterCount(filter) {
            var count = 0;
            filter.options.forEach((option) =&gt; {
                if(option.selected) {
                    count++;
                }
            });
            return count;
        },
        getFilterLabelWithCount(filter) {
            let count = 0;
            if(count = this.getSelectedFilterCount(filter)) {
                return filter.label + ' (' + count + ')';
            }
            return filter.label;
        },
        getActiveFilters() {
            let activeFilters = [];
            this.filters.forEach((filter) =&gt; {
                let activeOptions = [];
                filter.options.forEach((option) =&gt; {
                    if(option.selected) {
                        activeOptions.push(option);
                    }
                });
                if(activeOptions.length) {
                    let activeFilter = Object.assign({}, filter)
                    activeFilter.options = activeOptions;
                    activeFilters.push(activeFilter);
                }
            });
            return activeFilters;
        },
        toggleOption(toggleFilter,toggleOption) {
            var BreakException = {};
            try {
                this.filters.forEach((filter) =&gt; {
                    if (filter.prop == toggleFilter.prop) {
                        try {
                            filter.options.forEach((option) =&gt; {
                                if (option.value == toggleOption.value) {
                                    option.selected = !option.selected;
                                    throw BreakException;
                                }
                            });
                        } catch (e) {
                            if (e !== BreakException) throw e;
                        }
                        throw BreakException;
                    }
                });
            } catch (e) {
                if (e !== BreakException) throw e;
            }
            this.resetPager();
            this.refreshProducts(false);
        },
        intersectNextPage() {
            if(this.leave) {
                this.intersectNextPageTriggered = true;
                this.nextProductPage();
            }
        },
        intersectPrevPage() {
            if(this.leave) {
                this.intersectPrevPageTriggered = true;
                this.prevProductPage();
            }
        },
        intersectPageData() {
            return {
                leave : false,
                leaveTimeout : false,
                init() {
                    //this.setLeaveTimeout();
                },
                setLeaveTimeout() {
                    if(!this.leave) {
                        let self = this;
                        this.leaveTimeout = setTimeout(() =&gt; {self.leave = true;}, 1000);
                    }
                },
                removeLeaveTimeout() {
                    if(this.leaveTimeout) {
                        clearTimeout(this.leaveTimeout);
                    }
                }
            };
        },
        increaseQty(productId, increments){
            var id = 'qty['+productId+']',
                currentVal = parseInt(document.getElementById(id).value);
            document.getElementById(id).value = currentVal + increments;
        },
        decreaseQty(productId, increments){
            var id = 'qty['+productId+']',
                currentVal = parseInt(document.getElementById(id).value);
            if (currentVal &gt; increments) {
                document.getElementById(id).value = currentVal - increments;
            }
        }
    }
}

</pre></body></html>