Skip to content

Commit 1b67445

Browse files
authored
Feature/10456 question start index 1.1 fix #10456 (#10462)
* Support survey showQuestionNumbers: "recursive" & questionStartIndex: "1.1." fix #10456 * FIx unit tests * Remove duplication for getStartIndex function
1 parent a0ddb74 commit 1b67445

File tree

6 files changed

+114
-20
lines changed

6 files changed

+114
-20
lines changed

packages/survey-core/src/page.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ export class PageModel extends PanelModel implements IPage {
160160
protected canRenderFirstRows(): boolean {
161161
return !this.isDesignMode || this.visibleIndex == 0;
162162
}
163+
protected isQuestionIndexRecursive(): boolean {
164+
const res = super.isQuestionIndexRecursive();
165+
return res && this.isComplexIndex(this.getStartIndex());
166+
}
167+
protected getPanelStartIndex(index: number): number {
168+
if (this.isQuestionIndexRecursive()) return 0;
169+
return index;
170+
}
171+
getQuestionStartIndex(): string {
172+
const res = this.getStartIndex();
173+
if (this.isQuestionIndexRecursive()) {
174+
return this.getQuestionStartIndexVsVisibleIndex(res, this.visibleIndex);
175+
}
176+
return res;
177+
}
163178
/**
164179
* Returns `true` if this page is a start page.
165180
*

packages/survey-core/src/panel.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,13 +1367,34 @@ export class PanelModelBase extends SurveyElement<Question>
13671367
(el as any).setPropertyValueDirectly("effectiveColSpan", result.length);
13681368
return result;
13691369
}
1370-
protected getStartIndex(): string {
1371-
if (!!this.parent) return this.parent.getQuestionStartIndex();
1372-
if (!!this.survey) return this.survey.questionStartIndex;
1373-
return "";
1370+
protected isQuestionIndexRecursive(): boolean {
1371+
return !!this.survey && this.survey.showQuestionNumbers === "recursive";
13741372
}
13751373
getQuestionStartIndex(): string {
1376-
return this.getStartIndex();
1374+
const res = this.getStartIndex();
1375+
if (this.isQuestionIndexRecursive()) {
1376+
return this.getStartIndexSubs(res, true);
1377+
}
1378+
return res;
1379+
}
1380+
protected getStartIndexSubs(str: string, isLast: boolean): string {
1381+
if (!str || !this.isComplexIndex(str)) return str;
1382+
const lastChar = str[str.length - 1] === "." ? "." : "";
1383+
if (lastChar) {
1384+
str = str.substring(0, str.length - 1);
1385+
}
1386+
const lastIndex = str.lastIndexOf(".");
1387+
str += lastChar;
1388+
return isLast ? str.substring(lastIndex + 1) : str.substring(0, lastIndex + 1);
1389+
}
1390+
protected getQuestionStartIndexVsVisibleIndex(str: string, index: number): string {
1391+
const prefix = this.getStartIndexSubs(str, false);
1392+
const postfix = this.getStartIndexSubs(str, true);
1393+
return Helpers.getNumberByIndex(index, prefix) + postfix;
1394+
}
1395+
protected isComplexIndex(str: string): boolean {
1396+
const index = str.indexOf(".");
1397+
return index > -1 && index < str.length - 1;
13771398
}
13781399
getChildrenLayoutType(): string {
13791400
return "row";
@@ -2207,7 +2228,7 @@ export class PanelModel extends PanelModelBase implements IElement {
22072228
this.notifySurveyOnVisibilityChanged();
22082229
}
22092230
public addNoFromChild(no: string): string {
2210-
if (this.isQuestionIndexRecursive)
2231+
if (this.isQuestionIndexRecursive())
22112232
return this.calcNo() + no;
22122233
return super.addNoFromChild(no);
22132234
}
@@ -2251,8 +2272,10 @@ export class PanelModel extends PanelModelBase implements IElement {
22512272
this.setPropertyValue("questionStartIndex", val);
22522273
}
22532274
getQuestionStartIndex(): string {
2254-
if (!!this.questionStartIndex) return this.questionStartIndex;
2255-
return super.getQuestionStartIndex();
2275+
const res = this.questionStartIndex;
2276+
if (!!res && this.isQuestionIndexOnPanel && this.isComplexIndex(res))
2277+
return this.getQuestionStartIndexVsVisibleIndex(res, this.visibleIndex);
2278+
return res || super.getQuestionStartIndex();
22562279
}
22572280
/**
22582281
* A question number or letter (depends on the `questionStartIndex` property).
@@ -2298,7 +2321,7 @@ export class PanelModel extends PanelModelBase implements IElement {
22982321
protected beforeSetVisibleIndex(index: number): number {
22992322
if (this.isPage || !this.parent) return super.beforeSetVisibleIndex(index);
23002323
let visibleIndex = -1;
2301-
if ((this.showNumber || this.isQuestionIndexRecursive) && (this.isDesignMode || !this.locTitle.isEmpty || this.hasParentInQuestionIndex())) {
2324+
if ((this.showNumber || this.isQuestionIndexRecursive()) && (this.isDesignMode || !this.locTitle.isEmpty || this.hasParentInQuestionIndex())) {
23022325
visibleIndex = index;
23032326
}
23042327
this.setPropertyValue("visibleIndex", visibleIndex);
@@ -2311,13 +2334,12 @@ export class PanelModel extends PanelModelBase implements IElement {
23112334
return index;
23122335
}
23132336
private get isQuestionIndexOnPanel(): boolean {
2314-
return this.showQuestionNumbers === "onpanel" || this.isQuestionIndexRecursive;
2337+
return this.showQuestionNumbers === "onpanel" || this.isQuestionIndexRecursive();
23152338
}
2316-
private get isQuestionIndexRecursive(): boolean {
2317-
if (this.isPage) return false;
2339+
protected isQuestionIndexRecursive(): boolean {
23182340
const val = this.showQuestionNumbers;
23192341
if (val !== "default") return val === "recursive";
2320-
return !!this.survey && this.survey.showQuestionNumbers === "recursive";
2342+
return super.isQuestionIndexRecursive();
23212343
}
23222344
private hasParentInQuestionIndex(): boolean {
23232345
if (!this.isQuestionIndexOnPanel) return false;

packages/survey-core/src/question.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,9 +2125,11 @@ export class Question extends SurveyElement<Question>
21252125
private calcNo(): string {
21262126
if (!this.hasTitle || !this.showNumber) return "";
21272127
let parentIndex: number | undefined;
2128+
/*
21282129
if (!!this.parent) {
21292130
parentIndex = (<any>this.parent).visibleIndex;
21302131
}
2132+
*/
21312133
let no = Helpers.getNumberByIndex(this.visibleIndex, this.getStartIndex(), parentIndex);
21322134
if (!!this.parent) {
21332135
no = (<any>this.parent).addNoFromChild(no);
@@ -2137,11 +2139,6 @@ export class Question extends SurveyElement<Question>
21372139
}
21382140
return no;
21392141
}
2140-
protected getStartIndex(): string {
2141-
if (!!this.parent) return this.parent.getQuestionStartIndex();
2142-
if (!!this.survey) return this.survey.questionStartIndex;
2143-
return "";
2144-
}
21452142
public onSurveyLoad(): void {
21462143
this.isCustomWidgetRequested = false;
21472144
this.fireCallback(this.surveyLoadCallback);

packages/survey-core/src/survey-element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,11 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey
851851
public setVisibleIndex(index: number): number {
852852
return 0;
853853
}
854+
protected getStartIndex(): string {
855+
if (!!this.parent) return this.parent.getQuestionStartIndex();
856+
if (!!this.survey) return this.survey.questionStartIndex;
857+
return "";
858+
}
854859
public delete(doDispose: boolean): void { }
855860
//ILocalizableOwner
856861
locOwner: ILocalizableOwner;

packages/survey-core/tests/paneltests.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3478,7 +3478,7 @@ QUnit.test("panel.showQuestionNumbers: 'recursive'", function (assert) {
34783478
assert.equal(survey.getQuestionByName("q6").no, "2.2.", "q6.no");
34793479
assert.equal(survey.getQuestionByName("q7").no, "3.", "q7.no");
34803480
});
3481-
QUnit.test("panel.showQuestionNumbers: 'recursive'", function (assert) {
3481+
QUnit.test("survey.showQuestionNumbers: 'recursive' & panels", function (assert) {
34823482
const survey = new SurveyModel({
34833483
showQuestionNumbers: "recursive",
34843484
elements: [
@@ -3520,6 +3520,61 @@ QUnit.test("panel.showQuestionNumbers: 'recursive'", function (assert) {
35203520
assert.equal(survey.getQuestionByName("q6").no, "2.2.", "q6.no");
35213521
assert.equal(survey.getQuestionByName("q7").no, "3.", "q7.no");
35223522
});
3523+
QUnit.test("survey.showQuestionNumbers: 'recursive' & panels & questionStartIndex 1.1, Issue#10456", function (assert) {
3524+
const survey = new SurveyModel();
3525+
survey.fromJSON({
3526+
showQuestionNumbers: "recursive",
3527+
questionStartIndex: "1.1.",
3528+
pages: [{
3529+
elements: [
3530+
{
3531+
type: "panel", name: "panel1", title: "Panel 1",
3532+
showNumber: true,
3533+
elements: [
3534+
{
3535+
type: "panel", name: "panel2",
3536+
title: "Panel 2",
3537+
elements: [
3538+
{ type: "text", name: "q1" },
3539+
{ type: "text", name: "q2" }
3540+
]
3541+
},
3542+
{ type: "text", name: "q3" },
3543+
{ type: "text", name: "q4" }
3544+
]
3545+
},
3546+
{ type: "panel", name: "panel3", showNumber: true, title: "Panel 4", elements: [
3547+
{ type: "text", name: "q5" }
3548+
] },
3549+
{ type: "text", name: "q6" }
3550+
] },
3551+
{
3552+
elements: [
3553+
{
3554+
type: "panel", name: "panel4",
3555+
showNumber: true, title: "Panel 3",
3556+
elements: [
3557+
{ type: "text", name: "q7" },
3558+
{ type: "text", name: "q8" }
3559+
]
3560+
},
3561+
{ type: "text", name: "q9" }
3562+
] }]
3563+
});
3564+
assert.equal(survey.getPanelByName("panel1").no, "1.1.", "panel1.no");
3565+
assert.equal(survey.getPanelByName("panel2").no, "1.1.1.", "panel2.no");
3566+
assert.equal(survey.getPanelByName("panel3").no, "1.2.", "panel2.no");
3567+
assert.equal(survey.getPanelByName("panel4").no, "2.1.", "panel4.no");
3568+
assert.equal(survey.getQuestionByName("q1").no, "1.1.1.1.", "q1.no");
3569+
assert.equal(survey.getQuestionByName("q2").no, "1.1.1.2.", "q2.no");
3570+
assert.equal(survey.getQuestionByName("q3").no, "1.1.2.", "q3.no");
3571+
assert.equal(survey.getQuestionByName("q4").no, "1.1.3.", "q4.no");
3572+
assert.equal(survey.getQuestionByName("q5").no, "1.2.1.", "q5.no");
3573+
assert.equal(survey.getQuestionByName("q6").no, "1.3.", "q6.no");
3574+
assert.equal(survey.getQuestionByName("q7").no, "2.1.1.", "q7.no");
3575+
assert.equal(survey.getQuestionByName("q8").no, "2.1.2.", "q8.no");
3576+
assert.equal(survey.getQuestionByName("q9").no, "2.2.", "q9.no");
3577+
});
35233578
QUnit.test("Check that startWithNewLine doesn't trigger animation", (assert) => {
35243579
settings.animationEnabled = true;
35253580
const survey = new SurveyModel({

packages/survey-core/tests/surveyquestiontests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8378,7 +8378,7 @@ QUnit.test("survey.validateVisitedEmptyFields #8640", function (assert) {
83788378
assert.equal(q.errors.length, 1, q.name + " errors");
83798379
});
83808380
});
8381-
QUnit.test("Show parent number in the question #8813, #1", function (assert) {
8381+
QUnit.test("Show parent number in the question Bug#8813, #1", function (assert) {
83828382
const survey = new SurveyModel({
83838383
showQuestionNumbers: "on",
83848384
elements: [

0 commit comments

Comments
 (0)