2017-08-11 17:59:31 +03:00
import BaseLayout from "./baseLayout" ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
const Drawer = ( $ => {
2016-01-26 21:12:48 +03:00
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Constants
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2017-08-11 17:59:31 +03:00
const NAME = "drawer" ;
const DATA _KEY = ` bmd. ${ NAME } ` ;
const JQUERY _NAME = ` bmd ${ NAME . charAt ( 0 ) . toUpperCase ( ) + NAME . slice ( 1 ) } ` ;
const JQUERY _NO _CONFLICT = $ . fn [ JQUERY _NAME ] ;
2016-01-26 21:12:48 +03:00
const Keycodes = {
ESCAPE : 27
//ENTER: 13,
//SPACE: 32
2017-08-11 17:59:31 +03:00
} ;
2016-01-26 21:12:48 +03:00
const ClassName = {
2017-08-11 17:59:31 +03:00
IN : "in" ,
2016-03-21 17:56:51 +03:00
DRAWER _IN : ` bmd-drawer-in ` ,
DRAWER _OUT : ` bmd-drawer-out ` ,
2017-08-11 17:59:31 +03:00
DRAWER : "bmd-layout-drawer" ,
CONTAINER : "bmd-layout-container"
} ;
2016-01-26 21:12:48 +03:00
const Default = {
focusSelector : ` a, button, input `
2017-08-11 17:59:31 +03:00
} ;
2016-01-26 21:12:48 +03:00
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Class Definition
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
class Drawer extends BaseLayout {
// $element is expected to be the trigger
2016-03-21 17:56:51 +03:00
// i.e. <button class="btn bmd-btn-icon" for="search" data-toggle="drawer" data-target="#my-side-nav-drawer" aria-expanded="false" aria-controls="my-side-nav-drawer">
2016-01-26 21:12:48 +03:00
constructor ( $element , config ) {
2017-08-11 17:59:31 +03:00
super ( $element , $ . extend ( true , { } , Default , config ) ) ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
this . $toggles = $ (
` [data-toggle="drawer"][href="# ${ this . $element [ 0 ]
. id } "], [data-toggle=" drawer "][data-target=" # $ { this . $element [ 0 ] . id } " ] `
) ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
this . _addAria ( ) ;
2016-01-26 21:12:48 +03:00
// click or escape on the backdrop closes the drawer
2017-08-11 17:59:31 +03:00
this . $backdrop
. keydown ( ev => {
if ( ev . which === Keycodes . ESCAPE ) {
this . hide ( ) ;
}
} )
. click ( ( ) => {
this . hide ( ) ;
} ) ;
2016-01-26 21:12:48 +03:00
// escape on the drawer closes it
2017-08-11 17:59:31 +03:00
this . $element . keydown ( ev => {
2016-01-26 21:12:48 +03:00
if ( ev . which === Keycodes . ESCAPE ) {
2017-08-11 17:59:31 +03:00
this . hide ( ) ;
2016-01-26 21:12:48 +03:00
}
2017-08-11 17:59:31 +03:00
} ) ;
2016-01-26 21:12:48 +03:00
// any toggle button clicks
this . $toggles . click ( ( ) => {
2017-08-11 17:59:31 +03:00
this . toggle ( ) ;
} ) ;
2016-01-26 21:12:48 +03:00
}
dispose ( ) {
2017-08-11 17:59:31 +03:00
super . dispose ( DATA _KEY ) ;
this . $toggles = null ;
2016-01-26 21:12:48 +03:00
}
toggle ( ) {
if ( this . _isOpen ( ) ) {
2017-08-11 17:59:31 +03:00
this . hide ( ) ;
2016-01-26 21:12:48 +03:00
} else {
2017-08-11 17:59:31 +03:00
this . show ( ) ;
2016-01-26 21:12:48 +03:00
}
}
show ( ) {
if ( this . _isForcedClosed ( ) || this . _isOpen ( ) ) {
2017-08-11 17:59:31 +03:00
return ;
2016-01-26 21:12:48 +03:00
}
2020-01-12 19:45:02 +03:00
2020-01-12 19:54:25 +03:00
$ ( ".bmd-layout-drawer" ) . hide ( ) ;
this . $element . show ( ) ;
2017-08-11 17:59:31 +03:00
this . $toggles . attr ( "aria-expanded" , true ) ;
this . $element . attr ( "aria-expanded" , true ) ;
this . $element . attr ( "aria-hidden" , false ) ;
2016-01-26 21:12:48 +03:00
// focus on the first focusable item
2017-08-11 17:59:31 +03:00
let $focusOn = this . $element . find ( this . config . focusSelector ) ;
2016-01-26 21:12:48 +03:00
if ( $focusOn . length > 0 ) {
2017-08-11 17:59:31 +03:00
$focusOn . first ( ) . focus ( ) ;
2016-01-26 21:12:48 +03:00
}
2017-08-11 17:59:31 +03:00
this . $container . addClass ( ClassName . DRAWER _IN ) ;
2016-03-21 17:56:51 +03:00
// backdrop is responsively styled based on bmd-drawer-overlay, therefore style is none of our concern, simply add the marker class and let the scss determine if it should be displayed or not.
2017-08-11 17:59:31 +03:00
this . $backdrop . addClass ( ClassName . IN ) ;
2016-01-26 21:12:48 +03:00
}
hide ( ) {
if ( ! this . _isOpen ( ) ) {
2017-08-11 17:59:31 +03:00
return ;
2016-01-26 21:12:48 +03:00
}
2017-08-11 17:59:31 +03:00
this . $toggles . attr ( "aria-expanded" , false ) ;
this . $element . attr ( "aria-expanded" , false ) ;
this . $element . attr ( "aria-hidden" , true ) ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
this . $container . removeClass ( ClassName . DRAWER _IN ) ;
this . $backdrop . removeClass ( ClassName . IN ) ;
2016-01-26 21:12:48 +03:00
}
// ------------------------------------------------------------------------
// private
_isOpen ( ) {
2017-08-11 17:59:31 +03:00
return this . $container . hasClass ( ClassName . DRAWER _IN ) ;
2016-01-26 21:12:48 +03:00
}
_isForcedClosed ( ) {
2017-08-11 17:59:31 +03:00
return this . $container . hasClass ( ClassName . DRAWER _OUT ) ;
2016-01-26 21:12:48 +03:00
}
_addAria ( ) {
2017-08-11 17:59:31 +03:00
let isOpen = this . _isOpen ( ) ;
this . $element . attr ( "aria-expanded" , isOpen ) ;
this . $element . attr ( "aria-hidden" , isOpen ) ;
2016-01-26 21:12:48 +03:00
if ( this . $toggles . length ) {
2017-08-11 17:59:31 +03:00
this . $toggles . attr ( "aria-expanded" , isOpen ) ;
2016-01-26 21:12:48 +03:00
}
}
// ------------------------------------------------------------------------
// static
static _jQueryInterface ( config ) {
2017-08-11 17:59:31 +03:00
return this . each ( function ( ) {
let $element = $ ( this ) ;
let data = $element . data ( DATA _KEY ) ;
2016-01-26 21:12:48 +03:00
if ( ! data ) {
2017-08-11 17:59:31 +03:00
data = new Drawer ( $element , config ) ;
$element . data ( DATA _KEY , data ) ;
2016-01-26 21:12:48 +03:00
}
2017-08-11 17:59:31 +03:00
} ) ;
2016-01-26 21:12:48 +03:00
}
}
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* jQuery
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2017-08-11 17:59:31 +03:00
$ . fn [ JQUERY _NAME ] = Drawer . _jQueryInterface ;
$ . fn [ JQUERY _NAME ] . Constructor = Drawer ;
2016-01-26 21:12:48 +03:00
$ . fn [ JQUERY _NAME ] . noConflict = ( ) => {
2017-08-11 17:59:31 +03:00
$ . fn [ JQUERY _NAME ] = JQUERY _NO _CONFLICT ;
return Drawer . _jQueryInterface ;
} ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
return Drawer ;
} ) ( jQuery ) ;
2016-01-26 21:12:48 +03:00
2017-08-11 17:59:31 +03:00
export default Drawer ;