aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/actionmenu.js
blob: 5cc6021404035999e7702965f95ad59be0fdaac7 (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
(function ($) {
    'use strict';

    var last = null;

    // Open action menu on click on the icon
    $(document).on('click', '.action-menu-icon', function (event) {
        // Choose correct root element if menu was positioned absolutely
        let root_element = $(this).closest('.action-menu');
        if ($(this).closest('.action-menu-wrapper').length > 0) {
            root_element = $(this).data('action-menu-element');
        }

        var position = root_element.data('action-menu-reposition');
        if (position === undefined) {
            position = true;
        }
        // Obtain unique id for the root element and close other menus if neccessary
        const id = root_element.uniqueId().attr('id');
        if (last !== id) {
            STUDIP.ActionMenu.closeAll();
            last = id;
        }

        STUDIP.ActionMenu.create(root_element, position).toggle();

        // Stop event so the following close event will not be fired
        return false;
    });

    // Close action menu on click outside
    $(document).on('click', (event) => {
        if ($(event.target).closest('.action-menu-content').length === 0) {
            STUDIP.ActionMenu.closeAll();
        }
    });

    // Close all action menus when the escape key is pressed and rotate through all its items
    // when TAB or SHIFT + TAB is pressed.
    $(document).on('keydown', function(event) {
        if (event.key === 'Escape') {
            STUDIP.ActionMenu.closeAll();
        } else if (event.key === 'Tab') {
            //Check if the focus is inside an action menu:
            let menu = $(event.target).closest('.action-menu');
            if (menu.hasClass('is-open') && STUDIP.ActionMenu.tabThroughItems(menu, event.shiftKey)) {
                event.preventDefault();
            }
        }
    });

}(jQuery));