summaryrefslogtreecommitdiff
path: root/src/client/app/desktop/views/directives/user-preview.ts
blob: 8a4035881ad536ccaa408632a6c5507741d5e430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * マウスオーバーするとユーザーがプレビューされる要素を設定します
 */

import MkUserPreview from '../components/user-preview.vue';

export default {
	bind(el, binding, vn) {
		const self = el._userPreviewDirective_ = {} as any;

		self.user = binding.value;
		self.tag = null;
		self.showTimer = null;
		self.hideTimer = null;

		self.close = () => {
			if (self.tag) {
				self.tag.close();
				self.tag = null;
			}
		};

		const show = () => {
			if (self.tag) return;

			self.tag = new MkUserPreview({
				parent: vn.context,
				propsData: {
					user: self.user
				}
			}).$mount();

			const preview = self.tag.$el;
			const rect = el.getBoundingClientRect();
			const x = rect.left + el.offsetWidth + window.pageXOffset;
			const y = rect.top + window.pageYOffset;

			preview.style.top = y + 'px';
			preview.style.left = x + 'px';

			preview.addEventListener('mouseover', () => {
				clearTimeout(self.hideTimer);
			});

			preview.addEventListener('mouseleave', () => {
				clearTimeout(self.showTimer);
				self.hideTimer = setTimeout(self.close, 500);
			});

			document.body.appendChild(preview);
		};

		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);
		});
	},

	unbind(el, binding, vn) {
		const self = el._userPreviewDirective_;
		clearTimeout(self.showTimer);
		clearTimeout(self.hideTimer);
		self.close();
	}
};