Browse Source

use RouterEvents instead of filtering router events

pull/7537/head
Arman Ozak 6 years ago
parent
commit
ec84273b3c
  1. 17
      npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts
  2. 17
      npm/ng-packs/packages/theme-shared/src/lib/components/breadcrumb/breadcrumb.component.ts
  3. 22
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

17
npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts

@ -1,11 +1,11 @@
import { Component, Injector, Optional, SkipSelf, Type } from '@angular/core'; import { Component, Injector, Optional, SkipSelf, Type } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
import { eLayoutType } from '../enums/common'; import { eLayoutType } from '../enums/common';
import { ABP } from '../models'; import { ABP } from '../models';
import { ReplaceableComponents } from '../models/replaceable-components'; import { ReplaceableComponents } from '../models/replaceable-components';
import { LocalizationService } from '../services/localization.service'; import { LocalizationService } from '../services/localization.service';
import { ReplaceableComponentsService } from '../services/replaceable-components.service'; import { ReplaceableComponentsService } from '../services/replaceable-components.service';
import { RouterEvents } from '../services/router-events.service';
import { RoutesService } from '../services/routes.service'; import { RoutesService } from '../services/routes.service';
import { SubscriptionService } from '../services/subscription.service'; import { SubscriptionService } from '../services/subscription.service';
import { findRoute, getRoutePath } from '../utils/route-utils'; import { findRoute, getRoutePath } from '../utils/route-utils';
@ -44,6 +44,7 @@ export class DynamicLayoutComponent {
private localizationService: LocalizationService, private localizationService: LocalizationService,
private replaceableComponents: ReplaceableComponentsService, private replaceableComponents: ReplaceableComponentsService,
private subscription: SubscriptionService, private subscription: SubscriptionService,
private routerEvents: RouterEvents,
@Optional() @SkipSelf() dynamicLayoutComponent: DynamicLayoutComponent, @Optional() @SkipSelf() dynamicLayoutComponent: DynamicLayoutComponent,
) { ) {
if (dynamicLayoutComponent) return; if (dynamicLayoutComponent) return;
@ -52,16 +53,16 @@ export class DynamicLayoutComponent {
this.routes = injector.get(RoutesService); this.routes = injector.get(RoutesService);
this.getLayout(); this.getLayout();
this.subscription.addOne( this.checkLayoutOnNavigationEnd();
this.router.events.pipe(filter(event => event instanceof NavigationEnd)),
() => {
this.getLayout();
},
);
this.listenToLanguageChange(); this.listenToLanguageChange();
} }
private checkLayoutOnNavigationEnd() {
const navigationEnd$ = this.routerEvents.getNavigationEvents('End');
this.subscription.addOne(navigationEnd$, () => this.getLayout());
}
private getLayout() { private getLayout() {
let expectedLayout = (this.route.snapshot.data || {}).layout; let expectedLayout = (this.route.snapshot.data || {}).layout;

17
npm/ng-packs/packages/theme-shared/src/lib/components/breadcrumb/breadcrumb.component.ts

@ -1,7 +1,14 @@
import { ABP, getRoutePath, RoutesService, TreeNode, SubscriptionService } from '@abp/ng.core'; import {
ABP,
getRoutePath,
NavigationEvents,
RoutesService,
SubscriptionService,
TreeNode,
} from '@abp/ng.core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router'; import { Router } from '@angular/router';
import { filter, map, startWith } from 'rxjs/operators'; import { map, startWith } from 'rxjs/operators';
import { eThemeSharedRouteNames } from '../../enums'; import { eThemeSharedRouteNames } from '../../enums';
@Component({ @Component({
@ -18,12 +25,12 @@ export class BreadcrumbComponent implements OnInit {
private router: Router, private router: Router,
private routes: RoutesService, private routes: RoutesService,
private subscription: SubscriptionService, private subscription: SubscriptionService,
private navigationEvents: NavigationEvents,
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.subscription.addOne( this.subscription.addOne(
this.router.events.pipe( this.navigationEvents.getOneOf('End').pipe(
filter<NavigationEnd>(event => event instanceof NavigationEnd),
// tslint:disable-next-line:deprecation // tslint:disable-next-line:deprecation
startWith(null), startWith(null),
map(() => this.routes.search({ path: getRoutePath(this.router) })), map(() => this.routes.search({ path: getRoutePath(this.router) })),

22
npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

@ -1,4 +1,4 @@
import { AuthService, LocalizationParam, RestOccurError } from '@abp/ng.core'; import { AuthService, LocalizationParam, RestOccurError, RouterEvents } from '@abp/ng.core';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { import {
ApplicationRef, ApplicationRef,
@ -10,7 +10,7 @@ import {
Injector, Injector,
RendererFactory2, RendererFactory2,
} from '@angular/core'; } from '@angular/core';
import { NavigationError, ResolveEnd, Router } from '@angular/router'; import { NavigationError, ResolveEnd } from '@angular/router';
import { Actions, ofActionSuccessful } from '@ngxs/store'; import { Actions, ofActionSuccessful } from '@ngxs/store';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { filter, map } from 'rxjs/operators'; import { filter, map } from 'rxjs/operators';
@ -72,7 +72,7 @@ export class ErrorHandler {
constructor( constructor(
private actions: Actions, private actions: Actions,
private router: Router, private routerEvents: RouterEvents,
private confirmationService: ConfirmationService, private confirmationService: ConfirmationService,
private cfRes: ComponentFactoryResolver, private cfRes: ComponentFactoryResolver,
private rendererFactory: RendererFactory2, private rendererFactory: RendererFactory2,
@ -85,20 +85,16 @@ export class ErrorHandler {
} }
private listenToRouterError() { private listenToRouterError() {
this.router.events this.routerEvents
.pipe( .getNavigationEvents('Error')
filter(event => event instanceof NavigationError), .pipe(filter(this.filterRouteErrors))
filter(this.filterRouteErrors),
)
.subscribe(() => this.show404Page()); .subscribe(() => this.show404Page());
} }
private listenToRouterDataResolved() { private listenToRouterDataResolved() {
this.router.events this.routerEvents
.pipe( .getEvents(ResolveEnd)
filter(event => event instanceof ResolveEnd), .pipe(filter(() => !!this.componentRef))
filter(() => !!this.componentRef),
)
.subscribe(() => { .subscribe(() => {
this.componentRef.destroy(); this.componentRef.destroy();
this.componentRef = null; this.componentRef = null;

Loading…
Cancel
Save