All files / src app.js

84.31% Statements 43/51
54.54% Branches 18/33
75% Functions 3/4
85.71% Lines 42/49

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            1x 1x       1x 1x 1x       1x       1x 1x 1x 1x       1x 1x 1x           1x 1x 1x 1x 1x 1x 1x         1x           1x 1x     1x                             1x 2x 2x   2x 2x 2x       1x     1x     1x 1x 1x   1x 1x 1x     1x 1x   1x 1x        
import { parseString } from 'xml2js';
import { renderMeasurementResults } from './renderers/MeasurementRenderer.js';
import { renderAdminData } from './renderers/AdminRenderer.js';
import { version } from '../package.json';
 
import { EventEmitter } from 'events';
Eif (typeof window.EventEmitter !== 'function') {
  window.EventEmitter = EventEmitter;
}
 
export function init(xmlStr, options = {}) {
  const containerId = options.containerId || 'app';
  const appContainer = document.getElementById(containerId);
  Iif (!appContainer) {
    console.error('No container found with id', containerId);
    return;
  }
  appContainer.innerHTML = `<div id="disclaimer" style="background:orangered; color:#000; padding:8px; margin-bottom:10px; font-family: sans-serif; font-weight:bold;">
  This is a proof of concept and contains missing fields and errors. It is not for production use. dcc-viewer-js Version ${version}
</div>`;
  // Parse the XML string first
  const cleanXmlStr = xmlStr.trim();
  console.log("Clean XML string:", cleanXmlStr);
  parseString(cleanXmlStr, { explicitArray: false }, (err, result) => {
    Iif (err) {
      console.error('Error parsing XML:', err);
      return;
    }
    const dccData = result;
    const cert = dccData['dcc:digitalCalibrationCertificate'];
    Iif (!cert) {
      console.error("No dcc:digitalCalibrationCertificate found in XML");
      return;
    }
 
    // Extract available languages from the administrative data coreData section
    let languages = [];
    Eif (cert['dcc:administrativeData'] && cert['dcc:administrativeData']['dcc:coreData']) {
      const coreData = cert['dcc:administrativeData']['dcc:coreData'];
      let langs = coreData['dcc:usedLangCodeISO639_1'];
      Eif (langs) {
        Iif (!Array.isArray(langs)) { langs = [langs]; }
        languages = langs;
      }
    }
 
    // Use provided default language if available and present, else use the first available or fallback to 'en'
    let selectedLanguage = (options.language && languages.includes(options.language))
        ? options.language
        : (languages.length ? languages[0] : 'en');
 
    // Build a dynamic language dropdown based on the extracted languages
// Build a dynamic language dropdown based on the extracted languages
    const langSelect = document.createElement('select');
    langSelect.classList.add('lang-select');
 
// Mapping for custom option text with flag symbols for language codes
    const languageOptionText = {
      de: '๐Ÿ‡ฉ๐Ÿ‡ช DE Sprache',
      en: '๐Ÿ‡ฌ๐Ÿ‡ง EN Language', // Updated: UK flag instead of US
      fr: '๐Ÿ‡ซ๐Ÿ‡ท FR Langue',
      es: '๐Ÿ‡ช๐Ÿ‡ธ ES Idioma',
      ru: '๐Ÿ‡ท๐Ÿ‡บ RU ัะทั‹ะบ',
      cn: '๐Ÿ‡จ๐Ÿ‡ณ CN ่ฏญ่จ€',
      dk: '๐Ÿ‡ฉ๐Ÿ‡ฐ DK Sprog',
      nl: '๐Ÿ‡ณ๐Ÿ‡ฑ NL Taal',
      se: '๐Ÿ‡ธ๐Ÿ‡ช SE Sprรฅk',
      no: '๐Ÿ‡ณ๐Ÿ‡ด NO Sprรฅk',
      fi: '๐Ÿ‡ซ๐Ÿ‡ฎ FI Kieli',
      it: '๐Ÿ‡ฎ๐Ÿ‡น IT Lingua'
    };
 
    languages.forEach(lang => {
      const option = document.createElement('option');
      option.value = lang;
      // Use custom text if available; otherwise, fallback to the language code.
      option.textContent = languageOptionText[lang] || lang;
      if (lang === selectedLanguage) option.selected = true;
      langSelect.appendChild(option);
    });
 
    // When the language is changed, reinitialize the app with the same XML but new language.
    langSelect.addEventListener('change', (e) => {
      init(xmlStr, { containerId, language: e.target.value });
    });
    appContainer.appendChild(langSelect);
 
    // Create containers for admin data and measurement results
    const adminContainer = document.createElement('div');
    adminContainer.id = 'adminData';
    appContainer.appendChild(adminContainer);
 
    const measContainer = document.createElement('div');
    measContainer.id = 'measurementResults';
    appContainer.appendChild(measContainer);
 
    // Render admin and measurement data if available
    Eif (cert['dcc:administrativeData']) {
      renderAdminData(cert['dcc:administrativeData'], selectedLanguage);
    }
    Eif (cert['dcc:measurementResults']) {
      renderMeasurementResults(cert['dcc:measurementResults'], selectedLanguage);
    }
  });
}