2021-12-02 11:58:23 +00:00
|
|
|
// TODO: useTooltip関数使うようにしたい
|
|
|
|
// ただディレクティブ内でonUnmountedなどのcomposition api使えるのか不明
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
import { defineAsyncComponent, Directive, ref } from "vue";
|
|
|
|
import { isTouchUsing } from "@/scripts/touch";
|
|
|
|
import { popup, alert } from "@/os";
|
2020-06-03 04:30:17 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const start = isTouchUsing ? "touchstart" : "mouseover";
|
|
|
|
const end = isTouchUsing ? "touchend" : "mouseleave";
|
2020-06-03 04:30:17 +00:00
|
|
|
|
|
|
|
export default {
|
2020-10-17 11:12:00 +00:00
|
|
|
mounted(el: HTMLElement, binding, vn) {
|
2022-07-16 04:49:23 +00:00
|
|
|
const delay = binding.modifiers.noDelay ? 0 : 100;
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const self = ((el as any)._tooltipDirective_ = {} as any);
|
2020-06-03 04:30:17 +00:00
|
|
|
|
|
|
|
self.text = binding.value as string;
|
2020-10-17 11:12:00 +00:00
|
|
|
self._close = null;
|
2020-06-03 04:30:17 +00:00
|
|
|
self.showTimer = null;
|
|
|
|
self.hideTimer = null;
|
|
|
|
self.checkTimer = null;
|
|
|
|
|
|
|
|
self.close = () => {
|
2020-10-17 11:12:00 +00:00
|
|
|
if (self._close) {
|
2022-01-16 01:14:14 +00:00
|
|
|
window.clearInterval(self.checkTimer);
|
2020-10-17 11:12:00 +00:00
|
|
|
self._close();
|
|
|
|
self._close = null;
|
2020-06-03 04:30:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
if (binding.arg === "dialog") {
|
|
|
|
el.addEventListener("click", (ev) => {
|
2021-08-22 04:16:15 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2021-11-18 14:36:04 +00:00
|
|
|
alert({
|
2023-01-13 04:40:33 +00:00
|
|
|
type: "info",
|
2021-08-22 04:16:15 +00:00
|
|
|
text: binding.value,
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:07:47 +00:00
|
|
|
self.show = () => {
|
2020-06-03 04:30:17 +00:00
|
|
|
if (!document.body.contains(el)) return;
|
2020-10-17 11:12:00 +00:00
|
|
|
if (self._close) return;
|
|
|
|
if (self.text == null) return;
|
2020-06-03 04:30:17 +00:00
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
const showing = ref(true);
|
2023-01-13 04:40:33 +00:00
|
|
|
popup(
|
|
|
|
defineAsyncComponent(() => import("@/components/MkTooltip.vue")),
|
|
|
|
{
|
|
|
|
showing,
|
|
|
|
text: self.text,
|
|
|
|
asMfm: binding.modifiers.mfm,
|
|
|
|
direction: binding.modifiers.left
|
|
|
|
? "left"
|
|
|
|
: binding.modifiers.right
|
|
|
|
? "right"
|
|
|
|
: binding.modifiers.top
|
|
|
|
? "top"
|
|
|
|
: binding.modifiers.bottom
|
|
|
|
? "bottom"
|
|
|
|
: "top",
|
|
|
|
targetElement: el,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
"closed",
|
|
|
|
);
|
2020-06-03 04:30:17 +00:00
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
self._close = () => {
|
|
|
|
showing.value = false;
|
|
|
|
};
|
2020-06-03 04:30:17 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
el.addEventListener("selectstart", (ev) => {
|
2022-01-31 12:07:33 +00:00
|
|
|
ev.preventDefault();
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
el.addEventListener(
|
|
|
|
start,
|
|
|
|
() => {
|
|
|
|
window.clearTimeout(self.showTimer);
|
|
|
|
window.clearTimeout(self.hideTimer);
|
|
|
|
self.showTimer = window.setTimeout(self.show, delay);
|
|
|
|
},
|
|
|
|
{ passive: true },
|
|
|
|
);
|
|
|
|
|
|
|
|
el.addEventListener(
|
|
|
|
end,
|
|
|
|
() => {
|
|
|
|
window.clearTimeout(self.showTimer);
|
|
|
|
window.clearTimeout(self.hideTimer);
|
|
|
|
self.hideTimer = window.setTimeout(self.close, delay);
|
|
|
|
},
|
|
|
|
{ passive: true },
|
|
|
|
);
|
|
|
|
|
|
|
|
el.addEventListener("click", () => {
|
2022-01-16 01:14:14 +00:00
|
|
|
window.clearTimeout(self.showTimer);
|
2020-06-03 04:30:17 +00:00
|
|
|
self.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-09-29 18:07:47 +00:00
|
|
|
updated(el, binding) {
|
|
|
|
const self = el._tooltipDirective_;
|
|
|
|
self.text = binding.value as string;
|
|
|
|
},
|
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
unmounted(el, binding, vn) {
|
2020-06-03 04:30:17 +00:00
|
|
|
const self = el._tooltipDirective_;
|
2022-01-16 01:14:14 +00:00
|
|
|
window.clearInterval(self.checkTimer);
|
2023-01-13 06:17:18 +00:00
|
|
|
self.close();
|
2020-06-03 04:30:17 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
} as Directive;
|