Skip to content

Commit

Permalink
survey.onCurrentPageChanging/Changed calls several times on cancelPre…
Browse files Browse the repository at this point in the history
…view function fix #6564 (#6566)
  • Loading branch information
andrewtelnov authored Jul 23, 2023
1 parent e6f68f5 commit c6404eb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/survey-events-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export interface CurrentPageChangedEvent {
* A page that used to be current.
*/
oldCurrentPage: PageModel;
isAfterPreview: boolean;
}
export interface CurrentPageChangingEvent extends CurrentPageChangedEvent {
/**
Expand Down
35 changes: 25 additions & 10 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2955,15 +2955,17 @@ export class SurveyModel extends SurveyElementCore
if (newPage != null && vPages.indexOf(newPage) < 0) return;
if (newPage == this.currentPage) return;
var oldValue = this.currentPage;
if (!this.currentPageChanging(newPage, oldValue)) return;
if (!this.isShowingPreview && !this.currentPageChanging(newPage, oldValue)) return;
this.setPropertyValue("currentPage", newPage);
if (!!newPage) {
newPage.onFirstRendering();
newPage.updateCustomWidgets();
newPage.setWasShown(true);
}
this.locStrsChanged();
this.currentPageChanged(newPage, oldValue);
if(!this.isShowingPreview) {
this.currentPageChanged(newPage, oldValue);
}
}
private updateCurrentPage(): void {
if (this.isCurrentPageAvailable) return;
Expand Down Expand Up @@ -3246,7 +3248,8 @@ export class SurveyModel extends SurveyElementCore
isNextPage: diff === 1,
isPrevPage: diff === -1,
isGoingForward: diff > 0,
isGoingBackward: diff < 0
isGoingBackward: diff < 0,
isAfterPreview: this.changeCurrentPageFromPreview === true
};
}
/**
Expand Down Expand Up @@ -3822,14 +3825,10 @@ export class SurveyModel extends SurveyElementCore
*/
public cancelPreview(curPage: any = null) {
if (!this.isShowingPreview) return;
this.gotoPageFromPreview = curPage;
this.isShowingPreview = false;
if (Helpers.isValueEmpty(curPage) && this.visiblePageCount > 0) {
curPage = this.visiblePageCount - 1;
}
if (curPage !== null) {
this.currentPage = curPage;
}
}
private gotoPageFromPreview: PageModel;
public cancelPreviewByPage(panel: IPanel): any {
this.cancelPreview((<any>panel)["originalPage"]);
}
Expand Down Expand Up @@ -3929,8 +3928,22 @@ export class SurveyModel extends SurveyElementCore
this.runConditions();
this.updateAllElementsVisibility(this.pages);
this.updateVisibleIndexes();
this.currentPageNo = 0;
if(this.isShowingPreview) {
this.currentPageNo = 0;
} else {
let curPage = this.gotoPageFromPreview;
this.gotoPageFromPreview = null;
if (Helpers.isValueEmpty(curPage) && this.visiblePageCount > 0) {
curPage = this.visiblePages[this.visiblePageCount - 1];
}
if (!!curPage) {
this.changeCurrentPageFromPreview = true;
this.currentPage = curPage;
this.changeCurrentPageFromPreview = false;
}
}
}
private changeCurrentPageFromPreview: boolean;
private origionalPages: any;
protected onQuestionsOnPageModeChanged(oldValue: string) {
if (this.isShowingPreview) return;
Expand Down Expand Up @@ -6049,6 +6062,7 @@ export class SurveyModel extends SurveyElementCore
if (!page.name) page.name = this.generateNewName(this.pages, "page");
this.questionHashesPanelAdded(page);
this.updateVisibleIndexes();
if(!!this.runningPages) return;
if (!this.isLoadingFromJson) {
this.updateProgressText();
this.updateCurrentPage();
Expand All @@ -6058,6 +6072,7 @@ export class SurveyModel extends SurveyElementCore
}
protected doOnPageRemoved(page: PageModel) {
page.setSurveyImpl(null);
if(!!this.runningPages) return;
if (page === this.currentPage) {
this.updateCurrentPage();
}
Expand Down
42 changes: 42 additions & 0 deletions tests/surveyShowPreviewTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,48 @@ QUnit.test(
);
}
);
QUnit.test("showPreviewBeforeComplete = 'showAnsweredQuestions', onCurrentPageChanging/onCurrentPageChanged, bug#6564", function(assert) {
const survey = new SurveyModel({
pages: [
{ name: "p1", elements: [{ type: "text", name: "q1" }] },
{ name: "p2", elements: [{ type: "text", name: "q2" }] },
{ name: "p3", elements: [{ type: "text", name: "q3" }] },
],
});
survey.showPreviewBeforeComplete = "showAnsweredQuestions";
survey.data = { q2: "2", q3: "3" };
survey.currentPageNo = 2;
survey.showPreview();
let changingCounter = 0;
let changedCounter = 0;
let changingIsAfterPreview = 0;
let changedIsAfterPreview = 0;
survey.onCurrentPageChanging.add((sender, options) => {
changingCounter ++;
if(options.isAfterPreview) changingIsAfterPreview ++;
});
survey.onCurrentPageChanged.add((sender, options) => {
changedCounter ++;
if(options.isAfterPreview) changedIsAfterPreview ++;
});
survey.cancelPreview(survey.pages[1]);
assert.equal(changingCounter, 1, "onChanging is called one time");
assert.equal(changedCounter, 1, "onChanging is called one time");
assert.equal(changingIsAfterPreview, 1, "changingIsAfterPreview is called one time");
assert.equal(changedIsAfterPreview, 1, "changedIsAfterPreview is called one time");
survey.showPreview();
survey.cancelPreview(survey.pages[2]);
assert.equal(changingCounter, 2, "onChanging is called two times");
assert.equal(changedCounter, 2, "onChanging is called two times");
assert.equal(changingIsAfterPreview, 2, "changingIsAfterPreview is called two times");
assert.equal(changedIsAfterPreview, 2, "changedIsAfterPreview is called two times");
survey.showPreview();
survey.cancelPreview(survey.pages[2]);
assert.equal(changingCounter, 3, "onChanging is called three times");
assert.equal(changedCounter, 3, "onChanging is called three times");
assert.equal(changingIsAfterPreview, 3, "changingIsAfterPreview is called three times");
assert.equal(changedIsAfterPreview, 3, "changedIsAfterPreview is called three times");
});
QUnit.test(
"showPreviewBeforeComplete = 'showAnsweredQuestions', do not hide questions on running state",
function(assert) {
Expand Down

0 comments on commit c6404eb

Please sign in to comment.