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

201 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-10-20 01:40:40 +02:00
/*
* Conflicts with 'bootstrap/js/dist/dropdown'
*/
2021-08-18 20:51:15 +02:00
"use strict";
2021-08-18 20:51:15 +02:00
import $ from "jquery";
import Events from "../_events";
import "jquery-hoverintent/jquery.hoverIntent.js";
import MainUI from "../_main";
2020-02-17 19:47:05 +01:00
const HoverUI = (($) => {
// Constants
const W = window;
const D = document;
2021-08-18 20:51:15 +02:00
const $Html = $("html");
const $Body = $("body");
2021-08-18 20:51:15 +02:00
const NAME = "jsHoverUI";
const DATA_KEY = NAME;
class HoverUI {
// Constructor
constructor(el) {
2020-07-22 10:21:50 +02:00
console.log(`${NAME}: init`);
const ui = this;
const $el = $(el);
2021-01-04 07:45:07 +01:00
if (
$el.is(
2021-08-18 20:51:15 +02:00
'[target="_blank"],.external,[data-toggle="lightbox"],[data-lightbox-gallery]'
2021-01-04 07:45:07 +01:00
)
) {
return true;
}
ui.$el = $el;
// find parent
2021-08-18 20:51:15 +02:00
let $parent = $el.parents(".nav-item, .dropdown");
$parent = $parent && $parent.length ? $parent.first() : null;
//$parent = $parent ? $parent : $el.parent();
ui.$parent = $parent;
// find target
2021-08-18 20:51:15 +02:00
let $target = $el.data("target");
$target = $target && $target.length ? $target : null;
2019-12-31 12:55:51 +01:00
$target = $target
? $target
: $parent
2021-08-18 20:51:15 +02:00
? $parent.find(".dropdown-menu").first()
2020-07-22 10:22:54 +02:00
: null;
if (!$target || !$target.length) {
console.warn(`${NAME}: Missing target for:`);
console.warn($el);
return;
}
ui.$target = $target;
const $triger = $parent ? $parent : $el;
ui.$triger = $triger;
// integrate with dropdown-toggle
2021-08-18 20:51:15 +02:00
$('[data-toggle="dropdown"]').on("click touch", (e) => {
2020-07-22 10:21:50 +02:00
console.log(`${NAME}: dropdown click-touch`);
ui.hide();
});
if (!W.isTouch) {
$triger.hoverIntent({
sensitivity: 10,
interval: 50,
over: () => {
ui.show();
},
out: () => {
ui.hide();
},
});
}
2021-08-18 20:51:15 +02:00
$el.off("click touch");
$el.on("click touch", (e) => {
2020-07-22 10:21:50 +02:00
const size = MainUI.detectBootstrapScreenSize();
console.log(`${NAME}: click-touch size: ${size}`);
2020-02-17 19:06:39 +01:00
if (
2021-08-18 20:51:15 +02:00
size === "xs" ||
!$el.data("allow-click") ||
(W.IsTouchScreen && !$el.data("allow-touch-click"))
2020-02-17 19:06:39 +01:00
) {
2020-07-22 10:21:50 +02:00
console.log(`${NAME}: click-touch prevent click`);
e.stopPropagation();
2019-10-20 01:40:40 +02:00
e.preventDefault();
}
if (ui.isShown()) {
ui.hide();
} else {
ui.show();
}
});
2020-01-31 10:18:11 +01:00
$el.data(NAME, ui);
$triger.addClass(`${NAME}-active`);
}
isShown() {
2021-08-18 20:51:15 +02:00
return this.$target.hasClass("show");
}
show() {
const ui = this;
2019-12-31 12:55:51 +01:00
ui.$el
2021-08-18 20:51:15 +02:00
.parents(".dropdown")
.not(".active")
2019-12-31 12:55:51 +01:00
.each((i, el) => {
const $el = $(el);
2021-08-18 20:51:15 +02:00
$el.find(".dropdown").removeClass("show");
$el.addClass("show");
2019-12-31 12:55:51 +01:00
});
2021-08-18 20:51:15 +02:00
ui.$target.addClass("show");
}
hide() {
const ui = this;
2019-10-20 01:40:40 +02:00
const $el = ui.$target;
2021-08-18 20:51:15 +02:00
$el.removeClass("show");
$el.find(".dropdown-menu").removeClass("show");
$el.parent(".dropdown").removeClass("show");
}
dispose() {
const ui = this;
const $el = ui.$el;
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: dispose`);
ui.$triger.removeClass(`${NAME}-active`);
$.removeData($el, DATA_KEY);
ui.$el = null;
ui.$parent = null;
ui.$target = null;
ui.$triger = null;
}
static _jQueryInterface() {
2020-06-11 13:53:30 +02:00
return this.each(function () {
// attach functionality to el
const $el = $(this);
let data = $el.data(DATA_KEY);
if (!data) {
data = new HoverUI(this);
$el.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = HoverUI._jQueryInterface;
$.fn[NAME].Constructor = HoverUI;
2020-06-11 13:53:30 +02:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return HoverUI._jQueryInterface;
};
// auto-apply
$('[data-toggle="hover"]').ready(() => {
$('[data-toggle="hover"]').jsHoverUI();
});
2019-10-20 01:40:40 +02:00
// rewrite 'bootstrap/js/dist/dropdown'
2021-08-18 20:51:15 +02:00
$('[data-toggle="dropdown"]').on("click touch", (e) => {
2019-10-20 01:40:40 +02:00
e.preventDefault();
const $el = $(e.currentTarget);
2021-08-18 20:51:15 +02:00
const $parent = $el.parent(".dropdown");
2019-10-20 01:40:40 +02:00
2020-01-31 10:18:11 +01:00
// hide siblings
2021-08-18 20:51:15 +02:00
$parent.parent().find(".dropdown, .dropdown-menu").removeClass("show");
2020-01-31 10:18:11 +01:00
2021-08-18 20:51:15 +02:00
if ($parent.hasClass("show")) {
$parent.removeClass("show");
$parent.find(".dropdown-menu").removeClass("show");
2019-10-20 01:40:40 +02:00
} else {
2021-08-18 20:51:15 +02:00
$parent.addClass("show");
$parent.find(".dropdown-menu").addClass("show");
2019-10-20 01:40:40 +02:00
}
});
return HoverUI;
})($);
export default HoverUI;