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