]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-body.component.ts
LP1816480 Angular grid ARIA improvements
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-body.component.ts
1 import {Component, Input, OnInit, Host} from '@angular/core';
2 import {GridContext, GridColumn, GridRowSelector,
3     GridColumnSet, GridDataSource} from './grid';
4 import {GridComponent} from './grid.component';
5
6 @Component({
7   selector: 'eg-grid-body',
8   templateUrl: './grid-body.component.html'
9 })
10
11 export class GridBodyComponent implements OnInit {
12
13     @Input() context: GridContext;
14
15     constructor(@Host() private grid: GridComponent) {}
16
17     ngOnInit() {}
18
19     // Not using @HostListener because it only works globally.
20     onGridKeyDown(evt: KeyboardEvent) {
21         switch (evt.key) {
22             case 'ArrowUp':
23                 if (evt.shiftKey) {
24                     // Extend selection up one row
25                     this.context.selectMultiRowsPrevious();
26                 } else {
27                     this.context.selectPreviousRow();
28                 }
29                 evt.stopPropagation();
30                 break;
31             case 'ArrowDown':
32                 if (evt.shiftKey) {
33                     // Extend selection down one row
34                     this.context.selectMultiRowsNext();
35                 } else {
36                     this.context.selectNextRow();
37                 }
38                 evt.stopPropagation();
39                 break;
40             case 'ArrowLeft':
41             case 'PageUp':
42                 this.context.toPrevPage()
43                 .then(ok => this.context.selectFirstRow(), err => {});
44                 evt.stopPropagation();
45                 break;
46             case 'ArrowRight':
47             case 'PageDown':
48                 this.context.toNextPage()
49                 .then(ok => this.context.selectFirstRow(), err => {});
50                 evt.stopPropagation();
51                 break;
52             case 'a':
53                 // control-a means select all visible rows.
54                 // For consistency, select all rows in the current page only.
55                 if (evt.ctrlKey) {
56                     this.context.rowSelector.clear();
57                     this.context.selectRowsInPage();
58                     evt.preventDefault();
59                 }
60                 break;
61
62             case 'Enter':
63                 if (this.context.lastSelectedIndex) {
64                     this.grid.onRowActivate.emit(
65                         this.context.getRowByIndex(
66                             this.context.lastSelectedIndex)
67                     );
68                 }
69                 evt.stopPropagation();
70                 break;
71         }
72     }
73
74     onRowClick($event: any, row: any, idx: number) {
75
76         if (this.context.disableSelect) {
77             // Avoid any appearance or click behavior when row
78             // selection is disabled.
79             return;
80         }
81
82         const index = this.context.getRowIndex(row);
83
84         if (this.context.disableMultiSelect) {
85             this.context.selectOneRow(index);
86         } else if ($event.ctrlKey || $event.metaKey /* mac command */) {
87             if (this.context.toggleSelectOneRow(index)) {
88                 this.context.lastSelectedIndex = index;
89             }
90
91         } else if ($event.shiftKey) {
92             // TODO shift range click
93
94         } else {
95             this.context.selectOneRow(index);
96         }
97
98         this.grid.onRowClick.emit(row);
99     }
100
101     onRowDblClick(row: any) {
102         this.grid.onRowActivate.emit(row);
103     }
104
105 }
106