PluginProbe
Hostinger Tools / 3.0.48
Hostinger Tools v3.0.48
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / vue-frontend / src / utils / directives / tooltipDirective.ts

tooltipDirective.ts in Hostinger Tools 3.0.48, at vue-frontend/src/utils/directives/tooltipDirective.ts

97 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { DirectiveBinding } from 'vue';
2 import { MouseEvent } from '@/types/enums';
3
4 const TOOLTIP_ATTR = 'tooltip';
5
6 interface ComplexTooltip {
7 content: string;
8 autoWidth?: boolean;
9 }
10
11 type TooltipBinding = DirectiveBinding<string> | DirectiveBinding<ComplexTooltip>;
12
13 const isString = (value: any) => typeof value === 'string';
14
15 const getCurrentRotation = (el: HTMLElement) => {
16 const computedStyles = window.getComputedStyle(el, null);
17 const transformProp =
18 computedStyles.getPropertyValue('-webkit-transform') ||
19 computedStyles.getPropertyValue('-moz-transform') ||
20 computedStyles.getPropertyValue('-ms-transform') ||
21 computedStyles.getPropertyValue('-o-transform') ||
22 computedStyles.getPropertyValue('transform') ||
23 'none';
24 if (transformProp != 'none') {
25 const values = transformProp.split('(')[1].split(')')[0].split(',');
26
27 const angle = Math.round(Math.atan2(values[1] as any, values[0] as any) * (180 / Math.PI));
28
29 return angle < 0 ? angle + 360 : angle;
30 }
31
32 return 0;
33 };
34
35 const getTooltipClass = ({ modifiers }: DirectiveBinding, isRotated: boolean) => {
36 const position = Object.keys(modifiers)[0];
37
38 return `has-tooltip--${position || 'bottom'}${isRotated ? '-rotated' : ''}`;
39 };
40
41 const getTooltipContent = (binding: TooltipBinding) => {
42 if (!binding.value) return null;
43
44 return isString(binding.value) ? binding.value : (binding.value as ComplexTooltip).content;
45 };
46
47 const addTooltip = (el: HTMLElement, binding: TooltipBinding) => {
48 const content = getTooltipContent(binding);
49
50 if (!content) return removeTooltip(el, binding);
51
52 const rotation = getCurrentRotation(el);
53
54 el.setAttribute(TOOLTIP_ATTR, content as string);
55
56 document.documentElement.style.setProperty('--tooltip-rotation', `-${rotation}deg`);
57
58 if (!isString(binding.value) && (binding.value as ComplexTooltip).autoWidth) {
59 document.documentElement.style.setProperty('--tooltip-width', 'auto');
60 }
61
62 el.style.transition = '0s';
63 const zIndex = getComputedStyle(document.documentElement).getPropertyValue('--z-index-child-2');
64 el.style.zIndex = zIndex;
65 el.style.position = 'relative';
66
67 el.classList.add(getTooltipClass(binding, rotation === 180));
68 };
69
70 const removeTooltip = (el: HTMLElement, binding: TooltipBinding) => {
71 el.removeAttribute(TOOLTIP_ATTR);
72
73 el.style.zIndex = '';
74 el.style.position = '';
75
76 el.classList.remove(getTooltipClass(binding, true));
77 el.classList.remove(getTooltipClass(binding, false));
78 };
79
80 const unbind = (el: HTMLElement, binding: TooltipBinding) => {
81 el.removeEventListener(MouseEvent.MouseOver, () => addTooltip(el, binding));
82 el.removeEventListener(MouseEvent.MouseLeave, () => removeTooltip(el, binding));
83 el.removeEventListener(MouseEvent.Click, () => removeTooltip(el, binding));
84 };
85
86 const bind = (el: HTMLElement, binding: TooltipBinding) => {
87 el.addEventListener(MouseEvent.MouseOver, () => addTooltip(el, binding));
88 el.addEventListener(MouseEvent.MouseLeave, () => removeTooltip(el, binding));
89 el.addEventListener(MouseEvent.Click, () => removeTooltip(el, binding));
90 };
91
92 export const vTooltip = {
93 mounted: (el: HTMLElement, binding: TooltipBinding) => bind(el, binding),
94 updated: (el: HTMLElement, binding: TooltipBinding) => bind(el, binding),
95 beforeUnmount: (el: HTMLElement, binding: TooltipBinding) => unbind(el, binding)
96 };
97