@centreon/ui 24.4.64 → 24.4.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ import type {
2
+ FocusEvent as ReactFocusEvent,
3
+ MouseEvent as ReactMouseEvent,
4
+ TouchEvent as ReactTouchEvent
5
+ } from 'react';
6
+
7
+ import { Point } from '@visx/point';
8
+
9
+ type EventType =
10
+ | MouseEvent
11
+ | TouchEvent
12
+ | FocusEvent
13
+ | ReactFocusEvent
14
+ | ReactMouseEvent
15
+ | ReactTouchEvent;
16
+
17
+ type PointCoords = Pick<Point, 'x' | 'y'>;
18
+
19
+ const DEFAULT_POINT = { x: 0, y: 0 };
20
+
21
+ const isTouchEvent = (event?: EventType): event is TouchEvent =>
22
+ !!event && 'changedTouches' in event;
23
+
24
+ export const isMouseEvent = (event?: EventType): event is MouseEvent =>
25
+ !!event && 'clientX' in event;
26
+
27
+ const getXAndYFromEvent = (event?: EventType): PointCoords => {
28
+ if (!event) return { ...DEFAULT_POINT };
29
+
30
+ if (isTouchEvent(event)) {
31
+ return event.changedTouches.length > 0
32
+ ? {
33
+ x: event.changedTouches[0].clientX,
34
+ y: event.changedTouches[0].clientY
35
+ }
36
+ : { ...DEFAULT_POINT };
37
+ }
38
+
39
+ if (isMouseEvent(event)) {
40
+ return {
41
+ x: event.clientX,
42
+ y: event.clientY
43
+ };
44
+ }
45
+
46
+ return { ...DEFAULT_POINT };
47
+ };
48
+
49
+ export const localPoint = (event: EventType): PointCoords | null => {
50
+ return getXAndYFromEvent(event);
51
+ };
@@ -0,0 +1,25 @@
1
+ export interface ZoomState {
2
+ transformMatrix: {
3
+ scaleX: number;
4
+ scaleY: number;
5
+ skewX: number;
6
+ skewY: number;
7
+ translateX: number;
8
+ translateY: number;
9
+ };
10
+ }
11
+
12
+ export type MinimapPosition =
13
+ | 'top-left'
14
+ | 'top-right'
15
+ | 'bottom-left'
16
+ | 'bottom-right';
17
+
18
+ export interface ChildrenProps extends ZoomState {
19
+ contentClientRect: {
20
+ height: number;
21
+ width: number;
22
+ } | null;
23
+ height: number;
24
+ width: number;
25
+ }
@@ -0,0 +1,156 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ import { ProvidedZoom, Translate } from '@visx/zoom/lib/types';
4
+ import { equals, gt, isNil, pick } from 'ramda';
5
+ import { Point } from '@visx/point';
6
+
7
+ import { ZoomState } from './models';
8
+
9
+ export interface UseMinimapProps {
10
+ height: number;
11
+ isDraggingFromContainer: boolean;
12
+ minimapScale: number;
13
+ scale: number;
14
+ width: number;
15
+ zoom: ProvidedZoom<SVGSVGElement> & ZoomState;
16
+ }
17
+
18
+ interface UseMinimapState {
19
+ dragEnd: (e) => void;
20
+ dragStart: (e) => void;
21
+ move: (e) => void;
22
+ transformTo: (e) => void;
23
+ zoomInOut: (e) => void;
24
+ }
25
+
26
+ export const useMinimap = ({
27
+ width,
28
+ height,
29
+ zoom,
30
+ minimapScale,
31
+ isDraggingFromContainer,
32
+ scale
33
+ }: UseMinimapProps): UseMinimapState => {
34
+ const [startPoint, setStartPoint] = useState<Pick<Point, 'x' | 'y'> | null>(
35
+ null
36
+ );
37
+ const [startTranslate, setStartTranslate] = useState<Translate | null>(null);
38
+
39
+ const getMatrixPoint = useCallback(
40
+ (event, newScale?: number): { x: number; y: number } => {
41
+ const hasScale = scale > 1;
42
+ const point = {
43
+ x: event.nativeEvent.offsetX * (1 / minimapScale),
44
+ y: event.nativeEvent.offsetY * (1 / minimapScale)
45
+ };
46
+
47
+ const dx = -(
48
+ point.x * (newScale || zoom.transformMatrix.scaleX) -
49
+ width / 2
50
+ );
51
+ const dy = -(
52
+ point.y * (newScale || zoom.transformMatrix.scaleY) -
53
+ height / 2
54
+ );
55
+
56
+ return {
57
+ x: !hasScale ? dx : dx * scale - width / 2,
58
+ y: !hasScale ? dy : dy * scale - height / 2
59
+ };
60
+ },
61
+ [zoom.transformMatrix, scale, width, height, minimapScale]
62
+ );
63
+
64
+ const transformTo = useCallback(
65
+ (e): void => {
66
+ if (!isNil(e.nativeEvent.which) && !equals(e.nativeEvent.which, 1)) {
67
+ return;
68
+ }
69
+ const { x, y } = getMatrixPoint(e);
70
+ zoom.setTransformMatrix({
71
+ ...zoom.transformMatrix,
72
+ translateX: x,
73
+ translateY: y
74
+ });
75
+ },
76
+ [zoom.transformMatrix, scale]
77
+ );
78
+
79
+ const dragStart = (e): void => {
80
+ if (
81
+ (!isNil(e.nativeEvent.which) && !equals(e.nativeEvent.which, 1)) ||
82
+ isDraggingFromContainer
83
+ ) {
84
+ return;
85
+ }
86
+ setStartPoint(getMatrixPoint(e));
87
+ setStartTranslate(pick(['translateX', 'translateY'], zoom.transformMatrix));
88
+ };
89
+
90
+ const dragEnd = (): void => {
91
+ setStartPoint(null);
92
+ setStartTranslate(null);
93
+ };
94
+
95
+ const move = useCallback(
96
+ (e): void => {
97
+ if (!startPoint || !startTranslate) {
98
+ return;
99
+ }
100
+ const { x, y } = getMatrixPoint(e);
101
+
102
+ const diffX = startPoint.x - x;
103
+ const diffY = startPoint.y - y;
104
+
105
+ zoom.setTransformMatrix({
106
+ ...zoom.transformMatrix,
107
+ translateX: startTranslate.translateX - diffX,
108
+ translateY: startTranslate.translateY - diffY
109
+ });
110
+ },
111
+
112
+ [zoom.transformMatrix, isDraggingFromContainer, scale, startPoint]
113
+ );
114
+
115
+ const zoomInOut = useCallback(
116
+ (e): void => {
117
+ const isZoomIn = gt(0, e.deltaY);
118
+
119
+ const newScaleX = isZoomIn
120
+ ? zoom.transformMatrix.scaleX + 0.1
121
+ : zoom.transformMatrix.scaleX - 0.1;
122
+
123
+ const newScaleY = isZoomIn
124
+ ? zoom.transformMatrix.scaleX + 0.1
125
+ : zoom.transformMatrix.scaleX - 0.1;
126
+ const { x, y } = getMatrixPoint(e, newScaleX);
127
+
128
+ const diffX = x - zoom.transformMatrix.translateX;
129
+ const diffY = y - zoom.transformMatrix.translateY;
130
+
131
+ zoom.setTransformMatrix({
132
+ ...zoom.transformMatrix,
133
+ scaleX: newScaleX,
134
+ scaleY: newScaleY,
135
+ translateX: zoom.transformMatrix.translateX + diffX / 4,
136
+ translateY: zoom.transformMatrix.translateY + diffY / 4
137
+ });
138
+ },
139
+ [
140
+ zoom.transformMatrix,
141
+ width,
142
+ height,
143
+ isDraggingFromContainer,
144
+ scale,
145
+ startPoint
146
+ ]
147
+ );
148
+
149
+ return {
150
+ dragEnd,
151
+ dragStart,
152
+ move,
153
+ transformTo,
154
+ zoomInOut
155
+ };
156
+ };
@@ -0,0 +1,70 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ import { Point, ProvidedZoom, Translate } from '@visx/zoom/lib/types';
4
+ import { equals, isNil } from 'ramda';
5
+
6
+ import { localPoint } from './localPoint';
7
+ import { ZoomState } from './models';
8
+
9
+ const isLeftMouseButtonClicked = (e): boolean =>
10
+ !isNil(e.nativeEvent.which) && equals(e.nativeEvent.which, 1);
11
+
12
+ interface UseZoomState {
13
+ dragEnd: () => void;
14
+ dragStart: (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) => (e) => void;
15
+ isDragging: boolean;
16
+ move: (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) => (e) => void;
17
+ }
18
+
19
+ export const useZoom = (): UseZoomState => {
20
+ const [startTranslate, setStartTranslate] = useState<Translate | null>(null);
21
+ const [startPoint, setStartPoint] = useState<Point | null>(null);
22
+
23
+ const dragStart = useCallback(
24
+ (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) =>
25
+ (e): void => {
26
+ if (!isLeftMouseButtonClicked(e)) {
27
+ return;
28
+ }
29
+ const { translateX, translateY } = zoom.transformMatrix;
30
+ setStartPoint(localPoint(e) || null);
31
+ setStartTranslate({ translateX, translateY });
32
+ },
33
+ []
34
+ );
35
+
36
+ const move = useCallback(
37
+ (zoom: ProvidedZoom<SVGSVGElement> & ZoomState) =>
38
+ (e): void => {
39
+ if (!startPoint || !startTranslate) {
40
+ return;
41
+ }
42
+ const currentPoint = localPoint(e);
43
+ const dx = currentPoint
44
+ ? -(startPoint.x - currentPoint.x)
45
+ : -startPoint.x;
46
+ const dy = currentPoint
47
+ ? -(startPoint.y - currentPoint.y)
48
+ : -startPoint.y;
49
+
50
+ const translateX = startTranslate.translateX + dx;
51
+ const translateY = startTranslate.translateY + dy;
52
+ zoom.setTranslate({
53
+ translateX,
54
+ translateY
55
+ });
56
+ },
57
+ [startPoint, startTranslate]
58
+ );
59
+ const dragEnd = useCallback((): void => {
60
+ setStartPoint(null);
61
+ setStartTranslate(null);
62
+ }, []);
63
+
64
+ return {
65
+ dragEnd,
66
+ dragStart,
67
+ isDragging: Boolean(startPoint && startTranslate),
68
+ move
69
+ };
70
+ };
@@ -0,0 +1,55 @@
1
+ import { CSSProperties } from 'react';
2
+
3
+ interface Props {
4
+ contentClientRect: {
5
+ height: number;
6
+ width: number;
7
+ } | null;
8
+ }
9
+
10
+ export const applyTranformStylesForZoom = ({
11
+ contentClientRect
12
+ }: Props): CSSProperties => {
13
+ const contentRect = {
14
+ height: contentClientRect?.height || 1,
15
+ width: contentClientRect?.width || 1
16
+ };
17
+ const isPortrait = contentRect.height > contentRect.width;
18
+ const sizes = isPortrait ? ['width', 'height'] : ['height', 'width'];
19
+ const sizeScale = contentRect[sizes[0]] / contentRect[sizes[1]];
20
+
21
+ const lengthToUse = isPortrait
22
+ ? contentRect[sizes[1]] - contentRect[sizes[0]]
23
+ : contentRect[sizes[0]];
24
+
25
+ const t = sizeScale > 0.85 && isPortrait ? sizeScale * 4 : sizeScale / 2;
26
+ const xScaleFactor = sizeScale > 0.7 && !isPortrait ? 10 : 6;
27
+
28
+ return {
29
+ transform: `translate(-${isPortrait ? 0 : contentRect.width * (sizeScale / xScaleFactor)}px, -${lengthToUse * (isPortrait ? t + 0.08 : t / 2)}px)`
30
+ };
31
+ };
32
+
33
+ // DO NOT REMOVE: As the component is in work in progress, please this code in case we need
34
+ // const getAdditionalPadding = (): number => {
35
+ // if (additionalScale > 0.05) {
36
+ // return 0;
37
+ // }
38
+
39
+ // const padding =
40
+ // additionalScale > 0.012
41
+ // ? (1 / additionalScale) * (1 / zoom.transformMatrix.scaleY)
42
+ // : 1 / additionalScale / zoom.transformMatrix.scaleY;
43
+
44
+ // if (additionalScale < 0.009) {
45
+ // const tweakScale = scaleLinear({
46
+ // clamp: true,
47
+ // domain: [0.009, 0.002],
48
+ // range: [1, 8]
49
+ // });
50
+
51
+ // return padding - padding / tweakScale(additionalScale);
52
+ // }
53
+
54
+ // return padding;
55
+ // };
@@ -11,3 +11,4 @@ export * from './ItemComposition';
11
11
  export * from './Avatar';
12
12
  export * from './CollapsibleItem';
13
13
  export * from './Inputs';
14
+ export { default as Zoom } from './Zoom/Zoom';