aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/global_search.js
blob: 0e179b7bb0428cc29b4b1b3f5d38162e030d21ee (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
STUDIP.domReady(() => {
    // Clear search term
    $('#globalsearch-clear').on('click', function() {
        var before = $('#globalsearch-input').val();
        STUDIP.GlobalSearch.resetSearch();

        if ($('html').is('.responsive-display') && before.length === 0) {
            STUDIP.GlobalSearch.toggleSearchBar(false);
        }

        return false;
    });

    // Bind icon click to performing search.
    $('#globalsearch-icon').on('click', function() {
        STUDIP.GlobalSearch.doSearch();

        if ($('html').hasClass('responsified')) {
            var input = $('#globalsearch-input');
            input.toggleClass('hidden-small-down', false);
            input.focus();
        }

        return false;
    });

    // Enlarge search input on focus and show hints.
    $('#globalsearch-input').on('focus', function() {
        STUDIP.GlobalSearch.toggleSearchBar(true, false);
    }).on('keypress', (e) => {
        // Start search on Enter
        if (e.key === 'Enter') {
            STUDIP.GlobalSearch.doSearch();
            return false;
        }
    });
    $('#globalsearch-searchbar').on('keydown', function(e) {
        if (!['ArrowDown', 'ArrowUp'].includes(e.key)) {
            return;
        }

        e.preventDefault();

        // Get all possible items
        const items = $('#globalsearch-list [role=listitem]:visible');

        // Find focussed element
        const focussed = items.filter(':focus');

        // Get index of focussed element in all items
        let index = focussed.length > 0 ? items.index(focussed[0]) : null;

        // Move focussed element up or down in items
        if (e.key === 'ArrowDown') {
            index = (index ?? -1) + 1;
        } else {
            index = (index ?? items.length) - 1;
        }

        // Clamp index to sane boundaries
        if (index < 0) {
            index = 0;
        } else if (index > items.length - 1) {
            index = items.length - 1;
        }

        // Focus new element by index
        items.get(index).focus();
    });


    // Close search on click on page.
    $('#navigation-level-1, #current-page-structure, #main-footer').on('click', function() {
        if (!$('#globalsearch-input').hasClass('hidden-js')) {
            STUDIP.GlobalSearch.toggleSearchBar(false, false);
        }
    });

    // Show/hide hints on click.
    $('#globalsearch-togglehints').on('click', function() {
        var toggle = $('#globalsearch-togglehints'),
            currentText = toggle.text();

        toggle.text(toggle.data('toggle-text').trim());
        toggle.data('toggle-text', currentText);

        toggle.toggleClass('open');
    });

    // Delegate events on result container so we don't have to bind them
    // one by one
    $('#globalsearch-results').on('click', '.globalsearch-category a', function() {
        var category = $(this)
            .closest('.globalsearch-category')
            .data('category');
        STUDIP.GlobalSearch.expandCategory(category);
        return false;
    });

    // Key bindings.
    $(document).keydown(function(e) {
        // Don't do anything if a dialog is open
        if (STUDIP.Dialog.stack.length > 0) {
            return;
        }

        // ctrl + space
        if (e.which === 32 && e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) {
            e.preventDefault();
            if ($('#globalsearch-searchbar').hasClass('is-visible')) {
                STUDIP.GlobalSearch.toggleSearchBar(false, false);
                $('#globalsearch-input').blur();
            } else {
                $('#globalsearch-input').focus();
            }
            // escape
        } else if (e.which === 27 && !e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) {
            e.preventDefault();
            STUDIP.GlobalSearch.toggleSearchBar(false, true);
        }
    });

    // Start searching 750 ms after user stopped typing or content was added
    // by pasting text via right-click.
    $('#globalsearch-input').on('keyup paste',
        _.debounce(function() {
            STUDIP.GlobalSearch.doSearch();
        }, 750)
    );
});