From de62318f5c416be6ba317e0e625ef66f2cafec9f Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Thu, 16 Nov 2023 11:11:52 +0200 Subject: [PATCH] Survey.toJson() return modified json if the questionsOnPageMode property is singlePage fix #7359 --- src/jsonobject.ts | 10 ++++++- src/survey.ts | 16 +++++------ tests/surveytests.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/jsonobject.ts b/src/jsonobject.ts index 6975468aa4..4ade755dc8 100644 --- a/src/jsonobject.ts +++ b/src/jsonobject.ts @@ -263,6 +263,7 @@ export class JsonObjectProperty implements IObject { public minValue: any; private dataListValue: Array; public layout: string; + public onSerializeValue: (obj: any) => any; public onGetValue: (obj: any) => any; public onSettingValue: (obj: any, value: any) => any; public onSetValue: (obj: any, value: any, jsonConv: JsonObject) => any; @@ -349,6 +350,10 @@ export class JsonObjectProperty implements IObject { value === "" || Helpers.isValueEmpty(value) ); } + public getSerializableValue(obj: any): any { + if(!!this.onSerializeValue) return this.onSerializeValue(obj); + return this.getValue(obj); + } public getValue(obj: any): any { if (this.onGetValue) return this.onGetValue(obj); if (this.serializationProperty && !!obj[this.serializationProperty]) @@ -840,6 +845,9 @@ export class JsonMetadataClass { if (!!propInfo.baseValue) { prop.setBaseValue(propInfo.baseValue); } + if (propInfo.onSerializeValue) { + prop.onSerializeValue = propInfo.onSerializeValue; + } if (propInfo.onGetValue) { prop.onGetValue = propInfo.onGetValue; } @@ -1623,7 +1631,7 @@ export class JsonObject { (property.isLightSerializable === false && this.lightSerializing) ) return; - var value = property.getValue(obj); + var value = property.getSerializableValue(obj); if (!storeDefaults && property.isDefaultValueByObj(obj, value)) return; if (this.isValueArray(value)) { var arrValue = []; diff --git a/src/survey.ts b/src/survey.ts index 515c31fc0e..3d84622104 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -4039,17 +4039,17 @@ export class SurveyModel extends SurveyElementCore } } private changeCurrentPageFromPreview: boolean; - private origionalPages: any; + private originalPages: any; protected onQuestionsOnPageModeChanged(oldValue: string) { if (this.isShowingPreview) return; if (this.questionsOnPageMode == "standard" || this.isDesignMode) { - if (this.origionalPages) { - this.restoreOriginalPages(this.origionalPages); + if (this.originalPages) { + this.restoreOriginalPages(this.originalPages); } - this.origionalPages = undefined; + this.originalPages = undefined; } else { if (!oldValue || oldValue == "standard") { - this.origionalPages = this.pages.slice(0, this.pages.length); + this.originalPages = this.pages.slice(0, this.pages.length); } this.setupPagesForPageModes(this.isSinglePage); } @@ -5892,7 +5892,7 @@ export class SurveyModel extends SurveyElementCore page.num = isPageVisible ? page.visibleIndex + 1 : -1; } } - public fromJSON(json: any) { + public fromJSON(json: any): void { if (!json) return; this.questionHashesClear(); this.jsonErrors = null; @@ -5904,7 +5904,7 @@ export class SurveyModel extends SurveyElementCore this.onStateAndCurrentPageChanged(); this.updateState(); } - public setJsonObject(jsonObj: any) { + public setJsonObject(jsonObj: any): void { this.fromJSON(jsonObj); } private isEndLoadingFromJson: string = null; @@ -7489,7 +7489,7 @@ Serializer.addClass("survey", [ className: "htmlconditionitem", isArray: true }, { name: "loadingHtml:html", serializationProperty: "locLoadingHtml" }, - { name: "pages:surveypages", className: "page", isArray: true }, + { name: "pages:surveypages", className: "page", isArray: true, onSerializeValue: (obj: any): any => { return obj.originalPages || obj.pages; } }, { name: "elements", alternativeName: "questions", diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 5996038681..004c64f297 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -18082,3 +18082,71 @@ QUnit.test("clearInvisibleValues onHiddenContainer breaks defaultValueExpression assert.equal(q2.value, 24, "q1.value = B"); }); +QUnit.test("survey.toJSON() doesn't work correctly if questionsOnPageMode=singlePage is used #7359, #1", function (assert) { + const surveyJson = { + "questionsOnPageMode": "singlePage", + "pages": [ + { + "name": "page1", + "elements": [ + { + "type": "text", + "name": "first-name", + }, + { + "type": "text", + "name": "last-name", + }, + { + "type": "text", + "name": "birthdate", + "inputType": "date" + }, + ] + } + ], + }; + + const survey = new SurveyModel(surveyJson); + const prepareJSON = survey.toJSON(); + + assert.equal(surveyJson.pages[0].elements.length, 3, "surveyJson elements count"); + assert.equal(prepareJSON.pages[0].elements.length, 3, "prepareJSON elements count"); + + assert.deepEqual (surveyJson, prepareJSON); +}); +QUnit.test("survey.toJSON() doesn't work correctly if questionsOnPageMode=questionPerPage is used #7359, #2", function (assert) { + const surveyJson = { + "questionsOnPageMode": "questionPerPage", + "pages": [ + { + "name": "page1", + "elements": [ + { + "type": "text", + "name": "first-name", + }, + { + "type": "text", + "name": "last-name", + }, + { + "type": "text", + "name": "birthdate", + "inputType": "date" + }, + ] + } + ], + }; + + const survey = new SurveyModel(surveyJson); + const prepareJSON = survey.toJSON(); + + assert.equal(surveyJson.pages.length, 1, "surveyJson pages count"); + assert.equal(prepareJSON.pages.length, 1, "prepareJSON pages count"); + assert.equal(surveyJson.pages[0].elements.length, 3, "surveyJson elements count"); + assert.equal(prepareJSON.pages[0].elements.length, 3, "prepareJSON elements count"); + + assert.deepEqual (surveyJson, prepareJSON); +}); \ No newline at end of file