UNPKG

12.9 kBJavaScriptView Raw
1// ag-grid-react v26.2.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 __importDefault = (this && this.__importDefault) || function (mod) {
28 return (mod && mod.__esModule) ? mod : { "default": mod };
29};
30var __importStar = (this && this.__importStar) || function (mod) {
31 if (mod && mod.__esModule) return mod;
32 var result = {};
33 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
34 result["default"] = mod;
35 return result;
36};
37Object.defineProperty(exports, "__esModule", { value: true });
38var ag_grid_community_1 = require("ag-grid-community");
39var prop_types_1 = __importDefault(require("prop-types"));
40var react_1 = __importStar(require("react"));
41var legacyReactComponent_1 = require("./legacyReactComponent");
42var agGridColumn_1 = require("../shared/agGridColumn");
43var changeDetectionService_1 = require("../shared/changeDetectionService");
44var newReactComponent_1 = require("../shared/newReactComponent");
45var portalManager_1 = require("../shared/portalManager");
46var AgGridReactLegacy = /** @class */ (function (_super) {
47 __extends(AgGridReactLegacy, _super);
48 function AgGridReactLegacy(props) {
49 var _this = _super.call(this, props) || this;
50 _this.props = props;
51 _this.changeDetectionService = new changeDetectionService_1.ChangeDetectionService();
52 _this.api = null;
53 _this.destroyed = false;
54 _this.SYNCHRONOUS_CHANGE_PROPERTIES = ['context'];
55 _this.portalManager = new portalManager_1.PortalManager(_this, props.componentWrappingElement, props.maxComponentCreationTimeMs);
56 return _this;
57 }
58 AgGridReactLegacy.prototype.render = function () {
59 var _this = this;
60 return react_1.default.createElement('div', {
61 style: this.createStyleForDiv(),
62 className: this.props.className,
63 ref: function (e) {
64 _this.eGridDiv = e;
65 }
66 }, this.portalManager.getPortals());
67 };
68 AgGridReactLegacy.prototype.createStyleForDiv = function () {
69 return __assign({ height: '100%' }, (this.props.containerStyle || {}));
70 };
71 AgGridReactLegacy.prototype.componentDidMount = function () {
72 var modules = this.props.modules || [];
73 var gridParams = {
74 providedBeanInstances: {
75 agGridReact: this,
76 frameworkComponentWrapper: new ReactFrameworkComponentWrapper(this, this.portalManager)
77 },
78 modules: modules
79 };
80 var gridOptions = this.props.gridOptions || {};
81 var children = this.props.children;
82 if (agGridColumn_1.AgGridColumn.hasChildColumns(children)) {
83 gridOptions.columnDefs = agGridColumn_1.AgGridColumn.mapChildColumnDefs(children);
84 }
85 this.gridOptions = ag_grid_community_1.ComponentUtil.copyAttributesToGridOptions(gridOptions, this.props);
86 // don't need the return value
87 new ag_grid_community_1.Grid(this.eGridDiv, this.gridOptions, gridParams);
88 this.api = this.gridOptions.api;
89 this.columnApi = this.gridOptions.columnApi;
90 this.props.setGridApi(this.api, this.columnApi);
91 };
92 AgGridReactLegacy.prototype.getStrategyTypeForProp = function (propKey) {
93 if (propKey === 'rowData') {
94 if (this.props.rowDataChangeDetectionStrategy) {
95 return this.props.rowDataChangeDetectionStrategy;
96 }
97 else if (this.isImmutableDataActive()) {
98 return changeDetectionService_1.ChangeDetectionStrategyType.IdentityCheck;
99 }
100 }
101 // all other cases will default to DeepValueCheck
102 return changeDetectionService_1.ChangeDetectionStrategyType.DeepValueCheck;
103 };
104 AgGridReactLegacy.prototype.isImmutableDataActive = function () {
105 return (this.props.deltaRowDataMode || this.props.immutableData) ||
106 (this.props.gridOptions && (this.props.gridOptions.deltaRowDataMode || this.props.gridOptions.immutableData));
107 };
108 AgGridReactLegacy.prototype.shouldComponentUpdate = function (nextProps) {
109 this.processPropsChanges(this.props, nextProps);
110 // we want full control of the dom, as AG Grid doesn't use React internally,
111 // so for performance reasons we tell React we don't need render called after
112 // property changes.
113 return false;
114 };
115 AgGridReactLegacy.prototype.componentDidUpdate = function (prevProps) {
116 this.processPropsChanges(prevProps, this.props);
117 };
118 AgGridReactLegacy.prototype.processPropsChanges = function (prevProps, nextProps) {
119 var changes = {};
120 this.extractGridPropertyChanges(prevProps, nextProps, changes);
121 this.extractDeclarativeColDefChanges(nextProps, changes);
122 this.processSynchronousChanges(changes);
123 this.processAsynchronousChanges(changes);
124 };
125 AgGridReactLegacy.prototype.extractDeclarativeColDefChanges = function (nextProps, changes) {
126 // if columnDefs are provided on gridOptions we use those - you can't combine both
127 // we also skip if columnDefs are provided as a prop directly on AgGridReact
128 if ((this.props.gridOptions && this.props.gridOptions.columnDefs) || this.props.columnDefs) {
129 return;
130 }
131 var debugLogging = !!nextProps.debug;
132 var propKey = 'columnDefs';
133 var currentColDefs = this.gridOptions.columnDefs;
134 if (agGridColumn_1.AgGridColumn.hasChildColumns(nextProps.children)) {
135 var detectionStrategy = this.changeDetectionService.getStrategy(this.getStrategyTypeForProp(propKey));
136 var newColDefs = agGridColumn_1.AgGridColumn.mapChildColumnDefs(nextProps.children);
137 if (!detectionStrategy.areEqual(currentColDefs, newColDefs)) {
138 if (debugLogging) {
139 console.log("agGridReact: colDefs definitions changed");
140 }
141 changes[propKey] =
142 {
143 previousValue: currentColDefs,
144 currentValue: newColDefs
145 };
146 }
147 }
148 else if (currentColDefs && currentColDefs.length > 0) {
149 changes[propKey] =
150 {
151 previousValue: currentColDefs,
152 currentValue: []
153 };
154 }
155 };
156 AgGridReactLegacy.prototype.extractGridPropertyChanges = function (prevProps, nextProps, changes) {
157 var _this = this;
158 var debugLogging = !!nextProps.debug;
159 Object.keys(nextProps).forEach(function (propKey) {
160 if (ag_grid_community_1._.includes(ag_grid_community_1.ComponentUtil.ALL_PROPERTIES, propKey)) {
161 var changeDetectionStrategy = _this.changeDetectionService.getStrategy(_this.getStrategyTypeForProp(propKey));
162 if (!changeDetectionStrategy.areEqual(prevProps[propKey], nextProps[propKey])) {
163 if (debugLogging) {
164 console.log("agGridReact: [" + propKey + "] property changed");
165 }
166 changes[propKey] = {
167 previousValue: prevProps[propKey],
168 currentValue: nextProps[propKey]
169 };
170 }
171 }
172 });
173 ag_grid_community_1.ComponentUtil.getEventCallbacks().forEach(function (funcName) {
174 if (prevProps[funcName] !== nextProps[funcName]) {
175 if (debugLogging) {
176 console.log("agGridReact: [" + funcName + "] event callback changed");
177 }
178 changes[funcName] = {
179 previousValue: prevProps[funcName],
180 currentValue: nextProps[funcName]
181 };
182 }
183 });
184 };
185 AgGridReactLegacy.prototype.componentWillUnmount = function () {
186 if (this.api) {
187 this.api.destroy();
188 this.api = null;
189 }
190 this.destroyed = true;
191 this.portalManager.destroy();
192 };
193 AgGridReactLegacy.prototype.isDisableStaticMarkup = function () {
194 return this.props.disableStaticMarkup === true;
195 };
196 AgGridReactLegacy.prototype.isLegacyComponentRendering = function () {
197 return this.props.legacyComponentRendering === true;
198 };
199 AgGridReactLegacy.prototype.processSynchronousChanges = function (changes) {
200 var asyncChanges = __assign({}, changes);
201 if (Object.keys(asyncChanges).length > 0) {
202 var synchronousChanges_1 = {};
203 this.SYNCHRONOUS_CHANGE_PROPERTIES.forEach(function (synchronousChangeProperty) {
204 if (asyncChanges[synchronousChangeProperty]) {
205 synchronousChanges_1[synchronousChangeProperty] = asyncChanges[synchronousChangeProperty];
206 delete asyncChanges[synchronousChangeProperty];
207 }
208 });
209 if (Object.keys(synchronousChanges_1).length > 0 && !!this.api) {
210 ag_grid_community_1.ComponentUtil.processOnChange(synchronousChanges_1, this.gridOptions, this.api, this.columnApi);
211 }
212 }
213 return asyncChanges;
214 };
215 AgGridReactLegacy.prototype.processAsynchronousChanges = function (changes) {
216 var _this = this;
217 if (Object.keys(changes).length > 0) {
218 window.setTimeout(function () {
219 // destroyed?
220 if (_this.api) {
221 ag_grid_community_1.ComponentUtil.processOnChange(changes, _this.gridOptions, _this.api, _this.columnApi);
222 }
223 });
224 }
225 };
226 AgGridReactLegacy.MAX_COMPONENT_CREATION_TIME_IN_MS = 1000; // a second should be more than enough to instantiate a component
227 AgGridReactLegacy.defaultProps = {
228 legacyComponentRendering: false,
229 disableStaticMarkup: false,
230 maxComponentCreationTimeMs: AgGridReactLegacy.MAX_COMPONENT_CREATION_TIME_IN_MS
231 };
232 return AgGridReactLegacy;
233}(react_1.Component));
234exports.AgGridReactLegacy = AgGridReactLegacy;
235AgGridReactLegacy.propTypes = {
236 gridOptions: prop_types_1.default.object
237};
238addProperties(ag_grid_community_1.ComponentUtil.getEventCallbacks(), prop_types_1.default.func);
239addProperties(ag_grid_community_1.ComponentUtil.BOOLEAN_PROPERTIES, prop_types_1.default.bool);
240addProperties(ag_grid_community_1.ComponentUtil.STRING_PROPERTIES, prop_types_1.default.string);
241addProperties(ag_grid_community_1.ComponentUtil.OBJECT_PROPERTIES, prop_types_1.default.object);
242addProperties(ag_grid_community_1.ComponentUtil.ARRAY_PROPERTIES, prop_types_1.default.array);
243addProperties(ag_grid_community_1.ComponentUtil.NUMBER_PROPERTIES, prop_types_1.default.number);
244addProperties(ag_grid_community_1.ComponentUtil.FUNCTION_PROPERTIES, prop_types_1.default.func);
245function addProperties(listOfProps, propType) {
246 listOfProps.forEach(function (propKey) {
247 AgGridReactLegacy[propKey] = propType;
248 });
249}
250var ReactFrameworkComponentWrapper = /** @class */ (function (_super) {
251 __extends(ReactFrameworkComponentWrapper, _super);
252 function ReactFrameworkComponentWrapper(agGridReact, portalManager) {
253 var _this = _super.call(this) || this;
254 _this.agGridReact = agGridReact;
255 _this.portalManager = portalManager;
256 return _this;
257 }
258 ReactFrameworkComponentWrapper.prototype.createWrapper = function (UserReactComponent, componentType) {
259 if (this.agGridReact.isLegacyComponentRendering()) {
260 return new legacyReactComponent_1.LegacyReactComponent(UserReactComponent, this.agGridReact, this.portalManager, componentType);
261 }
262 else {
263 return new newReactComponent_1.NewReactComponent(UserReactComponent, this.portalManager, componentType);
264 }
265 };
266 return ReactFrameworkComponentWrapper;
267}(ag_grid_community_1.BaseComponentWrapper));