forked from onecx/onecx-portal-ui-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './lib/model/remote-webcomponent' | ||
export * from './lib/utils/webcomponent-bootstrap.utils' | ||
export * from './lib/utils/webcomponent-router-initializer.utils' | ||
export * from './lib/utils/webcomponent-router.utils' |
37 changes: 37 additions & 0 deletions
37
libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createCustomElement } from '@angular/elements' | ||
import { createApplication } from '@angular/platform-browser' | ||
import { EnvironmentProviders, NgZone, PlatformRef, Provider, Type, VERSION, Version, getPlatform } from '@angular/core' | ||
|
||
export async function bootstrapRemoteComponent( | ||
component: Type<any>, | ||
elementName: string, | ||
providers: (Provider | EnvironmentProviders)[] | ||
): Promise<void> { | ||
const app = await createApplication({ | ||
providers: [ | ||
(window as any)['@angular-architects/module-federation-tools'].ngZone | ||
? { | ||
provide: NgZone, | ||
useValue: (window as any)['@angular-architects/module-federation-tools'].ngZone, | ||
} | ||
: [], | ||
...providers, | ||
], | ||
}) | ||
|
||
const platform = getPlatform() | ||
let platformCache: Map<Version, PlatformRef> = (window as any)['@angular-architects/module-federation-tools'] | ||
.platformCache | ||
if (!platformCache) { | ||
platformCache = new Map<Version, PlatformRef>() | ||
;(window as any)['@angular-architects/module-federation-tools'].platformCache = platformCache | ||
} | ||
const version = VERSION | ||
platform && platformCache.set(version, platform) | ||
|
||
const myRemoteComponentAsWebComponent = createCustomElement(component, { | ||
injector: app.injector, | ||
}) | ||
|
||
customElements.define(elementName, myRemoteComponentAsWebComponent) | ||
} |
21 changes: 21 additions & 0 deletions
21
libs/angular-webcomponents/src/lib/utils/webcomponent-router-initializer.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Router } from '@angular/router' | ||
import { AppStateService } from '@onecx/portal-integration-angular' | ||
import { firstValueFrom, map } from 'rxjs' | ||
|
||
export function initializeRouter(router: Router, appStateService: AppStateService) { | ||
return () => | ||
firstValueFrom( | ||
appStateService.currentMfe$.asObservable().pipe( | ||
map((mfeInfo) => { | ||
const routes = router.config | ||
routes.forEach((route) => { | ||
route.data = { | ||
...route.data, | ||
mfeInfo: mfeInfo, | ||
} | ||
}) | ||
router.resetConfig(routes) | ||
}) | ||
) | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Route, UrlMatcher, UrlSegment, UrlSegmentGroup } from '@angular/router' | ||
|
||
export function match(prefix: string): UrlMatcher { | ||
return (url: UrlSegment[], UrlSegmentGroup: UrlSegmentGroup, route: Route) => { | ||
const mfeBaseHref: string = route?.data?.['mfeInfo']?.baseHref | ||
if (!mfeBaseHref) { | ||
console.warn('mfeInfo was not provided for route') | ||
} | ||
const baseHrefSegmentAmount = mfeBaseHref.split('/').filter((value) => value).length | ||
const urlWithoutBaseHref = url.slice(baseHrefSegmentAmount) | ||
const index = prefix === '' ? 0 : urlWithoutBaseHref.findIndex((u) => u.path === prefix) | ||
if (index >= 0) { | ||
return { consumed: url.slice(0, index + baseHrefSegmentAmount) } | ||
} | ||
return null | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters