Skip to content

Commit 8e83f84

Browse files
committed
feat(router): Be able to exclude specific paths from NgxUiLoaderRouter
1 parent fc9bf0b commit 8e83f84

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* The interface of ngx-ui-loader configuration
33
*/
44
export interface NgxUiLoaderRouterConfig {
5+
exclude?: string[]; // not show loader for the urls that start with these strings
6+
excludeRegexp?: string[]; // not show loader for the urls matching these regexps
57
loaderId?: string;
68
showForeground?: boolean;
79
}

projects/ngx-ui-loader/src/lib/router/ngx-ui-loader-router.module.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NgModule, ModuleWithProviders, Inject, Optional, SkipSelf } from '@angular/core';
2-
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router } from '@angular/router';
2+
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router, RouterEvent } from '@angular/router';
33

44
import { NgxUiLoaderService } from '../core/ngx-ui-loader.service';
55
import { NgxUiLoaderRouterConfig } from './ngx-ui-loader-router-config';
@@ -9,6 +9,9 @@ import { ROUTER_LOADER_ID } from './ngx-ui-loader-router.constants';
99
@NgModule({})
1010
export class NgxUiLoaderRouterModule {
1111

12+
private exclude: string[];
13+
private excludeRegexp: RegExp[];
14+
1215
/**
1316
* forRoot
1417
* @param routerConfig Configuration
@@ -50,26 +53,54 @@ export class NgxUiLoaderRouterModule {
5053
};
5154

5255
if (config) {
56+
if (config.exclude) {
57+
this.exclude = config.exclude.map(url => url.toLowerCase());
58+
}
59+
if (config.excludeRegexp) {
60+
this.excludeRegexp = config.excludeRegexp.map(regexp => new RegExp(regexp, 'i'));
61+
}
5362
defaultConfig = { ...defaultConfig, ...config };
5463
}
5564

5665
router.events
57-
.subscribe(event => {
66+
.subscribe((event: RouterEvent) => {
5867
if (event instanceof NavigationStart) {
59-
if (defaultConfig.showForeground) {
60-
ngxUiLoaderService.startLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
61-
} else {
62-
ngxUiLoaderService.startBackgroundLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
68+
if (!this.isIgnored(event.url)) {
69+
if (defaultConfig.showForeground) {
70+
ngxUiLoaderService.startLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
71+
} else {
72+
ngxUiLoaderService.startBackgroundLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
73+
}
6374
}
6475
}
6576

6677
if (event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
67-
if (defaultConfig.showForeground) {
68-
ngxUiLoaderService.stopLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
69-
} else {
70-
ngxUiLoaderService.stopBackgroundLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
78+
if (!this.isIgnored(event.url)) {
79+
if (defaultConfig.showForeground) {
80+
ngxUiLoaderService.stopLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
81+
} else {
82+
ngxUiLoaderService.stopBackgroundLoader(defaultConfig.loaderId, ROUTER_LOADER_ID);
83+
}
7184
}
7285
}
7386
});
7487
}
88+
89+
private isIgnored(url: string): boolean {
90+
if (this.exclude) {
91+
// do not show the loader for urls in the `exclude` list
92+
if (this.exclude.findIndex(str => url.toLowerCase().startsWith(str)) !== -1) {
93+
return true;
94+
}
95+
}
96+
97+
if (this.excludeRegexp) {
98+
// do not show the loader for urls which matches regexps in the `excludeRegexp` list
99+
if (this.excludeRegexp.findIndex(regexp => regexp.test(url)) !== -1) {
100+
return true;
101+
}
102+
}
103+
104+
return false;
105+
}
75106
}

0 commit comments

Comments
 (0)