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, GridRowFlairEntry} from './grid';
8 import {GridToolbarComponent} from './grid-toolbar.component';
11 * Main grid entry point.
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
24 export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
26 // Source of row data.
27 @Input() dataSource: GridDataSource;
29 // IDL class for auto-generation of columns
30 @Input() idlClass: string;
32 // True if any columns are sortable
33 @Input() sortable: boolean;
35 // True if the grid supports sorting of multiple columns at once
36 @Input() multiSortable: boolean;
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;
43 // Storage persist key / per-grid-type unique identifier
44 // The value is prefixed with 'eg.grid.'
46 // If persistKey is set to "disabled", or does not exist,
47 // the grid will not display a Save button to the user
48 @Input() persistKey: string;
50 @Input() disableSelect: boolean;
52 // Prevent selection of multiple rows
53 @Input() disableMultiSelect: boolean;
55 // Show an extra column in the grid where the caller can apply
56 // row-specific flair (material icons).
57 @Input() rowFlairIsEnabled: boolean;
59 // Returns a material icon name to display in the flar column
60 // (if enabled) for the given row.
61 @Input() rowFlairCallback: (row: any) => GridRowFlairEntry;
63 // Returns a space-separated list of CSS class names to apply to
65 @Input() rowClassCallback: (row: any) => string;
67 // Returns a space-separated list of CSS class names to apply to
68 // a given cell or all cells in a column.
69 @Input() cellClassCallback: (row: any, col: GridColumn) => string;
71 // comma-separated list of fields to show by default.
72 // This field takes precedence over hideFields.
73 // When a value is applied, any field not in this list will
75 @Input() showFields: string;
77 // comma-separated list of fields to hide.
78 // This does not imply all other fields should be visible, only that
79 // the selected fields will be hidden.
80 @Input() hideFields: string;
82 // When true, only display columns that are declared in the markup
83 // and leave all auto-generated fields hidden.
84 @Input() showDeclaredFieldsOnly: boolean;
86 // Allow the caller to jump directly to a specific page of
88 @Input() pageOffset: number;
89 // Pass in a default page size. May be overridden by settings.
90 @Input() pageSize: number;
92 // If true and an idlClass is specificed, the grid assumes
93 // datatype=link fields that link to classes which define a selector
94 // are fleshed with the linked object. And, instead of displaying
95 // the raw field value, displays the selector value from the linked
96 // object. The caller is responsible for fleshing the appropriate
97 // fields in the GridDataSource getRows handler.
99 // This only applies to auto-generated columns.
101 // For example, idlClass="aou" and field="ou_type", the display
102 // value will be ou_type().name() since "name" is the selector
103 // field on the "aout" class.
104 @Input() showLinkSelectors: boolean;
106 @Input() disablePaging: boolean;
110 // filterable: true if the result filtering controls
111 // should be displayed
112 @Input() filterable: boolean;
114 // sticky grid header
116 // stickyHeader: true of the grid header should be
117 // "sticky", i.e., remain visible if if the table is long
118 // and the user has scrolled far enough that the header
119 // would go out of view
120 @Input() stickyHeader: boolean;
122 context: GridContext;
124 // These events are emitted from our grid-body component.
125 // They are defined here for ease of access to the caller.
126 @Output() onRowActivate: EventEmitter<any>;
127 @Output() onRowClick: EventEmitter<any>;
129 @ViewChild('toolbar') toolbar: GridToolbarComponent;
132 private idl: IdlService,
133 private org: OrgService,
134 private store: ServerStoreService,
135 private format: FormatService
138 new GridContext(this.idl, this.org, this.store, this.format);
139 this.onRowActivate = new EventEmitter<any>();
140 this.onRowClick = new EventEmitter<any>();
145 if (!this.dataSource) {
146 throw new Error('<eg-grid/> requires a [dataSource]');
149 this.context.idlClass = this.idlClass;
150 this.context.dataSource = this.dataSource;
151 this.context.persistKey = this.persistKey;
152 this.context.isSortable = this.sortable === true;
153 this.context.isFilterable = this.filterable === true;
154 this.context.stickyGridHeader = this.stickyHeader === true;
155 this.context.isMultiSortable = this.multiSortable === true;
156 this.context.useLocalSort = this.useLocalSort === true;
157 this.context.disableSelect = this.disableSelect === true;
158 this.context.showLinkSelectors = this.showLinkSelectors === true;
159 this.context.disableMultiSelect = this.disableMultiSelect === true;
160 this.context.rowFlairIsEnabled = this.rowFlairIsEnabled === true;
161 this.context.showDeclaredFieldsOnly = this.showDeclaredFieldsOnly;
162 this.context.rowFlairCallback = this.rowFlairCallback;
163 this.context.disablePaging = this.disablePaging === true;
164 if (this.showFields) {
165 this.context.defaultVisibleFields = this.showFields.split(',');
167 if (this.hideFields) {
168 this.context.defaultHiddenFields = this.hideFields.split(',');
171 if (this.pageOffset) {
172 this.context.pager.offset = this.pageOffset;
176 this.context.pager.limit = this.pageSize;
179 // TS doesn't seem to like: let foo = bar || () => '';
180 this.context.rowClassCallback =
181 this.rowClassCallback || function () { return ''; };
182 this.context.cellClassCallback =
183 this.cellClassCallback || function() { return ''; };
189 this.context.initData();
193 this.context.destroy();
197 this.toolbar.printHtml();
201 this.context.reload();
203 reloadWithoutPagerReset() {
204 this.context.reloadWithoutPagerReset();