Dynamic object lists.
[wolnelektury.git] / src / catalogue / static / 2022 / book / filter.js
1 (function($) {
2
3     let unpagedSearch = null;
4     if (!$(".quick-filter").val() && !$('.l-pagination li').length) {
5         unpagedSearch = '';
6     }
7     
8     function get_page(page, search, ordering, callback) {
9         get_page_by_url('.?page=' + page + '&order=' + ordering + '&search=' + search, callback);
10     }
11     function get_page_by_url(url, callback) {
12         $.get(
13             url,
14             function(data) {
15                 html = $(data);
16                 objectList = $('#object-list', html);
17                 paginate = $('#paginate', html);
18
19                 ids = new Set(); 
20                 $(".icon-like", objectList).each(
21                     (i, e)=>{
22                         ids.add($(e).attr('data-book'));
23                     }
24                 );
25                 ids = [...ids].join(',');
26                 $.refreshLikes(ids);
27
28                 $('#book-list').html(objectList.children());
29                 $('#paginator').html(paginate.children());
30                 history.replaceState({}, '', url);
31                 callback && callback();
32             }
33         )
34     }
35
36     $("#paginator").on('click', 'a', function() {
37         get_page_by_url(url=$(this).attr('href'));
38         return false;
39     });
40
41     $(".quick-filter").each(function() {
42         let bookList = $('#' + $(this).data('for'));
43         let filterList = $('.' + $(this).data('filters'));
44
45         $(this).on('focus', function() {
46             filterList.addClass('filters-enabled');
47         });
48         $(this).on('blur', function() {
49             filterList.removeClass('filters-enabled');
50         });
51
52         $(this).on('input propertychange', function() {
53             let search = $(this).val().toLowerCase();
54
55             if (!search.startsWith(unpagedSearch)) {
56                 get_page(1, search, 'title', function() {
57                     if ($('.l-pagination li').length) {
58                         unpagedSearch = null;
59                     }
60                 })
61             } else {
62                 bookList.children().each(function() {
63                     found = !search ||
64                         $(".s", this).text().toLowerCase().search(search) != -1
65                     ;
66                     if (found) 
67                         $(this).fadeIn();
68                     else
69                         $(this).fadeOut();
70                 });
71             }
72
73             $('.filter-container', filterList).children().each(function() {
74                 found = !search ||
75                     $(this).text().toLowerCase().search(search) != -1
76                     ;
77                 if (found) 
78                     $(this).fadeIn();
79                 else
80                     $(this).fadeOut();
81             });
82         });
83
84     });
85
86     $(".l-books__sorting button").on('click', function() {
87         if ($(this).hasClass('is-active')) return;
88         $(".is-active", $(this).parent()).removeClass("is-active");
89         $(this).addClass("is-active");
90         let prop = $(this).attr('data-order');
91
92
93         // do we NOW have pages (possibly after filtering)?
94         // if we don't have pages, we can just sort here.
95         let havePages = $('.l-pagination li').length > 0;
96
97         $(".l-books__item").css('opacity', '0');
98         setTimeout(function() {
99             if (havePages) {
100                 get_page(1, '', prop);
101             } else {
102                 if (prop) {
103                     $(".l-books__item").each(function() {
104                         $(this).css('order', $(this).attr('data-' + prop));
105                     });
106                 } else {
107                     $(".l-books__item").css('order', '');
108                 }
109                 setTimeout(function() {
110                     $(".l-books__item").css('opacity', '100%');
111                 }, 200);
112             }
113         }, 200);
114     });
115     
116 })(jQuery);