paricafe/src/client/directives/user-preview.ts

76 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-02-16 11:24:10 -06:00
import MkUserPreview from '../components/user-preview.vue';
export default {
bind(el: HTMLElement, binding, vn) {
const self = (el as any)._userPreviewDirective_ = {} as any;
2018-02-16 11:24:10 -06:00
self.user = binding.value;
2018-02-18 23:29:42 -06:00
self.tag = null;
2018-02-16 11:24:10 -06:00
self.showTimer = null;
self.hideTimer = null;
2020-02-19 09:38:26 -06:00
self.checkTimer = null;
2018-02-16 11:24:10 -06:00
self.close = () => {
2018-02-18 23:29:42 -06:00
if (self.tag) {
2020-02-19 09:38:26 -06:00
clearInterval(self.checkTimer);
2018-02-18 23:29:42 -06:00
self.tag.close();
self.tag = null;
2018-02-16 11:24:10 -06:00
}
};
const show = () => {
2020-01-29 15:23:34 -06:00
if (!document.body.contains(el)) return;
2018-02-18 23:29:42 -06:00
if (self.tag) return;
2018-02-18 07:16:36 -06:00
2018-02-18 23:29:42 -06:00
self.tag = new MkUserPreview({
2018-02-16 11:24:10 -06:00
parent: vn.context,
propsData: {
user: self.user,
source: el
2018-02-16 11:24:10 -06:00
}
}).$mount();
2018-02-18 07:16:36 -06:00
self.tag.$on('mouseover', () => {
2018-02-16 11:24:10 -06:00
clearTimeout(self.hideTimer);
});
2018-02-18 07:16:36 -06:00
self.tag.$on('mouseleave', () => {
2018-02-16 11:24:10 -06:00
clearTimeout(self.showTimer);
self.hideTimer = setTimeout(self.close, 500);
});
2018-02-18 07:16:36 -06:00
2020-02-19 15:38:19 -06:00
document.body.appendChild(self.tag.$el);
2020-02-19 09:38:26 -06:00
self.checkTimer = setInterval(() => {
2020-02-19 15:38:19 -06:00
if (!document.body.contains(el)) {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.close();
}
2020-02-19 09:38:26 -06:00
}, 1000);
2018-02-16 11:24:10 -06:00
};
el.addEventListener('mouseover', () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.showTimer = setTimeout(show, 500);
});
el.addEventListener('mouseleave', () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.hideTimer = setTimeout(self.close, 500);
});
2020-01-31 18:45:30 -06:00
el.addEventListener('click', () => {
clearTimeout(self.showTimer);
self.close();
});
2018-02-16 11:24:10 -06:00
},
2018-02-18 07:16:36 -06:00
2018-02-16 11:24:10 -06:00
unbind(el, binding, vn) {
2018-02-18 23:29:42 -06:00
const self = el._userPreviewDirective_;
2020-02-19 09:38:26 -06:00
clearInterval(self.checkTimer);
2018-02-16 11:24:10 -06:00
}
};