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.attr('aria-expanded', true);
|
|
|
40 |
root.focus();
|
|
|
41 |
|
|
|
42 |
PubSub.publish(DrawerEvents.DRAWER_SHOWN, root);
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Hide the drawer.
|
|
|
47 |
*
|
|
|
48 |
* @param {Object} root The drawer container.
|
|
|
49 |
*/
|
|
|
50 |
const hide = root => {
|
|
|
51 |
// Ensure that it is a jQuery.
|
|
|
52 |
root = $(root);
|
|
|
53 |
|
|
|
54 |
root.addClass('hidden');
|
|
|
55 |
root.attr('aria-expanded', false);
|
|
|
56 |
Aria.hide(root.get());
|
|
|
57 |
|
|
|
58 |
PubSub.publish(DrawerEvents.DRAWER_HIDDEN, root);
|
|
|
59 |
};
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Check if the drawer is visible.
|
|
|
63 |
*
|
|
|
64 |
* @param {Object} root The drawer container.
|
|
|
65 |
* @return {boolean}
|
|
|
66 |
*/
|
|
|
67 |
const isVisible = (root) => {
|
|
|
68 |
let isHidden = root.hasClass('hidden');
|
|
|
69 |
return !isHidden;
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Toggle the drawer visibility.
|
|
|
74 |
*
|
|
|
75 |
* @param {Object} root The drawer container.
|
|
|
76 |
*/
|
|
|
77 |
const toggle = (root) => {
|
|
|
78 |
if (isVisible(root)) {
|
|
|
79 |
hide(root);
|
|
|
80 |
} else {
|
|
|
81 |
show(root);
|
|
|
82 |
}
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Add event listeners to toggle the drawer.
|
|
|
87 |
*
|
|
|
88 |
* @param {Object} root The drawer container.
|
|
|
89 |
* @param {Object} toggleElements The toggle elements.
|
|
|
90 |
*/
|
|
|
91 |
const registerToggles = (root, toggleElements) => {
|
|
|
92 |
let openTrigger = null;
|
|
|
93 |
toggleElements.attr('aria-expanded', isVisible(root));
|
|
|
94 |
|
|
|
95 |
toggleElements.on('click', (e) => {
|
|
|
96 |
e.preventDefault();
|
|
|
97 |
const wasVisible = isVisible(root);
|
|
|
98 |
toggle(root);
|
|
|
99 |
toggleElements.attr('aria-expanded', !wasVisible);
|
|
|
100 |
|
|
|
101 |
if (!wasVisible) {
|
|
|
102 |
// Remember which trigger element opened the drawer.
|
|
|
103 |
openTrigger = toggleElements.filter((index, element) => {
|
|
|
104 |
return element == e.target || element.contains(e.target);
|
|
|
105 |
});
|
|
|
106 |
} else if (openTrigger) {
|
|
|
107 |
// The drawer has gone from open to close so we need to set the focus back
|
|
|
108 |
// to the element that openend it.
|
|
|
109 |
openTrigger.focus();
|
|
|
110 |
openTrigger = null;
|
|
|
111 |
}
|
|
|
112 |
});
|
|
|
113 |
};
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Find the root element of the drawer based on the using the drawer content root's ID.
|
|
|
117 |
*
|
|
|
118 |
* @param {Object} contentRoot The drawer content's root element.
|
|
|
119 |
* @returns {*|jQuery}
|
|
|
120 |
*/
|
|
|
121 |
const getDrawerRoot = (contentRoot) => {
|
|
|
122 |
contentRoot = $(contentRoot);
|
|
|
123 |
return contentRoot.closest('[data-region="right-hand-drawer"]');
|
|
|
124 |
};
|
|
|
125 |
|
|
|
126 |
export default {
|
|
|
127 |
hide: hide,
|
|
|
128 |
show: show,
|
|
|
129 |
isVisible: isVisible,
|
|
|
130 |
toggle: toggle,
|
|
|
131 |
registerToggles: registerToggles,
|
|
|
132 |
getDrawerRoot: getDrawerRoot
|
|
|
133 |
};
|