1 import {Component, Input, OnInit, AfterViewInit, QueryList, ViewChildren} from '@angular/core';
2 import {GridContext, GridColumn, GridRowSelector,
3 GridColumnSet, GridDataSource} from './grid';
4 import {GridFilterControlComponent} from './grid-filter-control.component';
7 selector: 'eg-grid-header',
8 templateUrl: './grid-header.component.html'
11 export class GridHeaderComponent implements OnInit, AfterViewInit {
13 @Input() context: GridContext;
15 dragColumn: GridColumn;
17 batchRowCheckbox: boolean;
19 @ViewChildren(GridFilterControlComponent) filterControls: QueryList<GridFilterControlComponent>;
24 this.context.selectRowsInPageEmitter.subscribe(
25 () => this.batchRowCheckbox = true
30 this.context.filterControls = this.filterControls;
33 onColumnDragEnter($event: any, col: any) {
34 if (this.dragColumn && this.dragColumn.name !== col.name) {
35 col.isDragTarget = true;
37 $event.preventDefault();
40 onColumnDragLeave($event: any, col: any) {
41 col.isDragTarget = false;
42 $event.preventDefault();
45 onColumnDrop(col: GridColumn) {
46 this.context.columnSet.insertBefore(this.dragColumn, col);
47 this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
50 sortOneColumn(col: GridColumn) {
52 const sort = this.context.dataSource.sort;
54 if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
58 this.context.dataSource.sort = [{name: col.name, dir: dir}];
60 if (this.context.useLocalSort) {
61 this.context.sortLocal();
63 this.context.reload();
67 // Returns true if the provided column is sorting in the
68 // specified direction.
69 isColumnSorting(col: GridColumn, dir: string): boolean {
70 const sort = this.context.dataSource.sort.filter(c => c.name === col.name)[0];
71 return sort && sort.dir === dir;
74 handleBatchSelect($event) {
75 if ($event.target.checked) {
76 if (this.context.rowSelector.isEmpty() || !this.allRowsAreSelected()) {
77 // clear selections from other pages to avoid confusion.
78 this.context.rowSelector.clear();
82 this.context.rowSelector.clear();
87 this.context.selectRowsInPage();
90 allRowsAreSelected(): boolean {
91 const rows = this.context.dataSource.getPageOfRows(this.context.pager);
92 const indexes = rows.map(r => this.context.getRowIndex(r));
93 return this.context.rowSelector.contains(indexes);