From 3a6a20aa3205a5ff5d3463cf9187d2b0a0acff96 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Mon, 14 Oct 2024 17:23:24 +0300 Subject: [PATCH] Specialized Question Types - The question.isDesignMode doesn't return the actual value fix #8767 --- packages/survey-core/src/question_custom.ts | 37 +++++++++++++------ .../survey-core/tests/question_customtests.ts | 30 +++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/packages/survey-core/src/question_custom.ts b/packages/survey-core/src/question_custom.ts index 77d9849bad..fe810ab2c7 100644 --- a/packages/survey-core/src/question_custom.ts +++ b/packages/survey-core/src/question_custom.ts @@ -115,6 +115,7 @@ export interface ICustomQuestionTypeConfiguration { * A custom question. */ onLoaded?(question: Question): void; + onSetSurvey?(question: Question): void; /** * A function that is called after the entire question is rendered. * @@ -294,17 +295,25 @@ export class ComponentQuestionJSON { ); this.onInit(); } - public onInit() { - if (!this.json.onInit) return; - this.json.onInit(); + public onInit(): void { + if (!!this.json.onInit) { + this.json.onInit(); + } } - public onCreated(question: Question) { - if (!this.json.onCreated) return; - this.json.onCreated(question); + public onCreated(question: Question): void { + if (!!this.json.onCreated) { + this.json.onCreated(question); + } } - public onLoaded(question: Question) { - if (!this.json.onLoaded) return; - this.json.onLoaded(question); + public onLoaded(question: Question): void { + if (!!this.json.onLoaded) { + this.json.onLoaded(question); + } + } + public onSetSurvey(question: Question): void { + if (!!this.json.onSetSurvey) { + this.json.onSetSurvey(question); + } } public onAfterRender(question: Question, htmlElement: any): void { if (!this.json.onAfterRender) return; @@ -598,7 +607,7 @@ export abstract class QuestionCustomModelBase extends Question return res; } protected abstract getElement(): SurveyElement; - protected initElement(el: SurveyElement) { + protected initElement(el: SurveyElement): void { if (!el) return; el.setSurveyImpl(this); el.disableDesignActions = true; @@ -608,9 +617,15 @@ export abstract class QuestionCustomModelBase extends Question this.isSettingValOnLoading = true; super.setSurveyImpl(value, isLight); this.initElement(this.getElement()); + this.callSetSurvey(); this.isSettingValOnLoading = false; } - public onSurveyLoad() { + private callSetSurvey(): void { + if(!!this.survey) { + this.customQuestion?.onSetSurvey(this); + } + } + public onSurveyLoad(): void { super.onSurveyLoad(); if (!!this.getElement()) { this.getElement().onSurveyLoad(); diff --git a/packages/survey-core/tests/question_customtests.ts b/packages/survey-core/tests/question_customtests.ts index d2fa62f6c6..6ed910d2c0 100644 --- a/packages/survey-core/tests/question_customtests.ts +++ b/packages/survey-core/tests/question_customtests.ts @@ -3408,4 +3408,34 @@ QUnit.test("Dynamic serializable properties, bug#8852", function (assert) { assert.equal(q1.contentQuestion.maxRateDescription, "val2", "maxRateDescription"); assert.equal(q1.contentQuestion.hasMinRateDescription, true, "hasMinRateDescription"); assert.equal(q1.contentQuestion.hasMaxRateDescription, true, "hasMaxRateDescription"); +}); +QUnit.test("Single: Register and load from json", function (assert) { + let isDesignMode1Counter = 0; + let isDesignMode2Counter = 0; + ComponentCollection.Instance.add({ + name: "newquestion1", + questionJSON: { type: "text" }, + onSetSurvey: (question: Question): void => { + isDesignMode1Counter += question.isDesignMode ? 10: 1; + } + }); + ComponentCollection.Instance.add({ + name: "newquestion2", + elementsJSON: [{ type: "text", name: "q1" }], + onSetSurvey: (question: Question): void => { + isDesignMode2Counter += question.isDesignMode ? 10: 1; + } + }); + const survey = new SurveyModel(); + survey.setDesignMode(true); + survey.fromJSON({ + elements: [{ type: "newquestion1", name: "q1" }, { type: "newquestion2", name: "q2" }], + }); + assert.equal(isDesignMode1Counter, 10, "isDesignMode1 #1"); + assert.equal(isDesignMode2Counter, 10, "isDesignMode2 #1"); + survey.currentPage.addNewQuestion("newquestion1", "q3"); + survey.currentPage.addNewQuestion("newquestion2", "q4"); + assert.equal(isDesignMode1Counter, 20, "isDesignMode1 #2"); + assert.equal(isDesignMode2Counter, 20, "isDesignMode2 #2"); + ComponentCollection.Instance.clear(); }); \ No newline at end of file