diff --git a/src/page.ts b/src/page.ts index fa43626a88..f5cb59bc70 100644 --- a/src/page.ts +++ b/src/page.ts @@ -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]; } diff --git a/src/question.ts b/src/question.ts index 0b83496285..e898316ef5 100644 --- a/src/question.ts +++ b/src/question.ts @@ -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); @@ -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); } } diff --git a/src/question_matrixdropdownbase.ts b/src/question_matrixdropdownbase.ts index c27035930e..90806f5037 100644 --- a/src/question_matrixdropdownbase.ts +++ b/src/question_matrixdropdownbase.ts @@ -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; diff --git a/src/questionbase.ts b/src/questionbase.ts index cdd3def1bb..cef595242d 100644 --- a/src/questionbase.ts +++ b/src/questionbase.ts @@ -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; } diff --git a/src/survey.ts b/src/survey.ts index a783da0264..655f339002 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -24,6 +24,7 @@ module Survey { public questionTitleTemplate: string = ""; public showProgressBar: string = "off"; public storeOthersAsComment: boolean = true; + public goNextPageAutomatic: boolean = false; public pages: Array = new Array(); public triggers: Array = new Array(); public clearInvisibleValues: boolean = false; @@ -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; @@ -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 { + 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++) { @@ -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; @@ -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 = ""; @@ -591,6 +617,7 @@ module Survey { delete this.valuesHash[name]; } else { this.valuesHash[name] = newValue; + this.tryGoNextPageAutomatic(); } } questionVisibilityChanged(question: IQuestion, newValue: boolean) { @@ -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; }, diff --git a/tests/surveytests.ts b/tests/surveytests.ts index d80de2deab..0e69931b4a 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -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 = 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"); + (survey.currentPage.questions[0]).value = "3"; + (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");