UNPKG

digitalmarketplace-frontend

Version:

Digital Marketplace Frontend contains assets and components used in Digital Marketplace projects

150 lines (148 loc) 5.69 kB
var QuestionType; (function (QuestionType) { QuestionType[QuestionType["CONTROLLER"] = 0] = "CONTROLLER"; QuestionType[QuestionType["CONTROLLER_AND_TARGET"] = 1] = "CONTROLLER_AND_TARGET"; QuestionType[QuestionType["TARGET"] = 2] = "TARGET"; })(QuestionType || (QuestionType = {})); class QuestionInput { question; $input; targets; constructor(question, $input) { this.question = question; this.$input = $input; const targets = this.$input.attr('data-target'); this.targets = targets ? targets.split(' ') : []; } setEventListener() { this.$input.on('click', () => { this.question.updateTargetQuestionsVisibility(); }); } isChecked() { return this.$input.is(':checked'); } } class QuestionQuestion { questionWatcher; $question; id; questionType; questionInputs; possibleTargetIds; targetOf; constructor(questionWatcher, $question, questionType) { this.questionWatcher = questionWatcher; this.$question = $question; this.id = this.$question.attr('id'); this.questionType = questionType; if (this.questionType === QuestionType.CONTROLLER) { this.enableControllerQuestionMethods(); } else { this.enableTargetQuestionMethods(); } } enableControllerQuestionMethods() { this.questionInputs = this.$question.find('input').get().map((inputElement) => new QuestionInput(this, $(inputElement))); this.possibleTargetIds = new Set(this.questionInputs.map((questionInput) => questionInput.targets).flat()); } enableTargetQuestionMethods() { this.targetOf = []; } isVisible() { return !this.$question.hasClass('govuk-visually-hidden'); } currentTargetIds() { let targets; if (this.isVisible() && this.questionInputs) { targets = this.questionInputs.filter((questionInput) => questionInput.isChecked()).map((questionInput) => questionInput.targets).flat(); } else { targets = []; } return new Set(targets); } updateTargetQuestionsVisibility() { this.questionWatcher.updateTargetQuestionsVisibility(this.possibleTargetIds); } appendTargetOf(questionId) { if (this.targetOf !== undefined) { this.targetOf.push(questionId); } } toggleVisibility(isShown) { this.$question.toggleClass('govuk-visually-hidden', !isShown); this.$question.attr('aria-hidden', (!isShown).toString()); if (isShown) { this.$question.removeAttr('hidden'); } else { this.$question.attr('hidden', ''); } } } class Question { static moduleName = 'dm-question'; questions; constructor() { this.questions = {}; $(`[data-module="${Question.moduleName}"]`).each((_index, question) => { this.push(new QuestionQuestion(this, $(question), QuestionType.CONTROLLER)); }); for (const [questionId, question] of Object.entries(this.questions)) { for (const questionTargetId of question.possibleTargetIds || []) { if (questionTargetId in this.questions) { if (this.questions[questionTargetId].questionType === QuestionType.CONTROLLER) { this.questions[questionTargetId].questionType = QuestionType.CONTROLLER_AND_TARGET; this.questions[questionTargetId].enableTargetQuestionMethods(); } } else { this.push(new QuestionQuestion(this, $(`#${questionTargetId}`), QuestionType.TARGET)); } this.questions[questionTargetId].appendTargetOf(questionId); } } } init() { for (const question of Object.values(this.questions)) { if (question.questionType < QuestionType.TARGET) { for (const questionInput of question.questionInputs || []) { questionInput.setEventListener(); } if (question.questionType === QuestionType.CONTROLLER) { this.updateTargetQuestionsVisibility(question.possibleTargetIds); } } } } push(question) { this.questions[question.id] = question; } updateTargetQuestionsVisibility(possibleTargetIds, isShown) { for (const possibleTargetId of possibleTargetIds || []) { const targetQuestion = this.questions[possibleTargetId]; let questionIsShown; if (isShown === undefined) { const controllerQuestions = targetQuestion.targetOf?.map((questionId) => this.questions[questionId]); const targetsToShow = new Set(controllerQuestions?.map((controllerQuestion) => Array.from(controllerQuestion.currentTargetIds())).flat()); questionIsShown = targetsToShow.has(possibleTargetId); } else { questionIsShown = isShown; } targetQuestion.toggleVisibility(questionIsShown); if (targetQuestion.questionType === QuestionType.CONTROLLER_AND_TARGET) { if (questionIsShown) { this.updateTargetQuestionsVisibility(targetQuestion.possibleTargetIds); } else { this.updateTargetQuestionsVisibility(targetQuestion.possibleTargetIds, false); } } } } } export { Question }; //# sourceMappingURL=question.mjs.map