Skip to content

Commit

Permalink
survye.goNextPageAutomatic property (default is false) is added. On a…
Browse files Browse the repository at this point in the history
…nswering all questions on the page, the survey will go automatically to the next page or complete the survey: #37
  • Loading branch information
andrewtelnov committed Jul 27, 2016
1 parent 88236b3 commit e1f13a9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ module Survey {
}
}
}
public hasErrors(focuseOnFirstError: boolean = false): boolean {
public hasErrors(fireCallback: boolean = true, focuseOnFirstError: boolean = false): boolean {
var result = false;
var firstErrorQuestion = null;
for (var i = 0; i < this.questions.length; i++) {
if (this.questions[i].visible && this.questions[i].hasErrors()) {
if (this.questions[i].visible && this.questions[i].hasErrors(fireCallback)) {
if (focuseOnFirstError && firstErrorQuestion == null) {
firstErrorQuestion = this.questions[i];
}
Expand Down
8 changes: 4 additions & 4 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ module Survey {
this.setNewComment(newValue);
}
public isEmpty(): boolean { return this.value == null; }
public hasErrors(): boolean {
this.checkForErrors();
public hasErrors(fireCallback: boolean = true): boolean {
this.checkForErrors(fireCallback);
return this.errors.length > 0;
}
public get requiredText(): string { return this.survey != null && this.isRequired ? this.survey.requiredText : ""; }
private checkForErrors() {
private checkForErrors(fireCallback: boolean) {
var errorLength = this.errors ? this.errors.length : 0;
this.errors = [];
this.onCheckForErrors(this.errors);
Expand All @@ -125,7 +125,7 @@ module Survey {
this.errors.push(error);
}
}
if (errorLength != this.errors.length || errorLength > 0) {
if (fireCallback && (errorLength != this.errors.length || errorLength > 0)) {
this.fireCallback(this.errorsChangedCallback);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/question_matrixdropdownbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ module Survey {
}
this.isRowChanging = false;
}
public hasErrors(): boolean {
var errosInCells = this.hasErrorInCells();
return super.hasErrors() || errosInCells;
public hasErrors(fireCallback: boolean = true): boolean {
var errosInCells = this.hasErrorInCells(fireCallback);
return super.hasErrors(fireCallback) || errosInCells;
}
private hasErrorInCells(): boolean {
private hasErrorInCells(fireCallback: boolean): boolean {
if (!this.generatedVisibleRows) return false;
var res = false;
for (var colIndex = 0; colIndex < this.columns.length; colIndex++) {
for (var i = 0; i < this.generatedVisibleRows.length; i++) {
var cells = this.generatedVisibleRows[i].cells;
res = cells && cells[colIndex] && cells[colIndex].question && cells[colIndex].question.hasErrors() || res;
res = cells && cells[colIndex] && cells[colIndex].question && cells[colIndex].question.hasErrors(fireCallback) || res;
}
}
return res;
Expand Down
2 changes: 1 addition & 1 deletion src/questionbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Survey {
}
}
public get visibleIndex(): number { return this.visibleIndexValue; }
public hasErrors(): boolean { return false; }
public hasErrors(fireCallback: boolean = true): boolean { return false; }
public get hasTitle(): boolean { return false; }
public get hasComment(): boolean { return false; }
public get id(): string { return this.idValue; }
Expand Down
35 changes: 31 additions & 4 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Survey {
public questionTitleTemplate: string = "";
public showProgressBar: string = "off";
public storeOthersAsComment: boolean = true;
public goNextPageAutomatic: boolean = false;
public pages: Array<PageModel> = new Array<PageModel>();
public triggers: Array<SurveyTrigger> = new Array<SurveyTrigger>();
public clearInvisibleValues: boolean = false;
Expand Down Expand Up @@ -235,7 +236,7 @@ module Survey {
}
get isCurrentPageHasErrors(): boolean {
if (this.currentPage == null) return true;
return this.currentPage.hasErrors(true);
return this.currentPage.hasErrors(true, true);
}
public prevPage(): boolean {
if (this.isFirstPage) return false;
Expand Down Expand Up @@ -398,13 +399,23 @@ module Survey {
question.onSurveyValueChanged(newValue);
}
private checkOnPageTriggers() {
var questions = this.getCurrentPageQuestions();
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
var value = this.getValue(question.name);
this.checkTriggers(question.name, value, true);
}
}
private getCurrentPageQuestions(): Array<QuestionBase> {
var result = [];
var page = this.currentPage;
if (!page) return result;
for (var i = 0; i < page.questions.length; i++) {
var question = page.questions[i];
if (!question.visible || !question.name) continue;
var value = this.getValue(question.name);
this.checkTriggers(question.name, value, true);
result.push(question);
}
return result;
}
private checkTriggers(name: string, newValue: any, isOnNextPage: boolean) {
for (var i: number = 0; i < this.triggers.length; i++) {
Expand Down Expand Up @@ -558,6 +569,7 @@ module Survey {
}
this.notifyQuestionOnValueChanged(name, newValue);
this.checkTriggers(name, newValue, false);
this.tryGoNextPageAutomatic();
}
private isValueEqual(name: string, newValue: any): boolean {
if (newValue == "") newValue = null;
Expand All @@ -580,6 +592,20 @@ module Survey {
}
return true;
}
private tryGoNextPageAutomatic() {
if (!this.goNextPageAutomatic || !this.currentPage) return;
var questions = this.getCurrentPageQuestions();
for (var i = 0; i < questions.length; i++) {
if (!this.getValue(questions[i].name)) return;
}
if (!this.currentPage.hasErrors(false, false)) {
if (!this.isLastPage) {
this.nextPage();
} else {
this.doComplete();
}
}
}
getComment(name: string): string {
var result = this.data[name + this.commentPrefix];
if (result == null) result = "";
Expand All @@ -591,6 +617,7 @@ module Survey {
delete this.valuesHash[name];
} else {
this.valuesHash[name] = newValue;
this.tryGoNextPageAutomatic();
}
}
questionVisibilityChanged(question: IQuestion, newValue: boolean) {
Expand Down Expand Up @@ -644,7 +671,7 @@ module Survey {

JsonObject.metaData.addClass("survey", ["locale", "title", "completedHtml:html", "pages", "questions", "triggers:triggers", "surveyId", "surveyPostId", "cookieName", "sendResultOnPageNext:boolean",
"showNavigationButtons:boolean", "showTitle:boolean", "showPageTitles:boolean", "showPageNumbers:boolean", "showQuestionNumbers", "showProgressBar",
"storeOthersAsComment:boolean", "clearInvisibleValues:boolean", "requiredText", "pagePrevText", "pageNextText", "completeText", "questionStartIndex", "questionTitleTemplate"]);
"storeOthersAsComment:boolean", "goNextPageAutomatic:boolean", "clearInvisibleValues:boolean", "requiredText", "pagePrevText", "pageNextText", "completeText", "questionStartIndex", "questionTitleTemplate"]);
JsonObject.metaData.setPropertyValues("survey", "pages", "page");
JsonObject.metaData.setPropertyValues("survey", "questions", null, null,
function (obj) { return null; },
Expand Down
19 changes: 19 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,25 @@ module Survey.Tests {
survey.doMergeValues({ val2: { val2: 2 } }, dest);
assert.deepEqual({ val: 1, val2: { val1: "str", val2: 2 } }, dest);
});
QUnit.test("test goNextPageAutomatic property", function (assert) {
var survey = twoPageSimplestSurvey();

var dropDownQ = <QuestionDropdownModel>survey.pages[1].addNewQuestion("dropdown", "question5");
dropDownQ.choices = [1, 2, 3];
dropDownQ.hasOther = true;
survey.goNextPageAutomatic = true;
assert.equal(survey.currentPage.name, survey.pages[0].name, "the first page is default page");
survey.setValue("question1", 1);
survey.setValue("question2", 2);
assert.equal(survey.currentPage.name, survey.pages[1].name, "go to the second page automatically");
(<Question>survey.currentPage.questions[0]).value = "3";
(<Question>survey.currentPage.questions[1]).value = "4";
dropDownQ.value = dropDownQ.otherItem.value;
assert.equal(survey.currentPage.name, survey.pages[1].name, "stay on the second page");
assert.notEqual(survey.state, "completed", "survey is still running");
dropDownQ.comment = "other value";
assert.equal(survey.state, "completed", "complete the survey");
});
function twoPageSimplestSurvey() {
var survey = new SurveyModel();
var page = survey.addNewPage("Page 1");
Expand Down

0 comments on commit e1f13a9

Please sign in to comment.