PageSourceSearch

https://forkids.cz/wp-content/plugins/abf-custom-blocks/src/js/archiv-akci.js?ver=1790280004

js forkids.cz collected 2026-09-26 10:39:37 UTC 4,423 bytes, 96 lines download raw bytes

1/**
2 * Archiv akcí — client-side filtry nad server-rendered DOM.
3 * Žádné API: chips (typ akce, multi-select), selecty měsíc/rok, fulltext s debounce
4 * a normalizací diakritiky, odkrývání starších měsíců (initialMonths).
5 * Podpora více instancí přes scope na .aa-section.
6 */
7(function () {
8    'use strict';
9
10    var norm = function (s) {
11        return String(s || '').toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, '');
12    };
13
14    function initArchive(root) {
15        var chips = Array.prototype.slice.call(root.querySelectorAll('.aa-chip'));
16        var selMonth = root.querySelector('.aa-sel-month');
17        var selYear = root.querySelector('.aa-sel-year');
18        var search = root.querySelector('.aa-search');
19        var groups = Array.prototype.slice.call(root.querySelectorAll('.aa-group'));
20        var cards = Array.prototype.slice.call(root.querySelectorAll('.aa-card'));
21        var empty = root.querySelector('.aa-empty');
22        var more = root.querySelector('.aa-more');
23        var deferredOpen = false;
24
25        var activeTypes = []; // prázdné = vše
26
27        function filtersActive(q, m, y) {
28            return activeTypes.length > 0 || q !== '' || m !== '' || y !== '';
29        }
30
31        function apply() {
32            var q = norm(search ? search.value.trim() : '');
33            var m = selMonth ? selMonth.value : '';
34            var y = selYear ? selYear.value : '';
35            var anyFilter = filtersActive(q, m, y);
36            var visibleTotal = 0;
37
38            cards.forEach(function (card) {
39                var ym = card.getAttribute('data-ym') || '';
40                var ok = true;
41                if (activeTypes.length && activeTypes.indexOf(card.getAttribute('data-type') || '') === -1) ok = false;
42                if (ok && m && ym.slice(5, 7) !== m) ok = false;
43                if (ok && y && ym.slice(0, 4) !== y) ok = false;
44                if (ok && q && (card.getAttribute('data-q') || '').indexOf(q) === -1) ok = false;
45                card.hidden = !ok;
46                if (ok) visibleTotal++;
47            });
48
49            groups.forEach(function (g, gi) {
50                var visibleInGroup = g.querySelectorAll('.aa-card:not([hidden])').length;
51                // při aktivním filtru ignoruj odložené měsíce (uživatel hledá v celé historii)
52                var deferred = g.hasAttribute('data-deferred') && !deferredOpen && !anyFilter;
53                g.hidden = deferred || visibleInGroup === 0;
54            });
55
56            if (empty) empty.hidden = visibleTotal !== 0;
57            if (more) more.hidden = anyFilter || deferredOpen;
58        }
59
60        chips.forEach(function (chip) {
61            chip.addEventListener('click', function () {
62                var t = chip.getAttribute('data-type') || '';
63                if (t === '') {
64                    activeTypes = [];
65                    chips.forEach(function (c) { c.classList.toggle('is-on', (c.getAttribute('data-type') || '') === ''); });
66                } else {
67                    var idx = activeTypes.indexOf(t);
68                    if (idx === -1) activeTypes.push(t); else activeTypes.splice(idx, 1);
69                    chip.classList.toggle('is-on', idx === -1);
70                    var allChip = chips.filter(function (c) { return (c.getAttribute('data-type') || '') === ''; })[0];
71                    if (allChip) allChip.classList.toggle('is-on', activeTypes.length === 0);
72                }
73                // Stav musí slyšet i čtečka, ne jen vidět oko (třída is-on).
74                chips.forEach(function (c) { c.setAttribute('aria-pressed', c.classList.contains('is-on') ? 'true' : 'false'); });
75                apply();
76            });
77        });
78
79        var debounceTimer = null;
80        if (search) search.addEventListener('input', function () {
81            clearTimeout(debounceTimer);
82            debounceTimer = setTimeout(apply, 150);
83        });
84        if (selMonth) selMonth.addEventListener('change', apply);
85        if (selYear) selYear.addEventListener('change', apply);
86        if (more) more.addEventListener('click', function () { deferredOpen = true; apply(); });
87
88        apply();
89    }
90
91    function boot() {
92        Array.prototype.forEach.call(document.querySelectorAll('.aa-section'), initArchive);
93    }
94    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot);
95    else boot();
96})();

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.