Skip to content

Commit

Permalink
fix: disable row drag and drop while loading (#2957)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed Nov 3, 2021
1 parent c5320d2 commit 180898a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
7 changes: 4 additions & 3 deletions packages/vaadin-grid/src/vaadin-grid-drag-and-drop-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const DragAndDropMixin = (superClass) =>
}

static get observers() {
return ['_dragDropAccessChanged(rowsDraggable, dropMode, dragFilter, dropFilter)'];
return ['_dragDropAccessChanged(rowsDraggable, dropMode, dragFilter, dropFilter, loading)'];
}

/** @protected */
Expand Down Expand Up @@ -376,8 +376,9 @@ export const DragAndDropMixin = (superClass) =>
* @protected
*/
_filterDragAndDrop(row, model) {
const dragDisabled = !this.rowsDraggable || (this.dragFilter && !this.dragFilter(model));
const dropDisabled = !this.dropMode || (this.dropFilter && !this.dropFilter(model));
const loading = this.loading || row.hasAttribute('loading');
const dragDisabled = !this.rowsDraggable || loading || (this.dragFilter && !this.dragFilter(model));
const dropDisabled = !this.dropMode || loading || (this.dropFilter && !this.dropFilter(model));

const draggableElements = Array.from(row.children).map((cell) => cell._content);

Expand Down
105 changes: 92 additions & 13 deletions packages/vaadin-grid/test/drag-and-drop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import '../vaadin-grid.js';
describe('drag and drop', () => {
let grid, dragData;

const getTestItems = () => [
{ first: 'foo', last: 'bar' },
{ first: 'baz', last: 'qux' }
];

const getDraggable = (grid, rowIndex = 0) => {
const row = Array.from(grid.$.items.children).filter((row) => row.index === rowIndex)[0];
const cellContent = row.querySelector('slot').assignedNodes()[0];
Expand Down Expand Up @@ -108,25 +113,13 @@ describe('drag and drop', () => {
await aTimeout(1);

dragData = {};
grid.items = [
{ first: 'foo', last: 'bar' },
{ first: 'baz', last: 'qux' }
];
grid.items = getTestItems();
flushGrid(grid);
if (grid._safari) {
await aTimeout();
}
});

afterEach(() => {
fireDragEnd(grid.$.table);
fireDrop(grid.$.table);
grid.rowsDraggable = false;
grid.dropMode = null;
grid.style.height = '';
grid.selectedItems = [];
});

it('should not be draggable by default', () => {
expect(getDraggable(grid)).not.to.be.ok;
});
Expand Down Expand Up @@ -692,6 +685,45 @@ describe('drag and drop', () => {
const row = getRows(grid.$.items)[0];
expect(row.getAttribute('dragstart')).to.equal('');
});

describe('filtering row drag - lazy loading', () => {
let finishLoadingItems;

beforeEach(() => {
grid.dataProvider = (_params, callback) => {
finishLoadingItems = (items) => callback(items || getTestItems());
};
});

it('should disable row drag while loading items', () => {
expect(getDraggable(grid, 1)).not.to.be.ok;
expect(grid.$.items.children[1].hasAttribute('drag-disabled')).to.be.true;
});

it('should enable row drag once loading has finished', () => {
finishLoadingItems();
expect(getDraggable(grid, 1)).to.be.ok;
expect(grid.$.items.children[1].hasAttribute('drag-disabled')).to.be.false;
});

it('should not run drag filter while loading items', () => {
grid.dragFilter = sinon.spy();
expect(grid.dragFilter.called).to.be.false;
});

it('should disable row drag for rows without an item', () => {
finishLoadingItems([getTestItems()[0], undefined]);
expect(getDraggable(grid, 1)).not.to.be.ok;
expect(grid.$.items.children[1].hasAttribute('drag-disabled')).to.be.true;
});

it('should enable row drag once items are available', () => {
finishLoadingItems([getTestItems()[0], undefined]);
finishLoadingItems();
expect(getDraggable(grid, 1)).to.be.ok;
expect(grid.$.items.children[1].hasAttribute('drag-disabled')).to.be.false;
});
});
});

describe('filtering row drop', () => {
Expand Down Expand Up @@ -748,6 +780,53 @@ describe('drag and drop', () => {
const e = fireDragOver(row, 'above');
expect(e.defaultPrevented).to.be.false;
});

describe('filtering row drop - lazy loading', () => {
let finishLoadingItems;

beforeEach(() => {
grid.dataProvider = (_params, callback) => {
finishLoadingItems = (items) => callback(items || getTestItems());
};
});

it('should disable drop on row while loading items', () => {
const row = grid.$.items.children[1];
fireDragOver(row, 'above');
expect(row.hasAttribute('dragover')).to.be.false;
expect(row.hasAttribute('drop-disabled')).to.be.true;
});

it('should enable drop on row once loading has finished', () => {
finishLoadingItems();
const row = grid.$.items.children[1];
fireDragOver(row, 'above');
expect(row.hasAttribute('dragover')).to.be.true;
expect(row.hasAttribute('drop-disabled')).to.be.false;
});

it('should not run drop filter while loading items', () => {
grid.dropFilter = sinon.spy();
expect(grid.dropFilter.called).to.be.false;
});

it('should disable drop on row for rows without an item', () => {
finishLoadingItems([getTestItems()[0], undefined]);
const row = grid.$.items.children[1];
fireDragOver(row, 'above');
expect(row.hasAttribute('dragover')).to.be.false;
expect(row.hasAttribute('drop-disabled')).to.be.true;
});

it('should enable drop on row once items are available', () => {
finishLoadingItems([getTestItems()[0], undefined]);
finishLoadingItems();
const row = grid.$.items.children[1];
fireDragOver(row, 'above');
expect(row.hasAttribute('dragover')).to.be.true;
expect(row.hasAttribute('drop-disabled')).to.be.false;
});
});
});

describe('auto scroll', () => {
Expand Down

0 comments on commit 180898a

Please sign in to comment.