@skyux/router
Version:
This library was generated with [Nx](https://nx.dev).
381 lines (371 loc) • 17.7 kB
JavaScript
import * as i0 from '@angular/core';
import { Input, Optional, Directive, NgModule, Injectable, EventEmitter, inject, Renderer2, ElementRef, ApplicationRef, ChangeDetectorRef, DestroyRef, HostListener, Output } from '@angular/core';
import * as i2 from '@angular/common';
import { PathLocationStrategy } from '@angular/common';
import * as i1 from '@angular/router';
import { RouterLink, Router } from '@angular/router';
import * as i3 from '@skyux/core';
import * as i4 from '@skyux/config';
import { SkyAppConfig, SkyAppRuntimeConfigParamsProvider } from '@skyux/config';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { from, catchError, of, finalize } from 'rxjs';
/**
* @deprecated Use `skyHref` directive instead.
*/
class SkyAppLinkExternalDirective extends RouterLink {
set skyAppLinkExternal(commands) {
this.routerLink = commands;
}
#window;
#skyAppConfig;
#paramsProvider;
constructor(router, route, renderer, elementRef, platformLocation, window, skyAppConfig, paramsProvider, hostConfig) {
super(router, route, undefined, renderer, elementRef, new PathLocationStrategy(platformLocation, hostConfig ? hostConfig.host.url : skyAppConfig?.skyux.host?.url));
this.#window = window;
this.#skyAppConfig = skyAppConfig;
this.#paramsProvider = paramsProvider;
if (this.#window.nativeWindow.window.name &&
this.#window.nativeWindow.window.name !== '') {
this.target = this.#window.nativeWindow.window.name;
}
else {
this.target = '_top';
}
}
ngOnChanges(changes) {
this.queryParams = this.#mergeQueryParams(changes['queryParams']?.currentValue);
super.ngOnChanges(changes);
}
#mergeQueryParams(queryParams) {
const skyuxParams = this.#skyAppConfig
? this.#skyAppConfig.runtime.params.getAll()
: this.#paramsProvider?.params.getAll();
return Object.assign({}, this.queryParams, queryParams, skyuxParams);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkExternalDirective, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }, { token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i2.PlatformLocation }, { token: i3.SkyAppWindowRef }, { token: i4.SkyAppConfig, optional: true }, { token: i4.SkyAppRuntimeConfigParamsProvider, optional: true }, { token: i4.SkyAppConfigHost, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.15", type: SkyAppLinkExternalDirective, isStandalone: false, selector: "[skyAppLinkExternal]", inputs: { skyAppLinkExternal: "skyAppLinkExternal" }, usesInheritance: true, usesOnChanges: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkExternalDirective, decorators: [{
type: Directive,
args: [{
selector: '[skyAppLinkExternal]',
standalone: false,
}]
}], ctorParameters: () => [{ type: i1.Router }, { type: i1.ActivatedRoute }, { type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i2.PlatformLocation }, { type: i3.SkyAppWindowRef }, { type: i4.SkyAppConfig, decorators: [{
type: Optional
}] }, { type: i4.SkyAppRuntimeConfigParamsProvider, decorators: [{
type: Optional
}] }, { type: i4.SkyAppConfigHost, decorators: [{
type: Optional
}] }], propDecorators: { skyAppLinkExternal: [{
type: Input
}] } });
class SkyAppLinkDirective extends RouterLink {
set skyAppLink(commands) {
this.routerLink = commands;
}
#skyAppConfig;
#paramsProvider;
constructor(router, route, locationStrategy, renderer, elementRef, skyAppConfig, paramsProvider) {
super(router, route, undefined, renderer, elementRef, locationStrategy);
this.#skyAppConfig = skyAppConfig;
this.#paramsProvider = paramsProvider;
}
ngOnChanges(changes) {
this.queryParams = this.#mergeQueryParams(changes['queryParams']?.currentValue);
super.ngOnChanges(changes);
}
#mergeQueryParams(queryParams) {
const skyuxParams = this.#skyAppConfig
? this.#skyAppConfig.runtime.params.getAll(true)
: this.#paramsProvider?.params.getAll(true);
return Object.assign({}, this.queryParams, queryParams, skyuxParams);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkDirective, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }, { token: i2.LocationStrategy }, { token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i4.SkyAppConfig, optional: true }, { token: i4.SkyAppRuntimeConfigParamsProvider, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.15", type: SkyAppLinkDirective, isStandalone: false, selector: "[skyAppLink]", inputs: { skyAppLink: "skyAppLink" }, usesInheritance: true, usesOnChanges: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkDirective, decorators: [{
type: Directive,
args: [{
selector: '[skyAppLink]',
standalone: false,
}]
}], ctorParameters: () => [{ type: i1.Router }, { type: i1.ActivatedRoute }, { type: i2.LocationStrategy }, { type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i4.SkyAppConfig, decorators: [{
type: Optional
}] }, { type: i4.SkyAppRuntimeConfigParamsProvider, decorators: [{
type: Optional
}] }], propDecorators: { skyAppLink: [{
type: Input
}] } });
/* istanbul ignore next */
class SkyAppLinkModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkModule, declarations: [SkyAppLinkDirective, SkyAppLinkExternalDirective], exports: [SkyAppLinkDirective, SkyAppLinkExternalDirective] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyAppLinkModule, decorators: [{
type: NgModule,
args: [{
declarations: [SkyAppLinkDirective, SkyAppLinkExternalDirective],
exports: [SkyAppLinkDirective, SkyAppLinkExternalDirective],
}]
}] });
/**
* Return the link as-is.
*/
class SkyHrefResolverService {
resolveHref(args) {
return Promise.resolve({
url: args.url,
userHasAccess: true,
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefResolverService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefResolverService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefResolverService, decorators: [{
type: Injectable
}] });
class SkyHrefDirective {
constructor() {
/**
* Emits whether the link is available (true) or not (false).
*/
this.skyHrefChange = new EventEmitter();
this.#route = false;
this.#href = '';
this.#_skyHref = '';
this.#_skyHrefElse = 'hide';
this.#router = inject(Router);
this.#renderer = inject(Renderer2);
this.#element = inject(ElementRef);
this.#skyAppConfig = inject(SkyAppConfig, { optional: true });
this.#paramsProvider = inject(SkyAppRuntimeConfigParamsProvider, {
optional: true,
});
this.#hrefResolver = inject(SkyHrefResolverService, { optional: true });
this.#applicationRef = inject(ApplicationRef);
this.#changeDetectorRef = inject(ChangeDetectorRef);
this.#destroyRef = inject(DestroyRef);
/* istanbul ignore next */
this.#getUrlTree = () => {
const href = this.#href.toLowerCase();
if (!href ||
!this.#skyAppConfig?.skyux.host?.url ||
!this.#skyAppConfig?.runtime?.app?.base) {
return false;
}
const baseUrl = (this.#skyAppConfig.skyux.host.url +
this.#skyAppConfig.runtime.app.base.substr(0, this.#skyAppConfig.runtime.app.base.length - 1)).toLowerCase();
if (href === baseUrl ||
// Make sure the base URL is not simply a partial match of the base URL plus additional
// characters after the base URL that are not "terminating" characters
href.indexOf(baseUrl + '/') === 0 ||
href.indexOf(baseUrl + '?') === 0) {
const routePath = this.#href.substring(baseUrl.length);
return this.#router.parseUrl(routePath);
}
return false;
};
}
/**
* Provide a link as a string or as an array of strings to be joined by a slash to form a complete
* URL.
*
* @param skyHref
*/
set skyHref(skyHref) {
if (typeof skyHref === 'string') {
this.#_skyHref = skyHref;
}
else {
this.#_skyHref = skyHref ? skyHref.join('/') : '';
}
this.#checkRouteAccess();
}
get skyHref() {
return this.#_skyHref;
}
/**
* A collection of query URL parameters.
*/
set queryParams(value) {
if (value !== this.#_queryParams) {
this.#_queryParams = value;
this.#applyChanges(this.#getChanges());
}
}
get queryParams() {
return this.#_queryParams;
}
/**
* Set the behavior for when the link is not available to either hide the link or display unlinked text.
*
* @param value
*/
set skyHrefElse(value) {
this.#_skyHrefElse = value;
this.#applyChanges(this.#getChanges());
}
get skyHrefElse() {
return this.#_skyHrefElse;
}
#route;
#href;
#_queryParams;
#_skyHref;
#_skyHrefElse;
#router;
#renderer;
#element;
#skyAppConfig;
#paramsProvider;
#hrefResolver;
#applicationRef;
#changeDetectorRef;
#destroyRef;
#checkingSubscription;
onClick(button, ctrlKey, shiftKey, altKey, metaKey) {
if (!this.#route || !this.#route.userHasAccess) {
return false;
}
if (button !== 0 || ctrlKey || shiftKey || altKey || metaKey) {
return true;
}
const target = this.#element.nativeElement.getAttribute('target');
if (typeof target === 'string' && target !== '_self') {
return true;
}
const urlTree = this.#getUrlTree();
if (urlTree) {
void this.#router.navigateByUrl(urlTree);
return false;
}
return true;
}
#applyChanges(change) {
this.#renderer.addClass(this.#element.nativeElement, 'sky-href');
if (change.hidden) {
this.#renderer.setAttribute(this.#element.nativeElement, 'hidden', 'hidden');
}
else {
this.#renderer.removeAttribute(this.#element.nativeElement, 'hidden');
}
if (change.href) {
this.#renderer.setAttribute(this.#element.nativeElement, 'href', change.href);
}
else {
this.#renderer.removeAttribute(this.#element.nativeElement, 'href');
}
this.skyHrefChange.emit({ userHasAccess: !change.hidden });
}
#checkRouteAccess() {
this.#route = {
url: this.skyHref,
userHasAccess: false,
};
if (this.#hrefResolver && this.skyHref) {
this.#applyChanges(this.#getChanges());
this.#checkingSubscription?.unsubscribe();
try {
this.#checkingSubscription = from(this.#hrefResolver.resolveHref({ url: this.skyHref }))
.pipe(catchError(() => of({
url: this.skyHref,
userHasAccess: false,
})), takeUntilDestroyed(this.#destroyRef), finalize(() => {
this.#applyChanges(this.#getChanges());
}))
.subscribe((route) => {
this.#route = { ...route };
this.#changeDetectorRef.markForCheck();
this.#applicationRef.tick();
});
}
catch {
// Unable to resolve.
}
}
else {
// no resolver or skyHref is falsy
this.#route.userHasAccess = !!this.skyHref;
this.#applyChanges(this.#getChanges());
}
}
#getChanges() {
if (!this.#route || !this.#route.userHasAccess) {
return {
href: '',
hidden: this.skyHrefElse === 'hide',
};
}
else {
const params =
// The SkyAppRuntimeParamsProvider is provided in root, so it will never be undefined.
// TODO: rework the injectors so that #paramsProvider is not possibly undefined.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.#skyAppConfig?.runtime.params ?? this.#paramsProvider.params;
this.#href = params.getLinkUrl(this.#route.url, {
queryParams: this.queryParams ?? {},
});
return {
href: this.#href,
hidden: false,
};
}
}
/* istanbul ignore next */
#getUrlTree;
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.15", type: SkyHrefDirective, isStandalone: false, selector: "[skyHref]", inputs: { skyHref: "skyHref", queryParams: "queryParams", skyHrefElse: "skyHrefElse" }, outputs: { skyHrefChange: "skyHrefChange" }, host: { listeners: { "click": "onClick($event.button,$event.ctrlKey,$event.shiftKey,$event.altKey,$event.metaKey)" } }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefDirective, decorators: [{
type: Directive,
args: [{
selector: '[skyHref]',
standalone: false,
}]
}], propDecorators: { skyHref: [{
type: Input
}], queryParams: [{
type: Input
}], skyHrefElse: [{
type: Input
}], skyHrefChange: [{
type: Output
}], onClick: [{
type: HostListener,
args: ['click', [
'$event.button',
'$event.ctrlKey',
'$event.shiftKey',
'$event.altKey',
'$event.metaKey',
]]
}] } });
class SkyHrefModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefModule, declarations: [SkyHrefDirective], exports: [SkyHrefDirective] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyHrefModule, decorators: [{
type: NgModule,
args: [{
declarations: [SkyHrefDirective],
exports: [SkyHrefDirective],
}]
}] });
/**
* A service for creating and retrieving recently accessed links.
*/
class SkyRecentlyAccessedService {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyRecentlyAccessedService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyRecentlyAccessedService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: SkyRecentlyAccessedService, decorators: [{
type: Injectable
}] });
/**
* Generated bundle index. Do not edit.
*/
export { SkyAppLinkModule, SkyHrefModule, SkyHrefResolverService, SkyRecentlyAccessedService, SkyHrefDirective as λ1, SkyAppLinkExternalDirective as λ2, SkyAppLinkDirective as λ3 };
//# sourceMappingURL=skyux-router.mjs.map