]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid.component.ts
3bcc2cbe783ab1a76d47a521f139bba20390fd3c
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid.component.ts
1 import {Component, Input, Output, OnInit, AfterViewInit, EventEmitter,
2     OnDestroy, HostListener, ViewEncapsulation} from '@angular/core';
3 import {Subscription} from 'rxjs';
4 import {IdlService} from '@eg/core/idl.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {ServerStoreService} from '@eg/core/server-store.service';
7 import {FormatService} from '@eg/core/format.service';
8 import {GridContext, GridColumn, GridDataSource, GridRowFlairEntry} from './grid';
9
10 /**
11  * Main grid entry point.
12  */
13
14 @Component({
15   selector: 'eg-grid',
16   templateUrl: './grid.component.html',
17   styleUrls: ['grid.component.css'],
18   // share grid css globally once imported so all grid component CSS
19   // can live in grid.component.css and to avoid multiple copies of
20   // the CSS when multiple grids are displayed.
21   encapsulation: ViewEncapsulation.None
22 })
23
24 export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
25
26     // Source of row data.
27     @Input() dataSource: GridDataSource;
28
29     // IDL class for auto-generation of columns
30     @Input() idlClass: string;
31
32     // True if any columns are sortable
33     @Input() sortable: boolean;
34
35     // True if the grid supports sorting of multiple columns at once
36     @Input() multiSortable: boolean;
37
38     // If true, grid sort requests only operate on data that
39     // already exists in the grid data source -- no row fetching.
40     // The assumption is all data is already available.
41     @Input() useLocalSort: boolean;
42
43     // Storage persist key / per-grid-type unique identifier
44     // The value is prefixed with 'eg.grid.'
45     @Input() persistKey: string;
46
47     // Prevent selection of multiple rows
48     @Input() disableMultiSelect: boolean;
49
50     // Show an extra column in the grid where the caller can apply
51     // row-specific flair (material icons).
52     @Input() rowFlairIsEnabled: boolean;
53
54     // Returns a material icon name to display in the flar column
55     // (if enabled) for the given row.
56     @Input() rowFlairCallback: (row: any) => GridRowFlairEntry;
57
58     // Returns a space-separated list of CSS class names to apply to
59     // a given row
60     @Input() rowClassCallback: (row: any) => string;
61
62     // Returns a space-separated list of CSS class names to apply to
63     // a given cell or all cells in a column.
64     @Input() cellClassCallback: (row: any, col: GridColumn) => string;
65
66     // comma-separated list of fields to show by default.
67     // This field takes precedence over hideFields.
68     // When a value is applied, any field not in this list will
69     // be hidden.
70     @Input() showFields: string;
71
72     // comma-separated list of fields to hide.
73     // This does not imply all other fields should be visible, only that
74     // the selected fields will be hidden.
75     @Input() hideFields: string;
76
77     // Allow the caller to jump directly to a specific page of
78     // grid data.
79     @Input() pageOffset: number;
80
81     context: GridContext;
82
83     // These events are emitted from our grid-body component.
84     // They are defined here for ease of access to the caller.
85     @Output() onRowActivate: EventEmitter<any>;
86     @Output() onRowClick: EventEmitter<any>;
87
88     constructor(
89         private idl: IdlService,
90         private org: OrgService,
91         private store: ServerStoreService,
92         private format: FormatService
93     ) {
94         this.context =
95             new GridContext(this.idl, this.org, this.store, this.format);
96         this.onRowActivate = new EventEmitter<any>();
97         this.onRowClick = new EventEmitter<any>();
98     }
99
100     ngOnInit() {
101
102         if (!this.dataSource) {
103             throw new Error('<eg-grid/> requires a [dataSource]');
104         }
105
106         this.context.idlClass = this.idlClass;
107         this.context.dataSource = this.dataSource;
108         this.context.persistKey = this.persistKey;
109         this.context.isSortable = this.sortable === true;
110         this.context.isMultiSortable = this.multiSortable === true;
111         this.context.useLocalSort = this.useLocalSort === true;
112         this.context.disableMultiSelect = this.disableMultiSelect === true;
113         this.context.rowFlairIsEnabled = this.rowFlairIsEnabled  === true;
114         this.context.rowFlairCallback = this.rowFlairCallback;
115         if (this.showFields) {
116             this.context.defaultVisibleFields = this.showFields.split(',');
117         }
118         if (this.hideFields) {
119             this.context.defaultHiddenFields = this.hideFields.split(',');
120         }
121
122         if (this.pageOffset) {
123             this.context.pager.offset = this.pageOffset;
124         }
125
126         // TS doesn't seem to like: let foo = bar || () => '';
127         this.context.rowClassCallback =
128             this.rowClassCallback || function () { return ''; };
129         this.context.cellClassCallback =
130             this.cellClassCallback || function() { return ''; };
131
132         this.context.init();
133     }
134
135     ngAfterViewInit() {
136         this.context.initData();
137     }
138
139     ngOnDestroy() {
140         this.context.destroy();
141     }
142
143     reload() {
144         this.context.reload();
145     }
146 }
147
148
149