ervu-business-metrics/frontend/src/ts/mfe-app-tools.ts
Булат Хайруллин e42c97d285 mfe version
2024-10-24 18:22:18 +03:00

74 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {MfeConfigurationProvider} from "./modules/mfe/provider/mfe-configuration.provider";
import {NgModuleRef} from "@angular/core";
let childEventHandlerFromContainer = null;
export type ChildEventType = 'navigate' | 'token-request'
export type ParentEventType = 'navigate';
export function fireMfeEventToContainer(eventType: ChildEventType, eventData: any): Promise<any> {
if (typeof childEventHandlerFromContainer === 'function') {
return childEventHandlerFromContainer(eventType, eventData);
}
else {
throw new Error(
'Event fired from child MFE to container before being bootstrapped as MFE App',
);
}
}
export function bootstrapMfeApp(createApp: () => Promise<NgModuleRef<unknown> | void>) {
function mount(
element: HTMLElement,
settings: {
standalone: boolean,
useShadowDom?: boolean,
containerBaseUrl?: string,
componentBaseUrl?: string,
startUrl: string,
childEventHandler?: any,
params?: any
} = {
standalone: false,
startUrl: '/mfe/dashboard'
},
) {
let containerBaseUrl = settings.containerBaseUrl || ''; // префикс ресурса
let startUrl = settings.startUrl || ''; // ресурс хост-приложения
MfeConfigurationProvider.setPageBaseUrl(joinPath(containerBaseUrl, startUrl));
element.appendChild(createContainerForBootstrap())
childEventHandlerFromContainer = settings.childEventHandler;
createApp();
return {
parentEventHandler(eventType: ParentEventType, url: string) {
},
unmount() {
console.log("Unmounting dashboard application");
platformBrowserDynamic().destroy();
},
}
}
return {
mount,
};
}
function createContainerForBootstrap(): HTMLElement {
let mfeBootstrapContainer = document.createElement('div');
mfeBootstrapContainer.setAttribute(MfeConfigurationProvider.BASE_COMPONENT_ATTRIBUTE, '');
return mfeBootstrapContainer;
}
export function joinPath(...paths: string[]): string {
return '/' + paths
.filter(path => path)
.map(path => path.endsWith('/') ? path.substring(0, path.length - 1) : path)
.map(path => path.startsWith('/') ? path.substring(1, path.length) : path)
.join('/')
}