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
|
|
|
*
|
2024-06-26 13:23:40 +00:00
|
|
|
* @private
|
2019-12-16 23:47:54 +00:00
|
|
|
* @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.
|
2024-07-27 05:52:33 +00:00
|
|
|
*
|
2024-06-26 13:23:40 +00:00
|
|
|
* @ignore
|
2019-12-16 23:47:54 +00:00
|
|
|
*/
|
|
|
|
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
|
2024-06-26 13:23:40 +00:00
|
|
|
* @ignore
|
2019-12-16 23:47:54 +00:00
|
|
|
*/
|
|
|
|
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 = {
|
2024-06-26 13:23:40 +00:00
|
|
|
displayDrawer,
|
|
|
|
lockScroll,
|
|
|
|
discardDrawer
|
2019-12-16 23:47:54 +00:00
|
|
|
};
|