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
 * Controls the drawer.
18
 *
19
 * @module     core/drawer
20
 * @copyright  2019 Jun Pataleta <jun@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import $ from 'jquery';
24
import * as PubSub from 'core/pubsub';
25
import * as Aria from 'core/aria';
26
import DrawerEvents from 'core/drawer_events';
27
 
28
/**
29
 * Show the drawer.
30
 *
31
 * @param {Object} root The drawer container.
32
 */
33
const show = root => {
34
    // Ensure that it is a jQuery.
35
    root = $(root);
36
 
37
    Aria.unhide(root.get());
38
    root.removeClass('hidden');
39
    root.focus();
40
 
41
    PubSub.publish(DrawerEvents.DRAWER_SHOWN, root);
42
};
43
 
44
/**
45
 * Hide the drawer.
46
 *
47
 * @param {Object} root The drawer container.
48
 */
49
const hide = root => {
50
    // Ensure that it is a jQuery.
51
    root = $(root);
52
 
53
    root.addClass('hidden');
54
    Aria.hide(root.get());
55
 
56
    PubSub.publish(DrawerEvents.DRAWER_HIDDEN, root);
57
};
58
 
59
/**
60
 * Check if the drawer is visible.
61
 *
62
 * @param {Object} root The drawer container.
63
 * @return {boolean}
64
 */
65
const isVisible = (root) => {
66
    let isHidden = root.hasClass('hidden');
67
    return !isHidden;
68
};
69
 
70
/**
71
 * Toggle the drawer visibility.
72
 *
73
 * @param {Object} root The drawer container.
74
 */
75
const toggle = (root) => {
76
    if (isVisible(root)) {
77
        hide(root);
78
    } else {
79
        show(root);
80
    }
81
};
82
 
83
/**
84
 * Add event listeners to toggle the drawer.
85
 *
86
 * @param {Object} root The drawer container.
87
 * @param {Object} toggleElements The toggle elements.
88
 */
89
const registerToggles = (root, toggleElements) => {
90
    let openTrigger = null;
91
    toggleElements.attr('aria-expanded', isVisible(root));
92
 
93
    toggleElements.on('click', (e) => {
94
        e.preventDefault();
95
        const wasVisible = isVisible(root);
96
        toggle(root);
97
        toggleElements.attr('aria-expanded', !wasVisible);
98
 
99
        if (!wasVisible) {
100
            // Remember which trigger element opened the drawer.
101
            openTrigger = toggleElements.filter((index, element) => {
102
                return element == e.target || element.contains(e.target);
103
            });
104
        } else if (openTrigger) {
105
            // The drawer has gone from open to close so we need to set the focus back
106
            // to the element that openend it.
107
            openTrigger.focus();
108
            openTrigger = null;
109
        }
110
    });
111
};
112
 
113
/**
114
 * Find the root element of the drawer based on the using the drawer content root's ID.
115
 *
116
 * @param {Object} contentRoot The drawer content's root element.
117
 * @returns {*|jQuery}
118
 */
119
const getDrawerRoot = (contentRoot) => {
120
    contentRoot = $(contentRoot);
121
    return contentRoot.closest('[data-region="right-hand-drawer"]');
122
};
123
 
124
export default {
125
    hide: hide,
126
    show: show,
127
    isVisible: isVisible,
128
    toggle: toggle,
129
    registerToggles: registerToggles,
130
    getDrawerRoot: getDrawerRoot
131
};