All files / src/components/u-table-view.vue helper.js

4.81% Statements 4/83
0% Branches 0/50
0% Functions 0/12
5.33% Lines 4/75

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    7x                                                                                                           7x 7x           7x                                                                                                                                                                                      
import Color from './Color';
 
export const createTableHeaderExportHelper = (rowCount) => {
    const emptyPlaceholder = {};
    let headContents = Array.from({ length: rowCount }, () => []);
 
    let rowIndex = 0;
    let colIndex = 0;
    let ejectFlag = false;
 
    return {
        setCell,
        eject,
    };
 
    function setCell(content, isCurrentRowEnd = false, rowSpan = 1, colSpan = 1) {
        if (ejectFlag) { return; }
        headContents[rowIndex][colIndex] = content;
        for (let i = 0; i < rowSpan; ++i) {
            for (let j = 0; j < colSpan; ++j) {
                const rIdx = rowIndex + i;
                const cIdx = colIndex + j;
                if (i === 0 && j === 0) {
                    headContents[rIdx][cIdx] = content;
                } else {
                    headContents[rIdx][cIdx] = emptyPlaceholder;
                }
            }
        }
 
        // move rowIndex and colIndex to the right place
        let searchColStart = isCurrentRowEnd ? 0 : colIndex + colSpan;
        const searchRowStart = isCurrentRowEnd ? rowIndex + 1 : rowIndex;
        while (headContents[searchRowStart] && headContents[searchRowStart][searchColStart] === emptyPlaceholder)
            searchColStart++;
 
        const cols = Array.from({ length: colSpan }, (_, i) => i + colIndex);
 
        colIndex = searchColStart;
        rowIndex = searchRowStart;
 
        return cols;
    }
 
    function eject() {
        ejectFlag = true;
 
        const matrix = headContents.map((arr) =>
            arr.map((v) => (v === emptyPlaceholder ? null : v)),
        );
        headContents = null;
 
        return matrix;
    }
};
 
const removeUnit = (value, unit) => value.replace(unit, '');
const toUpperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
/**
 * 获取单元格样式
 * @param {*} elNode
 * @returns
 */
export const getXslxStyle = (elNode) => {
    const nodeComputedStyle = window.getComputedStyle(elNode);
    const wrapText = nodeComputedStyle.textWrap === 'wrap';
    let bgColor = Color.str2Color(nodeComputedStyle.backgroundColor);
    bgColor = removeUnit(bgColor.toARGBHEX(), '#');
    let color = Color.str2Color(nodeComputedStyle.color);
    color = removeUnit(color.toARGBHEX(), '#');
    let fontSize = removeUnit(nodeComputedStyle.fontSize, 'px');
    if (elNode.nodeName === 'TH') {
        // th上可能加style,而title上有自己的样式,需要合并
        const titleNode = elNode.querySelector('[class^="u-table-view_column-title__"]');
        if (titleNode) {
            const titleComputedStyle = window.getComputedStyle(titleNode);
            color = Color.str2Color(titleComputedStyle.color);
            color = removeUnit(color.toARGBHEX(), '#');
            fontSize = removeUnit(titleComputedStyle.fontSize, 'px');
        }
    } else if (elNode.nodeName === 'TD') {
        // 背景色在第一层的linear-layout上
        const childNode = elNode.children[0];
        if (childNode && childNode.className && childNode.className.startsWith('u-linear-layout__')) {
            const childComputedStyle = window.getComputedStyle(childNode);
            let childColor = Color.str2Color(childComputedStyle.color);
            childColor = removeUnit(childColor.toARGBHEX(), '#');
            let childBgColor = Color.str2Color(childComputedStyle.backgroundColor);
            childBgColor = removeUnit(childBgColor.toARGBHEX(), '#');
            if (childBgColor !== '00000000') {
                bgColor = childBgColor;
            }
            if (childColor !== '00000000') {
                color = childColor;
            }
        }
    }
    const border = {};
    const borders = ['top', 'right', 'bottom', 'left'];
    borders.forEach((borderName) => {
        const borderStyle = nodeComputedStyle[`border${toUpperCase(borderName)}Style`];
        if (borderStyle !== 'none') {
            border[borderName] = {
                style: borderStyle === 'solid' ? 'thin' : borderStyle,
                color: {
                    rgb: removeUnit(Color.str2Color(nodeComputedStyle[`border${toUpperCase(borderName)}Color`]).toARGBHEX(), '#'),
                },
            };
        }
    });
    if (nodeComputedStyle.boxShadow) {
        let color = nodeComputedStyle.boxShadow.match(/(?<=\()(.+?)(?=\))/g);
        if (color && color.length > 0) {
            color = Color.str2Color(`rgb(${color[0]})`);
            borders.forEach((borderName) => {
                border[borderName] = {
                    style: 'thin',
                    color: {
                        rgb: removeUnit(color.toARGBHEX(), '#'),
                    },
                };
            });
        }
    }
    const s = {
        font: {
            sz: fontSize,
            color: {
                rgb: color === '00000000' ? '' : color,
            },
        },
        fill: {
            fgColor: {
                rgb: bgColor === '00000000' ? '' : bgColor,
            },
        },
        alignment: {
            horizontal: nodeComputedStyle.textAlign === 'start' ? 'left' : nodeComputedStyle.textAlign,
            vertical: 'center',
            wrapText,
        },
    };
    if (Object.keys(border).length !== 0) {
        s.border = border;
    }
    const rect = {
        width: removeUnit(nodeComputedStyle.width, 'px'),
        height: removeUnit(nodeComputedStyle.height, 'px'),
    };
    return {
        s,
        rect,
    };
};