all files / component/data-grid/data-grid-column/ data-grid-column-layout-infos.ts

81.82% Statements 27/33
68.42% Branches 13/19
100% Functions 4/4
81.25% Lines 26/32
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                                      18× 18× 18× 18× 18× 18× 18×   18× 104× 104×       104×       104×       104× 12× 12×   92× 92×     104×     104×     18×                  
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { UnitValue } from '../../../common/core/graphics/unit-value';
import { IDejaGridColumn } from './data-grid-column';
 
export class DejaGridColumnsLayoutInfos {
    public percentColumns: IDejaGridColumn[];
    public fixedColumns: IDejaGridColumn[];
    public responsiveColumns: IDejaGridColumn[];
    public totalFixedWidth: number;
    public totalPercentWidth: number;
    public columnsWidth: {
        [name: string]: UnitValue;
    };
 
    constructor(columns: IDejaGridColumn[]) {
        this.columnsWidth = {};
        this.percentColumns = [];
        this.fixedColumns = [];
        this.responsiveColumns = [];
        this.totalFixedWidth = 0;
        this.totalPercentWidth = 0;
 
        columns.forEach((column) => {
            let width = new UnitValue(column.width);
            Iif (width.value === undefined) {
                width = new UnitValue(10, '%');
            }
 
            Iif (width.isInvalid()) {
                throw new Error('Invalid column width unit can be for example: 11px or 23%');
            }
 
            Iif (width.unit && width.unit !== 'px' && width.unit !== '%') {
                throw new Error('Column width unit can be only px or %');
            }
 
            if (width.unit === '%') {
                this.percentColumns.push(column);
                this.totalPercentWidth += width.value;
            } else {
                this.fixedColumns.push(column);
                this.totalFixedWidth += width.value;
            }
 
            if (typeof column.responsive === 'number' || column.responsive === true) {
                this.responsiveColumns.push(column);
            }
 
            this.columnsWidth[column.name] = width;
        });
 
        this.responsiveColumns.sort((c1, c2) => {
            Eif (c1.responsive === true) {
                return 1;
            } else if (c2.responsive === true) {
                return -1;
            } else {
                return +c1.responsive - +c2.responsive;
            }
        });
    }
}