File size: 2,541 Bytes
31e282f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function () {
  "use strict";

  function param(name) {
    return new URLSearchParams(window.location.search).get(name) || "";
  }

  function selectedTypes(root) {
    return Array.from(root.querySelectorAll(".ctx-catalog-filters input:checked")).map(
      function (input) {
        return input.value;
      }
    );
  }

  function searchableText(card) {
    return (
      card.getAttribute("data-type") +
      " " +
      card.getAttribute("data-search") +
      " " +
      card.textContent
    ).toLowerCase();
  }

  function render(root) {
    var queryInput = root.querySelector("#ctx-catalog-query");
    var count = root.querySelector("#ctx-catalog-count");
    var cards = Array.from(root.querySelectorAll(".ctx-catalog-card"));
    var selected = new Set(selectedTypes(root));
    var query = (queryInput ? queryInput.value : "").trim().toLowerCase();
    var visible = 0;

    cards.forEach(function (card) {
      var matchesType = selected.has(card.getAttribute("data-type"));
      var matchesQuery = !query || searchableText(card).indexOf(query) !== -1;
      var show = matchesType && matchesQuery;
      card.hidden = !show;
      if (show) visible += 1;
    });

    if (count) {
      count.textContent = visible + " tile" + (visible === 1 ? "" : "s") + " shown";
    }
  }

  function initCatalog() {
    var root = document.querySelector(".ctx-catalog-app");
    if (!root || root.getAttribute("data-catalog-ready") === "true") {
      return;
    }
    root.setAttribute("data-catalog-ready", "true");

    var queryInput = root.querySelector("#ctx-catalog-query");
    var initialType = param("type");
    var initialQuery = param("q");

    if (queryInput && initialQuery) {
      queryInput.value = initialQuery;
    }

    if (initialType) {
      root.querySelectorAll(".ctx-catalog-filters input").forEach(function (input) {
        input.checked = input.value === initialType;
      });
    }

    if (queryInput) {
      queryInput.addEventListener("input", function () {
        render(root);
      });
    }

    root.querySelectorAll(".ctx-catalog-filters input").forEach(function (input) {
      input.addEventListener("change", function () {
        render(root);
      });
    });

    render(root);
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", initCatalog, { once: true });
  } else {
    initCatalog();
  }

  if (window.document$ && typeof window.document$.subscribe === "function") {
    window.document$.subscribe(initCatalog);
  }
})();