mdb-ui-kit/js/drawer.js

175 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-08-11 17:59:31 +03:00
import BaseLayout from "./baseLayout";
2017-08-11 17:59:31 +03:00
const Drawer = ($ => {
/**
* ------------------------------------------------------------------------
* 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];
const Keycodes = {
ESCAPE: 27
//ENTER: 13,
//SPACE: 32
2017-08-11 17:59:31 +03:00
};
const ClassName = {
2017-08-11 17:59:31 +03:00
IN: "in",
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"
};
const Default = {
focusSelector: `a, button, input`
2017-08-11 17:59:31 +03:00
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Drawer extends BaseLayout {
// $element is expected to be the trigger
// 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">
constructor($element, config) {
2017-08-11 17:59:31 +03:00
super($element, $.extend(true, {}, Default, config));
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}"]`
);
2017-08-11 17:59:31 +03:00
this._addAria();
// 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();
});
// escape on the drawer closes it
2017-08-11 17:59:31 +03:00
this.$element.keydown(ev => {
if (ev.which === Keycodes.ESCAPE) {
2017-08-11 17:59:31 +03:00
this.hide();
}
2017-08-11 17:59:31 +03:00
});
// any toggle button clicks
this.$toggles.click(() => {
2017-08-11 17:59:31 +03:00
this.toggle();
});
}
dispose() {
2017-08-11 17:59:31 +03:00
super.dispose(DATA_KEY);
this.$toggles = null;
}
toggle() {
if (this._isOpen()) {
2017-08-11 17:59:31 +03:00
this.hide();
} else {
2017-08-11 17:59:31 +03:00
this.show();
}
}
show() {
if (this._isForcedClosed() || this._isOpen()) {
2017-08-11 17:59:31 +03:00
return;
}
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);
// focus on the first focusable item
2017-08-11 17:59:31 +03:00
let $focusOn = this.$element.find(this.config.focusSelector);
if ($focusOn.length > 0) {
2017-08-11 17:59:31 +03:00
$focusOn.first().focus();
}
2017-08-11 17:59:31 +03:00
this.$container.addClass(ClassName.DRAWER_IN);
// 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);
}
hide() {
if (!this._isOpen()) {
2017-08-11 17:59:31 +03:00
return;
}
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);
2017-08-11 17:59:31 +03:00
this.$container.removeClass(ClassName.DRAWER_IN);
this.$backdrop.removeClass(ClassName.IN);
}
// ------------------------------------------------------------------------
// private
_isOpen() {
2017-08-11 17:59:31 +03:00
return this.$container.hasClass(ClassName.DRAWER_IN);
}
_isForcedClosed() {
2017-08-11 17:59:31 +03:00
return this.$container.hasClass(ClassName.DRAWER_OUT);
}
_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);
if (this.$toggles.length) {
2017-08-11 17:59:31 +03:00
this.$toggles.attr("aria-expanded", isOpen);
}
}
// ------------------------------------------------------------------------
// 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);
if (!data) {
2017-08-11 17:59:31 +03:00
data = new Drawer($element, config);
$element.data(DATA_KEY, data);
}
2017-08-11 17:59:31 +03:00
});
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2017-08-11 17:59:31 +03:00
$.fn[JQUERY_NAME] = Drawer._jQueryInterface;
$.fn[JQUERY_NAME].Constructor = Drawer;
$.fn[JQUERY_NAME].noConflict = () => {
2017-08-11 17:59:31 +03:00
$.fn[JQUERY_NAME] = JQUERY_NO_CONFLICT;
return Drawer._jQueryInterface;
};
2017-08-11 17:59:31 +03:00
return Drawer;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default Drawer;