1 import {Component, Input, Output, OnInit, AfterViewInit, EventEmitter,
2 OnDestroy, 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';
10 * Main grid entry point.
15 templateUrl: './grid.component.html',
16 styleUrls: ['grid.component.css'],
17 // share grid css globally once imported so all grid component CSS
18 // can live in grid.component.css and to avoid multiple copies of
19 // the CSS when multiple grids are displayed.
20 encapsulation: ViewEncapsulation.None
23 export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
25 // Source of row data.
26 @Input() dataSource: GridDataSource;
28 // IDL class for auto-generation of columns
29 @Input() idlClass: string;
31 // True if any columns are sortable
32 @Input() sortable: boolean;
34 // True if the grid supports sorting of multiple columns at once
35 @Input() multiSortable: boolean;
37 // If true, grid sort requests only operate on data that
38 // already exists in the grid data source -- no row fetching.
39 // The assumption is all data is already available.
40 @Input() useLocalSort: boolean;
42 // Storage persist key / per-grid-type unique identifier
43 // The value is prefixed with 'eg.grid.'
45 // If persistKey is set to "disabled", or does not exist,
46 // the grid will not display a Save button to the user
47 @Input() persistKey: string;
49 @Input() disableSelect: boolean;
51 // Prevent selection of multiple rows
52 @Input() disableMultiSelect: boolean;
54 // Show an extra column in the grid where the caller can apply
55 // row-specific flair (material icons).
56 @Input() rowFlairIsEnabled: boolean;
58 // Returns a material icon name to display in the flar column
59 // (if enabled) for the given row.
60 @Input() rowFlairCallback: (row: any) => GridRowFlairEntry;
62 // Returns a space-separated list of CSS class names to apply to
64 @Input() rowClassCallback: (row: any) => string;
66 // Returns a space-separated list of CSS class names to apply to
67 // a given cell or all cells in a column.
68 @Input() cellClassCallback: (row: any, col: GridColumn) => string;
70 // comma-separated list of fields to show by default.
71 // This field takes precedence over hideFields.
72 // When a value is applied, any field not in this list will
74 @Input() showFields: string;
76 // comma-separated list of fields to hide.
77 // This does not imply all other fields should be visible, only that
78 // the selected fields will be hidden.
79 @Input() hideFields: string;
81 // When true, only display columns that are declared in the markup
82 // and leave all auto-generated fields hidden.
83 @Input() showDeclaredFieldsOnly: boolean;
85 // Allow the caller to jump directly to a specific page of
87 @Input() pageOffset: number;
88 // Pass in a default page size. May be overridden by settings.
89 @Input() pageSize: number;
91 // If true and an idlClass is specificed, the grid assumes
92 // datatype=link fields that link to classes which define a selector
93 // are fleshed with the linked object. And, instead of displaying
94 // the raw field value, displays the selector value from the linked
95 // object. The caller is responsible for fleshing the appropriate
96 // fields in the GridDataSource getRows handler.
98 // This only applies to auto-generated columns.
100 // For example, idlClass="aou" and field="ou_type", the display
101 // value will be ou_type().name() since "name" is the selector
102 // field on the "aout" class.
103 @Input() showLinkSelectors: boolean;
105 @Input() disablePaging: boolean;
109 // filterable: true if the result filtering controls
110 // should be displayed
111 @Input() filterable: boolean;
113 // sticky grid header
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;
121 context: GridContext;
123 // These events are emitted from our grid-body component.
124 // They are defined here for ease of access to the caller.
125 @Output() onRowActivate: EventEmitter<any>;
126 @Output() onRowClick: EventEmitter<any>;
129 private idl: IdlService,
130 private org: OrgService,
131 private store: ServerStoreService,
132 private format: FormatService
135 new GridContext(this.idl, this.org, this.store, this.format);
136 this.onRowActivate = new EventEmitter<any>();
137 this.onRowClick = new EventEmitter<any>();
142 if (!this.dataSource) {
143 throw new Error('<eg-grid/> requires a [dataSource]');
146 this.context.idlClass = this.idlClass;
147 this.context.dataSource = this.dataSource;
148 this.context.persistKey = this.persistKey;
149 this.context.isSortable = this.sortable === true;
150 this.context.isFilterable = this.filterable === true;
151 this.context.stickyGridHeader = this.stickyHeader === true;
152 this.context.isMultiSortable = this.multiSortable === true;
153 this.context.useLocalSort = this.useLocalSort === true;
154 this.context.disableSelect = this.disableSelect === true;
155 this.context.showLinkSelectors = this.showLinkSelectors === true;
156 this.context.disableMultiSelect = this.disableMultiSelect === true;
157 this.context.rowFlairIsEnabled = this.rowFlairIsEnabled === true;
158 this.context.showDeclaredFieldsOnly = this.showDeclaredFieldsOnly;
159 this.context.rowFlairCallback = this.rowFlairCallback;
160 this.context.disablePaging = this.disablePaging === true;
161 if (this.showFields) {
162 this.context.defaultVisibleFields = this.showFields.split(',');
164 if (this.hideFields) {
165 this.context.defaultHiddenFields = this.hideFields.split(',');
168 if (this.pageOffset) {
169 this.context.pager.offset = this.pageOffset;
173 this.context.pager.limit = this.pageSize;
176 // TS doesn't seem to like: let foo = bar || () => '';
177 this.context.rowClassCallback =
178 this.rowClassCallback || function () { return ''; };
179 this.context.cellClassCallback =
180 this.cellClassCallback || function() { return ''; };
186 this.context.initData();
190 this.context.destroy();
194 this.context.reload();
196 reloadWithoutPagerReset() {
197 this.context.reloadWithoutPagerReset();