export function processExistingConfig()

in packages/tablesorter-powerbi/src/ConfigBuilder.ts [218:259]


export function processExistingConfig(existingConfig: ITableSorterConfiguration, newColumns: ITableSorterColumn[]) {
    "use strict";
    const newColNames = newColumns.map(c => c.column);
    const oldConfig = merge({}, existingConfig);
    const oldCols = existingConfig.columns || [];
    const sortedColumn = existingConfig.sort && existingConfig.sort.column;

    // Filter out any columns that don't exist anymore
    existingConfig.columns = oldCols.filter(c => newColNames.indexOf(c.column) >= 0);

    // Override the domain, with the newest data
    oldCols.forEach(oldCol => {
        const newCol = newColumns.filter(m => m.column === oldCol.column)[0];
        if (newCol) {
            if (newCol.domain) {
                // Reset the domain, cause we now have a new set of data
                oldCol.domain = newCol.domain.slice(0) as any;
            }
            oldCol.color = newCol.color;
        }

        // If we had a sort, but there is no longer a column for it, or it's type has changed
        // then lets just remove the sort, cause clearly the user changed the underlying dataset.
        if (sortedColumn === oldCol.column &&
            (!newCol || newCol.type !== oldCol.type)) {
            delete existingConfig.sort;
        }

        // For some reason the user passed in the same column, but a different type
        if (newCol && oldCol.type !== newCol.type) {
            merge(oldCol, newCol);
        }
    });

    // If we have a layout
    if (existingConfig.layout && existingConfig.layout.primary) {
        existingConfig.layout.primary =
            syncLayoutColumns(existingConfig.layout.primary, existingConfig.columns, oldConfig.columns);
    }

    removeMissingColumns(existingConfig, newColumns);
}