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

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2020-12-24 23:42:33 +01:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
2019-06-08 17:20:51 +02:00
const SlidingMenu = (($) => {
// Constants
2021-08-18 20:51:15 +02:00
const NAME = "jsSlidingMenu";
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
class SlidingMenu {
// Constructor
2019-10-20 01:40:40 +02:00
constructor(element) {
2020-12-24 23:42:33 +01:00
console.log(`${NAME}: init`);
2019-10-20 01:40:40 +02:00
this._element = element;
const $element = $(this._element);
$element.addClass(`${NAME}-active`);
2019-06-08 17:20:51 +02:00
// esc button
2021-08-18 20:51:15 +02:00
$(window).on("keyup", (e) => {
2019-06-08 17:20:51 +02:00
if (e.which === 27) {
2019-10-20 01:40:40 +02:00
$element.find('.is-open[data-toggle="offcanvas"]').click();
2019-06-08 17:20:51 +02:00
}
2020-12-24 23:42:33 +01:00
});
2019-06-08 17:20:51 +02:00
}
// Public methods
dispose() {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: dispose`);
2019-06-08 17:20:51 +02:00
2019-10-20 01:40:40 +02:00
$(this._element).removeClass(`${NAME}-active`);
$.removeData(this._element, DATA_KEY);
this._element = null;
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
2019-10-20 01:40:40 +02:00
return this.each(function () {
// attach functionality to element
const $element = $(this);
let data = $element.data(DATA_KEY);
2019-06-08 17:20:51 +02:00
if (!data) {
data = new SlidingMenu(this);
2019-10-20 01:40:40 +02:00
$element.data(DATA_KEY, data);
2019-06-08 17:20:51 +02:00
}
});
}
}
// jQuery interface
$.fn[NAME] = SlidingMenu._jQueryInterface;
$.fn[NAME].Constructor = SlidingMenu;
2019-10-20 01:40:40 +02:00
$.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;