From 49977caed0ac6e7c6db217a42e960365c41f8a23 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Wed, 3 Jan 2024 12:02:55 +0200 Subject: [PATCH] The creator.onPanelAdded event is invoked when reordering panels fix #7621 --- src/drag-drop-page-helper-v1.ts | 3 +- src/survey.ts | 25 ++++++++------ tests/dragdroptests.ts | 58 +++++++++++++++++++++++++++------ tests/questionDropdownTests.ts | 1 + tests/question_tagbox_tests.ts | 1 + 5 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/drag-drop-page-helper-v1.ts b/src/drag-drop-page-helper-v1.ts index 53edc69ca3..bee463d280 100644 --- a/src/drag-drop-page-helper-v1.ts +++ b/src/drag-drop-page-helper-v1.ts @@ -125,9 +125,8 @@ export class DragDropPageHelperV1 { elementsToResetSWNL.push(target); } } - + (this.page.survey as SurveyModel).startMovingQuestion(); if (!!src && !!src.parent) { - (this.page.survey as SurveyModel).startMovingQuestion(); isSamePanel = row.panel == src.parent; if (isSamePanel) { row.panel.dragDropMoveElement(src, target, targetIndex); diff --git a/src/survey.ts b/src/survey.ts index 2693b92c0a..3e157895de 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -6550,7 +6550,7 @@ export class SurveyModel extends SurveyElementCore } this.updateVisibleIndexes(); this.setCalculatedWidthModeUpdater(); - if (!this.isMovingQuestion || this.isDesignMode && !settings.supportCreatorV2) { + if (this.canFireAddElement()) { this.onQuestionAdded.fire(this, { question: question, name: question.name, @@ -6562,6 +6562,9 @@ export class SurveyModel extends SurveyElementCore }); } } + private canFireAddElement(): boolean { + return !this.isMovingQuestion || this.isDesignMode && !settings.supportCreatorV2; + } questionRemoved(question: Question) { this.questionHashesRemoved( question, @@ -6681,15 +6684,17 @@ export class SurveyModel extends SurveyElementCore } this.questionHashesPanelAdded((panel)); this.updateVisibleIndexes(); - this.onPanelAdded.fire(this, { - panel: panel, - name: panel.name, - index: index, - parent: parentPanel, - page: rootPanel, - parentPanel: parentPanel, - rootPanel: rootPanel, - }); + if (this.canFireAddElement()) { + this.onPanelAdded.fire(this, { + panel: panel, + name: panel.name, + index: index, + parent: parentPanel, + page: rootPanel, + parentPanel: parentPanel, + rootPanel: rootPanel, + }); + } } panelRemoved(panel: PanelModel) { this.updateVisibleIndexes(); diff --git a/tests/dragdroptests.ts b/tests/dragdroptests.ts index 297848ef9e..7d4dfc1a5d 100644 --- a/tests/dragdroptests.ts +++ b/tests/dragdroptests.ts @@ -990,35 +990,73 @@ QUnit.test("Move flow panel up", function (assert) { QUnit.test("Check onQuestionAdded event is not fired", function (assert) { var survey = new SurveyModel(); var page = survey.addNewPage("p1"); + var question = page.addNewQuestion("question1"); var q1 = page.addNewQuestion("text", "q1"); var q2 = page.addNewQuestion("text", "q2"); var q3 = page.addNewQuestion("text", "q3"); - var target = new QuestionTextModel("q1"); + var target = new QuestionTextModel("question1"); var eventIsFired = false; survey.onQuestionAdded .add(function (sender, options) { eventIsFired = true; }); - page.dragDropStart(q1, target); - page.dragDropMoveTo(q3, true); + page.dragDropStart(question, target); + page.dragDropMoveTo(q1, true); page.dragDropFinish(); - assert.notOk(eventIsFired, "onQuestionAdded event is not fired while dragging"); + survey.setDesignMode(true); - target = new QuestionTextModel("q3"); - page.dragDropStart(q3, target); - page.dragDropMoveTo(q1, true); + page.dragDropStart(question, target); + page.dragDropMoveTo(q2, true); page.dragDropFinish(); - assert.notOk(eventIsFired, "onQuestionAdded event is fired while dragging, Creator V1"); + assert.ok(eventIsFired, "onQuestionAdded event is fired while dragging, Creator V1"); eventIsFired = false; settings.supportCreatorV2 = true; - target = new QuestionTextModel("q3"); - page.dragDropStart(q1, target); + page.dragDropStart(question, target); page.dragDropMoveTo(q3, true); page.dragDropFinish(); settings.supportCreatorV2 = false; assert.notOk(eventIsFired, "onQuestionAdded event is not fired while dragging, Creator V2"); + + page.addNewQuestion("text", "q5"); + assert.ok(eventIsFired, "onQuestionAdded event is fired on adding new question"); +}); +QUnit.test("Check onPanelAdded event is not fired", function (assert) { + settings.supportCreatorV2 = false; + var survey = new SurveyModel(); + var page = survey.addNewPage("p1"); + var panel1 = page.addNewPanel("panel1"); + var q1 = page.addNewQuestion("text", "q1"); + var q2 = page.addNewQuestion("text", "q2"); + var q3 = page.addNewQuestion("text", "q3"); + var target = new PanelModel("panel1"); + var eventIsFired = false; + survey.onPanelAdded + .add(function (sender, options) { + eventIsFired = true; + }); + page.dragDropStart(panel1, target); + page.dragDropMoveTo(q1, true); + page.dragDropFinish(); + assert.notOk(eventIsFired, "onPanelAdded event is not fired while dragging"); + + survey.setDesignMode(true); + page.dragDropStart(panel1, target); + page.dragDropMoveTo(q2, true); + page.dragDropFinish(); + assert.ok(eventIsFired, "onPanelAdded event is fired while dragging, Creator V1"); + + eventIsFired = false; + settings.supportCreatorV2 = true; + page.dragDropStart(panel1, target); + page.dragDropMoveTo(q3, true); + page.dragDropFinish(); + assert.notOk(eventIsFired, "onPanelAdded event is not fired while dragging, Creator V2"); + settings.supportCreatorV2 = false; + + page.addNewPanel("panel2"); + assert.ok(eventIsFired, "onPanelAdded event is fired on adding new panel"); }); diff --git a/tests/questionDropdownTests.ts b/tests/questionDropdownTests.ts index 46babf3e31..8a4b79a109 100644 --- a/tests/questionDropdownTests.ts +++ b/tests/questionDropdownTests.ts @@ -302,6 +302,7 @@ QUnit.test("deserialize showOptionsCaption & optionsCaption to placeholder & all }); QUnit.test("question.showClearButton", assert => { + settings.supportCreatorV2 = false; const json = { questions: [ { diff --git a/tests/question_tagbox_tests.ts b/tests/question_tagbox_tests.ts index c7ed472a51..0d261ed4c0 100644 --- a/tests/question_tagbox_tests.ts +++ b/tests/question_tagbox_tests.ts @@ -1294,6 +1294,7 @@ QUnit.test("TagBox readOnlyText property should be reactive, Bug#6830", (assert) assert.equal(q.dropdownListModel.filterStringPlaceholder, "en-sel", "dropdownlist en, #3"); }); QUnit.test("question.showClearButton", assert => { + settings.supportCreatorV2 = false; const json = { questions: [ {