UNPKG

piral-ng

Version:

Plugin for integrating Angular components in Piral.

77 lines 3.16 kB
import { APP_BASE_HREF } from '@angular/common'; import { createApplication } from '@angular/platform-browser'; import { ɵresetCompiledComponents as reset, } from '@angular/core'; import { CoreRoutingService } from './CoreRoutingService'; import { contextName, piralName } from './constants'; import { CONTEXT, PIRAL } from './injection'; function isLazyLoader(thing) { return typeof thing === 'function' && thing.hasOwnProperty('prototype') && thing.hasOwnProperty('arguments'); } export * from './injection'; export function createConverter(options) { const update = (ref, props) => { if (ref) { const ct = ref.componentType; if (ct?.ɵcmp?.inputs?.Props) { ref.setInput('Props', props); } } }; let app = undefined; return (component) => ({ type: 'html', component: { mount(element, props, ctx, locals) { if (!app) { const { piral } = props; app = createApplication({ ...options, providers: [ ...options.providers, CoreRoutingService, { provide: APP_BASE_HREF, useValue: ctx.publicPath }, { provide: contextName, useValue: ctx }, { provide: CONTEXT, useValue: ctx }, { provide: piralName, useValue: piral }, { provide: PIRAL, useValue: piral }, ], }); piral.on('unload-pilet', (ev) => { if (ev.name === piral.meta.name && typeof reset === 'function') { // pretty much a cleanup step for Angular. reset(); } }); } locals.active = true; app .then((appRef) => { if (isLazyLoader(component)) { const lazyComponent = component(); return lazyComponent.then((componentExports) => [appRef, componentExports.default]); } return [appRef, component]; }) .then(([appRef, component]) => { if (locals.active) { const ref = appRef.bootstrap(component, element); // Start the routing service. appRef.injector.get(CoreRoutingService); update(ref, props); locals.component = ref; } }); }, update(_1, props, _2, locals) { update(locals.component, props); }, unmount(element, locals) { locals.active = false; locals.component?.destroy(); locals.component = undefined; element.remove(); }, }, }); } //# sourceMappingURL=standalone.js.map