2024-03-24 20:53:43 +00:00
|
|
|
const $drawerContainer = $( document.body );
|
|
|
|
const BODY_CLASS_SCROLL_LOCKED = 'has-drawer--with-scroll-locked';
|
2019-12-16 23:47:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Discard a drawer from display on the page.
|
2020-06-02 21:21:44 +00:00
|
|
|
*
|
2019-12-16 23:47:54 +00:00
|
|
|
* @ignore
|
|
|
|
* @param {Drawer} drawer
|
|
|
|
*/
|
|
|
|
function discardDrawer( drawer ) {
|
|
|
|
// remove the class
|
2024-03-29 09:27:19 +00:00
|
|
|
$drawerContainer.removeClass( BODY_CLASS_SCROLL_LOCKED );
|
2019-12-16 23:47:54 +00:00
|
|
|
// FIXME: queue removal from DOM (using setTimeout so that any animations have time to run)
|
|
|
|
// This works around an issue in MobileFrontend that the Drawer onBeforeHide method is
|
|
|
|
// called /before/ the animation for closing has completed. This needs to be accounted
|
|
|
|
// for in Drawer so this function can be synchronous.
|
2024-06-03 12:06:43 +00:00
|
|
|
setTimeout( () => {
|
2020-01-14 19:38:41 +00:00
|
|
|
// detach the node from the DOM. Use detach rather than remove to allow reuse without
|
|
|
|
// losing any existing events.
|
|
|
|
drawer.$el.detach();
|
|
|
|
}, 100 );
|
2019-12-16 23:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock scroll of viewport.
|
|
|
|
*/
|
|
|
|
function lockScroll() {
|
|
|
|
$drawerContainer.addClass( BODY_CLASS_SCROLL_LOCKED );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Drawer} drawer to display
|
|
|
|
* @param {Object} options for display
|
|
|
|
* @param {boolean} options.hideOnScroll whether a scroll closes the drawer
|
|
|
|
*/
|
|
|
|
function displayDrawer( drawer, options ) {
|
|
|
|
$drawerContainer.append( drawer.$el );
|
|
|
|
drawer.show();
|
|
|
|
if ( options.hideOnScroll ) {
|
2024-06-03 12:06:43 +00:00
|
|
|
$( window ).one( 'scroll.drawer', () => {
|
2019-12-16 23:47:54 +00:00
|
|
|
drawer.hide();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
displayDrawer: displayDrawer,
|
|
|
|
lockScroll: lockScroll,
|
|
|
|
discardDrawer: discardDrawer
|
|
|
|
};
|