all files / app/utils/ grouped_grid.ts

36.51% Statements 23/63
0% Branches 0/14
9.09% Functions 1/11
33.33% Lines 20/60
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            1×   1× 1× 1× 1×                             1×     1×   1× 1× 1× 1× 1× 1×   1×                   1×                   1×                                                                                             1×             1×       1×     1×  
/* Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */
 
import { Component, EventEmitter, Output, Input } from '@angular/core';
import { Column } from './column';
import { ColumnHeader } from './column_header';
import { Grid } from './grid';
import { AppState } from '../app.service';
import { Caption } from '../model/caption';
 
@Component({
    selector: 'grid',
    inputs: [
        'rows: rows',
        'grouped_header: grouped_header',
        'context: context',
        'rowsOnPage: rowsOnPage'
    ],
    template: require('./../../templates/grouped_grid.html'),
    styles: [
        require('../../styles/grouped_grid.less')
    ],
})
export class GroupedGrid extends Grid {
 
    @Output('action')
    actionEmitter: EventEmitter<any> = new EventEmitter();
 
    @Input() tools: boolean = false;
    @Input() caption: Caption = null;
    @Input() loadMoreCount: number | boolean;
    @Input() loadedRowCount: number;
    @Input() enableGroupOperation: boolean = false;
    @Input() id: string;
 
    @Input() highlightPhrase: string = '';
 
    constructor(
        public appState: AppState
    ) {
        super(appState);
    }
    context: any;
 
    _grouped_header: boolean;
    get grouped_header(): boolean {
        return this._grouped_header;
    }
    set grouped_header(value: boolean){
        this._grouped_header = value;
        this._headers = null;
    }
 
    subheaders: Array<ColumnHeader> = [];
    _headers: Array<ColumnHeader> = null;
    get headers(): Array<ColumnHeader>{
        if (this._headers == null) {
            let component = this;
            this._headers = [];
            let groups = {};
            this.subheaders = [];
            this.columns.forEach(function(column: Column){
                let header = new ColumnHeader(column);
 
                if (component.grouped_header) {
                    if (header.isGroup) {
                        let subheader = new ColumnHeader(column);
                        subheader.isGroup = false;
                        subheader.colSpan = 1;
                        subheader.rowSpan = 1;
                        subheader.descr = column.descr;
 
                        component.subheaders.push(subheader);
                        if (groups.hasOwnProperty(column.group)) {
                            if (!column.hidden) {
                                groups[column.group]++;
                            }
                            return;
                        } else {
                            groups[column.group] = 1;
                        }
                    }
                } else {
                    header.rowSpan = 2;
                    header.colSpan = 1;
                    //header.descr = column.descr;
                    header.isGroup = false;
                }
 
                component._headers.push(header);
            });
 
            this._headers.forEach(function(header: ColumnHeader){
                if (header.isGroup)
                    header.colSpan = groups[header.descr];
            });
        }
        return this._headers;
    }
 
    private _columns: Column[];
 
    get columns(): Column[] {
        return this._columns;
    }
    set columns(val: Column[]) {
        this._columns = val;
        this._headers = null;
    }
    reset() {
        this._headers = null;
    }
 
    actionClick(event, action, row) {
        this.actionEmitter.emit({event, action, row});
    }
}