diff --git a/packages/survey-core/src/survey.ts b/packages/survey-core/src/survey.ts index fa94921290..1ff7fb098a 100644 --- a/packages/survey-core/src/survey.ts +++ b/packages/survey-core/src/survey.ts @@ -3161,6 +3161,27 @@ export class SurveyModel extends SurveyElementCore this.valuesHash = {}; this.setDataCore(data, !data); } + public calcWithExpressionMap(map: any) { + const data = this.data; + const res = {}; + Object.keys(map).forEach(key => { + const mapValue = (typeof (map[key]) === "object") ? map[key] : { expr: map[key] } as any; + const val = new ExpressionRunner(mapValue.expr).run(data); + let newVal = null; + if (typeof (mapValue.values) === "object") { + newVal = mapValue.values[val]; + } + else { + if (mapValue.values === undefined) { + newVal = val; + } else { + newVal = Array.isArray(val) ? val.indexOf(mapValue.values) >= 0 : mapValue.values == val; + } + } + res[key] = newVal; + }); + return res; + } /** * Merges a specified data object with the object from the [`data`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#data) property. * @@ -8188,7 +8209,7 @@ export class SurveyModel extends SurveyElementCore const advHeader = layoutElement && layoutElement.data as Cover; if (this.showTOC && !(advHeader && advHeader.hasBackground)) { if (container === "center") { - containerLayoutElements.push(layoutElement); + containerLayoutElements.push(layoutElement); } } else { if (layoutElement.container === container) { diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 251cf86d11..fac47c0bd7 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -21986,3 +21986,98 @@ QUnit.test("Don't rise onPageAdded when mooving question", function (assert) { assert.equal(pageAddedRaisedCount, 0, "onPageAdded is not raised"); assert.equal(survey.pages[0].name, "page2", "page2 is the first page"); }); +QUnit.test("test calcWithExpressionMap function", function (assert) { + var survey = new SurveyModel({ + "pages": [ + { + "name": "page1", + "elements": [ + { + "type": "text", + "name": "question1" + }, + { + "type": "text", + "name": "question2" + } + ] + } + ] + }); + assert.deepEqual(survey.data, {}, "there is no data"); + survey.data = { question1: "item1", question2: 5 }; + assert.deepEqual( + survey.data, + { question1: "item1", question2: 5 }, + "set the object" + ); + + const map = { + "f1": "{question1}", + "f2": { + expr: "{question2}" + }, + "f1_1": "question1" + }; + + const mappedData = survey.calcWithExpressionMap(map); + + assert.deepEqual( + mappedData, + { f1: "item1", f2: 5, f1_1: "question1" }, + "set the object" + ); +}); + +QUnit.test("test calcWithExpressionMap function for radio and checkboxes", function (assert) { + var survey = new SurveyModel({ + "pages": [ + { + "name": "page1", + "elements": [ + { + "type": "radiogroup", + "name": "question1", + "choiсes": ["item1", "item2", "item3"] + }, + { + "type": "checkboxes", + "name": "question2", + "choiсes": ["item1", "item2", "item3"] + } + ] + } + ] + }); + survey.data = { question1: "item1", question2: ["item1", "item3"] }; + + const map = { + "f1": { + expr: "{question1}", + values: { + "item1": "1", + "item2": "2", + "item3": "3" + } + }, + "f2_1": { + expr: "{question2}", + values: "item1" + }, + "f2_2": { + expr: "{question2}", + values: "item2" + }, + "f2_3": { + expr: "{question2}", + values: "item3" + } + }; + + const mappedData = survey.calcWithExpressionMap(map); + + assert.deepEqual( + mappedData, + { f1: "1", f2_1: true, f2_2: false, f2_3: true }, + ); +});