All files / src/renderers InfluenceConditionsRenderer.js

86.45% Statements 134/155
59.37% Branches 57/96
92.85% Functions 13/14
86.27% Lines 132/153

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264              1x 1x       1x 1x 1x 1x       1x       1x 1x 1x   4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x   3x 3x 3x     4x 4x     4x 4x 4x 4x 4x 2x 2x 2x     4x 4x 4x 4x 4x     4x 4x 12x 12x 12x 12x 12x   4x     4x 8x     8x 8x 8x 8x 8x     8x 8x 8x 8x 8x   8x 8x 8x 8x 8x           8x   8x     8x 8x 8x 8x 8x   8x   4x   4x 4x   1x         28x 18x 18x 26x 18x             8x 8x 8x       8x                     11x 4x     7x 4x 4x 4x     4x 8x 8x 8x 8x 8x 8x   8x 8x     8x   8x     8x   4x   8x   4x     3x                                             3x 3x 3x 3x 3x 3x 3x 3x   3x 3x     3x   3x             8x 8x 8x 24x 14x     8x         8x     8x 8x 14x     14x         8x  
// src/renderers/InfluenceConditionsRenderer.js
import { DSIUnit } from "dsiunits-js";
import tippy from 'tippy.js';
import 'tippy.js/dist/tippy.css';
 
export class InfluenceConditionsRenderer {
    constructor(influenceConditionsData, language) {
        this.data = influenceConditionsData;
        this.language = language;
    }
 
    render() {
        const container = document.createElement('div');
        container.innerHTML = `<h3>Influence Conditions</h3>`;
        let conditions = this.data['dcc:influenceCondition'];
        Iif (!conditions) {
            container.textContent = 'No influence conditions available.';
            return container;
        }
        Iif (!Array.isArray(conditions)) {
            conditions = [conditions];
        }
        // Reset default browser styles for details and summary
        const detailsStyle = 'margin: 0; padding: 0; border: 1px solid #ccc; padding: 8px; margin-bottom: 10px;';
        const summaryStyle = 'margin: 0; padding: 0; list-style: none;';
        conditions.forEach(condition => {
            // Create a collapsible section for each influence condition
            const details = document.createElement('details');
            details.setAttribute('style', detailsStyle);
            details.style.border = '1px solid #ccc';
            details.style.padding = '8px';
            details.style.marginBottom = '10px';
            details.open = false; // collapsed by default
            // Summary: always visible, shows the condition name and, if only one quantity exists, its value (plain text)
            const summary = document.createElement('summary');
            summary.style.fontWeight = 'bold';
            const condName = this._getText(condition['dcc:name']);
            let summaryText = condName;
            const quantities = this._getQuantities(condition);
            if (quantities.length === 1) {
                // Use plain flag for summary display so that HTML is not double-escaped
                const qty = quantities[0];
                const valueStr = this._formatQuantity(qty, true);
                summaryText += ` : ${valueStr}`;
            }
            // Use innerHTML so that HTML markup in the unit is rendered
            summary.innerHTML = summaryText;
            details.appendChild(summary);
 
            // Expanded content: condition description and quantities table
            const contentDiv = document.createElement('div');
            contentDiv.style.marginTop = '8px';
            contentDiv.style.fontFamily = 'sans-serif';
            const descText = this._getText(condition['dcc:description']);
            if (descText) {
                const p = document.createElement('p');
                p.textContent = descText;
                contentDiv.appendChild(p);
            }
            // Render quantities table if available
            Eif (condition['dcc:data'] && condition['dcc:data']['dcc:quantity']) {
                let quantities = this._getQuantities(condition);
                const table = document.createElement('table');
                table.style.width = '100%';
                table.style.borderCollapse = 'collapse';
 
                // Header row for the table (removed "Additional Info" column)
                const headerRow = document.createElement('tr');
                ['Quantity', 'Value', 'Description'].forEach(text => {
                    const th = document.createElement('th');
                    th.textContent = text;
                    th.style.border = '1px solid #ccc';
                    th.style.padding = '4px';
                    headerRow.appendChild(th);
                });
                table.appendChild(headerRow);
 
                // Render each quantity as a row
                quantities.forEach(q => {
                    const row = document.createElement('tr');
 
                    // Column 1: Quantity name
                    const nameCell = document.createElement('td');
                    nameCell.style.border = '1px solid #ccc';
                    nameCell.style.padding = '4px';
                    nameCell.textContent = this._getText(q['dcc:name']);
                    row.appendChild(nameCell);
 
                    // Column 2: Value (with unit, uncertainty, merged) – rendered with HTML markup
                    const valueCell = document.createElement('td');
                    valueCell.style.border = '1px solid #ccc';
                    valueCell.style.padding = '4px';
                    const formattedValue = this._formatQuantity(q, false);
                    valueCell.innerHTML = formattedValue;
                    // Attach tooltip for additional data if present
                    const additional = this._getAdditionalData(q);
                    Eif (Object.keys(additional).length > 0) {
                        const infoSpan = document.createElement('span');
                        infoSpan.textContent = ' ⓘ';
                        tippy(infoSpan, {
                            content: formatAdditionalInfoHTML(additional),
                            allowHTML: true,
                            theme: 'light'
                        })
 
                        valueCell.appendChild(infoSpan);
                    }
                    row.appendChild(valueCell);
 
                    // Column 3: Description for the quantity
                    const descCell = document.createElement('td');
                    descCell.style.border = '1px solid #ccc';
                    descCell.style.padding = '4px';
                    descCell.textContent = this._getText(q['dcc:description']);
                    row.appendChild(descCell);
 
                    table.appendChild(row);
                });
                contentDiv.appendChild(table);
            }
            details.appendChild(contentDiv);
            container.appendChild(details);
        });
        return container;
    }
 
