PageSourceSearch

https://www.dinamopress.it/wp-content/plugins/cp-press/assets/js/cp-press-search.js?ver=5.7.19

js dinamopress.it collected 2026-10-02 07:22:41 UTC 11,466 bytes, 348 lines download raw bytes

1(function($, _){
2    Backbone.ajax = function(){
3        var args = Array.prototype.slice.call(arguments, 0);
4
5        args[0]['data']['_front_ajax'] = 1;
6        return Backbone.$.ajax.apply(Backbone.$, args);
7    };
8
9    var WPPost = Backbone.Model.extend({
10        url: ajaxurl+'?action=cppress_search'
11    });
12
13    var SearchResults = Backbone.Collection.extend({
14        model: WPPost,
15        url: ajaxurl+'?action=cppress_search',
16
17        foundPosts: 0,
18
19        paginate: function(page, query){
20            var _that = this;
21            if(query.hasOwnProperty('offset')){
22                query.offset = (page-1) * query.posts_per_page;
23            }
24            this.fetch({
25                data: {query: query},
26                success: function(collection, response, options){
27                    _that.set(response.posts);
28                    _that.foundPosts = response.total;
29                    _that.trigger('sync:paginate');
30                },
31                error: function(collection, response, options){
32                    _that.trigger('sync:paginate:error');
33                }
34            });
35            this.trigger('sync:paginate');
36        },
37
38        search: function(query){
39            var _that = this;
40            this.fetch({
41                data: {query: query},
42                reset: true,
43                success: function(collection, response, options){
44                    _that.set(response.posts);
45                    _that.foundPosts = response.total;
46                    _that.trigger('sync:search')
47                },
48                error: function(collection, response, options){
49                    _that.trigger('sync:search:error')
50                }
51            });
52        }
53    });
54
55    var SearchItem = Backbone.View.extend({
56        model: null,
57        tagName: 'div',
58        template: null,
59        col: 'col-md-12',
60        classes: [],
61
62        initialize: function(options){
63            this.model = options.model;
64            if(options.hasOwnProperty('col')){
65                this.col = options.col;
66            }
67            if(options.hasOwnProperty('classes')){
68                this.classes = options.classes;
69            }
70
71            if(_.isNull(options.template)){
72                if($('#search-item-template').length > 0){
73                    this.template = _.template($('#search-item-template').html());
74                }
75            }else{
76                if($(options.template).length > 0) {
77                    this.template = _.template($(options.template).html());
78                }
79            }
80
81            if(options.hasOwnProperty('tagName')){
82                this.tagName = options.tagName;
83            }
84        },
85
86        render: function(){
87            this.$el.addClass(this.col);
88            _.each(this.classes, function(c){
89               this.$el.addClass(c);
90            }, this);
91            try {
92                this.$el.append(this.template({data: this.model.attributes}));
93            }catch(err){
94                console.error('You must specify a template for search item with id #search-item-template');
95            }
96            return this;
97        }
98
99    });
100
101    var Search = Backbone.View.extend({
102
103        collection: null,
104        col: 'col-md-12',
105        classes: {},
106        template: null,
107        searchItemTag: 'div',
108
109        initialize: function(options){
110            this.collection = options.collection;
111            if(options.hasOwnProperty('col')){
112                this.col = options.col;
113            }
114            if(options.hasOwnProperty('classes')){
115                this.classes = options.classes;
116            }
117
118            if(!_.isUndefined(options.template)){
119                this.template = options.template;
120            }
121
122            if(options.hasOwnProperty('searchItemTag')){
123                this.searchItemTag = options.searchItemTag;
124            }
125        },
126
127        render: function(){
128            this.$el.html('');
129            this.collection.each(function(item, key){
130                item.set('count', key);
131                this.addItem(item);
132            }, this);
133
134            return this;
135        },
136
137        addItem: function(item){
138            var searchItem = new SearchItem({
139                model: item,
140                col: this.col,
141                classes: this.classes,
142                template: this.template,
143                tagName: this.searchItemTag,
144            });
145            this.$el.insertAndFadeIn(searchItem.render().$el);
146        }
147
148    });
149
150    var Filters = Backbone.View.extend({
151        events: {
152            'click .dropdown-title': 'toggle',
153            'change .dropdown-input': 'dropdown',
154            'keyup .simple-input': 'simple',
155            'keydown .simple-input': 'prevent',
156            'click .tags-item a': 'removeFilter'
157        },
158
159        checked: [],
160        checkedLabel: [],
161
162        collection: null,
163
164        query: null,
165        originalQuery: null,
166
167        itemsView: null,
168
169        paginator: null,
170
171        toggled: null,
172
173        $infoBox: null,
174
175        filterActiveTemplate: _.template('<li class="tags-item"> <a href="#" data-filter="<%= filter_active %>"><%= tag %></a> </li>'),
176        filterActive: null,
177
178        timeoutId: null,
179
180        initialize: function(options){
181            this.itemsView = options.itemsView;
182            this.query = this.$el.data('query');
183            this.originalQuery = _.clone(this.query);
184            this.collection = options.collection;
185            if(!_.isUndefined(options.paginator)) {
186                this.paginator = options.paginator;
187            }
188            this.$infoBox = this.$el.find('.filters-info-box');
189            this.listenTo(this.collection, 'sync:search', this.render);
190        },
191
192        render: function(){
193            this.$el.data('query', this.query);
194            if(null !== this.toggled){
195                this.toggle(this.toggled);
196            }
197            if(null === this.paginator){
198                this.itemsView.render();
199                this.refreshInfoBox();
200                $(window).trigger('filter.onsuccess');
201            }else{
202                this.paginator.refresh(this.query);
203                this.refreshInfoBox();
204            }
205        },
206
207        refreshInfoBox: function(){
208            var $tagsList = this.$infoBox.find('.filters-active ul.tags');
209            this.$infoBox.find('.filters-result-count span.filters-result-count-number').html(this.collection.foundPosts);
210            if(this.$infoBox.find('.filters-active').hasClass('is-empty')){
211                this.$infoBox.find('.filters-active').removeClass('is-empty');
212            }
213            if(this.checkedLabel !== null){
214                $tagsList.prepend(this.filterActiveTemplate({
215                    tag: this.checkedLabel,
216                    filter_active: _.escape(JSON.stringify(this.filterActive))
217                }));
218            }
219
220        },
221
222        removeFilter: function(e){
223            e.preventDefault();
224            $(window).trigger('filter.onsearch');
225            var $$ = $(e.target);
226            var data = ($$.data());
227            var $checked = $('.dropdown-input:checked');
228            if(data.hasOwnProperty('filter')){
229                var filter = data.filter;
230                var _that = this;
231                _.each(filter, function(f, k){
232                    if(this.query.hasOwnProperty(k)){
233                        delete this.query[k];
234                        $checked.each(function(){
235                           _.each(f, function(v){
236                               if(v === $(this).val()){
237                                   $(this).prop('checked', false);
238                                   $(this).removeAttr('checked');
239                               }
240                           }, this);
241                        });
242                    }
243                }, this);
244                $$.parent().fadeOutAndRemove();
245            }else if(data.hasOwnProperty('filterRemoveAll')){
246                this.query = _.clone(this.originalQuery);
247                $checked.each(function(){
248                    $(this).prop('checked', false);
249                });
250                $('.tags-item:not(.tags-item-clear-all)').remove();
251            }
252            this.checkedLabel = null;
253            this.filterActive = null;
254            this.collection.search(this.query);
255        },
256
257        toggle: function(e){
258            var $$ = $(e.target);
259            this.toggled = e;
260            $$.siblings('ul.dropdown-list').toggle();
261        },
262
263        prevent: function(e){
264            var code = (e.keyCode ? e.keyCode : e.which);
265            if(code === 13){
266                e.preventDefault();
267            }
268        },
269
270        simple: function(e){
271            var $$ = $(e.target);
272            var code = (e.keyCode ? e.keyCode : e.which);
273            if(code === 13){
274                e.preventDefault();
275                this.simpleSearch($$, e.target.value);
276                return;
277            }
278            clearTimeout(this.timeoutId);
279            this.timeoutId = setTimeout(_.bind(this.simpleSearch, this, $$, e.target.value), 500)
280        },
281
282        simpleSearch: function($input, searchStr){
283            _.extend(this.query, {'s': searchStr});
284            $(window).trigger('filter.onsearch');
285            this.collection.search(this.query);
286        },
287
288        dropdown: function(e){
289            var $$ = $(e.target);
290            $(window).trigger('filter.onsearch');
291            var inputQuery = $$.data('input-query');
292            var type = $$.data('type');
293            this.checkedLabel = $$.parents('label').find('span').text();
294            var queryObj = {
295                value: $$.val()
296            };
297            if(!this.checked.hasOwnProperty(type)){
298                this.checked[type] = [];
299            }
300            $.pushIfNotExists(this.checked[type], queryObj, function(e){
301                return e.value === queryObj.value;
302            });
303
304            if(!_.isUndefined(this[type])){
305                this.filterActive = this[type](queryObj, inputQuery);
306                _.extend(this.query, this.filterActive);
307            }else{
308                this.filterActive = this.custom(queryObj, inputQuery, type);
309                _.extend(this.query, this.filterActive);
310            }
311
312            this.collection.search(this.query);
313        },
314
315        category: function(queryObj, inputQuery){
316            _.each(this.checked['category'], function(obj, key){
317                if(inputQuery.hasOwnProperty('category__in')){
318                    inputQuery['category__in'].push(obj.value);
319                }
320            }, this);
321            return inputQuery
322        },
323
324        post_tag: function(queryObj, inputQuery){
325            _.each(this.checked['post_tag'], function(obj, key){
326                if(inputQuery.hasOwnProperty('tag__in')){
327                    inputQuery['tag__in'].push(obj.value);
328                }
329            }, this);
330            return inputQuery
331        },
332
333        custom: function(queryObj, inputQuery, type){
334            _.each(this.checked[type], function(obj, key){
335                if(inputQuery.hasOwnProperty('tax_query')){
336                    inputQuery['tax_query'][0]['terms'].push(obj.value);
337                }
338            }, this);
339            return inputQuery;
340        }
341    });
342
343    Backbone.WPPost = WPPost;
344    Backbone.SearchResults = SearchResults;
345    Backbone.Search = Search;
346    Backbone.Filters = Filters;
347
348})(jQuery, _);

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.