UNPKG

15.9 kBJavaScriptView Raw
1import { __values, __extends } from 'tslib';
2import { Injectable, Component, NgModule } from '@angular/core';
3import { Http, Headers, ConnectionBackend, RequestOptions, HttpModule } from '@angular/http';
4import { Subject } from 'rxjs/Rx';
5import { Subject as Subject$1 } from 'rxjs/Subject';
6import { LocalStorageService } from 'angular-2-local-storage';
7
8var Setting = /** @class */ (function () {
9 function Setting(key, value) {
10 this.key = key;
11 this.value = value;
12 }
13 return Setting;
14}());
15var AppConfig = /** @class */ (function () {
16 function AppConfig(http) {
17 this.http = http;
18 this.paths = [];
19 this.config = [];
20 this.env = null;
21 this.subject = new Subject();
22 this.initialized = this.subject.asObservable();
23 }
24 AppConfig.prototype.getPresentationConfigs = function () {
25 return this.config.filter(function (x) { return x.key.toLowerCase().endsWith("config"); });
26 };
27 AppConfig.prototype.get = function (key) {
28 var setting = this.config.filter(function (x) { return x.key.toLowerCase() == key.toLowerCase(); });
29 return setting.length > 0 ? setting[0].value : null;
30 };
31 AppConfig.prototype.getEnv = function (key) {
32 return this.env[key];
33 };
34 AppConfig.prototype.addNavigationItems = function (applicationName, paths) {
35 var _loop_1 = function (path) {
36 if (this_1.paths.filter(function (x) { return x.value.name == path.name; }).length == 0) {
37 this_1.paths.push(new Setting(applicationName, { name: path.name, route: path.route }));
38 }
39 };
40 var this_1 = this;
41 try {
42 for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
43 var path = paths_1_1.value;
44 _loop_1(path);
45 }
46 }
47 catch (e_1_1) { e_1 = { error: e_1_1 }; }
48 finally {
49 try {
50 if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
51 }
52 finally { if (e_1) throw e_1.error; }
53 }
54 var e_1, _a;
55 };
56 AppConfig.prototype.fetchNavigationItems = function (applicationName) {
57 return this.paths.filter(function (x) { return x.key === applicationName; }).map(function (x) { return new Setting(x.value.name, x.value.route); });
58 };
59 AppConfig.prototype.initialize = function (settings, env) {
60 this.config = this.config.concat(settings);
61 this.env = env;
62 this.subject.next(true);
63 };
64 return AppConfig;
65}());
66AppConfig.decorators = [
67 { type: Injectable },
68];
69AppConfig.ctorParameters = function () { return [
70 { type: Http, },
71]; };
72var contentHeaders = new Headers();
73contentHeaders.append('Accept', 'application/json');
74contentHeaders.append('Content-Type', 'application/json');
75var LoaderService = /** @class */ (function () {
76 function LoaderService() {
77 this.loaderSubject = new Subject$1();
78 this.loaderState = this.loaderSubject.asObservable();
79 }
80 LoaderService.prototype.show = function () {
81 this.loaderSubject.next(({ show: true }));
82 };
83 LoaderService.prototype.hide = function () {
84 this.loaderSubject.next(({ show: false }));
85 };
86 return LoaderService;
87}());
88LoaderService.decorators = [
89 { type: Injectable },
90];
91LoaderService.ctorParameters = function () { return []; };
92var LoaderComponent = /** @class */ (function () {
93 function LoaderComponent(loaderService) {
94 this.loaderService = loaderService;
95 this.show = false;
96 }
97 LoaderComponent.prototype.ngOnInit = function () {
98 var _this = this;
99 this.subscription = this.loaderService.loaderState
100 .subscribe(function (state) {
101 _this.show = state.show;
102 });
103 };
104 LoaderComponent.prototype.ngOnDestroy = function () {
105 this.subscription.unsubscribe();
106 };
107 return LoaderComponent;
108}());
109LoaderComponent.decorators = [
110 { type: Component, args: [{
111 selector: 'angular-loader',
112 template: "<div [class.loader-hidden]=\"!show\">\n <div class=\"loader-overlay\">\n <div>\n <mat-progress-bar color=\"warn\" mode=\"indeterminate\" *ngIf=\"show\"></mat-progress-bar>\n </div>\n </div>\n </div>",
113 styles: [".loader-hidden {\n visibility: hidden;\n }\n .loader-overlay {\n position: absolute;\n width:100%;\n top:0;\n left: 0;\n opacity: 1;\n z-index: 9999999999;\n }"],
114 },] },
115];
116LoaderComponent.ctorParameters = function () { return [
117 { type: LoaderService, },
118]; };
119var HttpIntegrationService = /** @class */ (function () {
120 function HttpIntegrationService() {
121 this.subject = new Subject$1();
122 this.OnError = this.subject.asObservable();
123 }
124 HttpIntegrationService.prototype.error = function (res) {
125 this.subject.next(res);
126 };
127 return HttpIntegrationService;
128}());
129HttpIntegrationService.decorators = [
130 { type: Injectable },
131];
132var InterceptedHttp = /** @class */ (function (_super) {
133 __extends(InterceptedHttp, _super);
134 function InterceptedHttp(backend, defaultOptions, localStorageService, loaderService, httpIntegrationService) {
135 var _this = _super.call(this, backend, defaultOptions) || this;
136 _this.localStorageService = localStorageService;
137 _this.loaderService = loaderService;
138 _this.httpIntegrationService = httpIntegrationService;
139 return _this;
140 }
141 InterceptedHttp.prototype.get = function (url, options) {
142 var _this = this;
143 this.showLoader();
144 return _super.prototype.get.call(this, url, this.getRequestOptionArgs(options))
145 .do(function (res) {
146 _this.onSuccess(res);
147 }, function (error) {
148 _this.onError(error);
149 })
150 .finally(function () {
151 _this.onEnd();
152 });
153 };
154 InterceptedHttp.prototype.post = function (url, body, options) {
155 var _this = this;
156 this.showLoader();
157 return _super.prototype.post.call(this, url, body, this.getRequestOptionArgs(options))
158 .do(function (res) {
159 _this.onSuccess(res);
160 }, function (error) {
161 _this.onError(error);
162 })
163 .finally(function () {
164 _this.onEnd();
165 });
166 };
167 InterceptedHttp.prototype.put = function (url, body, options) {
168 var _this = this;
169 this.showLoader();
170 return _super.prototype.put.call(this, url, body, this.getRequestOptionArgs(options))
171 .do(function (res) {
172 _this.onSuccess(res);
173 }, function (error) {
174 _this.onError(error);
175 })
176 .finally(function () {
177 _this.onEnd();
178 });
179 };
180 InterceptedHttp.prototype.delete = function (url, options) {
181 var _this = this;
182 this.showLoader();
183 return _super.prototype.delete.call(this, url, this.getRequestOptionArgs(options))
184 .do(function (res) {
185 _this.onSuccess(res);
186 }, function (error) {
187 _this.onError(error);
188 })
189 .finally(function () {
190 _this.onEnd();
191 });
192 };
193 InterceptedHttp.prototype.getRequestOptionArgs = function (options) {
194 if (options == null) {
195 options = new RequestOptions();
196 }
197 if (options.headers == null) {
198 options.headers = new Headers();
199 }
200 if (!options.withCredentials)
201 this.createAuthorizationHeader(options.headers);
202 return options;
203 };
204 InterceptedHttp.prototype.createAuthorizationHeader = function (headers) {
205 if (this.localStorageService.get('id_token') != null) {
206 headers.append('Authorization', this.localStorageService.get('id_token'));
207 }
208 };
209 InterceptedHttp.prototype.onSuccess = function (res) {
210 console.log('Request successful');
211 };
212 InterceptedHttp.prototype.onError = function (res) {
213 console.log('Error, status code: ' + res.status);
214 this.httpIntegrationService.error(res);
215 };
216 InterceptedHttp.prototype.onEnd = function () {
217 this.hideLoader();
218 };
219 InterceptedHttp.prototype.showLoader = function () {
220 this.loaderService.show();
221 };
222 InterceptedHttp.prototype.hideLoader = function () {
223 this.loaderService.hide();
224 };
225 return InterceptedHttp;
226}(Http));
227InterceptedHttp.decorators = [
228 { type: Injectable },
229];
230InterceptedHttp.ctorParameters = function () { return [
231 { type: ConnectionBackend, },
232 { type: RequestOptions, },
233 { type: LocalStorageService, },
234 { type: LoaderService, },
235 { type: HttpIntegrationService, },
236]; };
237function httpFactory(xhrBackend, requestOptions, localStorageService, loaderService, httpIntegrationService) {
238 return new InterceptedHttp(xhrBackend, requestOptions, localStorageService, loaderService, httpIntegrationService);
239}
240if (!Array.prototype.add) {
241 Array.prototype.add = function (item) {
242 this.push(item);
243 };
244}
245if (!Array.prototype.addRange) {
246 Array.prototype.addRange = function (items) {
247 var _this = this;
248 items.forEach(function (x) { return _this.push(x); });
249 };
250}
251if (!Array.prototype.remove) {
252 Array.prototype.remove = function (item) {
253 var index = this.indexOf(item);
254 this.splice(index, 0);
255 return index != -1;
256 };
257}
258if (!Array.prototype.where) {
259 Array.prototype.where = function (func) {
260 var items = (this.filter(func));
261 return items;
262 };
263}
264if (!Array.prototype.orderBy) {
265 Array.prototype.orderBy = function (propertyExpression) {
266 var result = Array();
267 var compareFunction = function (item1, item2) {
268 if (propertyExpression(item1) > propertyExpression(item2))
269 return 1;
270 if (propertyExpression(item2) > propertyExpression(item1))
271 return -1;
272 return 0;
273 };
274 for (var i = 0; i < ((this)).length; i++) {
275 return ((this)).sort(compareFunction);
276 }
277 return result;
278 };
279}
280if (!Array.prototype.orderByDescending) {
281 Array.prototype.orderByDescending = function (propertyExpression) {
282 var result = Array();
283 var compareFunction = function (item1, item2) {
284 if (propertyExpression(item1) > propertyExpression(item2))
285 return -1;
286 if (propertyExpression(item2) > propertyExpression(item1))
287 return 1;
288 return 0;
289 };
290 for (var i = 0; i < ((this)).length; i++) {
291 return ((this)).sort(compareFunction);
292 }
293 return result;
294 };
295}
296if (!Array.prototype.single) {
297 Array.prototype.single = function (func) {
298 if (func === void 0) { func = null; }
299 var items = this;
300 if (func != null)
301 items = (this.filter(func));
302 if (items.length > 1)
303 throw new SequenceContainsMoreThanOneElementException();
304 if (items.length == 0)
305 throw new SequenceDoesNotContainException();
306 return items[0];
307 };
308}
309if (!Array.prototype.singleOrDefault) {
310 Array.prototype.singleOrDefault = function (func) {
311 if (func === void 0) { func = null; }
312 var items = this;
313 if (func != null)
314 items = (this.filter(func));
315 if (items.length > 1)
316 throw new SequenceContainsMoreThanOneElementException();
317 if (items.length == 0)
318 return null;
319 return items[0];
320 };
321}
322if (!Array.prototype.first) {
323 Array.prototype.first = function (func) {
324 if (func === void 0) { func = null; }
325 var items = this;
326 if (func != null)
327 items = (this.filter(func));
328 if (items.any())
329 return items[0];
330 else
331 throw new SequenceDoesNotContainException();
332 };
333}
334if (!Array.prototype.firstOrDefault) {
335 Array.prototype.firstOrDefault = function (func) {
336 if (func === void 0) { func = null; }
337 var items = this;
338 if (func != null)
339 items = (this.filter(func));
340 if (items.length > 0)
341 return items[0];
342 else
343 return null;
344 };
345}
346if (!Array.prototype.last) {
347 Array.prototype.last = function (func) {
348 if (func === void 0) { func = null; }
349 var items = this.reverse();
350 if (func != null)
351 items = (this.filter(func));
352 items = (this.filter(func));
353 if (items.length > 1)
354 return items[0];
355 else
356 throw new SequenceDoesNotContainException();
357 };
358}
359if (!Array.prototype.lastOrDefault) {
360 Array.prototype.lastOrDefault = function (func) {
361 if (func === void 0) { func = null; }
362 var items = this.reverse();
363 if (func != null)
364 items = (this.filter(func));
365 if (items.length > 0)
366 return items[0];
367 else
368 return null;
369 };
370}
371if (!Array.prototype.any) {
372 Array.prototype.any = function (func) {
373 if (func === void 0) { func = null; }
374 var items = this;
375 if (func != null)
376 items = (this.filter(func));
377 return items.length > 0;
378 };
379}
380if (!Array.prototype.all) {
381 Array.prototype.all = function (func) {
382 var items = (this.filter(func));
383 return items.length == this.length;
384 };
385}
386var SequenceDoesNotContainException = /** @class */ (function (_super) {
387 __extends(SequenceDoesNotContainException, _super);
388 function SequenceDoesNotContainException() {
389 var _this = _super.call(this) || this;
390 Object.setPrototypeOf(_this, SequenceDoesNotContainException.prototype);
391 return _this;
392 }
393 return SequenceDoesNotContainException;
394}(Error));
395var SequenceContainsMoreThanOneElementException = /** @class */ (function (_super) {
396 __extends(SequenceContainsMoreThanOneElementException, _super);
397 function SequenceContainsMoreThanOneElementException() {
398 var _this = _super.call(this) || this;
399 Object.setPrototypeOf(_this, SequenceContainsMoreThanOneElementException.prototype);
400 return _this;
401 }
402 return SequenceContainsMoreThanOneElementException;
403}(Error));
404var ArrayExtensions = /** @class */ (function () {
405 function ArrayExtensions() {
406 }
407 return ArrayExtensions;
408}());
409if (!String.prototype.fromCamelCase) {
410 String.prototype.fromCamelCase = function () {
411 return this.replace(/([a-z](?=[A-Z]))/g, '$1 ');
412 };
413}
414var StringExtensions = /** @class */ (function () {
415 function StringExtensions() {
416 }
417 return StringExtensions;
418}());
419var CoreModule = /** @class */ (function () {
420 function CoreModule() {
421 }
422 return CoreModule;
423}());
424CoreModule.decorators = [
425 { type: NgModule, args: [{
426 imports: [AppConfig, HttpModule],
427 declarations: [],
428 providers: [AppConfig],
429 exports: [LoaderComponent]
430 },] },
431];
432
433export { CoreModule, LoaderComponent, httpFactory, AppConfig, Setting, contentHeaders, LoaderService, HttpIntegrationService, InterceptedHttp, ArrayExtensions, StringExtensions };
434//# sourceMappingURL=easy-core.js.map