aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/static/js/wiki/dropdown.js
blob: a914a4abd3b0f938793aa2bdd637fb5584f98c2b (plain) (blame)
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
(function() {
    window.dropdowns = {};

    let elements = document.getElementsByClassName("dropdown");

    for (let element of elements) {
        let menu_element = element.getElementsByClassName("dropdown-menu")[0];

        function show() {
            $(element).addClass("is-active");
        }

        function hide() {
            $(element).removeClass("is-active");
        }

        function handle_event(e) {
            show();

            $(document.body).on("click." + menu_element.id, function() {
                hide();

                $(document.body).off("click." + menu_element.id);
            });

            e.stopPropagation();
        }

        $(element).click(handle_event);
        $(element).hover(handle_event);
        $(element).mouseleave(hide);

        window.dropdowns[menu_element.id] = element;
    }
})();