angular2-data-table
Version:
angular2-data-table is a Angular2 component for presenting large and complex data.
185 lines (158 loc) • 4.74 kB
text/typescript
import {
Component, Output, EventEmitter, Input, HostBinding
} from '@angular/core';
import { SortType, SelectionType } from '../../types';
import { columnsByPin, columnGroupWidths, columnsByPinArr, translateXY } from '../../utils';
import { DataTableColumnDirective } from '../columns';
export class DataTableHeaderComponent {
sortAscendingIcon: any;
sortDescendingIcon: any;
scrollbarH: boolean;
innerWidth: number;
offsetX: number;
sorts: any[];
sortType: SortType;
allRowsSelected: boolean;
selectionType: SelectionType;
set headerHeight(val: any) {
if(val !== 'auto') {
this._headerHeight = `${val}px`;
} else {
this._headerHeight = val;
}
}
get headerHeight() {
return this._headerHeight;
}
set columns(val: any[]) {
this._columns = val;
const colsByPin = columnsByPin(val);
this.columnsByPin = columnsByPinArr(val);
this.columnGroupWidths = columnGroupWidths(colsByPin, val);
}
get columns(): any[] {
return this._columns;
}
sort: EventEmitter<any> = new EventEmitter();
reorder: EventEmitter<any> = new EventEmitter();
resize: EventEmitter<any> = new EventEmitter();
select: EventEmitter<any> = new EventEmitter();
columnsByPin: any;
columnGroupWidths: any;
_columns: any[];
_headerHeight: string;
get headerWidth(): string {
if(this.scrollbarH) {
return this.innerWidth + 'px';
}
return '100%';
}
trackByGroups(index: number, colGroup: any): any {
return colGroup.type;
}
onColumnResized(width: number, column: DataTableColumnDirective): void {
if (width <= column.minWidth) {
width = column.minWidth;
} else if(width >= column.maxWidth) {
width = column.maxWidth;
}
this.resize.emit({
column,
prevValue: column.width,
newValue: width
});
}
onColumnReordered({ prevIndex, newIndex, model }: any): void {
this.reorder.emit({
column: model,
prevValue: prevIndex,
newValue: newIndex
});
}
onSort({ column, prevValue, newValue }: any): void {
const sorts = this.calcNewSorts(column, prevValue, newValue);
this.sort.emit({
sorts,
column,
prevValue,
newValue
});
}
calcNewSorts(column: any, prevValue: number, newValue: number): any[] {
let idx = 0;
let sorts = this.sorts.map((s, i) => {
s = Object.assign({}, s);
if(s.prop === column.prop) idx = i;
return s;
});
if (newValue === undefined) {
sorts.splice(idx, 1);
} else if (prevValue) {
sorts[idx].dir = newValue;
} else {
if (this.sortType === SortType.single) {
sorts.splice(0, this.sorts.length);
}
sorts.push({ dir: newValue, prop: column.prop });
}
return sorts;
}
stylesByGroup(group: string): any {
const widths = this.columnGroupWidths;
const offsetX = this.offsetX;
let styles = {
width: `${widths[group]}px`
};
if(group === 'center') {
translateXY(styles, offsetX * -1, 0);
} else if(group === 'right') {
const totalDiff = widths.total - this.innerWidth;
const offset = totalDiff * -1;
translateXY(styles, offset, 0);
}
return styles;
}
}