Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable spellcheck/spell-checker */
import {
afterEach, describe, expect, it,
} from '@jest/globals';
import Sortable from '@js/ui/sortable';
import type { DragMoveEvent, DragStartEvent, ReorderEvent } from '@js/ui/sortable_types';
import type { Options as CardViewOptions } from '@ts/grids/new/card_view/options';
import CardView from '@ts/grids/new/card_view/widget';
import { rerender } from 'inferno';

import { CLASSES as HEADER_PANEL_CLASSES } from './header_panel';
import { CLASSES as ITEM_CLASSES } from './item';

const cardViews: CardView[] = [];

const setup = (options: CardViewOptions): void => {
const container = document.createElement('div');
document.body.append(container);

cardViews.push(new CardView(container, options));

rerender();
};

const getHeaderPanelSortable = (): Sortable => {
const element = document.querySelector(`.${HEADER_PANEL_CLASSES.sortable}`);

if (!element) {
throw new Error('Header panel sortable not found');
}

return Sortable.getInstance(element) as Sortable;
};

const getHeaderCaptions = (): string[] => Array.from(
document.querySelectorAll(`.${ITEM_CLASSES.item}`),
).map((item) => item.textContent?.trim() ?? '');

const dragHeader = (fromIndex: number, lastMoveToIndex: number, dropToIndex: number): void => {
const sortable = getHeaderPanelSortable();
const onDragStart = sortable.option('onDragStart') as (e: DragStartEvent) => void;
const onDragMove = sortable.option('onDragMove') as (e: DragMoveEvent) => void;
const onReorder = sortable.option('onReorder') as (e: ReorderEvent) => void;

const dragStartEvent = { fromIndex } as DragStartEvent;
onDragStart(dragStartEvent);
const { itemData } = dragStartEvent;

onDragMove({
component: sortable,
element: sortable.element(),
toComponent: sortable,
event: { clientX: 0, clientY: 0 },
fromIndex,
toIndex: lastMoveToIndex,
itemData,
} as unknown as DragMoveEvent);

onReorder({
component: sortable,
element: sortable.element(),
toComponent: sortable,
fromIndex,
toIndex: dropToIndex,
itemData,
} as unknown as ReorderEvent);

rerender();
};

describe('CardView', () => {
describe('HeaderPanel column reordering', () => {
afterEach(() => {
cardViews.splice(0).forEach((cardView) => cardView.dispose());
document.body.innerHTML = '';
});

it.each([
{
name: 'right, the last drag move reported the previous index (T1324855)',
fromIndex: 0,
lastMoveToIndex: 0,
dropToIndex: 1,
expected: ['B', 'A', 'C'],
},
{
name: 'left, the last drag move reported the previous index (T1324855)',
fromIndex: 2,
lastMoveToIndex: 2,
dropToIndex: 1,
expected: ['A', 'C', 'B'],
},
{
name: 'right, the last drag move reported the drop index',
fromIndex: 0,
lastMoveToIndex: 1,
dropToIndex: 1,
expected: ['B', 'A', 'C'],
},
])('moves a column $name', ({
fromIndex, lastMoveToIndex, dropToIndex, expected,
}) => {
setup({
dataSource: [{ a: 1, b: 2, c: 3 }],
columns: ['a', 'b', 'c'],
allowColumnReordering: true,
});

expect(getHeaderCaptions()).toEqual(['A', 'B', 'C']);

dragHeader(fromIndex, lastMoveToIndex, dropToIndex);

expect(getHeaderCaptions()).toEqual(expected);
});

it('does not drop a column between columns that cannot be reordered when the last drag move reported another index', () => {
setup({
dataSource: [{ a: 1, b: 2, c: 3 }],
columns: [
'a',
{ dataField: 'b', allowReordering: false },
{ dataField: 'c', allowReordering: false },
],
allowColumnReordering: true,
});

dragHeader(0, 0, 1);

expect(getHeaderCaptions()).toEqual(['A', 'B', 'C']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface Props extends Omit<SortableProps, 'onAdd' | 'onReorder' | 'drag

const ALLOWED_DRAGGING_DISTANCE = 20;

const canInsertBetween = (
columnBefore: Column | undefined,
columnAfter: Column | undefined,
): boolean => !!columnBefore?.allowReordering || !!columnAfter?.allowReordering;

const CLASS = {
widget: 'dx-widget',
columnSortable: 'dx-cardview-column-sortable',
Expand Down Expand Up @@ -107,7 +112,20 @@ export class ColumnSortable extends Component<Props> {
return;
}

this.props.onColumnMove?.(e.itemData.column, e.toIndex, e.itemData);
const draggingData: DraggingColumnData = {
...e.itemData,
...this.getNeighborColumns(e),
};
const {
source, destination, columnBefore, columnAfter,
} = draggingData;
const isHeaderPanelReorder = source === 'header-panel-main' && destination === 'header-panel-main';

if (isHeaderPanelReorder && !canInsertBetween(columnBefore, columnAfter)) {
return;
}

this.props.onColumnMove?.(draggingData.column, e.toIndex, draggingData);
};

// TODO: move all none-native approaches to sortable wrapper
Expand Down Expand Up @@ -217,7 +235,7 @@ export class ColumnSortable extends Component<Props> {
if (source === 'header-panel-main' && destination === 'header-panel-main') {
const isDragCloseEnough = yDistance <= ALLOWED_DRAGGING_DISTANCE;
const canReorder = column.allowReordering;
const canInsert = !!columnBefore?.allowReordering || !!columnAfter?.allowReordering;
const canInsert = canInsertBetween(columnBefore, columnAfter);

const isMoving = isDragCloseEnough && canInsert && canReorder;

Expand All @@ -234,7 +252,10 @@ export class ColumnSortable extends Component<Props> {
}

private getNeighborColumns(
e: SortableTypes.DragStartEvent | SortableTypes.DragMoveEvent,
e: SortableTypes.DragStartEvent
| SortableTypes.DragMoveEvent
| SortableTypes.AddEvent
| SortableTypes.ReorderEvent,
): { columnBefore: Column | undefined; columnAfter: Column | undefined } {
const { source, destination } = e.itemData as DraggingColumnData;

Expand Down
Loading