1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | var utils_1 = require("../utils");
|
10 | var Context = (function () {
|
11 | function Context(params, logger) {
|
12 | this.beans = {};
|
13 | this.componentsMappedByName = {};
|
14 | this.destroyed = false;
|
15 | if (!params || !params.beans) {
|
16 | return;
|
17 | }
|
18 | this.contextParams = params;
|
19 | this.logger = logger;
|
20 | this.logger.log(">> creating ag-Application Context");
|
21 | this.setupComponents();
|
22 | this.createBeans();
|
23 | var beans = utils_1.Utils.mapObject(this.beans, function (beanEntry) { return beanEntry.beanInstance; });
|
24 | this.wireBeans(beans);
|
25 | this.logger.log(">> ag-Application Context ready - component is alive");
|
26 | }
|
27 | Context.prototype.setupComponents = function () {
|
28 | var _this = this;
|
29 | if (this.contextParams.components) {
|
30 | this.contextParams.components.forEach(function (componentMeta) { return _this.addComponent(componentMeta); });
|
31 | }
|
32 | };
|
33 | Context.prototype.addComponent = function (componentMeta) {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var classEscaped = componentMeta.componentName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
39 |
|
40 | var classUpperCase = classEscaped.toUpperCase();
|
41 |
|
42 | this.componentsMappedByName[classUpperCase] = componentMeta.theClass;
|
43 | };
|
44 | Context.prototype.createComponent = function (element, afterPreCreateCallback) {
|
45 | var key = element.nodeName;
|
46 | if (this.componentsMappedByName && this.componentsMappedByName[key]) {
|
47 | var newComponent = new this.componentsMappedByName[key]();
|
48 | this.wireBean(newComponent, afterPreCreateCallback);
|
49 | return newComponent;
|
50 | }
|
51 | else {
|
52 | return null;
|
53 | }
|
54 | };
|
55 | Context.prototype.wireBean = function (bean, afterPreCreateCallback) {
|
56 | if (!bean) {
|
57 | throw Error("Can't wire to bean since it is null");
|
58 | }
|
59 | this.wireBeans([bean], afterPreCreateCallback);
|
60 | };
|
61 | Context.prototype.wireBeans = function (beans, afterPreCreateCallback) {
|
62 | this.autoWireBeans(beans);
|
63 | this.methodWireBeans(beans);
|
64 | this.preConstruct(beans);
|
65 |
|
66 |
|
67 | if (utils_1.Utils.exists(afterPreCreateCallback)) {
|
68 | beans.forEach(afterPreCreateCallback);
|
69 | }
|
70 | this.postConstruct(beans);
|
71 | };
|
72 | Context.prototype.createBeans = function () {
|
73 | var _this = this;
|
74 |
|
75 | this.contextParams.beans.forEach(this.createBeanEntry.bind(this));
|
76 |
|
77 | if (this.contextParams.overrideBeans) {
|
78 | this.contextParams.overrideBeans.forEach(this.createBeanEntry.bind(this));
|
79 | }
|
80 |
|
81 | utils_1.Utils.iterateObject(this.beans, function (key, beanEntry) {
|
82 | var constructorParamsMeta;
|
83 | if (beanEntry.bean.__agBeanMetaData && beanEntry.bean.__agBeanMetaData.autowireMethods && beanEntry.bean.__agBeanMetaData.autowireMethods.agConstructor) {
|
84 | constructorParamsMeta = beanEntry.bean.__agBeanMetaData.autowireMethods.agConstructor;
|
85 | }
|
86 | var constructorParams = _this.getBeansForParameters(constructorParamsMeta, beanEntry.bean.name);
|
87 | var newInstance = applyToConstructor(beanEntry.bean, constructorParams);
|
88 | beanEntry.beanInstance = newInstance;
|
89 | _this.logger.log("bean " + _this.getBeanName(newInstance) + " created");
|
90 | });
|
91 | };
|
92 | Context.prototype.createBeanEntry = function (Bean) {
|
93 | var metaData = Bean.__agBeanMetaData;
|
94 | if (!metaData) {
|
95 | var beanName = void 0;
|
96 | if (Bean.prototype.constructor) {
|
97 | beanName = Bean.prototype.constructor.name;
|
98 | }
|
99 | else {
|
100 | beanName = "" + Bean;
|
101 | }
|
102 | console.error("context item " + beanName + " is not a bean");
|
103 | return;
|
104 | }
|
105 | var beanEntry = {
|
106 | bean: Bean,
|
107 | beanInstance: null,
|
108 | beanName: metaData.beanName
|
109 | };
|
110 | this.beans[metaData.beanName] = beanEntry;
|
111 | };
|
112 | Context.prototype.autoWireBeans = function (beans) {
|
113 | var _this = this;
|
114 | beans.forEach(function (bean) { return _this.autoWireBean(bean); });
|
115 | };
|
116 | Context.prototype.methodWireBeans = function (beans) {
|
117 | var _this = this;
|
118 | beans.forEach(function (bean) {
|
119 | if (!bean) {
|
120 | throw Error("Can't wire to bean since it is null");
|
121 | }
|
122 | return _this.methodWireBean(bean);
|
123 | });
|
124 | };
|
125 | Context.prototype.autoWireBean = function (bean) {
|
126 | var _this = this;
|
127 | var currentBean = bean;
|
128 | var _loop_1 = function () {
|
129 | var currentConstructor = currentBean.constructor;
|
130 | if (currentConstructor.__agBeanMetaData && currentConstructor.__agBeanMetaData.agClassAttributes) {
|
131 | var attributes = currentConstructor.__agBeanMetaData.agClassAttributes;
|
132 | if (!attributes) {
|
133 | return { value: void 0 };
|
134 | }
|
135 | var beanName_1 = this_1.getBeanName(currentConstructor);
|
136 | attributes.forEach(function (attribute) {
|
137 | var otherBean = _this.lookupBeanInstance(beanName_1, attribute.beanName, attribute.optional);
|
138 | bean[attribute.attributeName] = otherBean;
|
139 | });
|
140 | }
|
141 | currentBean = Object.getPrototypeOf(currentBean) ? Object.getPrototypeOf(currentBean) : null;
|
142 | };
|
143 | var this_1 = this;
|
144 | while (currentBean != null) {
|
145 | var state_1 = _loop_1();
|
146 | if (typeof state_1 === "object")
|
147 | return state_1.value;
|
148 | }
|
149 | };
|
150 | Context.prototype.getBeanName = function (constructor) {
|
151 | if (constructor.__agBeanMetaData && constructor.__agBeanMetaData.beanName) {
|
152 | return constructor.__agBeanMetaData.beanName;
|
153 | }
|
154 | var constructorString = constructor.toString();
|
155 | var beanName = constructorString.substring(9, constructorString.indexOf("("));
|
156 | return beanName;
|
157 | };
|
158 | Context.prototype.methodWireBean = function (bean) {
|
159 | var _this = this;
|
160 | var autowiredMethods;
|
161 | if (bean.constructor.__agBeanMetaData && bean.constructor.__agBeanMetaData.autowireMethods) {
|
162 | autowiredMethods = bean.constructor.__agBeanMetaData.autowireMethods;
|
163 | }
|
164 | utils_1.Utils.iterateObject(autowiredMethods, function (methodName, wireParams) {
|
165 |
|
166 | if (methodName === "agConstructor") {
|
167 | return;
|
168 | }
|
169 | var beanName = _this.getBeanName(bean.constructor);
|
170 | var initParams = _this.getBeansForParameters(wireParams, beanName);
|
171 | bean[methodName].apply(bean, initParams);
|
172 | });
|
173 | };
|
174 | Context.prototype.getBeansForParameters = function (parameters, beanName) {
|
175 | var _this = this;
|
176 | var beansList = [];
|
177 | if (parameters) {
|
178 | utils_1.Utils.iterateObject(parameters, function (paramIndex, otherBeanName) {
|
179 | var otherBean = _this.lookupBeanInstance(beanName, otherBeanName);
|
180 | beansList[Number(paramIndex)] = otherBean;
|
181 | });
|
182 | }
|
183 | return beansList;
|
184 | };
|
185 | Context.prototype.lookupBeanInstance = function (wiringBean, beanName, optional) {
|
186 | if (optional === void 0) { optional = false; }
|
187 | if (beanName === "context") {
|
188 | return this;
|
189 | }
|
190 | else if (this.contextParams.seed && this.contextParams.seed.hasOwnProperty(beanName)) {
|
191 | return this.contextParams.seed[beanName];
|
192 | }
|
193 | else {
|
194 | var beanEntry = this.beans[beanName];
|
195 | if (beanEntry) {
|
196 | return beanEntry.beanInstance;
|
197 | }
|
198 | if (!optional) {
|
199 | console.error("ag-Grid: unable to find bean reference " + beanName + " while initialising " + wiringBean);
|
200 | }
|
201 | return null;
|
202 | }
|
203 | };
|
204 | Context.prototype.postConstruct = function (beans) {
|
205 | beans.forEach(function (bean) {
|
206 |
|
207 | if (bean.constructor.__agBeanMetaData && bean.constructor.__agBeanMetaData.postConstructMethods) {
|
208 | bean.constructor.__agBeanMetaData && bean.constructor.__agBeanMetaData.postConstructMethods.forEach(function (methodName) { return bean[methodName](); });
|
209 | }
|
210 | });
|
211 | };
|
212 | Context.prototype.preConstruct = function (beans) {
|
213 | beans.forEach(function (bean) {
|
214 |
|
215 | if (bean.constructor.__agBeanMetaData && bean.constructor.__agBeanMetaData.preConstructMethods) {
|
216 | bean.constructor.__agBeanMetaData.preConstructMethods.forEach(function (methodName) { return bean[methodName](); });
|
217 | }
|
218 | });
|
219 | };
|
220 | Context.prototype.getBean = function (name) {
|
221 | return this.lookupBeanInstance("getBean", name, true);
|
222 | };
|
223 | Context.prototype.destroy = function () {
|
224 |
|
225 | if (this.destroyed) {
|
226 | return;
|
227 | }
|
228 | this.logger.log(">> Shutting down ag-Application Context");
|
229 |
|
230 | utils_1.Utils.iterateObject(this.beans, function (key, beanEntry) {
|
231 | var bean = beanEntry.beanInstance;
|
232 | if (bean.constructor.__agBeanMetaData && bean.constructor.__agBeanMetaData.preDestroyMethods) {
|
233 | bean.constructor.__agBeanMetaData.preDestroyMethods.forEach(function (methodName) { return bean[methodName](); });
|
234 | }
|
235 | });
|
236 | this.destroyed = true;
|
237 | this.logger.log(">> ag-Application Context shut down - component is dead");
|
238 | };
|
239 | return Context;
|
240 | }());
|
241 | exports.Context = Context;
|
242 |
|
243 |
|
244 | function applyToConstructor(constructor, argArray) {
|
245 | var args = [null].concat(argArray);
|
246 | var factoryFunction = constructor.bind.apply(constructor, args);
|
247 | return new factoryFunction();
|
248 | }
|
249 | function PreConstruct(target, methodName, descriptor) {
|
250 | var props = getOrCreateProps(target.constructor);
|
251 | if (!props.postConstructMethods) {
|
252 | props.preConstructMethods = [];
|
253 | }
|
254 | props.preConstructMethods.push(methodName);
|
255 | }
|
256 | exports.PreConstruct = PreConstruct;
|
257 | function PostConstruct(target, methodName, descriptor) {
|
258 | var props = getOrCreateProps(target.constructor);
|
259 | if (!props.postConstructMethods) {
|
260 | props.postConstructMethods = [];
|
261 | }
|
262 | props.postConstructMethods.push(methodName);
|
263 | }
|
264 | exports.PostConstruct = PostConstruct;
|
265 | function PreDestroy(target, methodName, descriptor) {
|
266 | var props = getOrCreateProps(target.constructor);
|
267 | if (!props.preDestroyMethods) {
|
268 | props.preDestroyMethods = [];
|
269 | }
|
270 | props.preDestroyMethods.push(methodName);
|
271 | }
|
272 | exports.PreDestroy = PreDestroy;
|
273 | function Bean(beanName) {
|
274 | return function (classConstructor) {
|
275 | var props = getOrCreateProps(classConstructor);
|
276 | props.beanName = beanName;
|
277 | };
|
278 | }
|
279 | exports.Bean = Bean;
|
280 | function Autowired(name) {
|
281 | return function (target, propertyKey, descriptor) {
|
282 | autowiredFunc(target, name, false, target, propertyKey, null);
|
283 | };
|
284 | }
|
285 | exports.Autowired = Autowired;
|
286 | function Optional(name) {
|
287 | return function (target, propertyKey, descriptor) {
|
288 | autowiredFunc(target, name, true, target, propertyKey, null);
|
289 | };
|
290 | }
|
291 | exports.Optional = Optional;
|
292 | function autowiredFunc(target, name, optional, classPrototype, methodOrAttributeName, index) {
|
293 | if (name === null) {
|
294 | console.error("ag-Grid: Autowired name should not be null");
|
295 | return;
|
296 | }
|
297 | if (typeof index === "number") {
|
298 | console.error("ag-Grid: Autowired should be on an attribute");
|
299 | return;
|
300 | }
|
301 |
|
302 | var props = getOrCreateProps(target.constructor);
|
303 | if (!props.agClassAttributes) {
|
304 | props.agClassAttributes = [];
|
305 | }
|
306 | props.agClassAttributes.push({
|
307 | attributeName: methodOrAttributeName,
|
308 | beanName: name,
|
309 | optional: optional
|
310 | });
|
311 | }
|
312 | function Qualifier(name) {
|
313 | return function (classPrototype, methodOrAttributeName, index) {
|
314 | var constructor = typeof classPrototype == "function" ? classPrototype : classPrototype.constructor;
|
315 | var props;
|
316 | if (typeof index === "number") {
|
317 |
|
318 | var methodName = void 0;
|
319 | if (methodOrAttributeName) {
|
320 | props = getOrCreateProps(constructor);
|
321 | methodName = methodOrAttributeName;
|
322 | }
|
323 | else {
|
324 | props = getOrCreateProps(constructor);
|
325 | methodName = "agConstructor";
|
326 | }
|
327 | if (!props.autowireMethods) {
|
328 | props.autowireMethods = {};
|
329 | }
|
330 | if (!props.autowireMethods[methodName]) {
|
331 | props.autowireMethods[methodName] = {};
|
332 | }
|
333 | props.autowireMethods[methodName][index] = name;
|
334 | }
|
335 | };
|
336 | }
|
337 | exports.Qualifier = Qualifier;
|
338 | function getOrCreateProps(target) {
|
339 | if (!target.hasOwnProperty("__agBeanMetaData")) {
|
340 | target.__agBeanMetaData = {};
|
341 | }
|
342 | return target.__agBeanMetaData;
|
343 | }
|