Proyectos de Subversion Moodle

Rev

| 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
 * Moves wrapping navigation items into a more menu.
18
 *
19
 * @module     core/moremenu
20
 * @copyright  2021 Moodle
21
 * @author     Bas Brands <bas@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import $ from 'jquery';
26
import menu_navigation from "core/menu_navigation";
27
/**
28
 * Moremenu selectors.
29
 */
30
const Selectors = {
31
    regions: {
32
        moredropdown: '[data-region="moredropdown"]',
33
        morebutton: '[data-region="morebutton"]'
34
    },
35
    classes: {
36
        dropdownitem: 'dropdown-item',
37
        dropdownmoremenu: 'dropdownmoremenu',
38
        hidden: 'd-none',
39
        active: 'active',
40
        nav: 'nav',
41
        navlink: 'nav-link',
42
        observed: 'observed',
43
    },
44
    attributes: {
45
        menu: '[role="menu"]',
46
        dropdowntoggle: '[data-toggle="dropdown"]'
47
    }
48
};
49
 
50
let isTabListMenu = false;
51
 
52
/**
53
 * Auto Collapse navigation items that wrap into a dropdown menu.
54
 *
55
 * @param {HTMLElement} menu The navbar container.
56
 */
57
const autoCollapse = menu => {
58
 
59
    const maxHeight = menu.parentNode.offsetHeight + 1;
60
 
61
    const moreDropdown = menu.querySelector(Selectors.regions.moredropdown);
62
    const moreButton = menu.querySelector(Selectors.regions.morebutton);
63
 
64
    // If the menu items wrap and the menu height is larger than the height of the
65
    // parent then start pushing navlinks into the moreDropdown.
66
    if (menu.offsetHeight > maxHeight) {
67
        moreButton.classList.remove(Selectors.classes.hidden);
68
 
69
        let menuHeight = 0;
70
        const menuNodes = Array.from(menu.children).reverse();
71
        menuNodes.forEach(item => {
72
            if (!item.classList.contains(Selectors.classes.dropdownmoremenu)) {
73
                // After moving the menu items into the moreDropdown check again
74
                // if the menu height is still larger then the height of the parent.
75
                if (menu.offsetHeight > maxHeight) {
76
                    // Move this node into the more dropdown menu.
77
                    moveIntoMoreDropdown(menu, item, true);
78
                } else if (menuHeight > maxHeight) {
79
                    moveIntoMoreDropdown(menu, item, true);
80
                    menuHeight = 0;
81
                }
82
            } else if (menu.offsetHeight > maxHeight) {
83
                // Assign menu height to be used to check with menu parent.
84
                menuHeight = menu.offsetHeight;
85
            }
86
        });
87
    } else {
88
        // If the menu height is smaller than the height of the parent, then try returning navlinks to the menu.
89
        if ('children' in moreDropdown) {
90
            // Iterate through the nodes within the more dropdown menu.
91
            Array.from(moreDropdown.children).forEach(item => {
92
                // Don't move the node to the more menu if it is explicitly defined that
93
                // this node should be displayed in the more dropdown menu at all times.
94
                if (menu.offsetHeight < maxHeight && item.dataset.forceintomoremenu !== 'true') {
95
                    const lastNode = moreDropdown.removeChild(item);
96
                    // Move this node from the more dropdown menu into the main section of the menu.
97
                    moveOutOfMoreDropdown(menu, lastNode);
98
                }
99
            });
100
            // If there are no more nodes in the more dropdown menu we can hide the moreButton.
101
            if (Array.from(moreDropdown.children).length === 0) {
102
                moreButton.classList.add(Selectors.classes.hidden);
103
            }
104
        }
105
 
106
        if (menu.offsetHeight > maxHeight) {
107
            autoCollapse(menu);
108
        }
109
    }
110
    menu.parentNode.classList.add(Selectors.classes.observed);
111
};
112
 
113
/**
114
 * Move a node into the "more" dropdown menu.
115
 *
116
 * This method forces a given navigation node to be added and displayed within the "more" dropdown menu.
117
 *
118
 * @param {HTMLElement} menu The navbar moremenu.
119
 * @param {HTMLElement} navNode The navigation node.
120
 * @param {boolean} prepend Whether to prepend or append the node to the content in the more dropdown menu.
121
 */
122
const moveIntoMoreDropdown = (menu, navNode, prepend = false) => {
123
    const moreDropdown = menu.querySelector(Selectors.regions.moredropdown);
124
    const dropdownToggle = menu.querySelector(Selectors.attributes.dropdowntoggle);
125
 
126
    const navLink = navNode.querySelector('.' + Selectors.classes.navlink);
127
    // If there are navLinks that contain an active link in the moreDropdown
128
    // make the dropdownToggle in the moreButton active.
129
    if (navLink.classList.contains(Selectors.classes.active)) {
130
        dropdownToggle.classList.add(Selectors.classes.active);
131
        dropdownToggle.setAttribute('tabindex', '0');
132
        navLink.setAttribute('tabindex', '-1'); // So that we don't have a single tabbable menu item.
133
        // Remove aria-selected if the more menu is rendered as a tab list.
134
        if (isTabListMenu) {
135
            navLink.removeAttribute('aria-selected');
136
        }
137
        navLink.setAttribute('aria-current', 'true');
138
    }
139
 
140
    // This will become a menu item instead of a tab.
141
    navLink.setAttribute('role', 'menuitem');
142
 
143
    // Change the styling of the navLink to a dropdownitem and push it into
144
    // the moreDropdown.
145
    navLink.classList.remove(Selectors.classes.navlink);
146
    navLink.classList.add(Selectors.classes.dropdownitem);
147
    if (prepend) {
148
        moreDropdown.prepend(navNode);
149
    } else {
150
        moreDropdown.append(navNode);
151
    }
152
};
153
 
