Skip to content

Commit

Permalink
Bug/7633 survey try navigate to page (#7634)
Browse files Browse the repository at this point in the history
* survey.onCurrentPageChanged event can be fired multiple times on clicking TOC or progress bar button fix #7633

* Fix unit test #7633
  • Loading branch information
andrewtelnov authored Jan 9, 2024
1 parent 8da6ad4 commit 9e712b4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 28 deletions.
18 changes: 18 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3131,6 +3131,24 @@ export class SurveyModel extends SurveyElementCore
this.currentPageChanged(newPage, oldValue);
}
}
public tryNavigateToPage(page: PageModel): boolean {
if (this.isDesignMode) return false;
const index = this.visiblePages.indexOf(page);
if(index < 0) return false;
if(index === this.currentPageNo) return false;
if (index < this.currentPageNo) {
this.currentPageNo = index;
return true;
}
for (let i = this.currentPageNo; i < index; i++) {
const page = this.visiblePages[i];
if(!page.validate(true, true)) return false;
page.passed = true;
}
this.currentPage = page;
return true;
}

private updateCurrentPage(): void {
if (this.isCurrentPageAvailable) return;
this.currentPage = this.firstVisiblePage;
Expand Down
10 changes: 1 addition & 9 deletions src/surveyProgressButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export class SurveyProgressButtonsModel {
.toString();
}
public clickListElement(index: number): void {
if (this.survey.isDesignMode) return;
if (index < this.survey.currentPageNo) {
this.survey.currentPageNo = index;
}
else if (index > this.survey.currentPageNo) {
for (let i: number = this.survey.currentPageNo; i < index; i++) {
if (!this.survey.nextPage()) break;
}
}
this.survey.tryNavigateToPage(this.survey.visiblePages[index]);
}
}
18 changes: 2 additions & 16 deletions src/surveyToc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@ import { PopupModel } from "./popup";
import { SurveyModel } from "./survey";
import { IsTouch } from "./utils/devices";

export function tryNavigateToPage(survey: SurveyModel, page: PageModel) {
if (survey.isDesignMode) return;
const index = survey.visiblePages.indexOf(page);
if (index < survey.currentPageNo) {
survey.currentPageNo = index;
}
else if (index > survey.currentPageNo) {
for (let i = survey.currentPageNo; i < index; i++) {
if (!survey.nextPageUIClick()) return false;
}
}
return true;
}

export function tryFocusPage(survey: SurveyModel, panel: PanelModelBase) {
export function tryFocusPage(survey: SurveyModel, panel: PanelModelBase): boolean {
if (survey.isDesignMode) return true;
panel.focusFirstQuestion();
return true;
Expand All @@ -40,7 +26,7 @@ export function createTOCListModel(survey: SurveyModel, onAction?: () => void) {
}
!!onAction && onAction();
if (page instanceof PageModel) {
return tryNavigateToPage(survey, page);
return survey.tryNavigateToPage(page);
}
return tryFocusPage(survey, page);
},
Expand Down
8 changes: 5 additions & 3 deletions tests/surveyProgressButtonsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SurveyProgressButtonsModel } from "../src/surveyProgressButtons";
export default QUnit.module("SurveyProgressButtons");

QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
let json: any = {
const json: any = {
"pages": [
{
"name": "page1",
Expand Down Expand Up @@ -35,8 +35,8 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
}
]
};
let survey: SurveyModel = new SurveyModel(json);
let progress: SurveyProgressButtonsModel = new SurveyProgressButtonsModel(survey);
const survey: SurveyModel = new SurveyModel(json);
const progress: SurveyProgressButtonsModel = new SurveyProgressButtonsModel(survey);
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementCurrent,
"1) Page 1 style is current");
Expand All @@ -46,6 +46,7 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
"", "1) Page 3 style is empty");

progress.clickListElement(2);
assert.equal(survey.currentPageNo, 2, "currentPageNo #1");
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementPassed,
"2) Page 1 style is passed");
Expand All @@ -57,6 +58,7 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
"2) Page 3 style is current");

progress.clickListElement(0);
assert.equal(survey.currentPageNo, 0, "currentPageNo #2");
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementPassed + " " +
survey.css.progressButtonsListElementCurrent,
Expand Down
65 changes: 65 additions & 0 deletions tests/surveyTOCTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,68 @@ QUnit.test("TOC shouldn't show search", function (assert) {
const tocListModel = createTOCListModel(survey);
assert.equal(tocListModel.searchEnabled, false, "Search in TOC should be disabled");
});
QUnit.test("survey.tryNavigateToPage", function (assert) {
let json: any = {
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "question1"
}
]
},
{
"name": "page2",
"elements": [
{
"type": "text",
"name": "question2",
"isRequired": true
}
]
},
{
"name": "page3",
"elements": [
{
"type": "text",
"name": "question3",
"isRequired": true
}
]
},
{
"name": "page4",
"elements": [
{
"type": "text",
"name": "question4"
}
]
}
]
};
const survey = new SurveyModel(json);
const pages = new Array<string>();
survey.onCurrentPageChanged.add((sender, options) => {
pages.push(options.newCurrentPage.name);
});
assert.equal(survey.currentPageNo, 0, "currentPageNo #1");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), false, "navigate #1");
assert.equal(survey.currentPageNo, 1, "currentPageNo #2");
assert.equal(survey.tryNavigateToPage(survey.pages[2]), false, "navigate #2");
assert.equal(survey.currentPageNo, 1, "currentPageNo #3");
assert.equal(survey.tryNavigateToPage(survey.pages[0]), true, "navigate #3");
assert.equal(survey.currentPageNo, 0, "currentPageNo #4");
survey.setValue("question2", "val2");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), false, "navigate #4");
assert.equal(survey.currentPageNo, 2, "currentPageNo #4");
assert.equal(survey.tryNavigateToPage(survey.pages[0]), true, "navigate #5");
assert.equal(survey.currentPageNo, 0, "currentPageNo #5");
survey.setValue("question3", "val3");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), true, "navigate #6");
assert.equal(survey.currentPageNo, 3, "currentPageNo #6");
assert.deepEqual(pages, ["page2", "page1", "page3", "page1", "page4"], "Check onCurrentPageChanged");
});

0 comments on commit 9e712b4

Please sign in to comment.