From ccbc787fedb7203492d9c1f2d0b87fe9d217d90b Mon Sep 17 00:00:00 2001 From: Aleksey Novikov Date: Mon, 30 Dec 2024 11:47:32 +0300 Subject: [PATCH] #9210 SurveyModel: renamings - onScrollingElementToTop, onIsAnswerCorrect (#9217) * #9210 SurveyModel: renamings - onScrollingElementToTop, onIsAnswerCorrect Fixes #9210 * #9210 - move obsolete args to old interface * #9210 - fix build * #9210 - fix getInCorrectAnswerCount * #9210 SurveyModel: renamings - onScrollingElementToTop, onIsAnswerCorrect Fixes #9210 * Add deprecation messages * #9210 SurveyModel: renamings - onScrollingElementToTop, onIsAnswerCorrect Fixes #9210 * #9210 - change code --------- Co-authored-by: RomanTsukanov --- packages/survey-core/src/question.ts | 11 ++++-- packages/survey-core/src/survey-events-api.ts | 32 ++++++++++++----- packages/survey-core/src/survey.ts | 36 +++++++++++++------ packages/survey-core/tests/surveytests.ts | 2 +- 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/packages/survey-core/src/question.ts b/packages/survey-core/src/question.ts index 81f52ed578..71c8da72d4 100644 --- a/packages/survey-core/src/question.ts +++ b/packages/survey-core/src/question.ts @@ -1925,7 +1925,7 @@ export class Question extends SurveyElement /** * A correct answer to this question. Specify this property if you want to [create a quiz](https://surveyjs.io/form-library/documentation/design-survey-create-a-quiz). * @see SurveyModel.getCorrectAnswerCount - * @see SurveyModel.getInCorrectAnswerCount + * @see SurveyModel.getIncorrectAnswerCount */ public get correctAnswer(): any { return this.getPropertyValue("correctAnswer"); @@ -1965,7 +1965,14 @@ export class Question extends SurveyElement protected checkIfAnswerCorrect(): boolean { const isEqual = Helpers.isTwoValueEquals(this.value, this.correctAnswer, this.getAnswerCorrectIgnoreOrder(), settings.comparator.caseSensitive, true); const correct = isEqual ? 1 : 0; - const options = { result: isEqual, correctAnswer: correct, correctAnswers: correct, incorrectAnswers: this.quizQuestionCount - correct }; + const incorrect = this.quizQuestionCount - correct; + const options = { + result: isEqual, + correctAnswers: correct, + correctAnswerCount: correct, + incorrectAnswers: incorrect, + incorrectAnswerCount: incorrect, + }; if (!!this.survey) { this.survey.onCorrectQuestionAnswer(this, options); } diff --git a/packages/survey-core/src/survey-events-api.ts b/packages/survey-core/src/survey-events-api.ts index 8d4785f013..c009f101c5 100644 --- a/packages/survey-core/src/survey-events-api.ts +++ b/packages/survey-core/src/survey-events-api.ts @@ -906,17 +906,27 @@ export interface DynamicPanelGetTabTitleEvent extends DynamicPanelCurrentIndexCh } export interface IsAnswerCorrectEvent extends QuestionEventMixin { /** - * The number of correct answers in a matrix where each row is considered as one quiz question. + * A Boolean property that specifies whether the answer is correct (`true`) or incorrect (`false`). Use the `options.question.value` and `options.question.correctAnswer` properties to check the answer. */ - correctAnswers: number; + result: boolean; /** - * The number of incorrect answers in a matrix where each row is considered as one quiz question. + * Obsolete. Use `options.correctAnswerCount` instead. */ - incorrectAnswers: number; + correctAnswers?: number; /** - * A Boolean property that specifies whether the answer is correct (`true`) or incorrect (`false`). Use the `options.question.value` and `options.question.correctAnswer` properties to check the answer. + * Obsolete. Use `options.incorrectAnswerCount` instead. */ - result: boolean; + incorrectAnswers?: number; +} +export interface CheckAnswerCorrectEvent extends IsAnswerCorrectEvent { + /** + * The number of correct answers in a matrix where each row is considered as one quiz question. + */ + correctAnswerCount: number; + /** + * The number of incorrect answers in a matrix where each row is considered as one quiz question. + */ + incorrectAnswerCount: number; } export interface DragDropAllowEvent { /** @@ -967,9 +977,9 @@ export interface ScrollingElementToTopEvent { */ elementId: string; /** - * A Boolean property that you can set to `true` if you want to cancel the scroll operation. + * Obsolete. Use `options.allow` instead. */ - cancel: boolean; + cancel?: boolean; /** * Obsolete. Use `options.element` instead. */ @@ -979,6 +989,12 @@ export interface ScrollingElementToTopEvent { */ page?: PageModel; } +export interface ScrollToTopEvent extends ScrollingElementToTopEvent { + /** + * A Boolean property that you can set to `false` if you want to cancel the scroll operation. + */ + allow: boolean; +} export interface GetQuestionTitleActionsEvent extends QuestionEventMixin, GetTitleActionsEventMixin { } export interface GetPanelTitleActionsEvent extends PanelEventMixin, GetTitleActionsEventMixin { } export interface GetPageTitleActionsEvent extends PageEventMixin, GetTitleActionsEventMixin { } diff --git a/packages/survey-core/src/survey.ts b/packages/survey-core/src/survey.ts index 873266540c..e133b61460 100644 --- a/packages/survey-core/src/survey.ts +++ b/packages/survey-core/src/survey.ts @@ -65,10 +65,12 @@ import { ShowingChoiceItemEvent, ChoicesLazyLoadEvent, GetChoiceDisplayValueEvent, MatrixRowAddedEvent, MatrixBeforeRowAddedEvent, MatrixRowRemovingEvent, MatrixRowRemovedEvent, MatrixAllowRemoveRowEvent, MatrixDetailPanelVisibleChangedEvent, MatrixCellCreatingEvent, MatrixCellCreatedEvent, MatrixAfterCellRenderEvent, MatrixCellValueChangedEvent, MatrixCellValueChangingEvent, MatrixCellValidateEvent, DynamicPanelModifiedEvent, DynamicPanelRemovingEvent, TimerPanelInfoTextEvent, DynamicPanelItemValueChangedEvent, - DynamicPanelGetTabTitleEvent, DynamicPanelCurrentIndexChangedEvent, IsAnswerCorrectEvent, DragDropAllowEvent, ScrollingElementToTopEvent, GetQuestionTitleActionsEvent, + DynamicPanelGetTabTitleEvent, DynamicPanelCurrentIndexChangedEvent, CheckAnswerCorrectEvent, DragDropAllowEvent, ScrollToTopEvent, GetQuestionTitleActionsEvent, GetPanelTitleActionsEvent, GetPageTitleActionsEvent, GetPanelFooterActionsEvent, GetMatrixRowActionsEvent, GetExpressionDisplayValueEvent, ServerValidateQuestionsEvent, MultipleTextItemAddedEvent, MatrixColumnAddedEvent, GetQuestionDisplayValueEvent, PopupVisibleChangedEvent, ChoicesSearchEvent, - OpenFileChooserEvent, OpenDropdownMenuEvent, ResizeEvent + OpenFileChooserEvent, OpenDropdownMenuEvent, ResizeEvent, + ScrollingElementToTopEvent, + IsAnswerCorrectEvent } from "./survey-events-api"; import { QuestionMatrixDropdownModelBase } from "./question_matrixdropdownbase"; import { QuestionMatrixDynamicModel } from "./question_matrixdynamic"; @@ -800,7 +802,12 @@ export class SurveyModel extends SurveyElementCore /** * An event that is raised to define whether a question answer is correct. Applies only to [quiz surveys](https://surveyjs.io/form-library/documentation/design-survey/create-a-quiz). */ - public onIsAnswerCorrect: EventBase = this.addEvent(); + public onCheckAnswerCorrect: EventBase = this.addEvent(); + /** + * Obsolete. Use the [`onCheckAnswerCorrect`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#onCheckAnswerCorrect) event instead. + * @deprecated + */ + public onIsAnswerCorrect: EventBase = this.onCheckAnswerCorrect; /** * An event that is raised when users drag and drop survey elements while designing the survey in [Survey Creator](https://surveyjs.io/survey-creator/documentation/overview). Use this event to control drag and drop operations. @@ -810,7 +817,12 @@ export class SurveyModel extends SurveyElementCore /** * An event this is raised before a survey element (usually page) is scrolled to the top. Use this event to cancel the scroll operation. */ - public onScrollingElementToTop: EventBase = this.addEvent(); + public onScrollToTop: EventBase = this.addEvent(); + /** + * Obsolete. Use the [`onScrollToTop`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#onScrollToTop) event instead. + * @deprecated + */ + public onScrollingElementToTop: EventBase = this.onScrollToTop; public onLocaleChangedEvent: EventBase = this.addEvent(); @@ -5389,15 +5401,16 @@ export class SurveyModel extends SurveyElementCore passedRootElement?: HTMLElement, onScolledCallback?: () => void ): any { - const options: ScrollingElementToTopEvent = { + const options: ScrollToTopEvent = { element: element, question: question, page: page, elementId: id, cancel: false, + allow: true, }; - this.onScrollingElementToTop.fire(this, options); - if (!options.cancel) { + this.onScrollToTop.fire(this, options); + if (!options.cancel && options.allow) { const elementPage = this.getPageByElement(element as IElement); if (this.isLazyRendering && !!elementPage) { let elementsToRenderBefore = 1; @@ -7169,7 +7182,7 @@ export class SurveyModel extends SurveyElementCore * For more information about quizzes, refer to the following tutorial: [Create a Quiz](https://surveyjs.io/form-library/documentation/design-survey/create-a-quiz). * @returns The number of correct answers in a quiz. * @see getQuizQuestionCount - * @see getInCorrectAnswerCount + * @see getIncorrectAnswerCount */ public getCorrectAnswerCount(): number { return this.getCorrectedAnswerCountCore(true); @@ -7192,7 +7205,10 @@ export class SurveyModel extends SurveyElementCore return res; } public getInCorrectedAnswerCount(): number { - return this.getInCorrectAnswerCount(); + return this.getIncorrectAnswerCount(); + } + public getInCorrectAnswerCount(): number { + return this.getIncorrectAnswerCount(); } /** * Returns the number of incorrect answers in a quiz. @@ -7201,7 +7217,7 @@ export class SurveyModel extends SurveyElementCore * @returns The number of incorrect answers in a quiz. * @see getCorrectAnswerCount */ - public getInCorrectAnswerCount(): number { + public getIncorrectAnswerCount(): number { return this.getCorrectedAnswerCountCore(false); } onCorrectQuestionAnswer(question: IQuestion, options: any): void { diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index d3700de7fb..77e0d6cf7f 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -17385,7 +17385,7 @@ QUnit.test("no scrolling to page top after focus a question on another page - ht let log = ""; const qName = "question66"; - survey.onScrollingElementToTop.add((s, o) => { + survey.onScrollToTop.add((s, o) => { log += "->" + o.element.name; }); survey.onCurrentPageChanged.add((s, o) => {