Skip to content

Commit

Permalink
Merge pull request #44 from guillotinaweb/update-prefix
Browse files Browse the repository at this point in the history
Allow to change navigation prefix at runtime
  • Loading branch information
ebrehault authored Aug 10, 2020
2 parents f155974 + 74bab1b commit cfad4ca
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.8.0 (2020-08-10)

## New feature

- Allow to change navigation prefix at runtime [ebrehault]

# 1.7.1 (2020-07-24)

## Improvement
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ Note: with `noAutoTraverse`, we will not traverse to the new location everytime
In our app module, we will declare our prefix:

```typescript
{ provide: NAVIGATION_PREFIX, useValue: '/files' },
{ provide: NAVIGATION_PREFIX, useValue: new BehaviorSubject('/files') },
```

And in order to make sure the transition between the 2 modes work fine, we will have to do (in app.component for example):
Expand Down
2 changes: 1 addition & 1 deletion projects/traversal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-traversal",
"version": "1.7.1",
"version": "1.8.0",
"license": "MIT",
"author": {
"name": "Eric Brehault",
Expand Down
12 changes: 4 additions & 8 deletions projects/traversal/src/lib/traverser.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,27 @@ import { Location } from '@angular/common';
import { Traverser, NAVIGATION_PREFIX } from './traverser';
import { Target } from './interfaces';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { Subject, BehaviorSubject } from 'rxjs';

@Directive({
selector: 'traverser-outlet',
})
export class TraverserOutlet implements OnInit, OnDestroy {
private viewInstance: any;
private prefix: string;
private terminator: Subject<void> = new Subject();

constructor(
private traverser: Traverser,
private location: Location,
private container: ViewContainerRef,
@Optional() @Inject(NAVIGATION_PREFIX) prefix: string,
private cdr: ChangeDetectorRef
) {
this.prefix = prefix || '';
}
) {}

ngOnInit() {
this.traverser.target.pipe(takeUntil(this.terminator)).subscribe((target: Target) => this.render(target));
this.traverser.traverse(this.location.path().slice(this.prefix.length), false);
this.traverser.traverse(this.location.path().replace('/' + this.traverser.getPrefix(), ''), false);
this.location.subscribe((loc) => {
const path = (loc.url || '').slice(this.prefix.length);
const path = (loc.url || '').replace('/' + this.traverser.getPrefix(), '');
this.traverser.traverse(path || '/', false); // when empty string traverse to root
});
}
Expand Down
16 changes: 6 additions & 10 deletions projects/traversal/src/lib/traverser.link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Directive, HostBinding, Input, OnInit, Optional, Inject, HostListener } from '@angular/core';
import { Traverser, NAVIGATION_PREFIX } from './traverser';
import { Directive, HostBinding, Input, OnInit, HostListener } from '@angular/core';
import { Traverser } from './traverser';
import { Normalizer } from './normalizer';

@Directive({
Expand All @@ -24,20 +24,16 @@ export class TraverserButton {
})
export class TraverserLink extends TraverserButton implements OnInit {
@HostBinding() href?: string;
private prefix: string;

constructor(
private privateTraverser: Traverser,
private normalizer: Normalizer,
@Optional() @Inject(NAVIGATION_PREFIX) prefix: string
) {
constructor(private privateTraverser: Traverser, private normalizer: Normalizer) {
super(privateTraverser);
this.prefix = prefix || '';
}

ngOnInit() {
if (!!this.traverseTo) {
this.href = this.prefix + this.normalizer.normalize(this.privateTraverser.getFullPath(this.traverseTo));
this.href =
this.privateTraverser.getPrefix() +
this.normalizer.normalize(this.privateTraverser.getFullPath(this.traverseTo));
}
}
}
30 changes: 18 additions & 12 deletions projects/traversal/src/lib/traverser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@angular/core';
import { Location } from '@angular/common';
import { HttpParams } from '@angular/common/http';
import { BehaviorSubject, of, Observable, Subject, combineLatest } from 'rxjs';
import { BehaviorSubject, of, Observable, Subject } from 'rxjs';
import { take, withLatestFrom, map } from 'rxjs/operators';
import { Resolver } from './resolver';
import { Marker } from './marker';
Expand All @@ -20,7 +20,7 @@ import { Target, HttpParamsOptions, ModuleWithViews, ViewMapping } from './inter

export type LazyView = () => Promise<Type<any>>;

export const NAVIGATION_PREFIX = new InjectionToken<string>('traversal.prefix');
export const NAVIGATION_PREFIX = new InjectionToken<BehaviorSubject<string>>('traversal.prefix');

@Injectable({
providedIn: 'root',
Expand All @@ -37,7 +37,6 @@ export class Traverser {
private lazy: { [id: string]: LazyView } = {};
private lazyModules: { [id: string]: boolean } = {};
private tiles: { [name: string]: { [target: string]: any } } = {};
private prefix: string;

constructor(
private location: Location,
Expand All @@ -47,9 +46,8 @@ export class Traverser {
private ngResolver: ComponentFactoryResolver,
private compiler: Compiler,
private injector: Injector,
@Optional() @Inject(NAVIGATION_PREFIX) prefix: string
@Optional() @Inject(NAVIGATION_PREFIX) private prefix: BehaviorSubject<string>
) {
this.prefix = prefix || '';
this.target = new BehaviorSubject(this.getEmptyTarget());
let answers: boolean[] = [];
this.echo.subscribe((ok) => {
Expand Down Expand Up @@ -104,8 +102,8 @@ export class Traverser {
}
navigateTo = this.target.value.contextPath + navigateTo;
}
if (this.location.path() !== this.prefix + navigateTo) {
this.location.go(this.prefix + navigateTo);
if (this.location.path() !== this.getPrefix() + navigateTo) {
this.location.go(this.getPrefix() + navigateTo);
}
}
let components = this.views[view];
Expand All @@ -119,7 +117,7 @@ export class Traverser {
}

traverseHere(includeHash = true) {
const here = this.location.path().slice(this.prefix.length);
const here = this.location.path().replace('/' + this.getPrefix(), '');
this.traverse(!includeHash ? here.split('?')[0] : here);
}

Expand Down Expand Up @@ -249,9 +247,9 @@ export class Traverser {
? ({
context,
path,
prefixedPath: this.prefix + path,
prefixedPath: this.getPrefix() + path,
contextPath,
prefixedContextPath: this.prefix + contextPath,
prefixedContextPath: this.getPrefix() + contextPath,
view: viewOrTile,
component: comp,
query: new HttpParams({ fromString: queryString || '' } as HttpParamsOptions),
Expand Down Expand Up @@ -317,9 +315,9 @@ export class Traverser {
component: null,
context: {},
contextPath: '',
prefixedContextPath: this.prefix,
prefixedContextPath: this.getPrefix(),
path: '',
prefixedPath: this.prefix,
prefixedPath: this.getPrefix(),
query: new HttpParams(),
view: 'view',
};
Expand All @@ -344,4 +342,12 @@ export class Traverser {
})
);
}

getPrefix(): string {
return !!this.prefix ? this.prefix.getValue() : '';
}

setPrefix(prefix: string) {
this.prefix.next(prefix);
}
}

0 comments on commit cfad4ca

Please sign in to comment.