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 |
* Toggling the visibility of the secondary navigation on mobile.
|
|
|
18 |
*
|
|
|
19 |
* @module theme_monocolor/drawers
|
|
|
20 |
* @copyright 2021 Bas Brands
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
import ModalBackdrop from 'core/modal_backdrop';
|
|
|
24 |
import Templates from 'core/templates';
|
|
|
25 |
import * as Aria from 'core/aria';
|
|
|
26 |
import {dispatchEvent} from 'core/event_dispatcher';
|
|
|
27 |
import {debounce} from 'core/utils';
|
|
|
28 |
import {isSmall, isLarge} from 'core/pagehelpers';
|
|
|
29 |
import Pending from 'core/pending';
|
|
|
30 |
import {setUserPreference} from 'core_user/repository';
|
|
|
31 |
// The jQuery module is only used for interacting with Boostrap 4. It can we removed when MDL-71979 is integrated.
|
|
|
32 |
import jQuery from 'jquery';
|
|
|
33 |
|
|
|
34 |
let backdropPromise = null;
|
|
|
35 |
|
|
|
36 |
const drawerMap = new Map();
|
|
|
37 |
|
|
|
38 |
const SELECTORS = {
|
|
|
39 |
BUTTONS: '[data-toggler="drawers"]',
|
|
|
40 |
CLOSEBTN: '[data-toggler="drawers"][data-action="closedrawer"]',
|
|
|
41 |
OPENBTN: '[data-toggler="drawers"][data-action="opendrawer"]',
|
|
|
42 |
TOGGLEBTN: '[data-toggler="drawers"][data-action="toggle"]',
|
|
|
43 |
DRAWERS: '[data-region="fixed-drawer"]',
|
|
|
44 |
DRAWERCONTENT: '.drawercontent',
|
|
|
45 |
PAGECONTENT: '#page-content',
|
|
|
46 |
HEADERCONTENT: '.drawerheadercontent',
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
const CLASSES = {
|
|
|
50 |
SCROLLED: 'scrolled',
|
|
|
51 |
SHOW: 'show',
|
|
|
52 |
NOTINITIALISED: 'not-initialized',
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Pixel thresshold to auto-hide drawers.
|
|
|
57 |
*
|
|
|
58 |
* @type {Number}
|
|
|
59 |
*/
|
|
|
60 |
const THRESHOLD = 20;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Try to get the drawer z-index from the page content.
|
|
|
64 |
*
|
|
|
65 |
* @returns {Number|null} the z-index of the drawer.
|
|
|
66 |
* @private
|
|
|
67 |
*/
|
|
|
68 |
const getDrawerZIndex = () => {
|
|
|
69 |
const drawer = document.querySelector(SELECTORS.DRAWERS);
|
|
|
70 |
if (!drawer) {
|
|
|
71 |
return null;
|
|
|
72 |
}
|
|
|
73 |
return parseInt(window.getComputedStyle(drawer).zIndex, 10);
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Add a backdrop to the page.
|
|
|
78 |
*
|
|
|
79 |
* @returns {Promise} rendering of modal backdrop.
|
|
|
80 |
* @private
|
|
|
81 |
*/
|
|
|
82 |
const getBackdrop = () => {
|
|
|
83 |
if (!backdropPromise) {
|
|
|
84 |
backdropPromise = Templates.render('core/modal_backdrop', {})
|
|
|
85 |
.then(html => new ModalBackdrop(html))
|
|
|
86 |
.then(modalBackdrop => {
|
|
|
87 |
const drawerZindex = getDrawerZIndex();
|
|
|
88 |
if (drawerZindex) {
|
|
|
89 |
modalBackdrop.setZIndex(getDrawerZIndex() - 1);
|
|
|
90 |
}
|
|
|
91 |
modalBackdrop.getAttachmentPoint().get(0).addEventListener('click', e => {
|
|
|
92 |
e.preventDefault();
|
|
|
93 |
Drawers.closeAllDrawers();
|
|
|
94 |
});
|
|
|
95 |
return modalBackdrop;
|
|
|
96 |
})
|
|
|
97 |
.catch();
|
|
|
98 |
}
|
|
|
99 |
return backdropPromise;
|
|
|
100 |
};
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Get the button element to open a specific drawer.
|
|
|
104 |
*
|
|
|
105 |
* @param {String} drawerId the drawer element Id
|
|
|
106 |
* @return {HTMLElement|undefined} the open button element
|
|
|
107 |
* @private
|
|
|
108 |
*/
|
|
|
109 |
const getDrawerOpenButton = (drawerId) => {
|
|
|
110 |
let openButton = document.querySelector(`${SELECTORS.OPENBTN}[data-target="${drawerId}"]`);
|
|
|
111 |
if (!openButton) {
|
|
|
112 |
openButton = document.querySelector(`${SELECTORS.TOGGLEBTN}[data-target="${drawerId}"]`);
|
|
|
113 |
}
|
|
|
114 |
return openButton;
|
|
|
115 |
};
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Disable drawer tooltips.
|
|
|
119 |
*
|
|
|
120 |
* @param {HTMLElement} drawerNode the drawer main node
|
|
|
121 |
* @private
|
|
|
122 |
*/
|
|
|
123 |
const disableDrawerTooltips = (drawerNode) => {
|
|
|
124 |
const buttons = [
|
|
|
125 |
drawerNode.querySelector(SELECTORS.CLOSEBTN),
|
|
|
126 |
getDrawerOpenButton(drawerNode.id),
|
|
|
127 |
];
|
|
|
128 |
buttons.forEach(button => {
|
|
|
129 |
if (!button) {
|
|
|
130 |
return;
|
|
|
131 |
}
|
|
|
132 |
disableButtonTooltip(button);
|
|
|
133 |
});
|
|
|
134 |
};
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Disable the button tooltips.
|
|
|
138 |
*
|
|
|
139 |
* @param {HTMLElement} button the button element
|
|
|
140 |
* @param {boolean} enableOnBlur if the tooltip must be re-enabled on blur.
|
|
|
141 |
* @private
|
|
|
142 |
*/
|
|
|
143 |
const disableButtonTooltip = (button, enableOnBlur) => {
|
|
|
144 |
if (button.hasAttribute('data-original-title')) {
|
|
|
145 |
// The jQuery is still used in Boostrap 4. It can we removed when MDL-71979 is integrated.
|
|
|
146 |
jQuery(button).tooltip('disable');
|
|
|
147 |
button.setAttribute('title', button.dataset.originalTitle);
|
|
|
148 |
} else {
|
|
|
149 |
button.dataset.disabledToggle = button.dataset.toggle;
|
|
|
150 |
button.removeAttribute('data-toggle');
|
|
|
151 |
}
|
|
|
152 |
if (enableOnBlur) {
|
|
|
153 |
button.dataset.restoreTooltipOnBlur = true;
|
|
|
154 |
}
|
|
|
155 |
};
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Enable drawer tooltips.
|
|
|
159 |
*
|
|
|
160 |
* @param {HTMLElement} drawerNode the drawer main node
|
|
|
161 |
* @private
|
|
|
162 |
*/
|
|
|
163 |
const enableDrawerTooltips = (drawerNode) => {
|
|
|
164 |
const buttons = [
|
|
|
165 |
drawerNode.querySelector(SELECTORS.CLOSEBTN),
|
|
|
166 |
getDrawerOpenButton(drawerNode.id),
|
|
|
167 |
];
|
|
|
168 |
buttons.forEach(button => {
|
|
|
169 |
if (!button) {
|
|
|
170 |
return;
|
|
|
171 |
}
|
|
|
172 |
enableButtonTooltip(button);
|
|
|
173 |
});
|
|
|
174 |
};
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Enable the button tooltips.
|
|
|
178 |
*
|
|
|
179 |
* @param {HTMLElement} button the button element
|
|
|
180 |
* @private
|
|
|
181 |
*/
|
|
|
182 |
const enableButtonTooltip = (button) => {
|
|
|
183 |
// The jQuery is still used in Boostrap 4. It can we removed when MDL-71979 is integrated.
|
|
|
184 |
if (button.hasAttribute('data-original-title')) {
|
|
|
185 |
jQuery(button).tooltip('enable');
|
|
|
186 |
button.removeAttribute('title');
|
|
|
187 |
} else if (button.dataset.disabledToggle) {
|
|
|
188 |
button.dataset.toggle = button.dataset.disabledToggle;
|
|
|
189 |
jQuery(button).tooltip();
|
|
|
190 |
}
|
|
|
191 |
delete button.dataset.restoreTooltipOnBlur;
|
|
|
192 |
};
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Add scroll listeners to a drawer element.
|
|
|
196 |
*
|
|
|
197 |
* @param {HTMLElement} drawerNode the drawer main node
|
|
|
198 |
* @private
|
|
|
199 |
*/
|
|
|
200 |
const addInnerScrollListener = (drawerNode) => {
|
|
|
201 |
const content = drawerNode.querySelector(SELECTORS.DRAWERCONTENT);
|
|
|
202 |
if (!content) {
|
|
|
203 |
return;
|
|
|
204 |
}
|
|
|
205 |
content.addEventListener("scroll", () => {
|
|
|
206 |
drawerNode.classList.toggle(
|
|
|
207 |
CLASSES.SCROLLED,
|
|
|
208 |
content.scrollTop != 0
|
|
|
209 |
);
|
|
|
210 |
});
|
|
|
211 |
};
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* The Drawers class is used to control on-screen drawer elements.
|
|
|
215 |
*
|
|
|
216 |
* It handles opening, and closing of drawer elements, as well as more detailed behaviours such as closing a drawer when
|
|
|
217 |
* another drawer is opened, and supports closing a drawer when the screen is resized.
|
|
|
218 |
*
|
|
|
219 |
* Drawers are instantiated on page load, and can also be toggled lazily when toggling any drawer toggle, open button,
|
|
|
220 |
* or close button.
|
|
|
221 |
*
|
|
|
222 |
* A range of show and hide events are also dispatched as detailed in the class
|
|
|
223 |
* {@link module:theme_monocolor/drawers#eventTypes eventTypes} object.
|
|
|
224 |
*
|
|
|
225 |
* @example <caption>Standard usage</caption>
|
|
|
226 |
*
|
|
|
227 |
* // The module just needs to be included to add drawer support.
|
|
|
228 |
* import 'theme_monocolor/drawers';
|
|
|
229 |
*
|
|
|
230 |
* @example <caption>Manually open or close any drawer</caption>
|
|
|
231 |
*
|
|
|
232 |
* import Drawers from 'theme_monocolor/drawers';
|
|
|
233 |
*
|
|
|
234 |
* const myDrawer = Drawers.getDrawerInstanceForNode(document.querySelector('.myDrawerNode');
|
|
|
235 |
* myDrawer.closeDrawer();
|
|
|
236 |
*
|
|
|
237 |
* @example <caption>Listen to the before show event and cancel it</caption>
|
|
|
238 |
*
|
|
|
239 |
* import Drawers from 'theme_monocolor/drawers';
|
|
|
240 |
*
|
|
|
241 |
* document.addEventListener(Drawers.eventTypes.drawerShow, e => {
|
|
|
242 |
* // The drawer which will be shown.
|
|
|
243 |
* window.console.log(e.target);
|
|
|
244 |
*
|
|
|
245 |
* // The instance of the Drawers class for this drawer.
|
|
|
246 |
* window.console.log(e.detail.drawerInstance);
|
|
|
247 |
*
|
|
|
248 |
* // Prevent this drawer from being shown.
|
|
|
249 |
* e.preventDefault();
|
|
|
250 |
* });
|
|
|
251 |
*
|
|
|
252 |
* @example <caption>Listen to the shown event</caption>
|
|
|
253 |
*
|
|
|
254 |
* document.addEventListener(Drawers.eventTypes.drawerShown, e => {
|
|
|
255 |
* // The drawer which was shown.
|
|
|
256 |
* window.console.log(e.target);
|
|
|
257 |
*
|
|
|
258 |
* // The instance of the Drawers class for this drawer.
|
|
|
259 |
* window.console.log(e.detail.drawerInstance);
|
|
|
260 |
* });
|
|
|
261 |
*/
|
|
|
262 |
export default class Drawers {
|
|
|
263 |
/**
|
|
|
264 |
* The underlying HTMLElement which is controlled.
|
|
|
265 |
*/
|
|
|
266 |
drawerNode = null;
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* The drawer page bounding box dimensions.
|
|
|
270 |
* @var {DOMRect} boundingRect
|
|
|
271 |
*/
|
|
|
272 |
boundingRect = null;
|
|
|
273 |
|
|
|
274 |
constructor(drawerNode) {
|
|
|
275 |
// Some behat tests may use fake drawer divs to test components in drawers.
|
|
|
276 |
if (drawerNode.dataset.behatFakeDrawer !== undefined) {
|
|
|
277 |
return;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
this.drawerNode = drawerNode;
|
|
|
281 |
|
|
|
282 |
if (isSmall()) {
|
|
|
283 |
this.closeDrawer({focusOnOpenButton: false, updatePreferences: false});
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
if (this.drawerNode.classList.contains(CLASSES.SHOW)) {
|
|
|
287 |
this.openDrawer({focusOnCloseButton: false});
|
|
|
288 |
} else if (this.drawerNode.dataset.forceopen == 1) {
|
|
|
289 |
if (!isSmall()) {
|
|
|
290 |
this.openDrawer({focusOnCloseButton: false});
|
|
|
291 |
}
|
|
|
292 |
} else {
|
|
|
293 |
Aria.hide(this.drawerNode);
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
// Disable tooltips in small screens.
|
|
|
297 |
if (isSmall()) {
|
|
|
298 |
disableDrawerTooltips(this.drawerNode);
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
addInnerScrollListener(this.drawerNode);
|
|
|
302 |
|
|
|
303 |
drawerMap.set(drawerNode, this);
|
|
|
304 |
|
|
|
305 |
drawerNode.classList.remove(CLASSES.NOTINITIALISED);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Whether the drawer is open.
|
|
|
310 |
*
|
|
|
311 |
* @returns {boolean}
|
|
|
312 |
*/
|
|
|
313 |
get isOpen() {
|
|
|
314 |
return this.drawerNode.classList.contains(CLASSES.SHOW);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
/**
|
|
|
318 |
* Whether the drawer should close when the window is resized
|
|
|
319 |
*
|
|
|
320 |
* @returns {boolean}
|
|
|
321 |
*/
|
|
|
322 |
get closeOnResize() {
|
|
|
323 |
return !!parseInt(this.drawerNode.dataset.closeOnResize);
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* The list of event types.
|
|
|
328 |
*
|
|
|
329 |
* @static
|
|
|
330 |
* @property {String} drawerShow See {@link event:theme_monocolor/drawers:show}
|
|
|
331 |
* @property {String} drawerShown See {@link event:theme_monocolor/drawers:shown}
|
|
|
332 |
* @property {String} drawerHide See {@link event:theme_monocolor/drawers:hide}
|
|
|
333 |
* @property {String} drawerHidden See {@link event:theme_monocolor/drawers:hidden}
|
|
|
334 |
*/
|
|
|
335 |
static eventTypes = {
|
|
|
336 |
/**
|
|
|
337 |
* An event triggered before a drawer is shown.
|
|
|
338 |
*
|
|
|
339 |
* @event theme_monocolor/drawers:show
|
|
|
340 |
* @type {CustomEvent}
|
|
|
341 |
* @property {HTMLElement} target The drawer that will be opened.
|
|
|
342 |
*/
|
|
|
343 |
drawerShow: 'theme_monocolor/drawers:show',
|
|
|
344 |
|
|
|
345 |
/**
|
|
|
346 |
* An event triggered after a drawer is shown.
|
|
|
347 |
*
|
|
|
348 |
* @event theme_monocolor/drawers:shown
|
|
|
349 |
* @type {CustomEvent}
|
|
|
350 |
* @property {HTMLElement} target The drawer that was be opened.
|
|
|
351 |
*/
|
|
|
352 |
drawerShown: 'theme_monocolor/drawers:shown',
|
|
|
353 |
|
|
|
354 |
/**
|
|
|
355 |
* An event triggered before a drawer is hidden.
|
|
|
356 |
*
|
|
|
357 |
* @event theme_monocolor/drawers:hide
|
|
|
358 |
* @type {CustomEvent}
|
|
|
359 |
* @property {HTMLElement} target The drawer that will be hidden.
|
|
|
360 |
*/
|
|
|
361 |
drawerHide: 'theme_monocolor/drawers:hide',
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* An event triggered after a drawer is hidden.
|
|
|
365 |
*
|
|
|
366 |
* @event theme_monocolor/drawers:hidden
|
|
|
367 |
* @type {CustomEvent}
|
|
|
368 |
* @property {HTMLElement} target The drawer that was be hidden.
|
|
|
369 |
*/
|
|
|
370 |
drawerHidden: 'theme_monocolor/drawers:hidden',
|
|
|
371 |
};
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
/**
|
|
|
375 |
* Get the drawer instance for the specified node
|
|
|
376 |
*
|
|
|
377 |
* @param {HTMLElement} drawerNode
|
|
|
378 |
* @returns {module:theme_monocolor/drawers}
|
|
|
379 |
*/
|
|
|
380 |
static getDrawerInstanceForNode(drawerNode) {
|
|
|
381 |
if (!drawerMap.has(drawerNode)) {
|
|
|
382 |
new Drawers(drawerNode);
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
return drawerMap.get(drawerNode);
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
/**
|
|
|
389 |
* Dispatch a drawer event.
|
|
|
390 |
*
|
|
|
391 |
* @param {string} eventname the event name
|
|
|
392 |
* @param {boolean} cancelable if the event is cancelable
|
|
|
393 |
* @returns {CustomEvent} the resulting custom event
|
|
|
394 |
*/
|
|
|
395 |
dispatchEvent(eventname, cancelable = false) {
|
|
|
396 |
return dispatchEvent(
|
|
|
397 |
eventname,
|
|
|
398 |
{
|
|
|
399 |
drawerInstance: this,
|
|
|
400 |
},
|
|
|
401 |
this.drawerNode,
|
|
|
402 |
{
|
|
|
403 |
cancelable,
|
|
|
404 |
}
|
|
|
405 |
);
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
/**
|
|
|
409 |
* Open the drawer.
|
|
|
410 |
*
|
|
|
411 |
* By default, openDrawer sets the page focus to the close drawer button. However, when a drawer is open at page
|
|
|
412 |
* load, this represents an accessibility problem as the initial focus changes without any user interaction. The
|
|
|
413 |
* focusOnCloseButton parameter can be set to false to prevent this behaviour.
|
|
|
414 |
*
|
|
|
415 |
* @param {object} args
|
|
|
416 |
* @param {boolean} [args.focusOnCloseButton=true] Whether to alter page focus when opening the drawer
|
|
|
417 |
*/
|
|
|
418 |
openDrawer({focusOnCloseButton = true} = {}) {
|
|
|
419 |
|
|
|
420 |
const pendingPromise = new Pending('theme_monocolor/drawers:open');
|
|
|
421 |
const showEvent = this.dispatchEvent(Drawers.eventTypes.drawerShow, true);
|
|
|
422 |
if (showEvent.defaultPrevented) {
|
|
|
423 |
return;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
// Hide close button and header content while the drawer is showing to prevent glitchy effects.
|
|
|
427 |
this.drawerNode.querySelector(SELECTORS.CLOSEBTN)?.classList.toggle('hidden', true);
|
|
|
428 |
this.drawerNode.querySelector(SELECTORS.HEADERCONTENT)?.classList.toggle('hidden', true);
|
|
|
429 |
|
|
|
430 |
|
|
|
431 |
// Remove open tooltip if still visible.
|
|
|
432 |
let openButton = getDrawerOpenButton(this.drawerNode.id);
|
|
|
433 |
if (openButton && openButton.hasAttribute('data-original-title')) {
|
|
|
434 |
// The jQuery is still used in Boostrap 4. It can we removed when MDL-71979 is integrated.
|
|
|
435 |
jQuery(openButton)?.tooltip('hide');
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
Aria.unhide(this.drawerNode);
|
|
|
439 |
this.drawerNode.classList.add(CLASSES.SHOW);
|
|
|
440 |
|
|
|
441 |
const preference = this.drawerNode.dataset.preference;
|
|
|
442 |
if (preference && !isSmall() && (this.drawerNode.dataset.forceopen != 1)) {
|
|
|
443 |
setUserPreference(preference, true);
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
const state = this.drawerNode.dataset.state;
|
|
|
447 |
if (state) {
|
|
|
448 |
const page = document.getElementById('page');
|
|
|
449 |
page.classList.add(state);
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
this.boundingRect = this.drawerNode.getBoundingClientRect();
|
|
|
453 |
|
|
|
454 |
if (isSmall()) {
|
|
|
455 |
getBackdrop().then(backdrop => {
|
|
|
456 |
backdrop.show();
|
|
|
457 |
|
|
|
458 |
const pageWrapper = document.getElementById('page');
|
|
|
459 |
pageWrapper.style.overflow = 'hidden';
|
|
|
460 |
return backdrop;
|
|
|
461 |
})
|
|
|
462 |
.catch();
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
// Show close button and header content once the drawer is fully opened.
|
|
|
466 |
const closeButton = this.drawerNode.querySelector(SELECTORS.CLOSEBTN);
|
|
|
467 |
const headerContent = this.drawerNode.querySelector(SELECTORS.HEADERCONTENT);
|
|
|
468 |
if (focusOnCloseButton && closeButton) {
|
|
|
469 |
disableButtonTooltip(closeButton, true);
|
|
|
470 |
}
|
|
|
471 |
setTimeout(() => {
|
|
|
472 |
closeButton.classList.toggle('hidden', false);
|
|
|
473 |
headerContent.classList.toggle('hidden', false);
|
|
|
474 |
if (focusOnCloseButton) {
|
|
|
475 |
closeButton.focus();
|
|
|
476 |
}
|
|
|
477 |
pendingPromise.resolve();
|
|
|
478 |
}, 300);
|
|
|
479 |
|
|
|
480 |
this.dispatchEvent(Drawers.eventTypes.drawerShown);
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
/**
|
|
|
484 |
* Close the drawer.
|
|
|
485 |
*
|
|
|
486 |
* @param {object} args
|
|
|
487 |
* @param {boolean} [args.focusOnOpenButton=true] Whether to alter page focus when opening the drawer
|
|
|
488 |
* @param {boolean} [args.updatePreferences=true] Whether to update the user prewference
|
|
|
489 |
*/
|
|
|
490 |
closeDrawer({focusOnOpenButton = true, updatePreferences = true} = {}) {
|
|
|
491 |
|
|
|
492 |
const pendingPromise = new Pending('theme_monocolor/drawers:close');
|
|
|
493 |
|
|
|
494 |
const hideEvent = this.dispatchEvent(Drawers.eventTypes.drawerHide, true);
|
|
|
495 |
if (hideEvent.defaultPrevented) {
|
|
|
496 |
return;
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
// Hide close button and header content while the drawer is hiding to prevent glitchy effects.
|
|
|
500 |
const closeButton = this.drawerNode.querySelector(SELECTORS.CLOSEBTN);
|
|
|
501 |
closeButton?.classList.toggle('hidden', true);
|
|
|
502 |
const headerContent = this.drawerNode.querySelector(SELECTORS.HEADERCONTENT);
|
|
|
503 |
headerContent?.classList.toggle('hidden', true);
|
|
|
504 |
// Remove the close button tooltip if visible.
|
|
|
505 |
if (closeButton.hasAttribute('data-original-title')) {
|
|
|
506 |
// The jQuery is still used in Boostrap 4. It can we removed when MDL-71979 is integrated.
|
|
|
507 |
jQuery(closeButton)?.tooltip('hide');
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
const preference = this.drawerNode.dataset.preference;
|
|
|
511 |
if (preference && updatePreferences && !isSmall()) {
|
|
|
512 |
setUserPreference(preference, false);
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
const state = this.drawerNode.dataset.state;
|
|
|
516 |
if (state) {
|
|
|
517 |
const page = document.getElementById('page');
|
|
|
518 |
page.classList.remove(state);
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
Aria.hide(this.drawerNode);
|
|
|
522 |
this.drawerNode.classList.remove(CLASSES.SHOW);
|
|
|
523 |
|
|
|
524 |
getBackdrop().then(backdrop => {
|
|
|
525 |
backdrop.hide();
|
|
|
526 |
|
|
|
527 |
if (isSmall()) {
|
|
|
528 |
const pageWrapper = document.getElementById('page');
|
|
|
529 |
pageWrapper.style.overflow = 'visible';
|
|
|
530 |
}
|
|
|
531 |
return backdrop;
|
|
|
532 |
})
|
|
|
533 |
.catch();
|
|
|
534 |
|
|
|
535 |
// Move focus to the open drawer (or toggler) button once the drawer is hidden.
|
|
|
536 |
let openButton = getDrawerOpenButton(this.drawerNode.id);
|
|
|
537 |
if (openButton) {
|
|
|
538 |
disableButtonTooltip(openButton, true);
|
|
|
539 |
}
|
|
|
540 |
setTimeout(() => {
|
|
|
541 |
if (openButton && focusOnOpenButton) {
|
|
|
542 |
openButton.focus();
|
|
|
543 |
}
|
|
|
544 |
pendingPromise.resolve();
|
|
|
545 |
}, 300);
|
|
|
546 |
|
|
|
547 |
this.dispatchEvent(Drawers.eventTypes.drawerHidden);
|
|
|
548 |
}
|
|
|
549 |
|
|
|
550 |
/**
|
|
|
551 |
* Toggle visibility of the drawer.
|
|
|
552 |
*/
|
|
|
553 |
toggleVisibility() {
|
|
|
554 |
if (this.drawerNode.classList.contains(CLASSES.SHOW)) {
|
|
|
555 |
this.closeDrawer();
|
|
|
556 |
} else {
|
|
|
557 |
this.openDrawer();
|
|
|
558 |
}
|
|
|
559 |
}
|
|
|
560 |
|
|
|
561 |
/**
|
|
|
562 |
* Displaces the drawer outsite the page.
|
|
|
563 |
*
|
|
|
564 |
* @param {Number} scrollPosition the page current scroll position
|
|
|
565 |
*/
|
|
|
566 |
displace(scrollPosition) {
|
|
|
567 |
let displace = scrollPosition;
|
|
|
568 |
let openButton = getDrawerOpenButton(this.drawerNode.id);
|
|
|
569 |
if (scrollPosition === 0) {
|
|
|
570 |
this.drawerNode.style.transform = '';
|
|
|
571 |
if (openButton) {
|
|
|
572 |
openButton.style.transform = '';
|
|
|
573 |
}
|
|
|
574 |
return;
|
|
|
575 |
}
|
|
|
576 |
const state = this.drawerNode.dataset?.state;
|
|
|
577 |
const drawrWidth = this.drawerNode.offsetWidth;
|
|
|
578 |
let scrollThreshold = drawrWidth;
|
|
|
579 |
let direction = -1;
|
|
|
580 |
if (state === 'show-drawer-right') {
|
|
|
581 |
direction = 1;
|
|
|
582 |
scrollThreshold = THRESHOLD;
|
|
|
583 |
}
|
|
|
584 |
// LTR scroll is positive while RTL scroll is negative.
|
|
|
585 |
if (Math.abs(scrollPosition) > scrollThreshold) {
|
|
|
586 |
displace = Math.sign(scrollPosition) * (drawrWidth + THRESHOLD);
|
|
|
587 |
}
|
|
|
588 |
displace *= direction;
|
|
|
589 |
const transform = `translateX(${displace}px)`;
|
|
|
590 |
if (openButton) {
|
|
|
591 |
openButton.style.transform = transform;
|
|
|
592 |
}
|
|
|
593 |
this.drawerNode.style.transform = transform;
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
/**
|
|
|
597 |
* Prevent drawer from overlapping an element.
|
|
|
598 |
*
|
|
|
599 |
* @param {HTMLElement} currentFocus
|
|
|
600 |
*/
|
|
|
601 |
preventOverlap(currentFocus) {
|
|
|
602 |
// Start position drawer (aka. left drawer) will never overlap with the page content.
|
|
|
603 |
if (!this.isOpen || this.drawerNode.dataset?.state === 'show-drawer-left') {
|
|
|
604 |
return;
|
|
|
605 |
}
|
|
|
606 |
const drawrWidth = this.drawerNode.offsetWidth;
|
|
|
607 |
const element = currentFocus.getBoundingClientRect();
|
|
|
608 |
|
|
|
609 |
// The this.boundingRect is calculated only once and it is reliable
|
|
|
610 |
// for horizontal overlapping (which is the most common). However,
|
|
|
611 |
// it is not reliable for vertical overlapping because the drawer
|
|
|
612 |
// height can be changed by other elements like sticky footer.
|
|
|
613 |
// To prevent recalculating the boundingRect on every
|
|
|
614 |
// focusin event, we use horizontal overlapping as first fast check.
|
|
|
615 |
let overlapping = (
|
|
|
616 |
(element.right + THRESHOLD) > this.boundingRect.left &&
|
|
|
617 |
(element.left - THRESHOLD) < this.boundingRect.right
|
|
|
618 |
);
|
|
|
619 |
if (overlapping) {
|
|
|
620 |
const currentBoundingRect = this.drawerNode.getBoundingClientRect();
|
|
|
621 |
overlapping = (
|
|
|
622 |
(element.bottom) > currentBoundingRect.top &&
|
|
|
623 |
(element.top) < currentBoundingRect.bottom
|
|
|
624 |
);
|
|
|
625 |
}
|
|
|
626 |
|
|
|
627 |
if (overlapping) {
|
|
|
628 |
// Force drawer to displace out of the page.
|
|
|
629 |
let displaceOut = drawrWidth + 1;
|
|
|
630 |
if (window.right_to_left()) {
|
|
|
631 |
displaceOut *= -1;
|
|
|
632 |
}
|
|
|
633 |
this.displace(displaceOut);
|
|
|
634 |
} else {
|
|
|
635 |
// Reset drawer displacement.
|
|
|
636 |
this.displace(window.scrollX);
|
|
|
637 |
}
|
|
|
638 |
}
|
|
|
639 |
|
|
|
640 |
/**
|
|
|
641 |
* Close all drawers.
|
|
|
642 |
*/
|
|
|
643 |
static closeAllDrawers() {
|
|
|
644 |
drawerMap.forEach(drawerInstance => {
|
|
|
645 |
drawerInstance.closeDrawer();
|
|
|
646 |
});
|
|
|
647 |
}
|
|
|
648 |
|
|
|
649 |
/**
|
|
|
650 |
* Close all drawers except for the specified drawer.
|
|
|
651 |
*
|
|
|
652 |
* @param {module:theme_monocolor/drawers} comparisonInstance
|
|
|
653 |
*/
|
|
|
654 |
static closeOtherDrawers(comparisonInstance) {
|
|
|
655 |
drawerMap.forEach(drawerInstance => {
|
|
|
656 |
if (drawerInstance === comparisonInstance) {
|
|
|
657 |
return;
|
|
|
658 |
}
|
|
|
659 |
|
|
|
660 |
drawerInstance.closeDrawer();
|
|
|
661 |
});
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
/**
|
|
|
665 |
* Prevent drawers from covering the focused element.
|
|
|
666 |
*/
|
|
|
667 |
static preventCoveringFocusedElement() {
|
|
|
668 |
const currentFocus = document.activeElement;
|
|
|
669 |
// Focus on page layout elements should be ignored.
|
|
|
670 |
const pagecontent = document.querySelector(SELECTORS.PAGECONTENT);
|
|
|
671 |
if (!currentFocus || !pagecontent?.contains(currentFocus)) {
|
|
|
672 |
Drawers.displaceDrawers(window.scrollX);
|
|
|
673 |
return;
|
|
|
674 |
}
|
|
|
675 |
drawerMap.forEach(drawerInstance => {
|
|
|
676 |
drawerInstance.preventOverlap(currentFocus);
|
|
|
677 |
});
|
|
|
678 |
}
|
|
|
679 |
|
|
|
680 |
/**
|
|
|
681 |
* Prevent drawer from covering the content when the page content covers the full page.
|
|
|
682 |
*
|
|
|
683 |
* @param {Number} displace
|
|
|
684 |
*/
|
|
|
685 |
static displaceDrawers(displace) {
|
|
|
686 |
drawerMap.forEach(drawerInstance => {
|
|
|
687 |
drawerInstance.displace(displace);
|
|
|
688 |
});
|
|
|
689 |
}
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
/**
|
|
|
693 |
* Set the last used attribute for the last used toggle button for a drawer.
|
|
|
694 |
*
|
|
|
695 |
* @param {object} toggleButton The clicked button.
|
|
|
696 |
*/
|
|
|
697 |
const setLastUsedToggle = (toggleButton) => {
|
|
|
698 |
if (toggleButton.dataset.target) {
|
|
|
699 |
document.querySelectorAll(`${SELECTORS.BUTTONS}[data-target="${toggleButton.dataset.target}"]`)
|
|
|
700 |
.forEach(btn => {
|
|
|
701 |
btn.dataset.lastused = false;
|
|
|
702 |
});
|
|
|
703 |
toggleButton.dataset.lastused = true;
|
|
|
704 |
}
|
|
|
705 |
};
|
|
|
706 |
|
|
|
707 |
/**
|
|
|
708 |
* Set the focus to the last used button to open this drawer.
|
|
|
709 |
* @param {string} target The drawer target.
|
|
|
710 |
*/
|
|
|
711 |
const focusLastUsedToggle = (target) => {
|
|
|
712 |
const lastUsedButton = document.querySelector(`${SELECTORS.BUTTONS}[data-target="${target}"][data-lastused="true"`);
|
|
|
713 |
if (lastUsedButton) {
|
|
|
714 |
lastUsedButton.focus();
|
|
|
715 |
}
|
|
|
716 |
};
|
|
|
717 |
|
|
|
718 |
/**
|
|
|
719 |
* Register the event listeners for the drawer.
|
|
|
720 |
*
|
|
|
721 |
* @private
|
|
|
722 |
*/
|
|
|
723 |
const registerListeners = () => {
|
|
|
724 |
// Listen for show/hide events.
|
|
|
725 |
document.addEventListener('click', e => {
|
|
|
726 |
const toggleButton = e.target.closest(SELECTORS.TOGGLEBTN);
|
|
|
727 |
if (toggleButton && toggleButton.dataset.target) {
|
|
|
728 |
e.preventDefault();
|
|
|
729 |
const targetDrawer = document.getElementById(toggleButton.dataset.target);
|
|
|
730 |
const drawerInstance = Drawers.getDrawerInstanceForNode(targetDrawer);
|
|
|
731 |
setLastUsedToggle(toggleButton);
|
|
|
732 |
|
|
|
733 |
drawerInstance.toggleVisibility();
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
const openDrawerButton = e.target.closest(SELECTORS.OPENBTN);
|
|
|
737 |
if (openDrawerButton && openDrawerButton.dataset.target) {
|
|
|
738 |
e.preventDefault();
|
|
|
739 |
const targetDrawer = document.getElementById(openDrawerButton.dataset.target);
|
|
|
740 |
const drawerInstance = Drawers.getDrawerInstanceForNode(targetDrawer);
|
|
|
741 |
setLastUsedToggle(toggleButton);
|
|
|
742 |
|
|
|
743 |
drawerInstance.openDrawer();
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
const closeDrawerButton = e.target.closest(SELECTORS.CLOSEBTN);
|
|
|
747 |
if (closeDrawerButton && closeDrawerButton.dataset.target) {
|
|
|
748 |
e.preventDefault();
|
|
|
749 |
const targetDrawer = document.getElementById(closeDrawerButton.dataset.target);
|
|
|
750 |
const drawerInstance = Drawers.getDrawerInstanceForNode(targetDrawer);
|
|
|
751 |
|
|
|
752 |
drawerInstance.closeDrawer();
|
|
|
753 |
focusLastUsedToggle(closeDrawerButton.dataset.target);
|
|
|
754 |
}
|
|
|
755 |
});
|
|
|
756 |
|
|
|
757 |
// Close drawer when another drawer opens.
|
|
|
758 |
document.addEventListener(Drawers.eventTypes.drawerShow, e => {
|
|
|
759 |
if (isLarge()) {
|
|
|
760 |
return;
|
|
|
761 |
}
|
|
|
762 |
Drawers.closeOtherDrawers(e.detail.drawerInstance);
|
|
|
763 |
});
|
|
|
764 |
|
|
|
765 |
// Tooglers and openers blur listeners.
|
|
|
766 |
const btnSelector = `${SELECTORS.TOGGLEBTN}, ${SELECTORS.OPENBTN}, ${SELECTORS.CLOSEBTN}`;
|
|
|
767 |
document.addEventListener('focusout', (e) => {
|
|
|
768 |
const button = e.target.closest(btnSelector);
|
|
|
769 |
if (button?.dataset.restoreTooltipOnBlur !== undefined) {
|
|
|
770 |
enableButtonTooltip(button);
|
|
|
771 |
}
|
|
|
772 |
});
|
|
|
773 |
|
|
|
774 |
const closeOnResizeListener = () => {
|
|
|
775 |
if (isSmall()) {
|
|
|
776 |
let anyOpen = false;
|
|
|
777 |
drawerMap.forEach(drawerInstance => {
|
|
|
778 |
disableDrawerTooltips(drawerInstance.drawerNode);
|
|
|
779 |
if (drawerInstance.isOpen) {
|
|
|
780 |
if (drawerInstance.closeOnResize) {
|
|
|
781 |
drawerInstance.closeDrawer();
|
|
|
782 |
} else {
|
|
|
783 |
anyOpen = true;
|
|
|
784 |
}
|
|
|
785 |
}
|
|
|
786 |
});
|
|
|
787 |
|
|
|
788 |
if (anyOpen) {
|
|
|
789 |
getBackdrop().then(backdrop => backdrop.show()).catch();
|
|
|
790 |
}
|
|
|
791 |
} else {
|
|
|
792 |
drawerMap.forEach(drawerInstance => {
|
|
|
793 |
enableDrawerTooltips(drawerInstance.drawerNode);
|
|
|
794 |
});
|
|
|
795 |
getBackdrop().then(backdrop => backdrop.hide()).catch();
|
|
|
796 |
}
|
|
|
797 |
};
|
|
|
798 |
|
|
|
799 |
document.addEventListener('scroll', () => {
|
|
|
800 |
const body = document.querySelector('body');
|
|
|
801 |
if (window.scrollY >= window.innerHeight) {
|
|
|
802 |
body.classList.add(CLASSES.SCROLLED);
|
|
|
803 |
} else {
|
|
|
804 |
body.classList.remove(CLASSES.SCROLLED);
|
|
|
805 |
}
|
|
|
806 |
// Horizontal scroll listener to displace the drawers to prevent covering
|
|
|
807 |
// any possible sticky content.
|
|
|
808 |
Drawers.displaceDrawers(window.scrollX);
|
|
|
809 |
});
|
|
|
810 |
|
|
|
811 |
const preventOverlap = debounce(Drawers.preventCoveringFocusedElement, 100);
|
|
|
812 |
document.addEventListener('focusin', preventOverlap);
|
|
|
813 |
document.addEventListener('focusout', preventOverlap);
|
|
|
814 |
|
|
|
815 |
window.addEventListener('resize', debounce(closeOnResizeListener, 400));
|
|
|
816 |
};
|
|
|
817 |
|
|
|
818 |
registerListeners();
|
|
|
819 |
|
|
|
820 |
const drawers = document.querySelectorAll(SELECTORS.DRAWERS);
|
|
|
821 |
drawers.forEach(drawerNode => Drawers.getDrawerInstanceForNode(drawerNode));
|