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

PR: LIBRARY: Fixed onStartDrag event #8651

Merged
merged 3 commits into from
Aug 6, 2024
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
5 changes: 4 additions & 1 deletion src/dragdrop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export abstract class DragDropCore<T> implements IDragDropEngine {

public onDragStart: EventBase<DragDropCore<T>> = new EventBase();
public onDragEnd: EventBase<DragDropCore<T>> = new EventBase();
public onDragClear: EventBase<DragDropCore<T>> = new EventBase();
public onBeforeDrop = this.onDragStart;
public onAfterDrop = this.onDragEnd;

Expand Down Expand Up @@ -63,6 +64,8 @@ export abstract class DragDropCore<T> implements IDragDropEngine {
event
);
this.onStartDrag(event);
const fromElement = this.draggedElement && this.draggedElement.parent;
this.onDragStart.fire(this, { fromElement: fromElement, draggedElement: this.draggedElement });
}

protected onStartDrag(event?: PointerEvent): void {
Expand Down Expand Up @@ -230,7 +233,6 @@ export abstract class DragDropCore<T> implements IDragDropEngine {
public drop(): void {
if (this.allowDropHere) {
const fromElement = this.draggedElement.parent;
this.onDragStart.fire(this, { fromElement: fromElement, draggedElement: this.draggedElement });
const newElement = this.doDrop();
this.onDragEnd.fire(this, { fromElement: fromElement, draggedElement: newElement, toElement: this.dropTarget });
}
Expand All @@ -242,5 +244,6 @@ export abstract class DragDropCore<T> implements IDragDropEngine {
this.draggedElement = null;
this.isBottom = null;
this.parentElement = null;
this.onDragClear.fire(this, {});
}
}
15 changes: 12 additions & 3 deletions tests/dragdrophelpertests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ QUnit.test("dropTargetDataAttributeName for choices", function (assert) {
);
});

QUnit.test("choices: onDragStart and onDragEnd events", function (assert) {
QUnit.test("choices: onDragStart, onDragEnd, onDragClear events", function (assert) {
const survey = new SurveyModel({
elements: [
{
Expand All @@ -131,6 +131,7 @@ QUnit.test("choices: onDragStart and onDragEnd events", function (assert) {
);
let beforeCount = 0;
let afterCount = 0;
let clearCount = 0;
let draggedElementParent;

const ddHelper: any = new DragDropChoices(survey);
Expand All @@ -141,17 +142,25 @@ QUnit.test("choices: onDragStart and onDragEnd events", function (assert) {
afterCount++;
draggedElementParent = options.draggedElement;
});
ddHelper.onDragClear.add((sender, options) => {
clearCount++;
});

ddHelper.parentElement = question;
ddHelper.draggedElement = question.choices[2];

ddHelper["createDraggedElementShortcut"] = ()=>{};
ddHelper.dragInit(null, ddHelper.draggedElement, ddHelper.parentElement, document.createElement("div"));
assert.equal(beforeCount, 1);
ddHelper["allowDropHere"] = true;
ddHelper["domAdapter"]["draggedElementShortcut"] = document.body.appendChild(
document.createElement("div")
);
ddHelper["allowDropHere"] = true;
ddHelper["drop"]();
assert.equal(beforeCount, 1);
assert.equal(afterCount, 1);
assert.equal(draggedElementParent.name, "q");
ddHelper["clear"]();
assert.equal(clearCount, 1);
});

QUnit.test("DragDropRankingChoices shortcutClass getter", function (assert) {
Expand Down