PageSourceSearch

https://www.scylladb.com/wp-content/themes/scylladb/inc/cpt-ajax-filter/js/cpt-ajax-filter.js?ver=1781093212

js scylladb.com collected 2026-10-01 08:40:43 UTC 10,497 bytes, 293 lines download raw bytes

1(function ($) {
2    var loadingAllowed = true;
3
4    var CptAjaxFilter = {
5        init: function () {
6            var self = CptAjaxFilter;
7
8            self.el = {
9                wrap: $('#cpt-ajax-wrapper'),
10                moreBtn: $('.cpt-ajax-load-more'),
11                form: $('#cpt-ajax-filter-form'),
12                search: $('#cpt-ajax-search'),
13                filter: $('.cpt-ajax-filter'),
14                currentFilters: $('.cpt-ajax-current-filters')
15            };
16
17            self.config = {
18                templateItems: wp.template('cpt-ajax-items'),
19                templateCurrentFilters: wp.template('cpt-ajax-current-filters'),
20                post_type: self.el.wrap.data('post_type'),
21                posts_count: self.el.wrap.data('posts_count'),
22                tax: self.el.wrap.data('tax'),
23                page: 1,
24                posts_per_page: parseInt(self.el.wrap.data('posts_per_page')),
25                page_id: $( '#page-id' ).val()
26            };
27
28            self.initEvents();
29            self.setCurrentFilters();
30
31            if (self.config.posts_count > self.config.posts_per_page) {
32                self.el.moreBtn.show();
33            }
34
35        },
36
37        setCurrentFilters: function () {
38            var self = CptAjaxFilter;
39            var filters = self.el.form.serializeArray();
40            var data = {
41                items: []
42            };
43
44            if (filters.length) {
45
46                $.each(filters, function (index, item) {
47                    if (item.value) {
48                        if (item.name !== 'search') {
49                            item.value = $('.select-term option[value="' + item.value + '"]').text();
50                        }
51                        item.type = self.config.post_type;
52                        data.items.push(item);
53                    }
54                });
55
56                $('.cpt-ajax-current-filters').remove();
57
58                if (window.location.search) {
59                    self.el.filter.after(self.config.templateCurrentFilters(data));
60                }
61            }
62
63            self.setFilterStateClass();
64
65        },
66
67        initEvents: function () {
68            var self = CptAjaxFilter;
69
70            // Click on More Button
71            if ($("body").hasClass("page-users")) {
72                // Load More items on scroll
73                $(window).on('scroll touchend', function() {
74                    var $itemsWrap = $('.cpt-ajax-items');
75
76                    if(!$itemsWrap.length) return;
77
78                    if($(window).scrollTop() >= $('.cpt-ajax-items').offset().top + $('.cpt-ajax-items').outerHeight() - window.innerHeight) {
79                        if(loadingAllowed){
80                            self.filterItems(true);
81                            loadingAllowed = false;
82                        }
83
84                    }
85                });
86            }
87            else {
88                $(document).on('click', '.cpt-ajax-load-more', function (){
89                    var $itemsWrap = $(this).closest('.cpt-ajax-items');
90                    if (!$itemsWrap.length) return;
91                    if (loadingAllowed) {
92                        self.filterItems(true);
93                        loadingAllowed = false;
94                    }
95                    $(this).parent().remove();
96                });
97            }
98            /*self.el.moreBtn.on('click', function () {
99                self.filterItems(true);
100            });*/
101
102            // Load More items on scroll
103            // $(window).on('scroll touchend', function() {
104            //     var $itemsWrap = $('.cpt-ajax-items');
105            //
106            //     if(!$itemsWrap.length) return;
107            //
108            //     if($(window).scrollTop() >= $('.cpt-ajax-items').offset().top + $('.cpt-ajax-items').outerHeight() - window.innerHeight) {
109            //         if(loadingAllowed){
110            //             self.filterItems(true);
111            //             loadingAllowed = false;
112            //         }
113            //
114            //     }
115            // });
116
117            // Change Filter Form Selects
118            self.el.form.find('.select-term').change(function () {
119                self.setFilterStateClass();
120                self.updateQueryString();
121                self.filterItems();
122            });
123
124            // Submit Filter Form
125            self.el.form.submit(function (e) {
126                self.updateQueryString();
127                self.filterItems();
128                e.preventDefault();
129            });
130
131            // Click on Clear Filters Button
132            $(document).on('click', '.cpt-ajax-clear', function () {
133                self.resetForm();
134                self.clearQueryString();
135                self.filterItems();
136                self.setFilterStateClass();
137            });
138
139            // Click on Current Filters
140            $(document).on('click', '.current-filter-item', function () {
141                self.el.form.find('[name="' + $(this).data('name') + '"]').val('');
142
143                $(this).fadeOut(300, function () {
144                    $(this).remove();
145
146                    if ($('.current-filter-item').length === 0) {
147                        $('.cpt-ajax-clear').hide();
148                    }
149                });
150
151                self.updateQueryString();
152                self.filterItems();
153                self.setFilterStateClass();
154            });
155
156        },
157
158        setFilterStateClass: function(){
159            var self = CptAjaxFilter;
160
161            $.each(self.el.form.find('.select-term'), function (index, item) {
162                if ($(item).val() !== '' && $('.cpt-ajax-filter-term option').prop('disabled') !== true) {
163                    $(item).addClass('active');
164                } else {
165                    $(item).removeClass('active');
166                }
167            });
168        },
169
170        resetForm: function () {
171            CptAjaxFilter.el.form.find('select, input').val('');
172        },
173
174        updateQueryString: function () {
175            var filters = CptAjaxFilter.el.form.serializeArray();
176            var queryStr = '';
177
178            $.each(filters, function (index, item) {
179                if (item.value) {
180                    if (queryStr === '') {
181                        queryStr += '?';
182                    }
183
184                    queryStr += item.name + '=' + encodeURI(item.value) + '&';
185                }
186            });
187
188            queryStr = queryStr.slice(0, -1);
189
190            if (history.pushState) {
191                var newUrl = location.protocol + '//' + location.host + location.pathname + queryStr;
192                window.history.pushState({path: newUrl}, '', newUrl);
193            }
194        },
195
196        clearQueryString: function () {
197            if (history.pushState) {
198                var newUrl = location.protocol + '//' + location.host + location.pathname;
199                window.history.pushState({path: newUrl}, '', newUrl);
200            }
201
202            $('.cpt-ajax-current-filters').remove();
203        },
204
205        filterItems: function (loadMore) {
206            var self = CptAjaxFilter;
207
208            if (loadMore) {
209                self.config.page += 1;
210            } else {
211                self.config.page = 1;
212            }
213
214            var data = {
215                post_type: self.config.post_type,
216                posts_per_page: self.config.posts_per_page,
217                page: self.config.page,
218                tax: self.config.tax,
219                filters: self.el.form.serialize(),
220                page_id: self.config.page_id
221            };
222
223            self.el.moreBtn.attr('disabled', true);
224
225            if (!self.el.wrap.hasClass('loading')) {
226                self.el.wrap.addClass('loading');
227            }
228
229            self.getItems(data, function (response) {
230                // var found_items = response.data.posts_count;
231                // var total_pages = Math.ceil(parseInt(found_items) / parseInt(self.config.posts_per_page));
232
233                self.setCurrentFilters();
234
235                self.el.moreBtn.show();
236
237                loadingAllowed = true;
238
239                // Hide More button if last page
240                // if (total_pages === self.config.page || parseInt(found_items) === 0) {
241                if ( response.trim() == '<div style="display: none;" class="cpt-ajax-no-result">No Result Found</div>' ) {
242                    self.el.moreBtn.hide();
243                    loadingAllowed = false;
244                }
245
246                if (!loadMore) {
247                    self.el.wrap.find('.cpt-ajax-items').empty();
248
249                    // if (found_items === 0) {
250                    if ( response.trim() == '<div style="display: none;" class="cpt-ajax-no-result">No Result Found</div>' ) {
251                        self.el.wrap.find('.cpt-ajax-items').append('<div class="cpt-ajax-no-result">
251No result found, please reduce filters.</div>');
252                    }
253                }
254                // self.el.wrap.find('.cpt-ajax-items').append(self.config.templateItems(response.data));
255
256                self.el.wrap.find('.cpt-ajax-items').append( response );
257
258                self.el.wrap.find('.cpt-ajax-items').imagesLoaded().always( function() {
259                    self.el.wrap.find('.cpt-ajax-item:hidden').fadeIn(300);
260                });
261
262                self.el.moreBtn.attr('disabled', false);
263                self.el.wrap.removeClass('loading');
264            });
265        },
266
267        getItems: function (data, callback) {
268            $.ajax({
269                url: ajaxurl,
270                type: 'GET',
271                data: {
272                    action: 'cpt_filter',
273                    post_type: data.post_type,
274                    posts_per_page: data.posts_per_page,
275                    page: data.page || 1,
276                    tax: data.tax,
277                    filters: data.filters,
278                    page_id: data.page_id
279                },
280                success: function (response) {
281                    callback(response);
282                    initLightbox();
283                }
284            });
285        },
286
287    };
288
289    $(document).ready(function () {
290        CptAjaxFilter.init();
291    });
292
293}(typeof jQuery === 'function' ? jQuery : this));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.