piral-ng
Version:
Plugin for integrating Angular components in Piral.
77 lines • 2.98 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import { Inject, Injectable, NgZone, Optional } from '@angular/core';
import { Router } from '@angular/router';
import { BrowserPlatformLocation as ɵBrowserPlatformLocation } from '@angular/common';
const noop = function () { };
// deactivates the usual platform behavior; all these operations are performed via the RoutingService
// to avoid any conflict, e.g., double-booking URL changes in React and Angular
ɵBrowserPlatformLocation.prototype.pushState = noop;
ɵBrowserPlatformLocation.prototype.replaceState = noop;
ɵBrowserPlatformLocation.prototype.forward = noop;
ɵBrowserPlatformLocation.prototype.back = noop;
ɵBrowserPlatformLocation.prototype.historyGo = noop;
function normalize(url) {
const search = url.indexOf('?');
const hash = url.indexOf('#');
if (search !== -1 || hash !== -1) {
if (search === -1) {
return url.substring(0, hash);
}
else if (hash === -1) {
return url.substring(0, search);
}
else {
return url.substring(0, Math.min(search, hash));
}
}
return url;
}
let CoreRoutingService = class CoreRoutingService {
constructor(context, router, zone) {
this.context = context;
this.router = router;
this.zone = zone;
if (this.router) {
this.router.errorHandler = (error) => {
// Match in development and production
if (error.message.match('Cannot match any routes') || error.message.match('NG04002')) {
// ignore this special error
return undefined;
}
throw error;
};
const nav = this.context.navigation;
const queueNavigation = (url) => {
window.requestAnimationFrame(() => nav.push(url));
};
this.dispose = nav.listen(({ location }) => {
const path = location.pathname;
const url = `${path}${location.search}${location.hash}`;
this.zone.run(() => this.router.navigateByUrl(url));
});
this.subscription = this.router.events.subscribe((e) => {
if (e.type === 1) {
const routerUrl = normalize(e.urlAfterRedirects);
const locationUrl = nav.url;
if (routerUrl !== locationUrl) {
queueNavigation(e.urlAfterRedirects);
}
}
});
}
}
ngOnDestroy() {
this.dispose?.();
this.subscription?.unsubscribe();
}
};
CoreRoutingService = __decorate([
Injectable(),
__param(0, Inject('Context')),
__param(1, Optional()),
__param(2, Optional()),
__metadata("design:paramtypes", [Object, Router,
NgZone])
], CoreRoutingService);
export { CoreRoutingService };
//# sourceMappingURL=CoreRoutingService.js.map