]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-header.component.ts
591fc66c2ba58274c87cec81b3fe2eecb2219b3f
[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     batchRowCheckbox: boolean;
17
18     constructor() {}
19
20     ngOnInit() {
21         this.context.selectRowsInPageEmitter.subscribe(
22             () => this.batchRowCheckbox = true
23         );
24     }
25
26     onColumnDragEnter($event: any, col: any) {
27         if (this.dragColumn && this.dragColumn.name !== col.name) {
28             col.isDragTarget = true;
29         }
30         $event.preventDefault();
31     }
32
33     onColumnDragLeave($event: any, col: any) {
34         col.isDragTarget = false;
35         $event.preventDefault();
36     }
37
38     onColumnDrop(col: GridColumn) {
39         this.context.columnSet.insertBefore(this.dragColumn, col);
40         this.context.columnSet.columns.forEach(c => c.isDragTarget = false);
41     }
42
43     sortOneColumn(col: GridColumn) {
44         let dir = 'ASC';
45         const sort = this.context.dataSource.sort;
46
47         if (sort.length && sort[0].name === col.name && sort[0].dir === 'ASC') {
48             dir = 'DESC';
49         }
50
51         this.context.dataSource.sort = [{name: col.name, dir: dir}];
52
53         if (this.context.useLocalSort) {
54             this.context.sortLocal();
55         } else {
56             this.context.reload();
57         }
58     }
59
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;
65     }
66
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();
72                 this.selectAll();
73             }
74         } else {
75             this.context.rowSelector.clear();
76         }
77     }
78
79     selectAll() {
80         this.context.selectRowsInPage();
81     }
82
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);
87     }
88 }
89