2020-10-17 06:12:00 -05:00
|
|
|
import { Directive, ref } from 'vue';
|
|
|
|
import { isDeviceTouch } from '@/scripts/is-device-touch';
|
|
|
|
import { popup } from '@/os';
|
2020-06-02 23:30:17 -05:00
|
|
|
|
|
|
|
const start = isDeviceTouch ? 'touchstart' : 'mouseover';
|
|
|
|
const end = isDeviceTouch ? 'touchend' : 'mouseleave';
|
|
|
|
|
|
|
|
export default {
|
2020-10-17 06:12:00 -05:00
|
|
|
mounted(el: HTMLElement, binding, vn) {
|
2020-06-02 23:30:17 -05:00
|
|
|
const self = (el as any)._tooltipDirective_ = {} as any;
|
|
|
|
|
|
|
|
self.text = binding.value as string;
|
2020-10-17 06:12:00 -05:00
|
|
|
self._close = null;
|
2020-06-02 23:30:17 -05:00
|
|
|
self.showTimer = null;
|
|
|
|
self.hideTimer = null;
|
|
|
|
self.checkTimer = null;
|
|
|
|
|
|
|
|
self.close = () => {
|
2020-10-17 06:12:00 -05:00
|
|
|
if (self._close) {
|
2020-06-02 23:30:17 -05:00
|
|
|
clearInterval(self.checkTimer);
|
2020-10-17 06:12:00 -05:00
|
|
|
self._close();
|
|
|
|
self._close = null;
|
2020-06-02 23:30:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
const show = async e => {
|
2020-06-02 23:30:17 -05:00
|
|
|
if (!document.body.contains(el)) return;
|
2020-10-17 06:12:00 -05:00
|
|
|
if (self._close) return;
|
|
|
|
if (self.text == null) return;
|
2020-06-02 23:30:17 -05:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
const showing = ref(true);
|
|
|
|
popup(await import('@/components/ui/tooltip.vue'), {
|
|
|
|
showing,
|
|
|
|
text: self.text,
|
|
|
|
source: el
|
|
|
|
}, {}, 'closed');
|
2020-06-02 23:30:17 -05:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
self._close = () => {
|
|
|
|
showing.value = false;
|
|
|
|
};
|
2020-06-02 23:30:17 -05:00
|
|
|
};
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
el.addEventListener('selectstart', e => {
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2020-06-02 23:30:17 -05:00
|
|
|
el.addEventListener(start, () => {
|
|
|
|
clearTimeout(self.showTimer);
|
|
|
|
clearTimeout(self.hideTimer);
|
|
|
|
self.showTimer = setTimeout(show, 300);
|
2020-10-17 06:12:00 -05:00
|
|
|
}, { passive: true });
|
2020-06-02 23:30:17 -05:00
|
|
|
|
|
|
|
el.addEventListener(end, () => {
|
|
|
|
clearTimeout(self.showTimer);
|
|
|
|
clearTimeout(self.hideTimer);
|
|
|
|
self.hideTimer = setTimeout(self.close, 300);
|
2020-10-17 06:12:00 -05:00
|
|
|
}, { passive: true });
|
2020-06-02 23:30:17 -05:00
|
|
|
|
|
|
|
el.addEventListener('click', () => {
|
|
|
|
clearTimeout(self.showTimer);
|
|
|
|
self.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
unmounted(el, binding, vn) {
|
2020-06-02 23:30:17 -05:00
|
|
|
const self = el._tooltipDirective_;
|
|
|
|
clearInterval(self.checkTimer);
|
|
|
|
},
|
2020-10-17 06:12:00 -05:00
|
|
|
} as Directive;
|