]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid.component.ts
LP 1857351: admin-page grid column order respects specified fieldOrder
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid.component.ts
1 import {Component, Input, Output, OnInit, AfterViewInit, EventEmitter,
2     OnDestroy, ViewChild, ViewEncapsulation} from '@angular/core';
3 import {IdlService} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {ServerStoreService} from '@eg/core/server-store.service';
6 import {FormatService} from '@eg/core/format.service';
7 import {GridContext, GridColumn, GridDataSource,
8     GridCellTextGenerator, GridRowFlairEntry} from './grid';
9 import {GridToolbarComponent} from './grid-toolbar.component';
10
11 /**
12  * Main grid entry point.
13  */
14
15 @Component({
16   selector: 'eg-grid',
17   templateUrl: './grid.component.html',
18   styleUrls: ['grid.component.css'],
19   // share grid css globally once imported so all grid component CSS
20   // can live in grid.component.css and to avoid multiple copies of
21   // the CSS when multiple grids are displayed.
22   encapsulation: ViewEncapsulation.None
23 })
24
25 export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
26
27     // Source of row data.
28     @Input() dataSource: GridDataSource;
29
30     // IDL class for auto-generation of columns
31     @Input() idlClass: string;
32
33     // Comma-separated list of column names, to set the order of columns
34     // from auto-generated columns only
35     @Input() autoGeneratedColumnOrder: string;
36
37     // True if any columns are sortable
38     @Input() sortable: boolean;
39
40     // True if the grid supports sorting of multiple columns at once
41     @Input() multiSortable: boolean;
42
43     // If true, grid sort requests only operate on data that
44     // already exists in the grid data source -- no row fetching.
45     // The assumption is all data is already available.
46     @Input() useLocalSort: boolean;
47
48     // Storage persist key / per-grid-type unique identifier
49     // The value is prefixed with 'eg.grid.'
50     //
51     // If persistKey is set to "disabled", or does not exist,
52     // the grid will not display a Save button to the user
53     @Input() persistKey: string;
54
55     @Input() disableSelect: boolean;
56
57     // Prevent selection of multiple rows
58     @Input() disableMultiSelect: boolean;
59
60     // Show an extra column in the grid where the caller can apply
61     // row-specific flair (material icons).
62     @Input() rowFlairIsEnabled: boolean;
63
64     // Returns a material icon name to display in the flar column
65     // (if enabled) for the given row.
66     @Input() rowFlairCallback: (row: any) => GridRowFlairEntry;
67
68     // Returns a space-separated list of CSS class names to apply to
69     // a given row
70     @Input() rowClassCallback: (row: any) => string;
71
72     // Returns a space-separated list of CSS class names to apply to
73     // a given cell or all cells in a column.
74     @Input() cellClassCallback: (row: any, col: GridColumn) => string;
75
76     // comma-separated list of fields to show by default.
77     // This field takes precedence over hideFields.
78     // When a value is applied, any field not in this list will
79     // be hidden.
80     @Input() showFields: string;
81
82     // comma-separated list of fields to hide.
83     // This does not imply all other fields should be visible, only that
84     // the selected fields will be hidden.
85     @Input() hideFields: string;
86
87     // comma-separated list of fields to ignore when generating columns
88     // from the IDL.
89     // This does not imply all other fields should be available, only
90     // that the selected fields will be ignored.
91     @Input() ignoreFields: string;
92
93     // When true, only display columns that are declared in the markup
94     // and leave all auto-generated fields hidden.
95     @Input() showDeclaredFieldsOnly: boolean;
96
97     // Allow the caller to jump directly to a specific page of
98     // grid data.
99     @Input() pageOffset: number;
100     // Pass in a default page size.  May be overridden by settings.
101     @Input() pageSize: number;
102
103     @Input() showLinkSelectors: boolean;
104
105     @Input() disablePaging: boolean;
106
107     // result filtering
108     //
109     // filterable: true if the result filtering controls
110     // should be displayed
111     @Input() filterable: boolean;
112
113     // sticky grid header
114     //
115     // stickyHeader: true of the grid header should be
116     // "sticky", i.e., remain visible if if the table is long
117     // and the user has scrolled far enough that the header
118     // would go out of view
119     @Input() stickyHeader: boolean;
120
121     @Input() cellTextGenerator: GridCellTextGenerator;
122
123     context: GridContext;
124
125     // These events are emitted from our grid-body component.
126     // They are defined here for ease of access to the caller.
127     @Output() onRowActivate: EventEmitter<any>;
128     @Output() onRowClick: EventEmitter<any>;
129
130     @ViewChild('toolbar', { static: true }) toolbar: GridToolbarComponent;
131
132     constructor(
133         private idl: IdlService,
134         private org: OrgService,
135         private store: ServerStoreService,
136         private format: FormatService
137     ) {
138         this.context =
139             new GridContext(this.idl, this.org, this.store, this.format);
140         this.onRowActivate = new EventEmitter<any>();
141         this.onRowClick = new EventEmitter<any>();
142     }
143
144     ngOnInit() {
145
146         if (!this.dataSource) {
147             throw new Error('<eg-grid/> requires a [dataSource]');
148         }
149
150         this.context.idlClass = this.idlClass;
151         this.context.dataSource = this.dataSource;
152         this.context.persistKey = this.persistKey;
153         this.context.autoGeneratedColumnOrder = this.autoGeneratedColumnOrder;
154         this.context.isSortable = this.sortable === true;
155         this.context.isFilterable = this.filterable === true;
156         this.context.stickyGridHeader = this.stickyHeader === true;
157         this.context.isMultiSortable = this.multiSortable === true;
158         this.context.useLocalSort = this.useLocalSort === true;
159         this.context.disableSelect = this.disableSelect === true;
160         this.context.disableMultiSelect = this.disableMultiSelect === true;
161         this.context.rowFlairIsEnabled = this.rowFlairIsEnabled  === true;
162         this.context.showDeclaredFieldsOnly = this.showDeclaredFieldsOnly;
163         this.context.rowFlairCallback = this.rowFlairCallback;
164         this.context.disablePaging = this.disablePaging === true;
165         this.context.cellTextGenerator = this.cellTextGenerator;
166         this.context.ignoredFields = [];
167
168         if (this.showFields) {
169             this.context.defaultVisibleFields = this.showFields.split(',');
170         }
171         if (this.hideFields) {
172             this.context.defaultHiddenFields = this.hideFields.split(',');
173         }
174         if (this.ignoreFields) {
175             this.context.ignoredFields = this.ignoreFields.split(',');
176         }
177
178         if (this.pageOffset) {
179             this.context.pager.offset = this.pageOffset;
180         }
181
182         if (this.pageSize) {
183             this.context.pager.limit = this.pageSize;
184         }
185
186         // TS doesn't seem to like: let foo = bar || () => '';
187         this.context.rowClassCallback =
188             this.rowClassCallback || function () { return ''; };
189         this.context.cellClassCallback =
190             this.cellClassCallback || function() { return ''; };
191
192         if (this.showLinkSelectors) {
193             console.debug(
194                 'showLinkSelectors is deprecated and no longer has any effect');
195         }
196
197         this.context.init();
198     }
199
200     ngAfterViewInit() {
201         this.context.initData();
202     }
203
204     ngOnDestroy() {
205         this.context.destroy();
206     }
207
208     print = () => {
209         this.toolbar.printHtml();
210     }
211
212     reload() {
213         this.context.reload();
214     }
215     reloadWithoutPagerReset() {
216         this.context.reloadWithoutPagerReset();
217     }
218
219
220 }
221
222
223