1 import {Component, Input, OnInit, Host} from '@angular/core';
2 import {GridContext, GridColumn, GridRowSelector,
3 GridToolbarAction, GridColumnSet, GridDataSource} from './grid';
4 import {GridComponent} from './grid.component';
5 import {NgbPopover} from '@ng-bootstrap/ng-bootstrap';
8 selector: 'eg-grid-body',
9 templateUrl: './grid-body.component.html'
12 export class GridBodyComponent implements OnInit {
14 @Input() context: GridContext;
16 // Track the context menus so we can manually close them
17 // when another popover is opened.
18 contextMenus: NgbPopover[];
20 constructor(@Host() private grid: GridComponent) {
21 this.contextMenus = [];
26 // Not using @HostListener because it only works globally.
27 onGridKeyDown(evt: KeyboardEvent) {
31 // Extend selection up one row
32 this.context.selectMultiRowsPrevious();
34 this.context.selectPreviousRow();
36 evt.stopPropagation();
40 // Extend selection down one row
41 this.context.selectMultiRowsNext();
43 this.context.selectNextRow();
45 evt.stopPropagation();
49 this.context.toPrevPage()
50 .then(ok => this.context.selectFirstRow(), err => {});
51 evt.stopPropagation();
55 this.context.toNextPage()
56 .then(ok => this.context.selectFirstRow(), err => {});
57 evt.stopPropagation();
60 // control-a means select all visible rows.
61 // For consistency, select all rows in the current page only.
63 this.context.rowSelector.clear();
64 this.context.selectRowsInPage();
70 if (this.context.lastSelectedIndex) {
71 this.grid.onRowActivate.emit(
72 this.context.getRowByIndex(
73 this.context.lastSelectedIndex)
76 evt.stopPropagation();
81 handleRowClick($event: any, row: any) {
83 if (this.context.disableSelect) {
84 // Avoid any appearance or click behavior when row
85 // selection is disabled.
89 const index = this.context.getRowIndex(row);
91 if (this.context.disableMultiSelect) {
92 this.context.selectOneRow(index);
93 } else if ($event.ctrlKey || $event.metaKey /* mac command */) {
94 if (this.context.toggleSelectOneRow(index)) {
95 this.context.lastSelectedIndex = index;
98 } else if ($event.shiftKey) {
99 // TODO shift range click
102 this.context.selectOneRow(index);
106 onRowClick($event: any, row: any, idx: number) {
107 this.handleRowClick($event, row);
108 this.grid.onRowClick.emit(row);
111 onRowDblClick(row: any) {
112 this.grid.onRowActivate.emit(row);
115 performAction(action: GridToolbarAction) {
116 action.action(this.context.getSelectedRows());
119 // Apply row selection, track the new menu if needed,
120 // manually close any existing open menus, open selected menu.
121 onRowContextClick($event, row: any, contextMenu: NgbPopover) {
122 $event.preventDefault(); // prevent browser context menu
124 if (this.context.toolbarActions.length === 0) {
125 // No actions to render.
129 if (!this.context.rowIsSelected(row)) {
130 // If the focused row is not selected, select it.
131 // Otherwise, avoid modifying the row selection.
132 this.context.selectOneRow(this.context.getRowIndex(row));
135 const existing = this.contextMenus.filter(m => m === contextMenu)[0];
137 this.contextMenus.push(contextMenu);
140 // Force any previously opened menus to close, which does
141 // not naturally occur via context-click.
142 this.contextMenus.forEach(m => m.close());
144 contextMenu.open({gridContext: this.context});