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;
20 onColumnDragEnter($event: any, col: any) {
21 if (this.dragColumn && this.dragColumn.name !== col.name) {
22 col.isDragTarget = true;
24 $event.preventDefault();
27 onColumnDragLeave($event: any, col: any) {
28 col.isDragTarget = false;
29 $event.preventDefault();
32 onColumnDrop(col: GridColumn) {
33 this.context.columnSet.insertBefore(this.dragColumn, col);
34 this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
37 sortOneColumn(col: GridColumn) {
39 const sort = this.context.dataSource.sort;
41 if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
45 this.context.dataSource.sort = [{name: col.name, dir: dir}];
47 if (this.context.useLocalSort) {
48 this.context.sortLocal();
50 this.context.reload();
54 // Returns true if the provided column is sorting in the
55 // specified direction.
56 isColumnSorting(col: GridColumn, dir: string): boolean {
57 const sort = this.context.dataSource.sort.filter(c => c.name === col.name)[0];
58 return sort && sort.dir === dir;
61 handleBatchSelect($event) {
62 if ($event.target.checked) {
63 if (this.context.rowSelector.isEmpty() || !this.allRowsAreSelected()) {
64 // clear selections from other pages to avoid confusion.
65 this.context.rowSelector.clear();
69 this.context.rowSelector.clear();
74 const rows = this.context.dataSource.getPageOfRows(this.context.pager);
75 const indexes = rows.map(r => this.context.getRowIndex(r));
76 this.context.rowSelector.select(indexes);
79 allRowsAreSelected(): boolean {
80 const rows = this.context.dataSource.getPageOfRows(this.context.pager);
81 const indexes = rows.map(r => this.context.getRowIndex(r));
82 return this.context.rowSelector.contains(indexes);