All files / src/browser/shared dom-utils.ts

30.43% Statements 21/69
29.16% Branches 7/24
8.33% Functions 1/12
29.85% Lines 20/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                                                                                                                                                                                                                                              20x 20x   20x 20x 20x         20x   10x 10x 10x   5x 5x 5x   5x 5x 5x     20x     20x 20x 19x   1x                                      
/**
 * @module Shared/DomUtils
 * @description Shared DOM utility functions for dashboard components.
 * Provides loading states, error display, and accessibility helpers.
 
 *
 * @intelligence Intelligence presentation layer — standardized loading states, error recovery displays, and accessibility helpers ensuring WCAG 2.1 AA compliance across all intelligence dashboard components.
 *
 * @business User experience consistency — loading skeletons and error states prevent user frustration during data acquisition. Accessibility compliance (WCAG 2.1 AA) is a legal requirement for government clients and a competitive advantage.
 *
 * @marketing Inclusive design asset — accessibility compliance enables marketing to government agencies (mandatory WCAG requirement), educational institutions, and disability advocacy organizations. Demonstrates corporate social responsibility.
 * */
 
/**
 * Show a loading spinner overlay inside a container.
 */
export function showLoadingState(container: HTMLElement, message = 'Loading data...'): void {
  let overlay = container.querySelector('.chart-loading-overlay') as HTMLElement | null;
  if (!overlay) {
    overlay = document.createElement('div');
    overlay.className = 'chart-loading-overlay';
    overlay.setAttribute('role', 'status');
    overlay.setAttribute('aria-live', 'polite');
    container.style.position = 'relative';
    container.appendChild(overlay);
  }
  overlay.innerHTML = `
    <div class="loading-spinner" aria-hidden="true"></div>
    <p>${message}</p>
  `;
  overlay.style.display = 'flex';
}
 
/**
 * Show an empty state message inside a container.
 */
export function showEmptyState(container: HTMLElement, message = 'No data available'): void {
  let overlay = container.querySelector('.chart-loading-overlay') as HTMLElement | null;
  if (!overlay) {
    overlay = document.createElement('div');
    overlay.className = 'chart-loading-overlay';
    overlay.setAttribute('role', 'status');
    container.style.position = 'relative';
    container.appendChild(overlay);
  }
  overlay.innerHTML = `<p class="empty-message">${message}</p>`;
  overlay.style.display = 'flex';
}
 
/**
 * Show an error state message inside a container.
 */
export function showErrorState(container: HTMLElement, message = 'Failed to load data'): void {
  let overlay = container.querySelector('.chart-loading-overlay') as HTMLElement | null;
  if (!overlay) {
    overlay = document.createElement('div');
    overlay.className = 'chart-loading-overlay';
    overlay.setAttribute('role', 'status');
    overlay.setAttribute('aria-live', 'assertive');
    container.style.position = 'relative';
    container.appendChild(overlay);
  }
  overlay.innerHTML = `<p class="error-message" role="alert">${message}</p>`;
  overlay.style.display = 'flex';
}
 
/**
 * Hide all state overlays in a container.
 */
export function hideStateOverlays(container: HTMLElement): void {
  const overlay = container.querySelector('.chart-loading-overlay') as HTMLElement | null;
  if (overlay) {
    overlay.style.display = 'none';
  }
}
 
/**
 * Format a number with locale-appropriate separators.
 */
export function formatNumber(value: number, locale = 'sv-SE'): string {
  return new Intl.NumberFormat(locale).format(value);
}
 
/**
 * Format a number as percentage.
 */
export function formatPercent(value: number, decimals = 1): string {
  return `${value.toFixed(decimals)}%`;
}
 
/**
 * Debounce a function call.
 */
export function debounce<T extends (...args: unknown[]) => void>(
  fn: T,
  delay: number,
): (...args: Parameters<T>) => void {
  let timer: ReturnType<typeof setTimeout>;
  return (...args: Parameters<T>) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}
 
/**
 * Detect the current page language from the HTML lang attribute.
 */
export function detectLanguage(): string {
  return document.documentElement.lang || 'en';
}
 
// ─── Data Source Disclaimer ──────────────────────────────────────────────────
 
/**
 * Data source type for dashboard disclaimers.
 */
export type DataSourceType = 'live' | 'synthetic' | 'mock';
 
/**
 * Show a data source disclaimer banner inside a dashboard container.
 * Informs users whether data is live CSV, synthetic fallback, or mock.
 */
export function showDataSourceDisclaimer(
  container: HTMLElement,
  sourceType: DataSourceType,
): void {
  // Remove any existing disclaimer in this container
  const existing = container.querySelector('.data-source-disclaimer');
  if (existing) existing.remove();
 
  const disclaimer = document.createElement('div');
  disclaimer.className = `data-source-disclaimer data-source-${sourceType}`;
  disclaimer.setAttribute('role', 'status');
 
  let icon: string;
  let text: string;
 
  switch (sourceType) {
    case 'live':
      icon = '✅';
      text = 'Live data loaded from CIA Platform CSV exports';
      break;
    case 'synthetic':
      icon = '⚠️';
      text = 'Synthetic fallback data — live CSV sources unavailable';
      break;
    case 'mock':
      icon = '🔧';
      text = 'Mock demonstration data — not based on real parliamentary records';
      break;
  }
 
  disclaimer.textContent = `${icon} ${text}`;
 
  // Insert after the first heading if present, otherwise at the top of the container
  const heading = container.querySelector('h2, h3');
  if (heading) {
    heading.insertAdjacentElement('afterend', disclaimer);
  } else {
    container.prepend(disclaimer);
  }
}
 
/**
 * Announce a data point to screen readers via live region.
 */
export function announceToScreenReader(message: string): void {
  let region = document.getElementById('sr-announcements');
  if (!region) {
    region = document.createElement('div');
    region.id = 'sr-announcements';
    region.setAttribute('role', 'status');
    region.setAttribute('aria-live', 'polite');
    region.className = 'sr-only';
    document.body.appendChild(region);
  }
  region.textContent = message;
}