export function syncLayoutColumns()

in packages/tablesorter-powerbi/src/ConfigBuilder.ts [327:380]


export function syncLayoutColumns(
    layoutCols: ITableSorterLayoutColumn[],
    newCols: ITableSorterColumn[],
    oldCols: ITableSorterColumn[]) {
    "use strict";
    newCols = newCols || [];
    oldCols = oldCols || [];
    layoutCols = layoutCols || [];
    const columnFilter = (c: ITableSorterLayoutColumn) => {
        // If this column exists in the new sets of columns, pass the filter
        const newCol = newCols.filter(m => m.column === c.column)[0];
        const result = !!newCol;
        if (newCol) {

            // Bound the filted domain to the actual domain (in case they set a bad filter)
            const oldCol = oldCols.filter(m => m.column === c.column)[0];
            if (c.domain) {
                // It is filtered if the "filter" domain is different than the actual domain
                const isFiltered =
                    isValidDomain(c.domain) && isValidDomain(oldCol.domain) &&
                    (c.domain[0] !== oldCol.domain[0] || c.domain[1] !== oldCol.domain[1]);
                let lowerBound = newCol.domain[0];
                let upperBound = newCol.domain[1];

                // If it was filtered before, then copy over the filter, but bound it to the new domain
                if (isFiltered) {
                    lowerBound = Math.max(newCol.domain[0], c.domain[0]);
                    upperBound = Math.min(newCol.domain[1], c.domain[1]);
                }

                c.domain = [lowerBound, upperBound];
            }

            // Sync the layout column, with our defined cols
            if (c.type !== newCol.type) {
                merge(c, newCol, {
                    domain: c.domain,
                });

                // Remove the filter if there is one
                delete c.filter;
            }
        }

        if (c.children) {
            c.children = c.children.filter(columnFilter);
            return c.children.length > 0;
        }

        return result;
    };

    return layoutCols.filter(columnFilter);
}