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
 * Provide global helper code to enhance page elements.
18
 *
19
 * @module     core/page_global
20
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(
24
[
25
    'jquery',
26
    'core/custom_interaction_events',
27
    'core/str',
28
    'core/network'
29
],
30
function(
31
    $,
32
    CustomEvents,
33
    Str,
34
    Network
35
) {
36
 
37
    /**
38
     * Add an event handler for dropdown menus that wish to show their active item
39
     * in the dropdown toggle element.
40
     *
41
     * By default the handler will add the "active" class to the selected dropdown
42
     * item and set it's text as the HTML for the dropdown toggle.
43
     *
44
     * The behaviour of this handler is controlled by adding data attributes to
45
     * the HTML and requires the typically Bootstrap dropdown markup.
46
     *
47
     * data-show-active-item - Add to the .dropdown-menu element to enable default
48
     *                         functionality.
49
     * data-skip-active-class - Add to the .dropdown-menu to prevent this code from
50
     *                          adding the active class to the dropdown items
1441 ariadna 51
     * data-active-item-text - Add to an element within the data-bs-toggle="dropdown" element
1 efrain 52
     *                         to use it as the active option text placeholder otherwise the
1441 ariadna 53
     *                         data-bs-toggle="dropdown" element itself will be used.
1 efrain 54
     * data-active-item-button-aria-label-components - String components to set the aria
55
     *                         lable on the dropdown button. The string will be given the
56
     *                         active item text.
57
     */
58
    var initActionOptionDropdownHandler = function() {
59
        var body = $('body');
60
 
1441 ariadna 61
        // Ensure on load that the "current" item is always marked to ensure we make space for active item indicator.
62
        body.find('.dropdown-menu').each(function() {
63
            $(this).find('[aria-current="true"]').addClass('dropdown-item-active');
64
        });
65
 
1 efrain 66
        CustomEvents.define(body, [CustomEvents.events.activate]);
67
        body.on(CustomEvents.events.activate, '[data-show-active-item]', function(e) {
68
            // The dropdown item that the user clicked on.
69
            var option = $(e.target).closest('.dropdown-item');
70
            // The dropdown menu element.
71
            var menuContainer = option.closest('[data-show-active-item]');
72
 
73
            if (!option.hasClass('dropdown-item')) {
74
                // Ignore non Bootstrap dropdowns.
75
                return;
76
            }
77
 
78
            if (option.hasClass('active')) {
79
                // If it's already active then we don't need to do anything.
80
                return;
81
            }
82
 
83
            // Clear the active class from all other options.
84
            var dropdownItems = menuContainer.find('.dropdown-item');
1441 ariadna 85
            dropdownItems.removeClass(['active', 'dropdown-item-active']);
1 efrain 86
            dropdownItems.removeAttr('aria-current');
87
 
88
            if (!menuContainer.attr('data-skip-active-class')) {
89
                // Make this option active unless configured to ignore it.
90
                // Some code, for example the Bootstrap tabs, may want to handle
91
                // adding the active class itself.
92
                option.addClass('active');
93
            }
1441 ariadna 94
            option.addClass('dropdown-item-active');
1 efrain 95
 
96
            // Update aria attribute for active item.
97
            option.attr('aria-current', true);
98
 
99
            var activeOptionText = option.text();
1441 ariadna 100
            var dropdownToggle = menuContainer.parent().find('[data-bs-toggle="dropdown"]');
1 efrain 101
            var dropdownToggleText = dropdownToggle.find('[data-active-item-text]');
102
 
103
            if (dropdownToggleText.length) {
104
                // We have a specific placeholder for the active item text so
105
                // use that.
106
                dropdownToggleText.html(activeOptionText);
107
            } else {
108
                // Otherwise just replace all of the toggle text with the active item.
109
                dropdownToggle.html(activeOptionText);
110
            }
111
 
112
            var activeItemAriaLabelComponent = menuContainer.attr('data-active-item-button-aria-label-components');
113
            if (activeItemAriaLabelComponent) {
114
                // If we have string components for the aria label then load the string
115
                // and set the label on the dropdown toggle.
116
                var strParams = activeItemAriaLabelComponent.split(',');
117
                strParams.push(activeOptionText);
118
 
119
                Str.get_string(strParams[0].trim(), strParams[1].trim(), strParams[2].trim())
120
                    .then(function(string) {
121
                        dropdownToggle.attr('aria-label', string);
122
                        return string;
123
                    })
124
                    .catch(function() {
125
                        // Silently ignore that we couldn't load the string.
126
                        return false;
127
                    });
128
            }
129
        });
130
    };
131
 
132
    /**
133
     * Initialise the global helper functions.
134
     */
135
    var init = function() {
136
        initActionOptionDropdownHandler();
137
        Network.init();
138
    };
139
 
140
    return {
141
        init: init
142
    };
143
});