export default class DQMNavigation {
public calculateUnanswered(questions: QuestionReferenceModel[]): any {
return questions.reduce(
(unanswered: { required: number; notrequired: number }, question) => {
const selector = [data-dqm-input-id^="${question.questionid}"]
const matchingInputs = document.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(selector)
import { QuestionReferenceModel } from '../models/_group.model';
export default class DQMNavigation {
public calculateUnanswered(questions: QuestionReferenceModel[]): { required: number; notrequired: number } {
// Cache to avoid re-querying the DOM for the same radio/checkbox group.
// The key is the group `name`, the value is whether it's answered.
const groupAnswerCache = new Map<string, boolean>();
return questions.reduce(
(unanswered, question) => {
// This is the main DOM query per question. We can't avoid this
// without a larger architectural change, but we can optimize what happens next.
const selector = `[data-dqm-input-id^="${question.questionid}"]`;
const matchingInputs = document.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(selector);
if (matchingInputs.length === 0) {
return unanswered;
}
let hasAnswer = false;
let isRequired = question.required;
// Unified logic for both single and multi-input (table) questions.
for (const input of matchingInputs) {
// Determine requirement status. Once true, it stays true.
if (!isRequired && !input.disabled && input.required) {
isRequired = true;
}
// Check for an answer. Once found, we can stop checking.
if (!hasAnswer) {
hasAnswer = this.isAnswered(input, groupAnswerCache);
}
// Optimization: if we've already found an answer and determined it's required,
// we don't need to check other inputs for this question.
if (hasAnswer && isRequired) {
break;
}
}
if (isRequired && !hasAnswer) {
unanswered.required += 1;
} else if (!hasAnswer) {
unanswered.notrequired += 1;
}
return unanswered;
},
{ required: 0, notrequired: 0 }
);
}
/**
* Checks if a given form element is answered.
* @param el The element to check.
* @param groupAnswerCache A cache to store the answered status of radio/checkbox groups.
*/
private isAnswered(el: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, groupAnswerCache: Map<string, boolean>): boolean {
if (el.disabled) {
return true; // Disabled elements are considered "answered" or not-applicable.
}
if (el instanceof HTMLInputElement && (el.type === 'radio' || el.type === 'checkbox')) {
// For groups, use the cache to avoid repeated, expensive DOM queries.
const groupName = el.name;
if (!groupName) { // An ungrouped checkbox
return el.checked;
}
if (groupAnswerCache.has(groupName)) {
return groupAnswerCache.get(groupName)!;
}
// If not in cache, query the DOM ONCE for this group.
const groupSelector = `input[name="${groupName}"][type="${el.type}"]`;
const groupInputs = document.querySelectorAll<HTMLInputElement>(groupSelector);
const isGroupAnswered = Array.from(groupInputs).some(input => !input.disabled && input.checked);
// Store the result in the cache for subsequent checks.
groupAnswerCache.set(groupName, isGroupAnswered);
return isGroupAnswered;
} else if (el instanceof HTMLInputElement && el.type === 'file') {
// Handles file inputs that might have a pre-existing answer state.
return el.dataset.hasAnswer === 'true' || !!el.value;
} else {
// Standard check for text, select, textarea, etc.
return !!el.value;
}
}
}
By implementing these changes, you drastically reduce the number of DOM queries, which is the most impactful optimization for client-side JavaScript that interacts with the page structure. The code is also cleaner, more maintainable, and more robust.