Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Initializes and handles events in the user menu.
18
 *
19
 * @module     core/usermenu
20
 * @copyright  2021 Moodle
21
 * @author     Mihail Geshoski <mihail@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
1441 ariadna 25
import Carousel from 'theme_boost/bootstrap/carousel';
1 efrain 26
import {space, enter} from 'core/key_codes';
27
 
28
/**
29
 * User menu constants.
30
 */
31
const selectors = {
32
    userMenu: '.usermenu',
33
    userMenuCarousel: '.usermenu #usermenu-carousel',
34
    userMenuCarouselItem: '.usermenu #usermenu-carousel .carousel-item',
35
    userMenuCarouselItemActive: '.usermenu #usermenu-carousel .carousel-item.active',
36
    userMenuCarouselNavigationLink: '.usermenu #usermenu-carousel .carousel-navigation-link',
37
};
38
 
39
/**
40
 * Register event listeners.
41
 */
42
const registerEventListeners = () => {
43
    const userMenu = document.querySelector(selectors.userMenu);
1441 ariadna 44
    const userMenuCarousel = document.querySelector(selectors.userMenuCarousel);
1 efrain 45
 
46
    // Handle the 'shown.bs.dropdown' event (Fired when the dropdown menu is fully displayed).
1441 ariadna 47
    userMenu.addEventListener('shown.bs.dropdown', () => {
1 efrain 48
        const activeCarouselItem = document.querySelector(selectors.userMenuCarouselItemActive);
49
        // Set the focus on the active carousel item.
50
        activeCarouselItem.focus();
51
 
52
        userMenu.querySelectorAll(selectors.userMenuCarouselItem).forEach(element => {
53
            // Resize all non-active carousel items to match the height and width of the current active (main)
54
            // carousel item to avoid sizing inconsistencies. This has to be done once the dropdown menu is fully
55
            // displayed ('shown.bs.dropdown') as the offsetWidth and offsetHeight cannot be obtained when the
56
            // element is hidden.
57
            if (!element.classList.contains('active')) {
58
                element.style.width = activeCarouselItem.offsetWidth + 'px';
59
                element.style.height = activeCarouselItem.offsetHeight + 'px';
60
            }
61
        });
62
    });
63
 
64
    // Handle click events in the user menu.
65
    userMenu.addEventListener('click', (e) => {
66
 
67
        // Handle click event on the carousel navigation (control) links in the user menu.
68
        if (e.target.matches(selectors.userMenuCarouselNavigationLink)) {
69
            carouselManagement(e);
70
        }
71
    });
72
 
73
    userMenu.addEventListener('keydown', e => {
74
        // Handle keydown event on the carousel navigation (control) links in the user menu.
75
        if ((e.keyCode === space ||
76
            e.keyCode === enter) &&
77
            e.target.matches(selectors.userMenuCarouselNavigationLink)) {
78
            e.preventDefault();
79
            carouselManagement(e);
80
        }
81
    });
82
 
83
    /**
84
     * We do the same actions here even if the caller was a click or button press.
85
     *
86
     * @param {Event} e The triggering element and key presses etc.
87
     */
88
    const carouselManagement = e => {
89
        // By default the user menu dropdown element closes on a click event. This behaviour is not desirable
90
        // as we need to be able to navigate through the carousel items (submenus of the user menu) within the
91
        // user menu. Therefore, we need to prevent the propagation of this event and then manually call the
92
        // carousel transition.
93
        e.stopPropagation();
94
        // The id of the targeted carousel item.
95
        const targetedCarouselItemId = e.target.dataset.carouselTargetId;
96
        const targetedCarouselItem = userMenu.querySelector('#' + targetedCarouselItemId);
97
        // Get the position (index) of the targeted carousel item within the parent container element.
98
        const index = Array.from(targetedCarouselItem.parentNode.children).indexOf(targetedCarouselItem);
99
        // Navigate to the targeted carousel item.
1441 ariadna 100
        Carousel.getOrCreateInstance(userMenuCarousel).to(index);
1 efrain 101
    };
102
 
103
    // Handle the 'hide.bs.dropdown' event (Fired when the dropdown menu is being closed).
1441 ariadna 104
    userMenu.addEventListener('hide.bs.dropdown', () => {
1 efrain 105
        // Reset the state once the user menu dropdown is closed and return back to the first (main) carousel item
106
        // if necessary.
1441 ariadna 107
        Carousel.getOrCreateInstance(userMenuCarousel).to(0);
1 efrain 108
    });
109
 
110
    // Handle the 'slid.bs.carousel' event (Fired when the carousel has completed its slide transition).
1441 ariadna 111
    userMenuCarousel?.addEventListener('slid.bs.carousel', () => {
1 efrain 112
        const activeCarouselItem = userMenu.querySelector(selectors.userMenuCarouselItemActive);
113
        // Set the focus on the newly activated carousel item.
114
        activeCarouselItem.focus();
115
    });
116
};
117
 
118
/**
119
 * Initialize the user menu.
120
 */
121
const init = () => {
122
    registerEventListeners();
123
};
124
 
125
export default {
126
    init: init,
127
};