    // Helper: return language-specific text from a node
    _getText(node) {
        if (!node) return '';
        const content = node['dcc:content'];
        Eif (Array.isArray(content)) {
            const match = content.find(item => item.$ && item.$.lang === this.language) || content[0];
            return match._ || match;
        }
        return content._ || content;
    }
 
    // Helper: extract quantities array from an influence condition
    _getQuantities(condition) {
        let quantities = [];
        Eif (condition['dcc:data'] && condition['dcc:data']['dcc:quantity']) {
            quantities = Array.isArray(condition['dcc:data']['dcc:quantity'])
                ? condition['dcc:data']['dcc:quantity']
                : [condition['dcc:data']['dcc:quantity']];
        }
        return quantities;
    }
 
    /**
     * Helper: Format a quantity's value.
     * @param {Object} quantity - The quantity node.
     * @param {boolean} plain - If true, render unit as plain text; otherwise use HTML from DSIUnit.
     * @returns {string}
     */
    _formatQuantity(quantity, plain = false) {
        // Handle noQuantity (categorical data)
        if (quantity['dcc:noQuantity']) {
            return this._getText(quantity['dcc:noQuantity']);
        }
        // Handle si:hybrid block: map over si:real elements.
        if (quantity['si:hybrid']) {
            const hybrid = quantity['si:hybrid'];
            let reals = hybrid['si:real'];
            Iif (!Array.isArray(reals)) {
                reals = [reals];
            }
            const formatted = reals.map((real, index) => {
                const value = real['si:value'] ? real['si:value'].trim() : '';
                let unit = '';
                Eif (real['si:unit']) {
                    const rawUnit = real['si:unit'].trim();
                    const unitObj = new DSIUnit(rawUnit);
                    unit = plain ? unitObj.toHTML() : unitObj.toHTML({ oneLine: true });
                }
                let uncertainty = '';
                Eif (real['si:measurementUncertaintyUnivariate'] &&
                    real['si:measurementUncertaintyUnivariate']['si:expandedMU'] &&
                    real['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU']) {
                    uncertainty = real['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU'].trim();
                }
                let result = uncertainty
                    ? `${value} ± ${uncertainty} ${unit}`
                    : `${value} ${unit}`;
                if (index > 0) {
                    // For subsequent representations, wrap in a light gray span.
                    result = `<span style="color:lightgray;">(${result})</span>`;
                }
                return result;
            });
            return formatted.join(' ');
        }
        // Handle si:realListXMLList
        Iif (quantity['si:realListXMLList']) {
            const rl = quantity['si:realListXMLList'];
            const values = rl['si:valueXMLList'] ? rl['si:valueXMLList'].trim().split(/\s+/) : [];
            let unit = '';
            if (rl['si:unitXMLList']) {
                const rawUnit = rl['si:unitXMLList'].trim();
                const unitObj = new DSIUnit(rawUnit);
                unit = plain ? unitObj.toHTML() : unitObj.toHTML({ oneLine: true });
            }
            let uncertainty = [];
            if (rl['si:measurementUncertaintyUnivariateXMLList'] &&
                rl['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList'] &&
                rl['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList']['si:valueExpandedMUXMLList']) {
                uncertainty = rl['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList']['si:valueExpandedMUXMLList']
                    .trim().split(/\s+/);
            }
            const formatted = values.map((val, i) => {
                const unc = (uncertainty[i] !== undefined) ? uncertainty[i] : '';
                return unc ? `${val} ± ${unc} ${unit}` : `${val} ${unit}`;
            });
            return formatted.join(' / ');
        }
        // Handle si:real
        Eif (quantity['si:real']) {
            const real = quantity['si:real'];
            const value = real['si:value'] ? real['si:value'].trim() : '';
            let unit = '';
            Eif (real['si:unit']) {
                const rawUnit = real['si:unit'].trim();
                const unitObj = new DSIUnit(rawUnit);
                unit = plain ? unitObj.toHTML() : unitObj.toHTML({ oneLine: true });
            }
            let uncertainty = '';
            Eif (real['si:measurementUncertaintyUnivariate'] &&
                real['si:measurementUncertaintyUnivariate']['si:expandedMU'] &&
                real['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU']) {
                uncertainty = real['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU'].trim();
            }
            return uncertainty ? `${value} ± ${uncertainty} ${unit}` : `${value} ${unit}`;
        }
        return '';
    }
 
    // Helper: extract additional data (all keys except the common ones) for tooltip purposes
    _getAdditionalData(quantity) {
        const exclude = ['dcc:name', 'dcc:description', 'si:real', 'si:realListXMLList'];
        const additional = {};
        Object.keys(quantity).forEach(key => {
            if (!exclude.includes(key)) {
                additional[key] = quantity[key];
            }
        });
        return additional;
    }
}
 
function formatAdditionalInfoHTML(additional) {
    Iif (typeof additional !== 'object' || additional === null) {
        return String(additional);
    }
    let parts = [];
    for (const [key, value] of Object.entries(additional)) {
        const valStr = (typeof value === 'object' && value !== null)
            ? JSON.stringify(value)
            : String(value);
        parts.push(
            `<span class="tooltip-key" style="color:#007acc;">${key}</span>: ` +
            `<span class="tooltip-value" style="color:#cc7000;">${valStr}</span>`
        );
    }
    return parts.join(', ');
}