diff --git a/src/base-interfaces.ts b/src/base-interfaces.ts index da5d97fb6e..4b17406f7a 100644 --- a/src/base-interfaces.ts +++ b/src/base-interfaces.ts @@ -253,7 +253,7 @@ export interface ISurveyElement extends IShortcutText { getType(): string; setVisibleIndex(value: number): number; locStrsChanged(): any; - delete(): any; + delete(doDispose?: boolean): void; toggleState(): void; stateChangedCallback(): void; getTitleToolbar(): AdaptiveActionContainer; diff --git a/src/panel.ts b/src/panel.ts index 2be12f52cc..1ba0376ff6 100644 --- a/src/panel.ts +++ b/src/panel.ts @@ -312,9 +312,11 @@ export class PanelModelBase extends SurveyElement (this.showTitle && this.isDesignMode && settings.designMode.showEmptyTitles) ); } - public delete(): void { + public delete(doDispose: boolean = true): void { this.removeFromParent(); - this.dispose(); + if(doDispose) { + this.dispose(); + } } protected removeFromParent(): void {} protected canShowTitle(): boolean { return true; } diff --git a/src/question.ts b/src/question.ts index d9c59f9bb4..227cdec66a 100644 --- a/src/question.ts +++ b/src/question.ts @@ -260,9 +260,13 @@ export class Question extends SurveyElement public getPanel(): IPanel { return null; } - public delete(): void { + public delete(doDispose: boolean = true): void { this.removeFromParent(); - this.dispose(); + if(doDispose) { + this.dispose(); + } else { + this.resetDependedQuestions(); + } } protected removeFromParent(): void { if (!!this.parent) { @@ -2324,10 +2328,13 @@ export class Question extends SurveyElement } public dispose(): void { super.dispose(); + this.resetDependedQuestions(); + this.destroyResizeObserver(); + } + private resetDependedQuestions(): void { for (var i = 0; i < this.dependedQuestions.length; i++) { this.dependedQuestions[i].resetDependedQuestion(); } - this.destroyResizeObserver(); } } function makeNameValid(str: string): string { diff --git a/src/survey-element.ts b/src/survey-element.ts index 1c460df1ff..72634fb499 100644 --- a/src/survey-element.ts +++ b/src/survey-element.ts @@ -661,7 +661,7 @@ export class SurveyElement extends SurveyElementCore implements ISurvey public get isQuestion() { return false; } - public delete() { } + public delete(doDispose: boolean): void { } //ILocalizableOwner locOwner: ILocalizableOwner; /** diff --git a/tests/question_baseselecttests.ts b/tests/question_baseselecttests.ts index 4ae4b4eb5b..168eb30958 100644 --- a/tests/question_baseselecttests.ts +++ b/tests/question_baseselecttests.ts @@ -1173,6 +1173,21 @@ QUnit.test("Check isUsingCarryForward on deleting matrix dynamic question", func assert.notOk(q2.choicesFromQuestion, "it is empty"); assert.equal(q2.isUsingCarryForward, false, "Carryforward flag is unset"); }); +QUnit.test("Check isUsingCarryForward on deleting matrix dynamic question with doDispose = false parameter", function (assert) { + const survey = new SurveyModel(); + survey.setDesignMode(true); + survey.fromJSON({ elements: [ + { type: "matrixdynamic", name: "q1" }, + { type: "dropdown", name: "q2", choicesFromQuestion: "q1" } + ] }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + assert.equal(q2.choicesFromQuestion, "q1", "set correctly"); + assert.equal(q2.isUsingCarryForward, true, "Carryforward flag is set"); + q1.delete(false); + assert.notOk(q2.choicesFromQuestion, "it is empty"); + assert.equal(q2.isUsingCarryForward, false, "Carryforward flag is unset"); +}); QUnit.test("Use carryForward with panel dynamic + update data on survey.data=data;", function (assert) { const survey = new SurveyModel({ elements: [ { type: "checkbox", name: "q2", choicesFromQuestion: "q1", choiceValuesFromQuestion: "q1-q2", choiceTextsFromQuestion: "q1-q3" },