UNPKG

8.36 kBPlain TextView Raw
1import {GridOptions} from "./entities/gridOptions";
2import {GridOptionsWrapper} from "./gridOptionsWrapper";
3import {ColumnApi} from "./columnController/columnApi";
4import {ColumnController} from "./columnController/columnController";
5import {RowRenderer} from "./rendering/rowRenderer";
6import {FilterManager} from "./filter/filterManager";
7import {EventService} from "./eventService";
8import {GridPanel} from "./gridPanel/gridPanel";
9import {Logger, LoggerFactory} from "./logger";
10import {PopupService} from "./widgets/popupService";
11import {Utils as _} from "./utils";
12import {Autowired, Bean, Context, Optional, PostConstruct, PreDestroy} from "./context/context";
13import {IRowModel} from "./interfaces/iRowModel";
14import {FocusedCellController} from "./focusedCellController";
15import {Component} from "./widgets/component";
16import {ICompFactory} from "./interfaces/iCompFactory";
17import {IFrameworkFactory} from "./interfaces/iFrameworkFactory";
18import {GridApi} from "./gridApi";
19import {IToolPanel} from "./interfaces/iToolPanel";
20import {RefSelector} from "./widgets/componentAnnotations";
21import {IStatusBar} from "./interfaces/iStatusBar";
22import {observeResize} from "./resizeObserver";
23import {BodyHeightChangedEvent, Events, GridSizeChangedEvent} from "./events";
24
25@Bean('gridCore')
26export class GridCore extends Component {
27
28 private static TEMPLATE_NORMAL =
29 `<div class="ag-root-wrapper">
30 <div class="ag-root-wrapper-body" ref="rootWrapperBody">
31 <ag-grid-comp ref="gridPanel"></ag-grid-comp>
32 </div>
33 <ag-pagination></ag-pagination>
34 </div>`;
35
36 private static TEMPLATE_ENTERPRISE =
37 `<div class="ag-root-wrapper">
38 <ag-header-column-drop></ag-header-column-drop>
39 <div ref="rootWrapperBody" class="ag-root-wrapper-body">
40 <ag-grid-comp ref="gridPanel"></ag-grid-comp>
41 <ag-tool-panel ref="toolPanel"></ag-tool-panel>
42 </div>
43 <ag-status-bar ref="statusBar"></ag-status-bar>
44 <ag-pagination></ag-pagination>
45 </div>`;
46
47 @Autowired('enterprise') private enterprise: boolean;
48 @Autowired('gridOptions') private gridOptions: GridOptions;
49 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
50 @Autowired('rowModel') private rowModel: IRowModel;
51 @Autowired('frameworkFactory') private frameworkFactory: IFrameworkFactory;
52
53 @Autowired('columnController') private columnController: ColumnController;
54 @Autowired('rowRenderer') private rowRenderer: RowRenderer;
55 @Autowired('filterManager') private filterManager: FilterManager;
56 @Autowired('eventService') private eventService: EventService;
57
58 @Autowired('eGridDiv') private eGridDiv: HTMLElement;
59 @Autowired('$scope') private $scope: any;
60 @Autowired('quickFilterOnScope') private quickFilterOnScope: string;
61 @Autowired('popupService') private popupService: PopupService;
62 @Autowired('focusedCellController') private focusedCellController: FocusedCellController;
63 @Autowired('context') private context: Context;
64 @Autowired('loggerFactory') loggerFactory: LoggerFactory;
65
66 @Autowired('columnApi') private columnApi: ColumnApi;
67 @Autowired('gridApi') private gridApi: GridApi;
68
69 @Optional('rowGroupCompFactory') private rowGroupCompFactory: ICompFactory;
70 @Optional('pivotCompFactory') private pivotCompFactory: ICompFactory;
71
72 @RefSelector('statusBar') private statusBar: IStatusBar;
73 @RefSelector('gridPanel') private gridPanel: GridPanel;
74 @RefSelector('toolPanel') private toolPanelComp: IToolPanel;
75 @RefSelector('rootWrapperBody') private eRootWrapperBody: HTMLElement;
76
77 private finished: boolean;
78 private doingVirtualPaging: boolean;
79
80 private logger: Logger;
81
82 constructor() {
83 super();
84 }
85
86 @PostConstruct
87 public init(): void {
88
89 this.logger = this.loggerFactory.create('GridCore');
90
91 let template = this.enterprise ? GridCore.TEMPLATE_ENTERPRISE : GridCore.TEMPLATE_NORMAL;
92 this.setTemplate(template);
93 this.instantiate(this.context);
94
95 if (this.enterprise) {
96 this.toolPanelComp.registerGridComp(this.gridPanel);
97 this.statusBar.registerGridPanel(this.gridPanel);
98 }
99
100 this.gridOptionsWrapper.addLayoutElement(this.getGui());
101
102 // see what the grid options are for default of toolbar
103 this.showToolPanel(this.gridOptionsWrapper.isShowToolPanel());
104
105 this.eGridDiv.appendChild(this.getGui());
106 this.addDestroyFunc( () => {
107 this.eGridDiv.removeChild(this.getGui());
108 });
109
110 // if using angular, watch for quickFilter changes
111 if (this.$scope) {
112 let quickFilterUnregisterFn = this.$scope.$watch(this.quickFilterOnScope, (newFilter: any) => this.filterManager.setQuickFilter(newFilter) );
113 this.addDestroyFunc(quickFilterUnregisterFn);
114 }
115
116 // important to set rtl before doLayout, as setting the RTL class impacts the scroll position,
117 // which doLayout indirectly depends on
118 this.addRtlSupport();
119
120 this.finished = false;
121 this.addDestroyFunc( () => this.finished = true );
122
123 this.logger.log('ready');
124
125 this.gridOptionsWrapper.addLayoutElement(this.eRootWrapperBody);
126
127 const unsubscribeFromResize = observeResize(this.eGridDiv, this.onGridSizeChanged.bind(this) );
128 this.addDestroyFunc(() => unsubscribeFromResize() );
129 }
130
131 private onGridSizeChanged(): void {
132 let event: GridSizeChangedEvent = {
133 type: Events.EVENT_GRID_SIZE_CHANGED,
134 api: this.gridApi,
135 columnApi: this.columnApi,
136 clientWidth: this.eGridDiv.clientWidth,
137 clientHeight: this.eGridDiv.clientHeight
138 };
139 this.eventService.dispatchEvent(event);
140 }
141
142 public getPreferredWidth(): number {
143 let widthForCols = this.columnController.getBodyContainerWidth()
144 + this.columnController.getPinnedLeftContainerWidth()
145 + this.columnController.getPinnedRightContainerWidth();
146 let widthForToolpanel = this.toolPanelComp ? this.toolPanelComp.getPreferredWidth() : 0;
147 return widthForCols + widthForToolpanel;
148 }
149
150 private addRtlSupport(): void {
151 let cssClass = this.gridOptionsWrapper.isEnableRtl() ? 'ag-rtl' : 'ag-ltr';
152 _.addCssClass(this.getGui(), cssClass);
153 }
154
155 public getRootGui(): HTMLElement {
156 return this.getGui();
157 }
158
159 public showToolPanel(show: any) {
160 if (!this.toolPanelComp) {
161 if (show) {
162 console.warn('ag-Grid: toolPanel is only available in ag-Grid Enterprise');
163 }
164 return;
165 }
166
167 this.toolPanelComp.showToolPanel(show);
168 }
169
170 public isToolPanelShowing() {
171 return this.toolPanelComp.isToolPanelShowing();
172 }
173
174 // need to override, as parent class isn't marked with PreDestroy
175 @PreDestroy
176 public destroy() {
177 super.destroy();
178 this.logger.log('Grid DOM removed');
179 }
180
181 // Valid values for position are bottom, middle and top
182 public ensureNodeVisible(comparator: any, position: string = 'top') {
183 if (this.doingVirtualPaging) {
184 throw 'Cannot use ensureNodeVisible when doing virtual paging, as we cannot check rows that are not in memory';
185 }
186 // look for the node index we want to display
187 let rowCount = this.rowModel.getPageLastRow() + 1;
188 let comparatorIsAFunction = typeof comparator === 'function';
189 let indexToSelect = -1;
190 // go through all the nodes, find the one we want to show
191 for (let i = 0; i < rowCount; i++) {
192 let node = this.rowModel.getRow(i);
193 if (comparatorIsAFunction) {
194 if (comparator(node)) {
195 indexToSelect = i;
196 break;
197 }
198 } else {
199 // check object equality against node and data
200 if (comparator === node || comparator === node.data) {
201 indexToSelect = i;
202 break;
203 }
204 }
205 }
206 if (indexToSelect >= 0) {
207 this.gridPanel.ensureIndexVisible(indexToSelect, position);
208 }
209 }
210}
211