Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Survey.toJson() return modified json if the questionsOnPageMode prope… #7363

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export class JsonObjectProperty implements IObject {
public minValue: any;
private dataListValue: Array<string>;
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;
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = [];
Expand Down
16 changes: 8 additions & 8 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
68 changes: 68 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});