UNPKG

18.9 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
9 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
11 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
12 return c > 3 && r && Object.defineProperty(target, key, r), r;
13};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17Object.defineProperty(exports, "__esModule", { value: true });
18var context_1 = require("../../context/context");
19var gridOptionsWrapper_1 = require("../../gridOptionsWrapper");
20var utils_1 = require("../../utils");
21var componentProvider_1 = require("./componentProvider");
22var agComponentUtils_1 = require("./agComponentUtils");
23var componentMetadataProvider_1 = require("./componentMetadataProvider");
24var ComponentType;
25(function (ComponentType) {
26 ComponentType[ComponentType["AG_GRID"] = 0] = "AG_GRID";
27 ComponentType[ComponentType["FRAMEWORK"] = 1] = "FRAMEWORK";
28})(ComponentType = exports.ComponentType || (exports.ComponentType = {}));
29var ComponentSource;
30(function (ComponentSource) {
31 ComponentSource[ComponentSource["DEFAULT"] = 0] = "DEFAULT";
32 ComponentSource[ComponentSource["REGISTERED_BY_NAME"] = 1] = "REGISTERED_BY_NAME";
33 ComponentSource[ComponentSource["HARDCODED"] = 2] = "HARDCODED";
34})(ComponentSource = exports.ComponentSource || (exports.ComponentSource = {}));
35var ComponentResolver = (function () {
36 function ComponentResolver() {
37 }
38 /**
39 * This method returns the underlying representation of the component to be created. ie for Javascript the
40 * underlying function where we should be calling new into. In case of the frameworks, the framework class
41 * object that represents the component to be created.
42 *
43 * This method is handy for different reasons, for example if you want to check if a component has a particular
44 * method implemented without having to create the component, just by inspecting the source component
45 *
46 * It takes
47 * @param holder: This is the context for which this component needs to be created, it can be gridOptions
48 * (global) or columnDef mostly.
49 * @param propertyName: The name of the property used in ag-grid as a convention to refer to the component, it can be:
50 * 'floatingFilter', 'cellRenderer', is used to find if the user is specifying a custom component
51 * @param dynamicComponentParams: Params to be passed to the dynamic component function in case it needs to be
52 * invoked
53 * @param defaultComponentName: The name of the component to load if there is no component specified
54 */
55 ComponentResolver.prototype.getComponentToUse = function (holder, propertyName, dynamicComponentParams, defaultComponentName) {
56 /**
57 * There are five things that can happen when resolving a component.
58 * a) HardcodedFwComponent: That holder[propertyName]Framework has associated a Framework native component
59 * b) HardcodedJsComponent: That holder[propertyName] has associate a JS component
60 * c) hardcodedJsFunction: That holder[propertyName] has associate a JS function
61 * d) hardcodedNameComponent: That holder[propertyName] has associate a string that represents a component to load
62 * e) That none of the three previous are specified, then we need to use the DefaultRegisteredComponent
63 */
64 var hardcodedNameComponent = null;
65 var HardcodedJsComponent = null;
66 var hardcodedJsFunction = null;
67 var HardcodedFwComponent = null;
68 var dynamicComponentFn;
69 if (holder != null) {
70 var componentPropertyValue = holder[propertyName];
71 if (componentPropertyValue != null) {
72 if (typeof componentPropertyValue === 'string') {
73 hardcodedNameComponent = componentPropertyValue;
74 }
75 else if (this.agComponentUtils.doesImplementIComponent(componentPropertyValue)) {
76 HardcodedJsComponent = componentPropertyValue;
77 }
78 else {
79 hardcodedJsFunction = componentPropertyValue;
80 }
81 }
82 HardcodedFwComponent = holder[propertyName + "Framework"];
83 dynamicComponentFn = holder[propertyName + "Selector"];
84 }
85 /**
86 * Since we allow many types of flavors for specifying the components, let's make sure this is not an illegal
87 * combination
88 */
89 if ((HardcodedJsComponent && HardcodedFwComponent) ||
90 (hardcodedNameComponent && HardcodedFwComponent) ||
91 (hardcodedJsFunction && HardcodedFwComponent)) {
92 throw Error("ag-grid: you are trying to specify: " + propertyName + " twice as a component.");
93 }
94 if (HardcodedFwComponent && !this.frameworkComponentWrapper) {
95 throw Error("ag-grid: you are specifying a framework component but you are not using a framework version of ag-grid for : " + propertyName);
96 }
97 if (dynamicComponentFn && (hardcodedNameComponent || HardcodedJsComponent || hardcodedJsFunction || HardcodedFwComponent)) {
98 throw Error("ag-grid: you can't specify both, the selector and the component of ag-grid for : " + propertyName);
99 }
100 /**
101 * At this stage we are guaranteed to either have,
102 * DEPRECATED
103 * - A unique HardcodedFwComponent
104 * - A unique HardcodedJsComponent
105 * - A unique hardcodedJsFunction
106 * BY NAME- FAVOURED APPROACH
107 * - A unique hardcodedNameComponent
108 * - None of the previous, hence we revert to: RegisteredComponent
109 */
110 if (HardcodedFwComponent) {
111 // console.warn(`ag-grid: Since version 12.1.0 specifying a component directly is deprecated, you should register the component by name`);
112 // console.warn(`${HardcodedFwComponent}`);
113 return {
114 type: ComponentType.FRAMEWORK,
115 component: HardcodedFwComponent,
116 source: ComponentSource.HARDCODED,
117 dynamicParams: null
118 };
119 }
120 if (HardcodedJsComponent) {
121 // console.warn(`ag-grid: Since version 12.1.0 specifying a component directly is deprecated, you should register the component by name`);
122 // console.warn(`${HardcodedJsComponent}`);
123 return {
124 type: ComponentType.AG_GRID,
125 component: HardcodedJsComponent,
126 source: ComponentSource.HARDCODED,
127 dynamicParams: null
128 };
129 }
130 if (hardcodedJsFunction) {
131 // console.warn(`ag-grid: Since version 12.1.0 specifying a function directly is deprecated, you should register the component by name`);
132 // console.warn(`${hardcodedJsFunction}`);
133 return this.agComponentUtils.adaptFunction(propertyName, hardcodedJsFunction, ComponentType.AG_GRID, ComponentSource.HARDCODED);
134 }
135 if (dynamicComponentFn) {
136 var dynamicComponentDef = dynamicComponentFn(dynamicComponentParams);
137 if (dynamicComponentDef != null) {
138 if (dynamicComponentDef.component == null) {
139 dynamicComponentDef.component = defaultComponentName;
140 }
141 var dynamicComponent = this.resolveByName(propertyName, dynamicComponentDef.component);
142 return utils_1._.assign(dynamicComponent, {
143 dynamicParams: dynamicComponentDef.params
144 });
145 }
146 }
147 //^^^^^ABOVE DEPRECATED - AT THIS POINT WE ARE RESOLVING BY NAME
148 var componentNameToUse;
149 if (hardcodedNameComponent) {
150 componentNameToUse = hardcodedNameComponent;
151 }
152 else {
153 componentNameToUse = defaultComponentName;
154 }
155 return componentNameToUse == null ? null : this.resolveByName(propertyName, componentNameToUse);
156 };
157 ComponentResolver.prototype.resolveByName = function (propertyName, componentNameOpt) {
158 var componentName = componentNameOpt != null ? componentNameOpt : propertyName;
159 var registeredComponent = this.componentProvider.retrieve(componentName);
160 if (registeredComponent == null)
161 return null;
162 //If it is a FW it has to be registered as a component
163 if (registeredComponent.type == ComponentType.FRAMEWORK) {
164 return {
165 component: registeredComponent.component,
166 type: ComponentType.FRAMEWORK,
167 source: ComponentSource.REGISTERED_BY_NAME,
168 dynamicParams: null
169 };
170 }
171 //If it is JS it may be a function or a component
172 if (this.agComponentUtils.doesImplementIComponent(registeredComponent.component)) {
173 return {
174 component: registeredComponent.component,
175 type: ComponentType.AG_GRID,
176 source: (registeredComponent.source == componentProvider_1.RegisteredComponentSource.REGISTERED) ? ComponentSource.REGISTERED_BY_NAME : ComponentSource.DEFAULT,
177 dynamicParams: null
178 };
179 }
180 // This is a function
181 return this.agComponentUtils.adaptFunction(propertyName, registeredComponent.component, registeredComponent.type, (registeredComponent.source == componentProvider_1.RegisteredComponentSource.REGISTERED) ? ComponentSource.REGISTERED_BY_NAME : ComponentSource.DEFAULT);
182 };
183 /**
184 * Useful to check what would be the resultant params for a given object
185 * @param holder: This is the context for which this component needs to be created, it can be gridOptions
186 * (global) or columnDef mostly.
187 * @param propertyName: The name of the property used in ag-grid as a convention to refer to the component, it can be:
188 * 'floatingFilter', 'cellRenderer', is used to find if the user is specifying a custom component
189 * @param agGridParams: Params to be passed to the component and passed by ag-Grid. This will get merged with any params
190 * specified by the user in the configuration
191 * @returns {any} It merges the user agGridParams with the actual params specified by the user.
192 */
193 ComponentResolver.prototype.mergeParams = function (holder, propertyName, agGridParams, dynamicCustomParams, dynamicParams) {
194 if (dynamicParams === void 0) { dynamicParams = null; }
195 var customParamsRaw = holder ? holder[propertyName + "Params"] : null;
196 var finalParams = {};
197 utils_1._.mergeDeep(finalParams, agGridParams);
198 if (customParamsRaw != null) {
199 var customParams = null;
200 if (typeof customParamsRaw === 'function') {
201 customParams = customParamsRaw(dynamicCustomParams);
202 }
203 else {
204 customParams = customParamsRaw;
205 }
206 utils_1._.mergeDeep(finalParams, customParams);
207 }
208 utils_1._.mergeDeep(finalParams, dynamicParams);
209 if (!finalParams.api) {
210 finalParams.api = this.gridOptions.api;
211 }
212 return finalParams;
213 };
214 /**
215 * This method creates a component given everything needed to guess what sort of component needs to be instantiated
216 * It takes
217 * @param holderOpt: This is the context for which this component needs to be created, it can be gridOptions
218 * (global) or columnDef mostly.
219 * @param agGridParams: Params to be passed to the component and passed by ag-Grid. This will get merged with any params
220 * specified by the user in the configuration
221 * @param propertyName: The name of the property used in ag-grid as a convention to refer to the component, it can be:
222 * 'floatingFilter', 'cellRenderer', is used to find if the user is specifying a custom component
223 * @param dynamicComponentParams: Params to be passed to the dynamic component function in case it needs to be
224 * invoked
225 * @param defaultComponentName: The actual name of the component to instantiate, this is usually the same as propertyName, but in
226 * some cases is not, like floatingFilter, if it is the same is not necessary to specify
227 * @param mandatory: Handy method to tell if this should return a component ALWAYS. if that is the case, but there is no
228 * component found, it throws an error, by default all components are MANDATORY
229 * @param customInitParamsCb: A chance to customise the params passed to the init method. It receives what the current
230 * params are and the component that init is about to get called for
231 */
232 ComponentResolver.prototype.createAgGridComponent = function (holderOpt, agGridParams, propertyName, dynamicComponentParams, defaultComponentName, mandatory, customInitParamsCb) {
233 if (mandatory === void 0) { mandatory = true; }
234 var holder = holderOpt == null ? this.gridOptions : holderOpt;
235 //Create the component instance
236 var componentAndParams = this.newAgGridComponent(holder, propertyName, dynamicComponentParams, defaultComponentName, mandatory);
237 if (!componentAndParams)
238 return null;
239 // Wire the component and call the init method with the correct params
240 var finalParams = this.mergeParams(holder, propertyName, agGridParams, dynamicComponentParams, componentAndParams[1]);
241 // a temporary fix for AG-1574
242 // AG-1715 raised to do a wider ranging refactor to improve this
243 finalParams.agGridReact = this.context.getBean('agGridReact') ? utils_1._.cloneObject(this.context.getBean('agGridReact')) : {};
244 // AG-1716 - directly related to AG-1574 and AG-1715
245 finalParams.frameworkComponentWrapper = this.context.getBean('frameworkComponentWrapper') ? this.context.getBean('frameworkComponentWrapper') : {};
246 var deferredInit = this.initialiseComponent(componentAndParams[0], finalParams, customInitParamsCb);
247 if (deferredInit == null) {
248 return utils_1.Promise.resolve(componentAndParams[0]);
249 }
250 else {
251 var asPromise = deferredInit;
252 return asPromise.map(function (notRelevant) { return componentAndParams[0]; });
253 }
254 };
255 /**
256 * This method creates a component given everything needed to guess what sort of component needs to be instantiated
257 * It takes
258 * @param clazz: The class to instantiate,
259 * @param agGridParams: Params to be passed to the component and passed by ag-Grid. This will get merged with any params
260 * specified by the user in the configuration
261 * @param customInitParamsCb: A chance to customise the params passed to the init method. It receives what the current
262 * params are and the component that init is about to get called for
263 */
264 ComponentResolver.prototype.createInternalAgGridComponent = function (clazz, agGridParams, customInitParamsCb) {
265 var internalComponent = new clazz();
266 this.initialiseComponent(internalComponent, agGridParams, customInitParamsCb);
267 return internalComponent;
268 };
269 ComponentResolver.prototype.newAgGridComponent = function (holder, propertyName, dynamicComponentParams, defaultComponentName, mandatory) {
270 if (mandatory === void 0) { mandatory = true; }
271 var componentToUse = this.getComponentToUse(holder, propertyName, dynamicComponentParams, defaultComponentName);
272 if (!componentToUse || !componentToUse.component) {
273 if (mandatory) {
274 console.error("Error creating component " + propertyName + "=>" + defaultComponentName);
275 }
276 return null;
277 }
278 if (componentToUse.type === ComponentType.AG_GRID) {
279 return [
280 new componentToUse.component(),
281 componentToUse.dynamicParams
282 ];
283 }
284 //Using framework component
285 var FrameworkComponentRaw = componentToUse.component;
286 var thisComponentConfig = this.componentMetadataProvider.retrieve(propertyName);
287 return [
288 this.frameworkComponentWrapper.wrap(FrameworkComponentRaw, thisComponentConfig.mandatoryMethodList, thisComponentConfig.optionalMethodList, defaultComponentName),
289 componentToUse.dynamicParams
290 ];
291 };
292 ComponentResolver.prototype.initialiseComponent = function (component, agGridParams, customInitParamsCb) {
293 this.context.wireBean(component);
294 if (customInitParamsCb == null) {
295 return component.init(agGridParams);
296 }
297 else {
298 return component.init(customInitParamsCb(agGridParams, component));
299 }
300 };
301 __decorate([
302 context_1.Autowired("gridOptions"),
303 __metadata("design:type", Object)
304 ], ComponentResolver.prototype, "gridOptions", void 0);
305 __decorate([
306 context_1.Autowired("gridOptionsWrapper"),
307 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
308 ], ComponentResolver.prototype, "gridOptionsWrapper", void 0);
309 __decorate([
310 context_1.Autowired("context"),
311 __metadata("design:type", context_1.Context)
312 ], ComponentResolver.prototype, "context", void 0);
313 __decorate([
314 context_1.Autowired("agComponentUtils"),
315 __metadata("design:type", agComponentUtils_1.AgComponentUtils)
316 ], ComponentResolver.prototype, "agComponentUtils", void 0);
317 __decorate([
318 context_1.Autowired("componentMetadataProvider"),
319 __metadata("design:type", componentMetadataProvider_1.ComponentMetadataProvider)
320 ], ComponentResolver.prototype, "componentMetadataProvider", void 0);
321 __decorate([
322 context_1.Autowired("componentProvider"),
323 __metadata("design:type", componentProvider_1.ComponentProvider)
324 ], ComponentResolver.prototype, "componentProvider", void 0);
325 __decorate([
326 context_1.Optional("frameworkComponentWrapper"),
327 __metadata("design:type", Object)
328 ], ComponentResolver.prototype, "frameworkComponentWrapper", void 0);
329 ComponentResolver = __decorate([
330 context_1.Bean('componentResolver')
331 ], ComponentResolver);
332 return ComponentResolver;
333}());
334exports.ComponentResolver = ComponentResolver;