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