]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-header.component.ts
LP#1831788: add result filtering and other improvements to the Angular eg-grid
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-header.component.ts
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';
5
6 @Component({
7   selector: 'eg-grid-header',
8   templateUrl: './grid-header.component.html'
9 })
10
11 export class GridHeaderComponent implements OnInit, AfterViewInit {
12
13     @Input() context: GridContext;
14
15     dragColumn: GridColumn;
16
17     batchRowCheckbox: boolean;
18
19     @ViewChildren(GridFilterControlComponent) filterControls: QueryList<GridFilterControlComponent>;
20
21     constructor() {}
22
23     ngOnInit() {
24         this.context.selectRowsInPageEmitter.subscribe(
25             () => this.batchRowCheckbox = true
26         );
27     }
28
29     ngAfterViewInit() {
30         this.context.filterControls = this.filterControls;
31     }
32
33     onColumnDragEnter($event: any, col: any) {
34         if (this.dragColumn && this.dragColumn.name !== col.name) {
35             col.isDragTarget = true;
36         }
37         $event.preventDefault();
38     }
39
40     onColumnDragLeave($event: any, col: any) {
41         col.isDragTarget = false;
42         $event.preventDefault();
43     }
44
45     onColumnDrop(col: GridColumn) {
46         this.context.columnSet.insertBefore(this.dragColumn, col);
47         this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
48     }
49
50     sortOneColumn(col: GridColumn) {
51         let dir = 'ASC';
52         const sort = this.context.dataSource.sort;
53
54         if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
55             dir = 'DESC';
56         }
57
58         this.context.dataSource.sort = [{name: col.name, dir: dir}];
59
60         if (this.context.useLocalSort) {
61             this.context.sortLocal();
62         } else {
63             this.context.reload();
64         }
65     }
66
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;
72     }
73
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();
79                 this.selectAll();
80             }
81         } else {
82             this.context.rowSelector.clear();
83         }
84     }
85
86     selectAll() {
87         this.context.selectRowsInPage();
88     }
89
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);
94     }
95 }
96