Skip to content

Commit

Permalink
The onQuestionAdded event is raised when moving a question to another…
Browse files Browse the repository at this point in the history
… page using the Layout | Move the question to page property grid editor #9073 (#9074)
  • Loading branch information
andrewtelnov authored Nov 19, 2024
1 parent ed4e24d commit 91e647d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/survey-core/src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1461,25 +1461,22 @@ export class PanelModelBase extends SurveyElement<Question>
}
}
protected onAddElement(element: IElement, index: number): void {
const survey = this.survey;
const fireNotification = !!this.survey && (<any>element).prevSurvey !== this.survey;
element.setSurveyImpl(this.surveyImpl);
element.parent = this;
this.markQuestionListDirty();
if (this.canBuildRows()) {
this.updateRowsOnElementAdded(element);
}
if (element.isPanel) {
var p = <PanelModel>element;
if (this.survey) {
this.survey.panelAdded(p, index, this, this.root);
}
} else {
if (this.survey) {
var q = <Question>element;
this.survey.questionAdded(q, index, this, this.root);
if(fireNotification) {
if (element.isPanel) {
survey.panelAdded(<PanelModel>element, index, this, this.root);
} else {
survey.questionAdded(<Question>element, index, this, this.root);
}
}
if (!!this.addElementCallback) this.addElementCallback(element);
var self = this;
(<Base>(<any>element)).registerPropertyChangedHandlers(
["visible", "isVisible"], () => {
this.onElementVisibilityChanged(element);
Expand Down
2 changes: 2 additions & 0 deletions packages/survey-core/src/survey-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey

protected setPage(parent: IPanel, newPage: IPage): void {
const oldPage: IPage = this.getPage(parent);
(<any>this).prevSurvey = this.survey;

//fix for the creator v1: https://github.com/surveyjs/survey-creator/issues/1744
if (typeof newPage === "string") {
Expand All @@ -869,6 +870,7 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey
if (newPage) {
newPage.addElement(<IElement>(<any>this), -1);
}
(<any>this).prevSurvey = undefined;
}
protected getSearchableLocKeys(keys: Array<string>) {
keys.push("title");
Expand Down
22 changes: 22 additions & 0 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] };
Expand Down

0 comments on commit 91e647d

Please sign in to comment.