UNPKG

12.9 kBPlain TextView Raw
1import {GridOptions} from "./entities/gridOptions";
2import {GridOptionsWrapper} from "./gridOptionsWrapper";
3import {SelectionController} from "./selectionController";
4import {ColumnApi} from "./columnController/columnApi";
5import {ColumnController} from "./columnController/columnController";
6import {RowRenderer} from "./rendering/rowRenderer";
7import {HeaderRootComp} from "./headerRendering/headerRootComp";
8import {FilterManager} from "./filter/filterManager";
9import {ValueService} from "./valueService/valueService";
10import {EventService} from "./eventService";
11import {GridPanel} from "./gridPanel/gridPanel";
12import {GridApi} from "./gridApi";
13import {BalancedColumnTreeBuilder} from "./columnController/balancedColumnTreeBuilder";
14import {DisplayedGroupCreator} from "./columnController/displayedGroupCreator";
15import {ExpressionService} from "./valueService/expressionService";
16import {TemplateService} from "./templateService";
17import {PopupService} from "./widgets/popupService";
18import {Logger, LoggerFactory} from "./logger";
19import {ColumnUtils} from "./columnController/columnUtils";
20import {AutoWidthCalculator} from "./rendering/autoWidthCalculator";
21import {HorizontalResizeService} from "./headerRendering/horizontalResizeService";
22import {Context} from "./context/context";
23import {CsvCreator} from "./csvCreator";
24import {GridCore} from "./gridCore";
25import {StandardMenuFactory} from "./headerRendering/standardMenu";
26import {DragAndDropService} from "./dragAndDrop/dragAndDropService";
27import {DragService} from "./dragAndDrop/dragService";
28import {SortController} from "./sortController";
29import {FocusedCellController} from "./focusedCellController";
30import {MouseEventService} from "./gridPanel/mouseEventService";
31import {CellNavigationService} from "./cellNavigationService";
32import {Utils as _} from "./utils";
33import {FilterStage} from "./rowModels/clientSide/filterStage";
34import {SortStage} from "./rowModels/clientSide/sortStage";
35import {FlattenStage} from "./rowModels/clientSide/flattenStage";
36import {CellEditorFactory} from "./rendering/cellEditorFactory";
37import {Events, GridReadyEvent} from "./events";
38import {InfiniteRowModel} from "./rowModels/infinite/infiniteRowModel";
39import {ClientSideRowModel} from "./rowModels/clientSide/clientSideRowModel";
40import {CellRendererFactory} from "./rendering/cellRendererFactory";
41import {CellRendererService} from "./rendering/cellRendererService";
42import {ValueFormatterService} from "./rendering/valueFormatterService";
43import {AgCheckbox} from "./widgets/agCheckbox";
44import {BaseFrameworkFactory} from "./baseFrameworkFactory";
45import {IFrameworkFactory} from "./interfaces/iFrameworkFactory";
46import {ScrollVisibleService} from "./gridPanel/scrollVisibleService";
47import {Downloader} from "./downloader";
48import {XmlFactory} from "./xmlFactory";
49import {GridSerializer} from "./gridSerializer";
50import {StylingService} from "./styling/stylingService";
51import {ColumnHoverService} from "./rendering/columnHoverService";
52import {ColumnAnimationService} from "./rendering/columnAnimationService";
53import {SortService} from "./rowNodes/sortService";
54import {FilterService} from "./rowNodes/filterService";
55import {AutoGroupColService} from "./columnController/autoGroupColService";
56import {PaginationAutoPageSizeService, PaginationProxy} from "./rowModels/paginationProxy";
57import {ImmutableService} from "./rowModels/clientSide/immutableService";
58import {IRowModel} from "./interfaces/iRowModel";
59import {Constants} from "./constants";
60import {ValueCache} from "./valueService/valueCache";
61import {ChangeDetectionService} from "./valueService/changeDetectionService";
62import {AlignedGridsService} from "./alignedGridsService";
63import {PinnedRowModel} from "./rowModels/pinnedRowModel";
64import {ComponentResolver} from "./components/framework/componentResolver";
65import {ComponentRecipes} from "./components/framework/componentRecipes";
66import {ComponentProvider} from "./components/framework/componentProvider";
67import {AgComponentUtils} from "./components/framework/agComponentUtils";
68import {ComponentMetadataProvider} from "./components/framework/componentMetadataProvider";
69import {Beans} from "./rendering/beans";
70import {Environment} from "./environment";
71import {AnimationFrameService} from "./misc/animationFrameService";
72import {NavigationService} from "./gridPanel/navigationService";
73import {HeightScaler} from "./rendering/heightScaler";
74import {SelectableService} from "./rowNodes/selectableService";
75import {AutoHeightCalculator} from "./rendering/autoHeightCalculator";
76import {PaginationComp} from "./rowModels/pagination/paginationComp";
77
78export interface GridParams {
79 // used by Web Components
80 globalEventListener?: Function;
81
82 // these are used by ng1 only
83 $scope?: any;
84 $compile?: any;
85 quickFilterOnScope?: any;
86
87 // this allows the base frameworks (React, NG2, etc) to provide alternative cellRenderers and cellEditors
88 frameworkFactory?: IFrameworkFactory;
89
90 //bean instances to add to the context
91 seedBeanInstances?: {[key:string]:any}
92}
93
94export class Grid {
95
96 private context: Context;
97
98 private static enterpriseBeans: any[];
99 private static frameworkBeans: any[];
100 private static enterpriseComponents: any[];
101
102 // the default is ClientSideRowModel, which is also used for pagination.
103 // the enterprise adds viewport to this list.
104 private static RowModelClasses: any = {
105 infinite: InfiniteRowModel,
106 clientSide: ClientSideRowModel
107 };
108
109 public static setEnterpriseBeans(enterpriseBeans: any[], rowModelClasses: any): void {
110 this.enterpriseBeans = enterpriseBeans;
111
112 // the enterprise can inject additional row models. this is how it injects the viewportRowModel
113 _.iterateObject(rowModelClasses, (key: string, value: any)=> Grid.RowModelClasses[key] = value );
114 }
115
116 public static setEnterpriseComponents(components: any[]): void {
117 this.enterpriseComponents = components;
118 }
119
120 public static setFrameworkBeans(frameworkBeans: any[]): void {
121 this.frameworkBeans = frameworkBeans;
122 }
123
124 constructor(eGridDiv: HTMLElement, gridOptions: GridOptions, params?: GridParams) {
125
126 if (!eGridDiv) {
127 console.error('ag-Grid: no div element provided to the grid');
128 }
129 if (!gridOptions) {
130 console.error('ag-Grid: no gridOptions provided to the grid');
131 }
132
133 let rowModelClass = this.getRowModelClass(gridOptions);
134
135 let enterprise = _.exists(Grid.enterpriseBeans);
136
137 let frameworkFactory = params ? params.frameworkFactory : null;
138 if (_.missing(frameworkFactory)) {
139 frameworkFactory = new BaseFrameworkFactory();
140 }
141
142 let overrideBeans:any[]= [];
143
144 if (Grid.enterpriseBeans){
145 overrideBeans = overrideBeans.concat(Grid.enterpriseBeans);
146 }
147
148 if (Grid.frameworkBeans){
149 overrideBeans = overrideBeans.concat(Grid.frameworkBeans);
150 }
151
152 let seed = {
153 enterprise: enterprise,
154 gridOptions: gridOptions,
155 eGridDiv: eGridDiv,
156 $scope: params ? params.$scope : null,
157 $compile: params ? params.$compile : null,
158 quickFilterOnScope: params ? params.quickFilterOnScope : null,
159 globalEventListener: params ? params.globalEventListener : null,
160 frameworkFactory: frameworkFactory
161 };
162 if (params && params.seedBeanInstances) {
163 _.assign(seed, params.seedBeanInstances);
164 }
165
166 let components = [
167 {componentName: 'AgCheckbox', theClass: AgCheckbox},
168 {componentName: 'AgGridComp', theClass: GridPanel},
169 {componentName: 'AgHeaderRoot', theClass: HeaderRootComp},
170 {componentName: 'AgPagination', theClass: PaginationComp},
171 // niall put the below in for testing some PoC code, niall will
172 // remove this comment and code when PoC is over
173 ];
174
175 if (Grid.enterpriseComponents) {
176 components = components.concat(Grid.enterpriseComponents);
177 }
178
179 let contextParams = {
180 overrideBeans: overrideBeans,
181 seed: seed,
182 //Careful with the order of the beans here, there are dependencies between them that need to be kept
183 beans: [rowModelClass, Beans, PaginationAutoPageSizeService, GridApi, ComponentProvider, AgComponentUtils, ComponentMetadataProvider,
184 ComponentProvider, ComponentResolver, ComponentRecipes, HeightScaler, AutoHeightCalculator,
185 CellRendererFactory, HorizontalResizeService, PinnedRowModel, DragService,
186 DisplayedGroupCreator, EventService, GridOptionsWrapper, SelectionController,
187 FilterManager, ColumnController, PaginationProxy, RowRenderer, ExpressionService,
188 BalancedColumnTreeBuilder, CsvCreator, Downloader, XmlFactory, GridSerializer, TemplateService,
189 NavigationService, PopupService, ValueCache, ValueService, AlignedGridsService,
190 LoggerFactory, ColumnUtils, AutoWidthCalculator, PopupService, GridCore, StandardMenuFactory,
191 DragAndDropService, ColumnApi, FocusedCellController, MouseEventService,
192 CellNavigationService, FilterStage, SortStage, FlattenStage, FilterService,
193 CellEditorFactory, CellRendererService, ValueFormatterService, StylingService, ScrollVisibleService,
194 ColumnHoverService, ColumnAnimationService, SortService, SelectableService, AutoGroupColService,
195 ImmutableService, ChangeDetectionService, Environment, AnimationFrameService, SortController],
196 components: components,
197 debug: !!gridOptions.debug
198 };
199
200 let isLoggingFunc = ()=> contextParams.debug;
201 this.context = new Context(contextParams, new Logger('Context', isLoggingFunc));
202
203 this.setColumnsAndData();
204
205 this.dispatchGridReadyEvent(gridOptions);
206
207 if (gridOptions.debug) {
208 console.log('ag-Grid -> initialised successfully, enterprise = ' + enterprise);
209 }
210 }
211
212 private setColumnsAndData(): void {
213
214 let gridOptionsWrapper: GridOptionsWrapper = this.context.getBean('gridOptionsWrapper');
215 let columnController: ColumnController = this.context.getBean('columnController');
216 let rowModel: IRowModel = this.context.getBean('rowModel');
217
218 let columnDefs = gridOptionsWrapper.getColumnDefs();
219 let rowData = gridOptionsWrapper.getRowData();
220
221 let nothingToSet = _.missing(columnDefs) && _.missing(rowData);
222 if (nothingToSet) { return; }
223
224 if (_.exists(columnDefs)) {
225 columnController.setColumnDefs(columnDefs, "gridInitializing");
226 }
227
228 if (_.exists(rowData) && rowModel.getType()===Constants.ROW_MODEL_TYPE_CLIENT_SIDE) {
229 let clientSideRowModel = <ClientSideRowModel> rowModel;
230 clientSideRowModel.setRowData(rowData);
231 }
232 }
233
234 private dispatchGridReadyEvent(gridOptions: GridOptions): void {
235 let eventService: EventService = this.context.getBean('eventService');
236 let readyEvent: GridReadyEvent = {
237 type: Events.EVENT_GRID_READY,
238 api: gridOptions.api,
239 columnApi: gridOptions.columnApi
240 };
241 eventService.dispatchEvent(readyEvent);
242 }
243
244 private getRowModelClass(gridOptions: GridOptions): any {
245 let rowModelType = gridOptions.rowModelType;
246
247 //TODO: temporary measure before 'enterprise' is completely removed (similar handling in gridOptionsWrapper is also required)
248 rowModelType = rowModelType === 'enterprise' ? Constants.ROW_MODEL_TYPE_SERVER_SIDE : rowModelType;
249
250 if (_.exists(rowModelType)) {
251 let rowModelClass = Grid.RowModelClasses[rowModelType];
252 if (_.exists(rowModelClass)) {
253 return rowModelClass;
254 } else {
255 if (rowModelType==='normal') {
256 console.warn(`ag-Grid: normal rowModel deprecated. Should now be called client side row model instead.`);
257 return ClientSideRowModel;
258 }
259 console.error('ag-Grid: could not find matching row model for rowModelType ' + rowModelType);
260 if (rowModelType===Constants.ROW_MODEL_TYPE_VIEWPORT) {
261 console.error('ag-Grid: rowModelType viewport is only available in ag-Grid Enterprise');
262 }
263 if (rowModelType===Constants.ROW_MODEL_TYPE_SERVER_SIDE) {
264 console.error('ag-Grid: rowModelType server side is only available in ag-Grid Enterprise');
265 }
266 }
267 }
268 return ClientSideRowModel;
269 };
270
271 public destroy(): void {
272 this.context.destroy();
273 }
274
275}
\No newline at end of file