2019-12-16 23:47:54 +00:00
|
|
|
var $drawerContainer = $( document.body ),
|
2020-01-13 23:32:05 +00:00
|
|
|
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
|
2020-01-13 23:32:05 +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.
|
|
|
|
setTimeout( function () {
|
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 ) {
|
|
|
|
$( window ).one( 'scroll.drawer', function () {
|
|
|
|
drawer.hide();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
displayDrawer: displayDrawer,
|
|
|
|
lockScroll: lockScroll,
|
|
|
|
discardDrawer: discardDrawer
|
|
|
|
};
|