UNPKG

2.23 kBJavaScriptView Raw
1import {
2 findTooltip,
3 findTooltipTarget,
4 getOppositeSide,
5} from 'ember-tooltips/test-support/jquery';
6
7/**
8@method getPositionDifferences
9@param String side The side the tooltip should be on relative to the target
10
11Given a side, which represents the side of the target that
12the tooltip should render, this method identifies whether
13the tooltip or the target should be further away from the
14top left of the window.
15
16For example, if the side is 'top' then the target should
17be further away from the top left of the window than the
18tooltip because the tooltip should render above the target.
19
20If the side is 'right' then the tooltip should be further
21away from the top left of the window than the target
22because the tooltip should render to the right of the
23target.
24
25This method then returns an object with two numbers:
26
27- `expectedGreaterDistance` (expected greater number given the side)
28- `expectedLesserDistance` (expected lesser number given the side)
29
30These numbers can be used for calculations like determining
31whether a tooltip is on the correct side of the target or
32determining whether a tooltip is the correct distance from
33the target on the given side.
34*/
35
36export default function getPositionDifferences(options = {}) {
37 const { targetPosition, tooltipPosition } = getTooltipAndTargetPosition(options);
38 const { side } = options;
39
40 const distanceToTarget = targetPosition[side];
41 const distanceToTooltip = tooltipPosition[getOppositeSide(side)];
42 const shouldTooltipBeCloserThanTarget = side === 'top' || side === 'left';
43 const expectedGreaterDistance = shouldTooltipBeCloserThanTarget ? distanceToTarget : distanceToTooltip;
44 const expectedLesserDistance = shouldTooltipBeCloserThanTarget ? distanceToTooltip : distanceToTarget;
45
46 return { expectedGreaterDistance, expectedLesserDistance };
47}
48
49export function getTooltipAndTargetPosition(options = {}) {
50 const { selector, targetSelector } = options;
51 const [ target ] = findTooltipTarget(targetSelector);
52 const [ tooltip ] = findTooltip(selector, { targetSelector });
53
54 const targetPosition = target.getBoundingClientRect();
55 const tooltipPosition = tooltip.getBoundingClientRect();
56
57 return {
58 targetPosition,
59 tooltipPosition,
60 };
61}