Skip to content

Commit c126f9d

Browse files
committed
implement dnd for field
1 parent 1f4d5c4 commit c126f9d

18 files changed

Lines changed: 973 additions & 638 deletions

File tree

x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/draggable_dimension_button.tsx

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import React, { useMemo, useCallback, useContext, ReactElement } from 'react';
9+
import { isDraggedField } from '../../../../utils';
910
import { DragDrop, DragDropIdentifier, DragContext } from '../../../../drag_drop';
1011
import {
1112
Datasource,
@@ -14,23 +15,20 @@ import {
1415
DropType,
1516
DatasourceLayers,
1617
IndexPatternMap,
18+
DragDropOperation,
19+
Visualization,
1720
} from '../../../../types';
1821
import {
1922
getCustomDropTarget,
2023
getAdditionalClassesOnDroppable,
2124
getAdditionalClassesOnEnter,
22-
getDropProps,
2325
} from './drop_targets_utils';
2426

2527
export function DraggableDimensionButton({
26-
layerId,
27-
label,
28-
accessorIndex,
29-
groupIndex,
30-
layerIndex,
31-
columnId,
28+
order,
3229
group,
3330
onDrop,
31+
activeVisualization,
3432
onDragStart,
3533
onDragEnd,
3634
children,
@@ -39,100 +37,82 @@ export function DraggableDimensionButton({
3937
datasourceLayers,
4038
registerNewButtonRef,
4139
indexPatterns,
40+
target,
4241
}: {
43-
layerId: string;
44-
groupIndex: number;
45-
layerIndex: number;
42+
target: DragDropOperation & {
43+
id: string;
44+
humanData: {
45+
label: string;
46+
groupLabel: string;
47+
position: number;
48+
layerNumber: number;
49+
};
50+
};
51+
order: [2, number, number, number];
4652
onDrop: (source: DragDropIdentifier, dropTarget: DragDropIdentifier, dropType?: DropType) => void;
4753
onDragStart: () => void;
4854
onDragEnd: () => void;
55+
activeVisualization: Visualization<unknown, unknown>;
4956
group: VisualizationDimensionGroupConfig;
50-
label: string;
5157
children: ReactElement;
5258
layerDatasource?: Datasource<unknown, unknown>;
5359
datasourceLayers: DatasourceLayers;
5460
state: unknown;
55-
accessorIndex: number;
56-
columnId: string;
5761
registerNewButtonRef: (id: string, instance: HTMLDivElement | null) => void;
5862
indexPatterns: IndexPatternMap;
5963
}) {
6064
const { dragging } = useContext(DragContext);
6165

62-
const sharedDatasource =
63-
!isOperation(dragging) ||
64-
datasourceLayers?.[dragging.layerId]?.datasourceId === datasourceLayers?.[layerId]?.datasourceId
65-
? layerDatasource
66-
: undefined;
66+
let getDropProps;
6767

68-
const dropProps = getDropProps(
69-
{
70-
state,
71-
source: dragging,
72-
target: {
73-
layerId,
74-
columnId,
75-
groupId: group.groupId,
76-
filterOperations: group.filterOperations,
77-
prioritizedOperation: group.prioritizedOperation,
78-
},
79-
indexPatterns,
80-
},
81-
sharedDatasource
82-
);
68+
if (dragging) {
69+
if (!layerDatasource) {
70+
getDropProps = activeVisualization.getDropProps;
71+
} else if (
72+
isDraggedField(dragging) ||
73+
(isOperation(dragging) &&
74+
layerDatasource &&
75+
datasourceLayers?.[dragging.layerId]?.datasourceId ===
76+
datasourceLayers?.[target.layerId]?.datasourceId)
77+
) {
78+
getDropProps = layerDatasource.getDropProps;
79+
}
80+
}
81+
82+
const { dropTypes, nextLabel } = getDropProps?.({
83+
state,
84+
source: dragging,
85+
target,
86+
indexPatterns,
87+
}) || { dropTypes: [], nextLabel: '' };
8388

84-
const dropTypes = dropProps?.dropTypes;
85-
const nextLabel = dropProps?.nextLabel;
8689
const canDuplicate = !!(
87-
dropTypes &&
88-
(dropTypes.includes('replace_duplicate_incompatible') ||
89-
dropTypes.includes('replace_duplicate_compatible'))
90+
dropTypes.includes('replace_duplicate_incompatible') ||
91+
dropTypes.includes('replace_duplicate_compatible')
9092
);
9193

9294
const canSwap = !!(
93-
dropTypes &&
94-
(dropTypes.includes('swap_incompatible') || dropTypes.includes('swap_compatible'))
95+
dropTypes.includes('swap_incompatible') || dropTypes.includes('swap_compatible')
9596
);
9697

9798
const canCombine = Boolean(
98-
dropTypes &&
99-
(dropTypes.includes('combine_compatible') ||
100-
dropTypes.includes('field_combine') ||
101-
dropTypes.includes('combine_incompatible'))
99+
dropTypes.includes('combine_compatible') ||
100+
dropTypes.includes('field_combine') ||
101+
dropTypes.includes('combine_incompatible')
102102
);
103103

104104
const value = useMemo(
105105
() => ({
106-
columnId,
107-
groupId: group.groupId,
108-
layerId,
109-
id: columnId,
110-
filterOperations: group.filterOperations,
106+
...target,
111107
humanData: {
108+
...target.humanData,
112109
canSwap,
113110
canDuplicate,
114111
canCombine,
115-
label,
116-
groupLabel: group.groupLabel,
117-
position: accessorIndex + 1,
118112
nextLabel: nextLabel || '',
119-
layerNumber: layerIndex + 1,
120113
},
121114
}),
122-
[
123-
columnId,
124-
group.groupId,
125-
accessorIndex,
126-
layerId,
127-
label,
128-
group.groupLabel,
129-
nextLabel,
130-
group.filterOperations,
131-
canDuplicate,
132-
canSwap,
133-
canCombine,
134-
layerIndex,
135-
]
115+
[target, nextLabel, canDuplicate, canSwap, canCombine]
136116
);
137117

138118
const reorderableGroup = useMemo(
@@ -144,8 +124,8 @@ export function DraggableDimensionButton({
144124
);
145125

146126
const registerNewButtonRefMemoized = useCallback(
147-
(el) => registerNewButtonRef(columnId, el),
148-
[registerNewButtonRef, columnId]
127+
(el) => registerNewButtonRef(target.columnId, el),
128+
[registerNewButtonRef, target.columnId]
149129
);
150130

151131
const handleOnDrop = useCallback(
@@ -162,7 +142,7 @@ export function DraggableDimensionButton({
162142
getCustomDropTarget={getCustomDropTarget}
163143
getAdditionalClassesOnEnter={getAdditionalClassesOnEnter}
164144
getAdditionalClassesOnDroppable={getAdditionalClassesOnDroppable}
165-
order={[2, layerIndex, groupIndex, accessorIndex]}
145+
order={order}
166146
draggable
167147
dragType={isOperation(dragging) ? 'move' : 'copy'}
168148
dropTypes={dropTypes}

x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/drop_targets_utils.test.tsx

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)