Skip to content

Commit 5714ff6

Browse files
committed
feat(http): Module configuration can accept regexp list
1 parent c3199ea commit 5714ff6

File tree

6 files changed

+83
-370
lines changed

6 files changed

+83
-370
lines changed

README.md

Lines changed: 43 additions & 349 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "ngx-ui-loader-app",
2+
"name": "ngx-ui-loader",
33
"version": "7.1.2",
44
"scripts": {
55
"ng": "ng",

projects/ngx-ui-loader/src/lib/core/ngx-ui-loader.component.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Component, Input, OnInit, OnChanges, SimpleChanges, SimpleChange, OnDes
22
import { DomSanitizer, SafeResourceUrl, SafeStyle } from '@angular/platform-browser';
33
import { NgxUiLoaderService } from './ngx-ui-loader.service';
44
import { Subscription } from 'rxjs';
5+
import { filter } from 'rxjs/operators';
56

67
import { NgxUiLoaderConfig } from './ngx-ui-loader-config';
78
import { DirectionType, PositionType, SpinnerType } from './ngx-ui-loader.types';
89
import { POSITION, PB_DIRECTION, SPINNER } from './ngx-ui-loader.enums';
910
import { SPINNER_CONFIG } from './ngx-ui-loader.contants';
11+
import { ShowEvent } from './ngx-ui-loader.interfaces';
1012
import { coerceNumber } from './coercion';
1113

1214
@Component({
@@ -114,31 +116,27 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
114116
this.pbDirection = <DirectionType>this.validate('pbDirection', this.pbDirection, PB_DIRECTION, this.defaultConfig.pbDirection);
115117

116118
this.showForegroundWatcher = this.ngxService.showForeground$
119+
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
117120
.subscribe(data => {
118-
if (data.loaderId === this.loaderId) {
119-
this.showForeground = data.isShow;
120-
}
121+
this.showForeground = data.isShow;
121122
});
122123

123124
this.showBackgroundWatcher = this.ngxService.showBackground$
125+
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
124126
.subscribe(data => {
125-
if (data.loaderId === this.loaderId) {
126-
this.showBackground = data.isShow;
127-
}
127+
this.showBackground = data.isShow;
128128
});
129129

130130
this.foregroundClosingWatcher = this.ngxService.foregroundClosing$
131+
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
131132
.subscribe(data => {
132-
if (data.loaderId === this.loaderId) {
133-
this.foregroundClosing = data.isShow;
134-
}
133+
this.foregroundClosing = data.isShow;
135134
});
136135

137136
this.backgroundClosingWatcher = this.ngxService.backgroundClosing$
137+
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
138138
.subscribe(data => {
139-
if (data.loaderId === this.loaderId) {
140-
this.backgroundClosing = data.isShow;
141-
}
139+
this.backgroundClosing = data.isShow;
142140
});
143141
this.initialized = true;
144142
}

projects/ngx-ui-loader/src/lib/http/ngx-ui-loader-http-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* The interface of ngx-ui-loader configuration
33
*/
44
export interface NgxUiLoaderHttpConfig {
5-
exclude?: string[]; // not show loader for these api url
5+
exclude?: string[]; // not show loader for the api urls that start with these strings
6+
excludeRegexp?: string[]; // not show loader for the api urls matching these regexps
67
loaderId?: string;
78
showForeground?: boolean;
89
}

projects/ngx-ui-loader/src/lib/http/ngx-ui-loader-http.interceptor.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class NgxUiLoaderHttpInterceptor implements HttpInterceptor {
1313

1414
private count: number;
1515
private defaultConfig: NgxUiLoaderHttpConfig;
16+
private exclude: string[];
17+
private excludeRegexp: RegExp[];
1618

1719
/**
1820
* Constructor
@@ -30,18 +32,18 @@ export class NgxUiLoaderHttpInterceptor implements HttpInterceptor {
3032

3133
if (config) {
3234
if (config.exclude) {
33-
config.exclude = config.exclude.map(url => url.toLowerCase());
35+
this.exclude = config.exclude.map(url => url.toLowerCase());
36+
}
37+
if (config.excludeRegexp) {
38+
this.excludeRegexp = config.excludeRegexp.map(regexp => new RegExp(regexp, 'i'));
3439
}
3540
this.defaultConfig = { ...this.defaultConfig, ...config };
3641
}
3742
}
3843

3944
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
40-
if (this.defaultConfig.exclude) {
41-
// do not show the loader for api url in the `exclude` list
42-
if (this.defaultConfig.exclude.findIndex(url => req.url.toLowerCase().startsWith(url)) !== -1) {
43-
return next.handle(req);
44-
}
45+
if (this.isIgnored(req.url)) {
46+
return next.handle(req);
4547
}
4648

4749
this.count++;
@@ -66,4 +68,22 @@ export class NgxUiLoaderHttpInterceptor implements HttpInterceptor {
6668
}
6769
}));
6870
}
71+
72+
private isIgnored(url: string): boolean {
73+
if (this.exclude) {
74+
// do not show the loader for api urls in the `exclude` list
75+
if (this.exclude.findIndex(str => url.toLowerCase().startsWith(str)) !== -1) {
76+
return true;
77+
}
78+
}
79+
80+
if (this.excludeRegexp) {
81+
// do not show the loader for api urls which matches regexps in the `excludeRegexp` list
82+
if (this.excludeRegexp.findIndex(regexp => regexp.test(url)) !== -1) {
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
6989
}

0 commit comments

Comments
 (0)