]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid.component.ts
LP1821382 Grid showDeclaredFieldsOnly option; sort repair.
[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     @Input() disableSelect: boolean;
48
49     // Prevent selection of multiple rows
50     @Input() disableMultiSelect: boolean;
51
52     // Show an extra column in the grid where the caller can apply
53     // row-specific flair (material icons).
54     @Input() rowFlairIsEnabled: boolean;
55
56     // Returns a material icon name to display in the flar column
57     // (if enabled) for the given row.
58     @Input() rowFlairCallback: (row: any) => GridRowFlairEntry;
59
60     // Returns a space-separated list of CSS class names to apply to
61     // a given row
62     @Input() rowClassCallback: (row: any) => string;
63
64     // Returns a space-separated list of CSS class names to apply to
65     // a given cell or all cells in a column.
66     @Input() cellClassCallback: (row: any, col: GridColumn) => string;
67
68     // comma-separated list of fields to show by default.
69     // This field takes precedence over hideFields.
70     // When a value is applied, any field not in this list will
71     // be hidden.
72     @Input() showFields: string;
73
74     // comma-separated list of fields to hide.
75     // This does not imply all other fields should be visible, only that
76     // the selected fields will be hidden.
77     @Input() hideFields: string;
78
79     // When true, only display columns that are declared in the markup
80     // and leave all auto-generated fields hidden.
81     @Input() showDeclaredFieldsOnly: boolean;
82
83     // Allow the caller to jump directly to a specific page of
84     // grid data.
85     @Input() pageOffset: number;
86     // Pass in a default page size.  May be overridden by settings.
87     @Input() pageSize: number;
88
89     // If true and an idlClass is specificed, the grid assumes
90     // datatype=link fields that link to classes which define a selector
91     // are fleshed with the linked object.  And, instead of displaying
92     // the raw field value, displays the selector value from the linked
93     // object.  The caller is responsible for fleshing the appropriate
94     // fields in the GridDataSource getRows handler.
95     //
96     // This only applies to auto-generated columns.
97     //
98     // For example, idlClass="aou" and field="ou_type", the display
99     // value will be ou_type().name() since "name" is the selector
100     // field on the "aout" class.
101     @Input() showLinkSelectors: boolean;
102
103     @Input() disablePaging: boolean;
104
105     context: GridContext;
106
107     // These events are emitted from our grid-body component.
108     // They are defined here for ease of access to the caller.
109     @Output() onRowActivate: EventEmitter<any>;
110     @Output() onRowClick: EventEmitter<any>;
111
112     constructor(
113         private idl: IdlService,
114         private org: OrgService,
115         private store: ServerStoreService,
116         private format: FormatService
117     ) {
118         this.context =
119             new GridContext(this.idl, this.org, this.store, this.format);
120         this.onRowActivate = new EventEmitter<any>();
121         this.onRowClick = new EventEmitter<any>();
122     }
123
124     ngOnInit() {
125
126         if (!this.dataSource) {
127             throw new Error('<eg-grid/> requires a [dataSource]');
128         }
129
130         this.context.idlClass = this.idlClass;
131         this.context.dataSource = this.dataSource;
132         this.context.persistKey = this.persistKey;
133         this.context.isSortable = this.sortable === true;
134         this.context.isMultiSortable = this.multiSortable === true;
135         this.context.useLocalSort = this.useLocalSort === true;
136         this.context.disableSelect = this.disableSelect === true;
137         this.context.showLinkSelectors = this.showLinkSelectors === true;
138         this.context.disableMultiSelect = this.disableMultiSelect === true;
139         this.context.rowFlairIsEnabled = this.rowFlairIsEnabled  === true;
140         this.context.showDeclaredFieldsOnly = this.showDeclaredFieldsOnly;
141         this.context.rowFlairCallback = this.rowFlairCallback;
142         this.context.disablePaging = this.disablePaging === true;
143         if (this.showFields) {
144             this.context.defaultVisibleFields = this.showFields.split(',');
145         }
146         if (this.hideFields) {
147             this.context.defaultHiddenFields = this.hideFields.split(',');
148         }
149
150         if (this.pageOffset) {
151             this.context.pager.offset = this.pageOffset;
152         }
153
154         if (this.pageSize) {
155             this.context.pager.limit = this.pageSize;
156         }
157
158         // TS doesn't seem to like: let foo = bar || () => '';
159         this.context.rowClassCallback =
160             this.rowClassCallback || function () { return ''; };
161         this.context.cellClassCallback =
162             this.cellClassCallback || function() { return ''; };
163
164         this.context.init();
165     }
166
167     ngAfterViewInit() {
168         this.context.initData();
169     }
170
171     ngOnDestroy() {
172         this.context.destroy();
173     }
174
175     reload() {
176         this.context.reload();
177     }
178 }
179
180
181