154
/**
155
 * Move a node out of the "more" dropdown menu.
156
 *
157
 * This method forces a given node from the "more" dropdown menu to be displayed in the main section of the menu.
158
 *
159
 * @param {HTMLElement} menu The navbar moremenu.
160
 * @param {HTMLElement} navNode The navigation node.
161
 */
162
const moveOutOfMoreDropdown = (menu, navNode) => {
163
    const moreButton = menu.querySelector(Selectors.regions.morebutton);
164
    const dropdownToggle = menu.querySelector(Selectors.attributes.dropdowntoggle);
165
    const navLink = navNode.querySelector('.' + Selectors.classes.dropdownitem);
166
 
167
    // If the more menu is rendered as a tab list,
168
    // this will become a tab instead of a menuitem when moved out of the more menu dropdown.
169
    if (isTabListMenu) {
170
        navLink.setAttribute('role', 'tab');
171
    }
172
 
173
    // Stop displaying the active state on the dropdownToggle if
174
    // the active navlink is removed.
175
    if (navLink.classList.contains(Selectors.classes.active)) {
176
        dropdownToggle.classList.remove(Selectors.classes.active);
177
        dropdownToggle.setAttribute('tabindex', '-1');
178
        navLink.setAttribute('tabindex', '0');
179
        if (isTabListMenu) {
180
            // Replace aria selection state when necessary.
181
            navLink.removeAttribute('aria-current');
182
            navLink.setAttribute('aria-selected', 'true');
183
        }
184
    }
185
    navLink.classList.remove(Selectors.classes.dropdownitem);
186
    navLink.classList.add(Selectors.classes.navlink);
187
    menu.insertBefore(navNode, moreButton);
188
};
189
 
190
/**
191
 * Initialise the more menus.
192
 *
193
 * @param {HTMLElement} menu The navbar moremenu.
194
 */
195
export default menu => {
196
    isTabListMenu = menu.getAttribute('role') === 'tablist';
197
 
198
    // Select the first menu item if there's nothing initially selected.
199
    const hash = window.location.hash;
200
    if (!hash) {
201
        const itemRole = isTabListMenu ? 'tab' : 'menuitem';
202
        const menuListItem = menu.firstElementChild;
203
        const roleSelector = `[role=${itemRole}]`;
204
        const menuItem = menuListItem.querySelector(roleSelector);
205
        const ariaAttribute = isTabListMenu ? 'aria-selected' : 'aria-current';
206
        if (!menu.querySelector(`[${ariaAttribute}='true']`)) {
207
            menuItem.setAttribute(ariaAttribute, 'true');
208
            menuItem.setAttribute('tabindex', '0');
209
        }
210
    }
211
 
212
    // Pre-populate the "more" dropdown menu with navigation nodes which are set to be displayed in this menu
213
    // by default at all times.
214
    if ('children' in menu) {
215
        const moreButton = menu.querySelector(Selectors.regions.morebutton);
216
        const menuNodes = Array.from(menu.children);
217
        menuNodes.forEach((item) => {
218
            if (!item.classList.contains(Selectors.classes.dropdownmoremenu) &&
219
                    item.dataset.forceintomoremenu === 'true') {
220
                // Append this node into the more dropdown menu.
221
                moveIntoMoreDropdown(menu, item, false);
222
                // After adding the node into the more dropdown menu, make sure that the more dropdown menu button
223
                // is displayed.
224
                if (moreButton.classList.contains(Selectors.classes.hidden)) {
225
                    moreButton.classList.remove(Selectors.classes.hidden);
226
                }
227
            }
228
        });
229
    }
230
    // Populate the more dropdown menu with additional nodes if necessary, depending on the current screen size.
231
    autoCollapse(menu);
232
    menu_navigation(menu);
233
 
234
    // When the screen size changes make sure the menu still fits.
235
    window.addEventListener('resize', () => {
236
        autoCollapse(menu);
237
        menu_navigation(menu);
238
    });
239
 
240
    const toggledropdown = e => {
241
        const innerMenu = e.target.parentNode.querySelector(Selectors.attributes.menu);
242
        if (innerMenu) {
243
            innerMenu.classList.toggle('show');
244
        }
245
        e.stopPropagation();
246
    };
247
 
248
    // If there are dropdowns in the MoreMenu, add a new
249
    // event listener to show the contents on click and prevent the
250
    // moreMenu from closing.
251
    $('.' + Selectors.classes.dropdownmoremenu).on('show.bs.dropdown', function() {
252
        const moreDropdown = menu.querySelector(Selectors.regions.moredropdown);
253
        moreDropdown.querySelectorAll('.dropdown').forEach((dropdown) => {
254
            dropdown.removeEventListener('click', toggledropdown, true);
255
            dropdown.addEventListener('click', toggledropdown, true);
256
        });
257
    });
258
};