aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/design-system/src/helpers/positioning.ts
blob: b2105c606b187c691c7c568918edb547cb87b87e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * Positioning rules:
 * - Bottom = below the block, horizontally centered
 * - BottomLeft = below the block, horizontally left-aligned
 * - BottomRight = below the block, horizontally right-aligned
 * - Left = Left of the block, vertically centered
 * - LeftTop = on the left-side of the block, vertically top-aligned
 * - LeftBottom = on the left-side of the block, vertically bottom-aligned
 * - Right = Right of the block, vertically centered
 * - RightTop = on the right-side of the block, vertically top-aligned
 * - RightBottom = on the right-side of the block, vetically bottom-aligned
 * - Top = above the block, horizontally centered
 * - TopLeft = above the block, horizontally left-aligned
 * - TopRight = above the block, horizontally right-aligned
 */

export enum PopupPlacement {
  Bottom = 'bottom',
  BottomLeft = 'bottom-left',
  BottomRight = 'bottom-right',
  Left = 'left',
  LeftTop = 'left-top',
  LeftBottom = 'left-bottom',
  Right = 'right',
  RightTop = 'right-top',
  RightBottom = 'right-bottom',
  Top = 'top',
  TopLeft = 'top-left',
  TopRight = 'top-right',
}

export enum PopupZLevel {
  Content = 'content',
  Default = 'popup',
  Global = 'global',
  Absolute = 'absolute',
}

export type BasePlacement = Extract<
  PopupPlacement,
  PopupPlacement.Bottom | PopupPlacement.Top | PopupPlacement.Left | PopupPlacement.Right
>;

export const PLACEMENT_FLIP_MAP: { [key in PopupPlacement]: PopupPlacement } = {
  [PopupPlacement.Left]: PopupPlacement.Right,
  [PopupPlacement.LeftBottom]: PopupPlacement.RightBottom,
  [PopupPlacement.LeftTop]: PopupPlacement.RightTop,
  [PopupPlacement.Right]: PopupPlacement.Left,
  [PopupPlacement.RightBottom]: PopupPlacement.LeftBottom,
  [PopupPlacement.RightTop]: PopupPlacement.LeftTop,
  [PopupPlacement.Top]: PopupPlacement.Bottom,
  [PopupPlacement.TopLeft]: PopupPlacement.BottomLeft,
  [PopupPlacement.TopRight]: PopupPlacement.BottomRight,
  [PopupPlacement.Bottom]: PopupPlacement.Top,
  [PopupPlacement.BottomLeft]: PopupPlacement.TopLeft,
  [PopupPlacement.BottomRight]: PopupPlacement.TopRight,
};

const MARGIN_TO_EDGE = 4;

export function popupPositioning(
  toggleNode: Element,
  popupNode: Element,
  placement: PopupPlacement = PopupPlacement.Bottom,
) {
  const toggleRect = toggleNode.getBoundingClientRect();
  const popupRect = popupNode.getBoundingClientRect();

  let { left, top } = toggleRect;

  switch (placement) {
    case PopupPlacement.Bottom:
      left += toggleRect.width / 2 - popupRect.width / 2;
      top += toggleRect.height;
      break;
    case PopupPlacement.BottomLeft:
      top += toggleRect.height;
      break;
    case PopupPlacement.BottomRight:
      left += toggleRect.width - popupRect.width;
      top += toggleRect.height;
      break;
    case PopupPlacement.Left:
      left -= popupRect.width;
      top += toggleRect.height / 2 - popupRect.height / 2;
      break;
    case PopupPlacement.LeftTop:
      left -= popupRect.width;
      break;
    case PopupPlacement.LeftBottom:
      left -= popupRect.width;
      top += toggleRect.height - popupRect.height;
      break;
    case PopupPlacement.Right:
      left += toggleRect.width;
      top += toggleRect.height / 2 - popupRect.height / 2;
      break;
    case PopupPlacement.RightTop:
      left += toggleRect.width;
      break;
    case PopupPlacement.RightBottom:
      left += toggleRect.width;
      top += toggleRect.height - popupRect.height;
      break;
    case PopupPlacement.Top:
      left += toggleRect.width / 2 - popupRect.width / 2;
      top -= popupRect.height;
      break;
    case PopupPlacement.TopLeft:
      top -= popupRect.height;
      break;
    case PopupPlacement.TopRight:
      left += toggleRect.width - popupRect.width;
      top -= popupRect.height;
      break;
  }

  const inBoundariesLeft = Math.min(
    Math.max(left, getMinLeftPlacement(toggleRect)),
    getMaxLeftPlacement(toggleRect, popupRect),
  );

  const inBoundariesTop = Math.min(
    Math.max(top, getMinTopPlacement(toggleRect)),
    getMaxTopPlacement(toggleRect, popupRect),
  );

  return {
    height: popupRect.height,
    left: inBoundariesLeft,
    leftFix: inBoundariesLeft - left,
    top: inBoundariesTop,
    topFix: inBoundariesTop - top,
    width: popupRect.width,
  };
}

function getMinLeftPlacement(toggleRect: DOMRect) {
  return Math.min(
    MARGIN_TO_EDGE, // Left edge of the sceen
    toggleRect.left + toggleRect.width / 2, // Left edge of the screen when scrolled
  );
}

function getMaxLeftPlacement(toggleRect: DOMRect, popupRect: DOMRect) {
  return Math.max(
    document.documentElement.clientWidth - popupRect.width - MARGIN_TO_EDGE, // Right edge of the screen
    toggleRect.left + toggleRect.width / 2 - popupRect.width, // Right edge of the screen when scrolled
  );
}

function getMinTopPlacement(toggleRect: DOMRect) {
  return Math.min(
    MARGIN_TO_EDGE, // Top edge of the sceen
    toggleRect.top + toggleRect.height / 2, // Top edge of the screen when scrolled
  );
}

function getMaxTopPlacement(toggleRect: DOMRect, popupRect: DOMRect) {
  return Math.max(
    document.documentElement.clientHeight - popupRect.height - MARGIN_TO_EDGE, // Bottom edge of the screen
    toggleRect.top + toggleRect.height / 2 - popupRect.height, // Bottom edge of the screen when scrolled
  );
}