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
Expand Up @@ -23,7 +23,7 @@ import { createPath, foreachTree } from '../m_widget_utils';
import SortableModule from '../sortable/m_sortable';
import { ATTRIBUTES, CLASSES } from './const';
import { dragAndDropItemRender } from './dom';
import { reverseSortOrder } from './utils';
import { reverseSortOrder, shouldCancelDragging } from './utils';

const { Sortable } = SortableModule;

Expand Down Expand Up @@ -253,15 +253,7 @@ export class FieldChooserBase extends mixinWidget {
onDragging(e) {
const field = e.sourceElement.data('field');
const { targetGroup } = e;
e.cancel = false;

if (field.isMeasure === true) {
if (targetGroup === 'column' || targetGroup === 'row' || targetGroup === 'filter') {
e.cancel = true;
}
} else if (field.isMeasure === false && targetGroup === 'data') {
e.cancel = true;
}
e.cancel = shouldCancelDragging(field, targetGroup);
},
useIndicator: true,
onChanged(e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from '@jest/globals';

import { shouldCancelDragging } from './utils';

describe('shouldCancelDragging', () => {
it('should return false when field is undefined', () => {
expect(shouldCancelDragging(undefined, 'column')).toBe(false);
expect(shouldCancelDragging(undefined, 'row')).toBe(false);
expect(shouldCancelDragging(undefined, 'filter')).toBe(false);
expect(shouldCancelDragging(undefined, 'data')).toBe(false);
});

it('should return true when isMeasure is true and target is column', () => {
expect(shouldCancelDragging({ isMeasure: true }, 'column')).toBe(true);
});

it('should return true when isMeasure is true and target is row', () => {
expect(shouldCancelDragging({ isMeasure: true }, 'row')).toBe(true);
});

it('should return true when isMeasure is true and target is filter', () => {
expect(shouldCancelDragging({ isMeasure: true }, 'filter')).toBe(true);
});

it('should return false when isMeasure is true and target is data', () => {
expect(shouldCancelDragging({ isMeasure: true }, 'data')).toBe(false);
});

it('should return true when isMeasure is false and target is data', () => {
expect(shouldCancelDragging({ isMeasure: false }, 'data')).toBe(true);
});

it('should return false when isMeasure is false and target is column', () => {
expect(shouldCancelDragging({ isMeasure: false }, 'column')).toBe(false);
});

it('should return false when isMeasure is undefined', () => {
expect(shouldCancelDragging({}, 'data')).toBe(false);
expect(shouldCancelDragging({}, 'column')).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@ export const reverseSortOrder = (
): SortOrderType => (sortOrder === SORT_ORDER.descending
? SORT_ORDER.ascending
: SORT_ORDER.descending);

export const shouldCancelDragging = (
field: { isMeasure?: boolean } | undefined,
targetGroup: string,
): boolean => {
if (!field) {
return false;
}

if (field.isMeasure === true) {
return targetGroup === 'column' || targetGroup === 'row' || targetGroup === 'filter';
}

if (field.isMeasure === false && targetGroup === 'data') {
return true;
}

return false;
};
Loading