Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag&Drop in add_filter_modal [droppable groups] #18

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 113 additions & 32 deletions src/plugins/data/public/ui/query_string_input/add_filter_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import {
EuiText,
EuiIcon,
EuiFieldText,
EuiDragDropContext,
EuiDroppable,
EuiDraggable,
DropResult,
ResponderProvided,
} from '@elastic/eui';
import { XJsonLang } from '@kbn/monaco';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -146,6 +151,58 @@ export function AddFilterModal({
},
]);

const onDragEnd = useCallback(
(result: DropResult, provided: ResponderProvided) => {
if (result.destination) {
const [sourceGroup, sourceSubGroup, sourceId] = result.draggableId.split('-');
const [destGroup, destSubGroup] = result.destination.droppableId.split('-');
const targetIndex = localFilters.findIndex((f) => f.id === +sourceId);

let updatedFilters = [...localFilters];

const [reorderingItem] = updatedFilters.splice(targetIndex, 1);
const insertIndex = updatedFilters.findIndex(
(f) => f.groupId === +destGroup && f.subGroupId === +destSubGroup
);
updatedFilters.splice(insertIndex, 0, reorderingItem);

updatedFilters = localFilters.map((f, idx) => {
if (idx === targetIndex) {
const updatedTarget = {
...f,
subGroupId: +destSubGroup,
relationship: undefined,
id: idx,
};
if (+destSubGroup === 1)
// if inserting to the root it has to be with new filter group
return {
...updatedTarget,
groupId:
Math.max.apply(
Math,
localFilters.map((lcFilter) => lcFilter.groupId)
) + 1,
};
return { ...updatedTarget, groupId: +destGroup };
}
return { ...f, id: idx };
});

if (
updatedFilters[targetIndex - 1] &&
updatedFilters[targetIndex - 1].groupId === updatedFilters[targetIndex].groupId &&
updatedFilters[targetIndex - 1].subGroupId === updatedFilters[targetIndex].subGroupId
)
// give relationship of moved filter to the filter before in the source
updatedFilters[targetIndex - 1].relationship = updatedFilters[targetIndex].relationship;

setLocalFilters(updatedFilters);
}
},
[localFilters]
);

useEffect(() => {
const fetchQueries = async () => {
const allSavedQueries = await savedQueryService.getAllSavedQueries();
Expand Down Expand Up @@ -500,14 +557,22 @@ export function AddFilterModal({
: groupsCount === 1 && subGroup.length > 1
? 'kbnQueryBar__filterModalGroups'
: '';
return (
<>
<div className={classNames(classes)}>
{subGroup.map((localfilter, index) => {
return (
<>

const group = subGroup.map((localfilter, index) => {
return (
<>
<EuiDraggable
spacing="s"
key={localfilter.id}
index={Number(localfilter.id)}
draggableId={`${localfilter.groupId}-${localfilter.subGroupId}-${localfilter.id}`}
customDragHandle={true}
hasInteractiveChildren={true}
>
{(provided) => (
<EuiPanel paddingSize="s">
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={false}>
<EuiFlexItem grow={false} {...provided.dragHandleProps}>
<EuiIcon type="grab" size="s" />
</EuiFlexItem>

Expand All @@ -520,6 +585,7 @@ export function AddFilterModal({
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>

<EuiFlexItem grow={false}>
<EuiFlexGroup responsive={false} justifyContent="center">
{subGroup.length < 2 && (
Expand Down Expand Up @@ -568,6 +634,12 @@ export function AddFilterModal({
filtersOnGroup.length > 2
? localfilter?.subGroupId ?? 0
: (localfilter?.subGroupId ?? 0) + 1;
// const subGroupId =
// filtersOnGroup.length > 2
// ? localfilter?.subGroupId ?? 0
// : filtersOnGroup.length > 1
// ? (localfilter?.subGroupId ?? 0) + 1
// : 1;
const updatedLocalFilter = {
...localfilter,
relationship: 'AND',
Expand Down Expand Up @@ -634,32 +706,41 @@ export function AddFilterModal({
</EuiFlexGroup>
</EuiFlexItem>
</EuiFlexGroup>
</EuiPanel>
)}
</EuiDraggable>

{localfilter.relationship &&
localfilter.relationship === 'OR' &&
subGroup.length === 0 && (
<>
<EuiFlexGroup gutterSize="none" responsive={false}>
<EuiFlexItem>
<EuiHorizontalRule margin="s" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText
color="subdued"
className="kbnQueryBar__filterModalORText"
>
OR
</EuiText>
</EuiFlexItem>
<EuiFlexItem>
<EuiHorizontalRule margin="s" />
</EuiFlexItem>
</EuiFlexGroup>
</>
)}
{localfilter.relationship &&
localfilter.relationship === 'OR' &&
subGroup.length === 0 && (
<>
<EuiFlexGroup gutterSize="none" responsive={false}>
<EuiFlexItem>
<EuiHorizontalRule margin="s" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText color="subdued" className="kbnQueryBar__filterModalORText">
OR
</EuiText>
</EuiFlexItem>
<EuiFlexItem>
<EuiHorizontalRule margin="s" />
</EuiFlexItem>
</EuiFlexGroup>
</>
);
})}
)}
</>
);
});
return (
<>
<div className={classNames(classes)}>
<EuiDroppable
spacing="s"
droppableId={`${subGroup[0].groupId}-${subGroup[0].subGroupId}`}
>
{group}
</EuiDroppable>
</div>
<>
{subGroup.length > 0 && subGroupIdx !== subGroups.length - 1 && (
Expand Down Expand Up @@ -687,7 +768,7 @@ export function AddFilterModal({
);
GroupComponent.push(temp);
}
return GroupComponent;
return <EuiDragDropContext onDragEnd={onDragEnd}>{GroupComponent}</EuiDragDropContext>;
};

return (
Expand Down