]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-header.component.ts
Docs: merge 3.2 release notes
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-header.component.ts
1 import {Component, Input, OnInit} from '@angular/core';
2 import {GridContext, GridColumn, GridRowSelector,
3     GridColumnSet, GridDataSource} from './grid';
4
5 @Component({
6   selector: 'eg-grid-header',
7   templateUrl: './grid-header.component.html'
8 })
9
10 export class GridHeaderComponent implements OnInit {
11
12     @Input() context: GridContext;
13
14     dragColumn: GridColumn;
15
16     constructor() {}
17
18     ngOnInit() {}
19
20     onColumnDragEnter($event: any, col: any) {
21         if (this.dragColumn && this.dragColumn.name !== col.name) {
22             col.isDragTarget = true;
23         }
24         $event.preventDefault();
25     }
26
27     onColumnDragLeave($event: any, col: any) {
28         col.isDragTarget = false;
29         $event.preventDefault();
30     }
31
32     onColumnDrop(col: GridColumn) {
33         this.context.columnSet.insertBefore(this.dragColumn, col);
34         this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
35     }
36
37     sortOneColumn(col: GridColumn) {
38         let dir = 'ASC';
39         const sort = this.context.dataSource.sort;
40
41         if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
42             dir = 'DESC';
43         }
44
45         this.context.dataSource.sort = [{name: col.name, dir: dir}];
46
47         if (this.context.useLocalSort) {
48             this.context.sortLocal();
49         } else {
50             this.context.reload();
51         }
52     }
53
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;
59     }
60
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();
66                 this.selectAll();
67             }
68         } else {
69             this.context.rowSelector.clear();
70         }
71     }
72
73     selectAll() {
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);
77     }
78
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);
83     }
84 }
85