1 import {Component, Input, OnInit} from '@angular/core';
2 import {GridContext, GridColumn, GridRowSelector,
3 GridColumnSet, GridDataSource} from './grid';
6 selector: 'eg-grid-header',
7 templateUrl: './grid-header.component.html'
10 export class GridHeaderComponent implements OnInit {
12 @Input() context: GridContext;
14 dragColumn: GridColumn;
16 batchRowCheckbox: boolean;
21 this.context.selectRowsInPageEmitter.subscribe(
22 () => this.batchRowCheckbox = true
26 onColumnDragEnter($event: any, col: any) {
27 if (this.dragColumn && this.dragColumn.name !== col.name) {
28 col.isDragTarget = true;
30 $event.preventDefault();
33 onColumnDragLeave($event: any, col: any) {
34 col.isDragTarget = false;
35 $event.preventDefault();
38 onColumnDrop(col: GridColumn) {
39 this.context.columnSet.insertBefore(this.dragColumn, col);
40 this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
43 sortOneColumn(col: GridColumn) {
45 const sort = this.context.dataSource.sort;
47 if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
51 this.context.dataSource.sort = [{name: col.name, dir: dir}];
53 if (this.context.useLocalSort) {
54 this.context.sortLocal();
56 this.context.reload();
60 // Returns true if the provided column is sorting in the
61 // specified direction.
62 isColumnSorting(col: GridColumn, dir: string): boolean {
63 const sort = this.context.dataSource.sort.filter(c => c.name === col.name)[0];
64 return sort && sort.dir === dir;
67 handleBatchSelect($event) {
68 if ($event.target.checked) {
69 if (this.context.rowSelector.isEmpty() || !this.allRowsAreSelected()) {
70 // clear selections from other pages to avoid confusion.
71 this.context.rowSelector.clear();
75 this.context.rowSelector.clear();
80 this.context.selectRowsInPage();
83 allRowsAreSelected(): boolean {
84 const rows = this.context.dataSource.getPageOfRows(this.context.pager);
85 const indexes = rows.map(r => this.context.getRowIndex(r));
86 return this.context.rowSelector.contains(indexes);