UNPKG

15.4 kBJavaScriptView Raw
1// ag-grid-react v26.1.0
2"use strict";
3var __extends = (this && this.__extends) || (function () {
4 var extendStatics = function (d, b) {
5 extendStatics = Object.setPrototypeOf ||
6 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
8 return extendStatics(d, b);
9 };
10 return function (d, b) {
11 extendStatics(d, b);
12 function __() { this.constructor = d; }
13 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14 };
15})();
16var __assign = (this && this.__assign) || function () {
17 __assign = Object.assign || function(t) {
18 for (var s, i = 1, n = arguments.length; i < n; i++) {
19 s = arguments[i];
20 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21 t[p] = s[p];
22 }
23 return t;
24 };
25 return __assign.apply(this, arguments);
26};
27var __spreadArrays = (this && this.__spreadArrays) || function () {
28 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
29 for (var r = Array(s), k = 0, i = 0; i < il; i++)
30 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
31 r[k] = a[j];
32 return r;
33};
34var __importStar = (this && this.__importStar) || function (mod) {
35 if (mod && mod.__esModule) return mod;
36 var result = {};
37 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
38 result["default"] = mod;
39 return result;
40};
41var __importDefault = (this && this.__importDefault) || function (mod) {
42 return (mod && mod.__esModule) ? mod : { "default": mod };
43};
44Object.defineProperty(exports, "__esModule", { value: true });
45var react_1 = __importStar(require("react"));
46var prop_types_1 = __importDefault(require("prop-types"));
47var ag_grid_community_1 = require("ag-grid-community");
48var agGridColumn_1 = require("./agGridColumn");
49var changeDetectionService_1 = require("./changeDetectionService");
50var legacyReactComponent_1 = require("./legacyReactComponent");
51var newReactComponent_1 = require("./newReactComponent");
52var AgGridReactLegacy = /** @class */ (function (_super) {
53 __extends(AgGridReactLegacy, _super);
54 function AgGridReactLegacy(props) {
55 var _this = _super.call(this, props) || this;
56 _this.props = props;
57 _this.changeDetectionService = new changeDetectionService_1.ChangeDetectionService();
58 _this.api = null;
59 _this.portals = [];
60 _this.hasPendingPortalUpdate = false;
61 _this.destroyed = false;
62 _this.SYNCHRONOUS_CHANGE_PROPERTIES = ['context'];
63 return _this;
64 }
65 AgGridReactLegacy.prototype.render = function () {
66 var _this = this;
67 return react_1.default.createElement('div', {
68 style: this.createStyleForDiv(),
69 className: this.props.className,
70 ref: function (e) {
71 _this.eGridDiv = e;
72 }
73 }, this.portals);
74 };
75 AgGridReactLegacy.prototype.createStyleForDiv = function () {
76 return __assign({ height: '100%' }, (this.props.containerStyle || {}));
77 };
78 AgGridReactLegacy.prototype.componentDidMount = function () {
79 var modules = this.props.modules || [];
80 var gridParams = {
81 providedBeanInstances: {
82 agGridReact: this,
83 frameworkComponentWrapper: new ReactFrameworkComponentWrapper(this)
84 },
85 modules: modules
86 };
87 var gridOptions = this.props.gridOptions || {};
88 var children = this.props.children;
89 if (agGridColumn_1.AgGridColumn.hasChildColumns(children)) {
90 gridOptions.columnDefs = agGridColumn_1.AgGridColumn.mapChildColumnDefs(children);
91 }
92 this.gridOptions = ag_grid_community_1.ComponentUtil.copyAttributesToGridOptions(gridOptions, this.props);
93 // don't need the return value
94 new ag_grid_community_1.Grid(this.eGridDiv, this.gridOptions, gridParams);
95 this.api = this.gridOptions.api;
96 this.columnApi = this.gridOptions.columnApi;
97 this.props.setGridApi(this.api, this.columnApi);
98 };
99 AgGridReactLegacy.prototype.waitForInstance = function (reactComponent, resolve, startTime) {
100 var _this = this;
101 if (startTime === void 0) { startTime = Date.now(); }
102 // if the grid has been destroyed in the meantime just resolve
103 if (this.destroyed) {
104 resolve(null);
105 return;
106 }
107 if (reactComponent.rendered()) {
108 resolve(reactComponent);
109 }
110 else {
111 if (Date.now() - startTime >= this.props.maxComponentCreationTimeMs && !this.hasPendingPortalUpdate) {
112 // last check - we check if this is a null value being rendered - we do this last as using SSR to check the value
113 // can mess up contexts
114 if (reactComponent.isNullValue()) {
115 resolve(reactComponent);
116 return;
117 }
118 console.error("AG Grid: React Component '" + reactComponent.getReactComponentName() + "' not created within " + AgGridReactLegacy.MAX_COMPONENT_CREATION_TIME_IN_MS + "ms");
119 return;
120 }
121 window.setTimeout(function () {
122 _this.waitForInstance(reactComponent, resolve, startTime);
123 });
124 }
125 };
126 /**
127 * Mounts a react portal for components registered under the componentFramework.
128 * We do this because we want all portals to be in the same tree - in order to get
129 * Context to work properly.
130 */
131 AgGridReactLegacy.prototype.mountReactPortal = function (portal, reactComponent, resolve) {
132 this.portals = __spreadArrays(this.portals, [portal]);
133 this.waitForInstance(reactComponent, resolve);
134 this.batchUpdate();
135 };
136 AgGridReactLegacy.prototype.updateReactPortal = function (oldPortal, newPortal) {
137 this.portals[this.portals.indexOf(oldPortal)] = newPortal;
138 this.batchUpdate();
139 };
140 AgGridReactLegacy.prototype.batchUpdate = function () {
141 var _this = this;
142 if (this.hasPendingPortalUpdate) {
143 return;
144 }
145 setTimeout(function () {
146 if (_this.api) { // destroyed?
147 _this.forceUpdate(function () {
148 _this.hasPendingPortalUpdate = false;
149 });
150 }
151 });
152 this.hasPendingPortalUpdate = true;
153 };
154 AgGridReactLegacy.prototype.destroyPortal = function (portal) {
155 this.portals = this.portals.filter(function (curPortal) { return curPortal !== portal; });
156 this.batchUpdate();
157 };
158 AgGridReactLegacy.prototype.getStrategyTypeForProp = function (propKey) {
159 if (propKey === 'rowData') {
160 if (this.props.rowDataChangeDetectionStrategy) {
161 return this.props.rowDataChangeDetectionStrategy;
162 }
163 else if (this.isImmutableDataActive()) {
164 return changeDetectionService_1.ChangeDetectionStrategyType.IdentityCheck;
165 }
166 }
167 // all other cases will default to DeepValueCheck
168 return changeDetectionService_1.ChangeDetectionStrategyType.DeepValueCheck;
169 };
170 AgGridReactLegacy.prototype.isImmutableDataActive = function () {
171 return (this.props.deltaRowDataMode || this.props.immutableData) ||
172 (this.props.gridOptions && (this.props.gridOptions.deltaRowDataMode || this.props.gridOptions.immutableData));
173 };
174 AgGridReactLegacy.prototype.shouldComponentUpdate = function (nextProps) {
175 this.processPropsChanges(this.props, nextProps);
176 // we want full control of the dom, as AG Grid doesn't use React internally,
177 // so for performance reasons we tell React we don't need render called after
178 // property changes.
179 return false;
180 };
181 AgGridReactLegacy.prototype.componentDidUpdate = function (prevProps) {
182 this.processPropsChanges(prevProps, this.props);
183 };
184 AgGridReactLegacy.prototype.processPropsChanges = function (prevProps, nextProps) {
185 var changes = {};
186 this.extractGridPropertyChanges(prevProps, nextProps, changes);
187 this.extractDeclarativeColDefChanges(nextProps, changes);
188 this.processSynchronousChanges(changes);
189 this.processAsynchronousChanges(changes);
190 };
191 AgGridReactLegacy.prototype.extractDeclarativeColDefChanges = function (nextProps, changes) {
192 // if columnDefs are provided on gridOptions we use those - you can't combine both
193 // we also skip if columnDefs are provided as a prop directly on AgGridReact
194 if ((this.props.gridOptions && this.props.gridOptions.columnDefs) || this.props.columnDefs) {
195 return;
196 }
197 var debugLogging = !!nextProps.debug;
198 var propKey = 'columnDefs';
199 var currentColDefs = this.gridOptions.columnDefs;
200 if (agGridColumn_1.AgGridColumn.hasChildColumns(nextProps.children)) {
201 var detectionStrategy = this.changeDetectionService.getStrategy(this.getStrategyTypeForProp(propKey));
202 var newColDefs = agGridColumn_1.AgGridColumn.mapChildColumnDefs(nextProps.children);
203 if (!detectionStrategy.areEqual(currentColDefs, newColDefs)) {
204 if (debugLogging) {
205 console.log("agGridReact: colDefs definitions changed");
206 }
207 changes[propKey] =
208 {
209 previousValue: currentColDefs,
210 currentValue: newColDefs
211 };
212 }
213 }
214 else if (currentColDefs && currentColDefs.length > 0) {
215 changes[propKey] =
216 {
217 previousValue: currentColDefs,
218 currentValue: []
219 };
220 }
221 };
222 AgGridReactLegacy.prototype.extractGridPropertyChanges = function (prevProps, nextProps, changes) {
223 var _this = this;
224 var debugLogging = !!nextProps.debug;
225 Object.keys(nextProps).forEach(function (propKey) {
226 if (ag_grid_community_1._.includes(ag_grid_community_1.ComponentUtil.ALL_PROPERTIES, propKey)) {
227 var changeDetectionStrategy = _this.changeDetectionService.getStrategy(_this.getStrategyTypeForProp(propKey));
228 if (!changeDetectionStrategy.areEqual(prevProps[propKey], nextProps[propKey])) {
229 if (debugLogging) {
230 console.log("agGridReact: [" + propKey + "] property changed");
231 }
232 changes[propKey] = {
233 previousValue: prevProps[propKey],
234 currentValue: nextProps[propKey]
235 };
236 }
237 }
238 });
239 ag_grid_community_1.ComponentUtil.getEventCallbacks().forEach(function (funcName) {
240 var propsAny = _this.props;
241 if (propsAny[funcName] !== nextProps[funcName]) {
242 if (debugLogging) {
243 console.log("agGridReact: [" + funcName + "] event callback changed");
244 }
245 changes[funcName] = {
246 previousValue: prevProps[funcName],
247 currentValue: nextProps[funcName]
248 };
249 }
250 });
251 };
252 AgGridReactLegacy.prototype.componentWillUnmount = function () {
253 if (this.api) {
254 this.api.destroy();
255 this.api = null;
256 }
257 this.destroyed = true;
258 };
259 AgGridReactLegacy.prototype.isDisableStaticMarkup = function () {
260 return this.props.disableStaticMarkup === true;
261 };
262 AgGridReactLegacy.prototype.isLegacyComponentRendering = function () {
263 return this.props.legacyComponentRendering === true;
264 };
265 AgGridReactLegacy.prototype.processSynchronousChanges = function (changes) {
266 var asyncChanges = __assign({}, changes);
267 if (Object.keys(asyncChanges).length > 0) {
268 var synchronousChanges_1 = {};
269 this.SYNCHRONOUS_CHANGE_PROPERTIES.forEach(function (synchronousChangeProperty) {
270 if (asyncChanges[synchronousChangeProperty]) {
271 synchronousChanges_1[synchronousChangeProperty] = asyncChanges[synchronousChangeProperty];
272 delete asyncChanges[synchronousChangeProperty];
273 }
274 });
275 if (Object.keys(synchronousChanges_1).length > 0 && !!this.api) {
276 ag_grid_community_1.ComponentUtil.processOnChange(synchronousChanges_1, this.gridOptions, this.api, this.columnApi);
277 }
278 }
279 return asyncChanges;
280 };
281 AgGridReactLegacy.prototype.processAsynchronousChanges = function (changes) {
282 var _this = this;
283 if (Object.keys(changes).length > 0) {
284 window.setTimeout(function () {
285 // destroyed?
286 if (_this.api) {
287 ag_grid_community_1.ComponentUtil.processOnChange(changes, _this.gridOptions, _this.api, _this.columnApi);
288 }
289 });
290 }
291 };
292 AgGridReactLegacy.MAX_COMPONENT_CREATION_TIME_IN_MS = 1000; // a second should be more than enough to instantiate a component
293 AgGridReactLegacy.defaultProps = {
294 legacyComponentRendering: false,
295 disableStaticMarkup: false,
296 maxComponentCreationTimeMs: AgGridReactLegacy.MAX_COMPONENT_CREATION_TIME_IN_MS
297 };
298 return AgGridReactLegacy;
299}(react_1.Component));
300exports.AgGridReactLegacy = AgGridReactLegacy;
301AgGridReactLegacy.propTypes = {
302 gridOptions: prop_types_1.default.object
303};
304addProperties(ag_grid_community_1.ComponentUtil.getEventCallbacks(), prop_types_1.default.func);
305addProperties(ag_grid_community_1.ComponentUtil.BOOLEAN_PROPERTIES, prop_types_1.default.bool);
306addProperties(ag_grid_community_1.ComponentUtil.STRING_PROPERTIES, prop_types_1.default.string);
307addProperties(ag_grid_community_1.ComponentUtil.OBJECT_PROPERTIES, prop_types_1.default.object);
308addProperties(ag_grid_community_1.ComponentUtil.ARRAY_PROPERTIES, prop_types_1.default.array);
309addProperties(ag_grid_community_1.ComponentUtil.NUMBER_PROPERTIES, prop_types_1.default.number);
310addProperties(ag_grid_community_1.ComponentUtil.FUNCTION_PROPERTIES, prop_types_1.default.func);
311function addProperties(listOfProps, propType) {
312 listOfProps.forEach(function (propKey) {
313 AgGridReactLegacy[propKey] = propType;
314 });
315}
316var ReactFrameworkComponentWrapper = /** @class */ (function (_super) {
317 __extends(ReactFrameworkComponentWrapper, _super);
318 function ReactFrameworkComponentWrapper(agGridReact) {
319 var _this = _super.call(this) || this;
320 _this.agGridReact = agGridReact;
321 return _this;
322 }
323 ReactFrameworkComponentWrapper.prototype.createWrapper = function (UserReactComponent, componentType) {
324 return this.agGridReact.isLegacyComponentRendering() ?
325 new legacyReactComponent_1.LegacyReactComponent(UserReactComponent, this.agGridReact, componentType) :
326 new newReactComponent_1.NewReactComponent(UserReactComponent, this.agGridReact, componentType);
327 };
328 return ReactFrameworkComponentWrapper;
329}(ag_grid_community_1.BaseComponentWrapper));