angular2-data-table
Version:
angular2-data-table is a Angular2 component for presenting large and complex data.
146 lines (119 loc) • 3.5 kB
text/typescript
import {
Component, Input, HostBinding, ElementRef, Output, EventEmitter, HostListener
} from '@angular/core';
import {
columnsByPin, columnGroupWidths, columnsByPinArr,
translateXY, Keys, scrollbarWidth
} from '../../utils';
export class DataTableBodyRowComponent {
set columns(val: any[]) {
this._columns = val;
this.recalculateColumns(val);
}
get columns(): any[] {
return this._columns;
}
set innerWidth(val: number) {
this._innerWidth = val;
this.recalculateColumns();
}
get innerWidth(): number {
return this._innerWidth;
}
row: any;
offsetX: number;
rowHeight: number;
isSelected: boolean;
get isEvenRow(): boolean {
return this.row.$$index % 2 === 0;
}
get isOddRow(): boolean {
return this.row.$$index % 2 !== 0;
}
activate: EventEmitter<any> = new EventEmitter();
element: any;
columnGroupWidths: any;
columnsByPin: any;
_columns: any[];
_innerWidth: number;
constructor(element: ElementRef) {
this.element = element.nativeElement;
}
trackByGroups(index: number, colGroup: any): any {
return colGroup.type;
}
stylesByGroup(group: string) {
const widths = this.columnGroupWidths;
const offsetX = this.offsetX;
let styles = {
width: `${widths[group]}px`
};
if(group === 'left') {
translateXY(styles, offsetX, 0);
} else if(group === 'right') {
const bodyWidth = parseInt(this.innerWidth + '', 0);
const totalDiff = widths.total - bodyWidth;
const offsetDiff = totalDiff - offsetX;
const offset = (offsetDiff + scrollbarWidth) * -1;
translateXY(styles, offset, 0);
}
return styles;
}
onActivate(event: any, index: number) {
event.cellIndex = index;
event.rowElement = this.element;
this.activate.emit(event);
}
onKeyDown(event: KeyboardEvent): void {
const keyCode = event.keyCode;
const isTargetRow = event.target === this.element;
const isAction =
keyCode === Keys.return ||
keyCode === Keys.down ||
keyCode === Keys.up ||
keyCode === Keys.left ||
keyCode === Keys.right;
if(isAction && isTargetRow) {
event.preventDefault();
event.stopPropagation();
this.activate.emit({
type: 'keydown',
event,
row: this.row,
rowElement: this.element
});
}
}
recalculateColumns(val: any[] = this.columns): void {
const colsByPin = columnsByPin(val);
this.columnsByPin = columnsByPinArr(val);
this.columnGroupWidths = columnGroupWidths(colsByPin, val);
}
}