diff --git a/packages/survey-core/src/panel.ts b/packages/survey-core/src/panel.ts index 3be7e650d3..94ab39605c 100644 --- a/packages/survey-core/src/panel.ts +++ b/packages/survey-core/src/panel.ts @@ -1461,25 +1461,22 @@ export class PanelModelBase extends SurveyElement } } protected onAddElement(element: IElement, index: number): void { + const survey = this.survey; + const fireNotification = !!this.survey && (element).prevSurvey !== this.survey; element.setSurveyImpl(this.surveyImpl); element.parent = this; this.markQuestionListDirty(); if (this.canBuildRows()) { this.updateRowsOnElementAdded(element); } - if (element.isPanel) { - var p = element; - if (this.survey) { - this.survey.panelAdded(p, index, this, this.root); - } - } else { - if (this.survey) { - var q = element; - this.survey.questionAdded(q, index, this, this.root); + if(fireNotification) { + if (element.isPanel) { + survey.panelAdded(element, index, this, this.root); + } else { + survey.questionAdded(element, index, this, this.root); } } if (!!this.addElementCallback) this.addElementCallback(element); - var self = this; ((element)).registerPropertyChangedHandlers( ["visible", "isVisible"], () => { this.onElementVisibilityChanged(element); diff --git a/packages/survey-core/src/survey-element.ts b/packages/survey-core/src/survey-element.ts index 294a142123..69dfd967fb 100644 --- a/packages/survey-core/src/survey-element.ts +++ b/packages/survey-core/src/survey-element.ts @@ -855,6 +855,7 @@ export class SurveyElement extends SurveyElementCore implements ISurvey protected setPage(parent: IPanel, newPage: IPage): void { const oldPage: IPage = this.getPage(parent); + (this).prevSurvey = this.survey; //fix for the creator v1: https://github.com/surveyjs/survey-creator/issues/1744 if (typeof newPage === "string") { @@ -869,6 +870,7 @@ export class SurveyElement extends SurveyElementCore implements ISurvey if (newPage) { newPage.addElement((this), -1); } + (this).prevSurvey = undefined; } protected getSearchableLocKeys(keys: Array) { keys.push("title"); diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 6f0216df9f..2f43ea388e 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -16171,6 +16171,28 @@ QUnit.test("Randomized questions and onQuestionAdded", function (assert) { survey.currentPageNo = 1; assert.equal(counter, 0, "onQuestionAdded is not fired"); }); +QUnit.test("onQuestionAdded & changing parent", function (assert) { + const survey = new SurveyModel({ + pages: [ + { elements: [{ type: "text", name: "q1" }, { type: "text", name: "q2" }] }, + { + questionsOrder: "random", + elements: [{ type: "text", name: "q3" }, { type: "text", name: "q4" }] + } + ] + }); + var counter = 0; + survey.onQuestionAdded.add((sender, options) => { + counter++; + }); + assert.equal(counter, 0, "onQuestionAdded is not fired, #1"); + survey.getQuestionByName("q1").page = survey.pages[1]; + survey.getQuestionByName("q3").page = survey.pages[0]; + assert.equal(counter, 0, "onQuestionAdded is not fired, #2"); + const q = new QuestionTextModel("q5"); + q.page = survey.pages[1]; + assert.equal(counter, 1, "onQuestionAdded is fired for q5, #3"); +}); QUnit.test("Set values into radiogroup and checkbox questions before creating them", function (assert) { const survey = new SurveyModel(); survey.data = { q1: 1, q2: [1, 2] };