Skip to content

Commit

Permalink
Fixed #5291 - creator.onQuestionAdded event is not fired on dragging …
Browse files Browse the repository at this point in the history
…an item from a toolbox (#5300)

Co-authored-by: tsv2013 <tsv2013@noreply.github.com>
  • Loading branch information
tsv2013 and tsv2013 committed Mar 5, 2024
1 parent 9226e4c commit 9554c19
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 63 deletions.
7 changes: 4 additions & 3 deletions packages/survey-creator-core/src/survey-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,10 @@ export class DragDropSurveyElements extends DragDropCore<any> {
}
};

(page.survey as SurveyModel).startMovingQuestion();
if (src.parent || src.page) {
(src.parent || src.page).removeElement(src);
const srcContainer = src.parent || src.page;
if (!!srcContainer) {
(page.survey as SurveyModel).startMovingQuestion();
srcContainer.removeElement(src);
}
const dest = this.dragOverIndicatorElement?.isPanel ? this.dragOverIndicatorElement : this.dropTarget;
const isTargetIsContainer = dest.isPanel || dest.isPage;
Expand Down
200 changes: 140 additions & 60 deletions packages/survey-creator-core/tests/dragdrop-elements.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,11 @@ test("drag drop to panel vertical", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [{
"name": "q1",
"type": "text",
}]
{
"type": "panel", "name": "p1", elements: [{
"name": "q1",
"type": "text",
}]
}
],
}]
Expand All @@ -831,16 +832,17 @@ test("drag drop to panel vertical", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
}
]
{
"type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
}
]
}
],
}]
Expand All @@ -854,20 +856,21 @@ test("drag drop to panel vertical", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
},
{
"name": "q3",
"type": "text",
}
]
{
"type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
},
{
"name": "q3",
"type": "text",
}
]
}
],
}]
Expand Down Expand Up @@ -897,10 +900,11 @@ test("drag drop to panel horizontal", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [{
"name": "q1",
"type": "text",
}]
{
"type": "panel", "name": "p1", elements: [{
"name": "q1",
"type": "text",
}]
}
],
}]
Expand All @@ -914,17 +918,18 @@ test("drag drop to panel horizontal", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
"startWithNewLine": false
}
]
{
"type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
"startWithNewLine": false
}
]
}
],
}]
Expand All @@ -938,22 +943,23 @@ test("drag drop to panel horizontal", () => {
pages: [{
name: "page1",
"elements": [
{ "type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
"startWithNewLine": false
},
{
"name": "q3",
"type": "text",
"startWithNewLine": false
}
]
{
"type": "panel", "name": "p1", elements: [
{
"name": "q2",
"type": "text",
},
{
"name": "q1",
"type": "text",
"startWithNewLine": false
},
{
"name": "q3",
"type": "text",
"startWithNewLine": false
}
]
}
],
}]
Expand Down Expand Up @@ -1105,7 +1111,7 @@ test("Support onDragDropAllow, Bug#4572", (): any => {
let sourceName = "";
let parentName = "";
creator.onDragDropAllow.add((sender, options) => {
counter ++;
counter++;
surveyQuestionCount = options.survey.getAllQuestions().length;
targetName = options.target.name;
sourceName = options.source.name;
Expand Down Expand Up @@ -1222,4 +1228,78 @@ test("Do not allow to drag inside panel", () => {
checkAllowDragOver(p2);
checkAllowDragOver(q1);
checkAllowDragOver(q2);
});

test("onQuestionAdded doesn't fire when drag drop existing element", () => {
const json = {
"elements": [
{ "type": "text", "name": "q1", },
{ "type": "text", "name": "q2", },
{ "type": "text", "name": "q3", },
]
};
const survey = new SurveyModel(json);
let log = "";
survey.onQuestionAdded.add((s, o) => {
log += "->added:" + o.question.name;
});

const q1 = survey.getQuestionByName("q1");
const q3 = survey.getQuestionByName("q3");

const ddHelper: any = new DragDropSurveyElements(survey);
ddHelper.draggedElement = q3;

ddHelper.dragOverCore(q1, DragTypeOverMeEnum.Bottom);
ddHelper.doDrop();
expect(survey.toJSON()).toStrictEqual({
"pages": [
{
"name": "page1",
"elements": [
{ "name": "q1", "type": "text", },
{ "name": "q3", "type": "text", },
{ "name": "q2", "type": "text", },
],
},
],
});
expect(log).toBe("");
});
test("onQuestionAdded fires when drag drop new element", () => {
const json = {
"elements": [
{ "type": "text", "name": "q1", },
{ "type": "text", "name": "q2", },
{ "type": "text", "name": "q3", },
]
};
const survey = new SurveyModel(json);
let log = "";
survey.onQuestionAdded.add((s, o) => {
log += "->added:" + o.question.name;
});

const q1 = survey.getQuestionByName("q1");
const q4 = new QuestionTextModel("q4");

const ddHelper: any = new DragDropSurveyElements(survey);
ddHelper.draggedElement = q4;

ddHelper.dragOverCore(q1, DragTypeOverMeEnum.Bottom);
ddHelper.doDrop();
expect(survey.toJSON()).toStrictEqual({
"pages": [
{
"name": "page1",
"elements": [
{ "name": "q1", "type": "text", },
{ "name": "q4", "type": "text", },
{ "name": "q2", "type": "text", },
{ "name": "q3", "type": "text", },
],
},
],
});
expect(log).toBe("->added:q4");
});

0 comments on commit 9554c19

Please sign in to comment.