webpack-bootstrap-ui-kit/src/js/_components/_ui.menu.js

63 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-06-08 17:20:51 +02:00
import $ from 'jquery';
const SlidingMenu = (($) => {
// Constants
const NAME = 'jsSlidingMenu';
const DATA_KEY = NAME;
class SlidingMenu {
// Constructor
constructor(el) {
const $el = $(this._el);
this.$el = $el;
$el.addClass(`${NAME}-active`);
2019-06-08 17:20:51 +02:00
// esc button
$(window).on('keyup', ((e) => {
2019-06-08 17:20:51 +02:00
if (e.which === 27) {
$el.find('.is-open[data-toggle="offcanvas"]').click();
2019-06-08 17:20:51 +02:00
}
}));
}
// Public methods
dispose() {
console.log(`Disposing: ${NAME} els`);
2019-06-08 17:20:51 +02:00
this.$el.removeClass(`${NAME}-active`);
$.removeData(this.$el, DATA_KEY);
this.$el = null;
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
return this.each(function() {
// attach functionality to el
const $el = $(this);
let data = $el.data(DATA_KEY);
2019-06-08 17:20:51 +02:00
if (!data) {
data = new SlidingMenu(this);
$el.data(DATA_KEY, data);
2019-06-08 17:20:51 +02:00
}
});
}
}
// jQuery interface
$.fn[NAME] = SlidingMenu._jQueryInterface;
$.fn[NAME].Constructor = SlidingMenu;
$.fn[NAME].noConflict = function() {
2019-06-08 17:20:51 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return SlidingMenu._jQueryInterface;
};
// auto-apply
$(`.ui.${NAME}`).ready(() => {
$(`.ui.${NAME}`).jsSlidingMenu();
});
return SlidingMenu;
})($);
export default SlidingMenu;