Skip to content

Commit

Permalink
Add an API method that would return survey data with or without pages…
Browse files Browse the repository at this point in the history
… and panels fix #8421
  • Loading branch information
andrewtelnov committed Jun 28, 2024
1 parent ca92644 commit 847fcc0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ export class JsonObject {
options?: ISaveToJSONOptions | boolean
): any {
if (!obj || !obj.getType) return obj;
if (typeof obj.getData === "function") return obj.getData();
if (!obj.isSurvey && typeof obj.getData === "function") return obj.getData();
var result = {};
if (property != null && !property.className) {
(<any>result)[JsonObject.typePropertyName] = property.getObjType(
Expand Down
41 changes: 20 additions & 21 deletions src/survey-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ export abstract class SurveyElementCore extends Base implements ILocalizableOwne
protected createLocTitleProperty(): LocalizableString {
return this.createLocalizableString("title", this, true);
}
/**
* Returns `true` if the survey element is a page.
* @see Base.getType
*/
public get isPage(): boolean { return false; }
/**
* Returns `true` if the survey element is a panel.
* @see Base.getType
*/
public get isPanel(): boolean { return false; }
/**
* Returns `true` if the survey element is a question.
* @see Base.getType
*/
public get isQuestion(): boolean { return false; }
/**
* Returns `true` if the element is a survey.
* @see Base.getType
*/
public get isSurvey(): boolean { return false; }
/**
* A title for the survey element. If `title` is undefined, the `name` property value is displayed instead.
*
Expand Down Expand Up @@ -671,27 +691,6 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey
public setVisibleIndex(index: number): number {
return 0;
}
/**
* Returns `true` if the survey element is a page.
* @see Base.getType
*/
public get isPage(): boolean {
return false;
}
/**
* Returns `true` if the survey element is a panel.
* @see Base.getType
*/
public get isPanel(): boolean {
return false;
}
/**
* Returns `true` if the survey element is a question.
* @see Base.getType
*/
public get isQuestion(): boolean {
return false;
}
public delete(doDispose: boolean): void { }
//ILocalizableOwner
locOwner: ILocalizableOwner;
Expand Down
6 changes: 6 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,12 @@ export class SurveyModel extends SurveyElementCore
this.runConditions();
this.updateAllQuestionsValue(clearData);
}
public get isSurvey(): boolean { return true; }
public getData(options?: { includePages?: boolean, includePanels?: boolean}): any {
const opt = options || { includePages: false, includePanels: false };
if(!opt.includePages && !opt.includePanels) return this.data;
return this.getStructuredData(!!opt.includePages, !opt.includePanels ? (opt.includePages ? 1 : 0) : -1);
}
public getStructuredData(includePages: boolean = true, level: number = -1): any {
if (level === 0) return this.data;
const data: any = {};
Expand Down
24 changes: 24 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17956,6 +17956,30 @@ QUnit.test("getStructuredData function", function (assert) {
}, "includePages: false, level: 3");

});
QUnit.test("getData function", function (assert) {
const survey = new SurveyModel(structedDataSurveyJSON);
survey.setValue("q1", 100);
survey.setValue("q2", 200);
survey.setValue("q3", 300);
survey.setValue("q21", 2100);
survey.setValue("q22", 2200);
const data = survey.data;
assert.deepEqual(survey.getData(), data, "survey.getData()");
assert.deepEqual(survey.getData({}), data, "survey.getData({})");
assert.deepEqual(survey.getData({ includePages: false, includePanels: false }), data, "survey.getData({ includePages: false, includePanels: false })");
assert.deepEqual(survey.getData({ includePages: true, includePanels: false }), {
page1: { q1: 100, q2: 200, q3: 300 },
page2: { q21: 2100, q22: 2200 },
}, "survey.getData({ includePages: true, includePanels: false })");
assert.deepEqual(survey.getData({ includePages: true, includePanels: true }), {
page1: { q1: 100, panel1: { q2: 200, panel2: { q3: 300 } } },
page2: { q21: 2100, panel21: { q22: 2200 } },
}, "survey.getData({ includePages: true, includePanels: true })");
assert.deepEqual(survey.getData({ includePages: false, includePanels: true }), {
q1: 100, panel1: { q2: 200, panel2: { q3: 300 } },
q21: 2100, panel21: { q22: 2200 },
}, "survey.getData({ includePages: true, includePanels: true })");
});
QUnit.test("setStructuredData function", function (assert) {
const survey = new SurveyModel(structedDataSurveyJSON);
survey.setStructuredData({
Expand Down

0 comments on commit 847fcc0

Please sign in to comment.