mizzkey/src/client/directives/tooltip.ts

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-09-05 19:38:57 +09:00
import { Directive } from 'vue';
2020-09-06 12:30:27 +09:00
import { isDeviceTouch } from '@/scripts/is-device-touch';
2020-09-21 14:02:13 +09:00
import { popup } from '@/os';
const start = isDeviceTouch ? 'touchstart' : 'mouseover';
const end = isDeviceTouch ? 'touchend' : 'mouseleave';
export default {
2020-09-05 19:38:57 +09:00
mounted(el: HTMLElement, binding, vn) {
const self = (el as any)._tooltipDirective_ = {} as any;
self.text = binding.value as string;
2020-09-21 14:02:13 +09:00
self._close = null;
self.showTimer = null;
self.hideTimer = null;
self.checkTimer = null;
self.close = () => {
2020-09-21 14:02:13 +09:00
if (self._close) {
clearInterval(self.checkTimer);
2020-09-21 14:02:13 +09:00
self._close();
self._close = null;
}
};
2020-09-21 14:02:13 +09:00
const show = async e => {
if (!document.body.contains(el)) return;
2020-09-21 14:02:13 +09:00
if (self._close) return;
2020-09-21 14:02:13 +09:00
const promise = popup(await import('@/components/ui/tooltip.vue'), {
text: self.text,
source: el
});
2020-09-21 14:02:13 +09:00
self._close = () => {
promise.cancel();
};
};
el.addEventListener(start, () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.showTimer = setTimeout(show, 300);
});
el.addEventListener(end, () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.hideTimer = setTimeout(self.close, 300);
});
el.addEventListener('click', () => {
clearTimeout(self.showTimer);
self.close();
});
},
2020-09-05 19:38:57 +09:00
unmounted(el, binding, vn) {
const self = el._tooltipDirective_;
clearInterval(self.checkTimer);
},
2020-09-05 19:38:57 +09:00
} as Directive;