Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 3x 3x 1x 1x 2x 3x 34x 3x 8x 3x 42x 42x 3x 41x 41x 41x 39x 39x 39x 39x 39x 2x 2x 2x 2x 3x 3x 42x 42x 1x 1x 41x 41x 41x 3x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import {
ComponentRef,
createNgModule,
EnvironmentInjector,
Injector,
ViewContainerRef
} from '@angular/core';
import { Observable, ReplaySubject, Subject, throwError } from 'rxjs';
import { SiWebComponentEditorWrapperComponent } from './components/web-component-wrapper/si-web-component-editor-wrapper.component';
import { SiWebComponentWrapperComponent } from './components/web-component-wrapper/si-web-component-wrapper.component';
import {
FederatedBridgeModule,
FederatedModule,
WebComponent,
WidgetComponentFactory,
WidgetComponentTypeFactory,
WidgetInstance,
WidgetInstanceEditor
} from './model/widgets.model';
type WidgetFactoryType = FederatedModule['factoryType'] | FederatedBridgeModule['factoryType'];
export type SetupComponentFn = <T>(
factory: WidgetComponentFactory,
componentName: string,
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector
) => Observable<ComponentRef<T>>;
/** @internal */
export const widgetFactoryRegistry = {
_factories: {} as { [key in WidgetFactoryType]?: SetupComponentFn },
register(name: WidgetFactoryType, factoryFn: SetupComponentFn) {
this._factories[name] = factoryFn;
},
getFactoryFn(name: WidgetFactoryType) {
return this._factories[name];
},
hasFactoryFn(name: WidgetFactoryType) {
return this._factories[name] !== undefined;
}
};
export const setupWidgetInstance = (
widgetComponentFactory: WidgetComponentFactory,
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector
): Observable<ComponentRef<WidgetInstance>> =>
setupComponent(
widgetComponentFactory,
host,
injector,
envInjector,
'componentName'
) as Observable<ComponentRef<WidgetInstance>>;
export const setupWidgetEditor = (
widgetComponentFactory: WidgetComponentFactory,
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector
): Observable<ComponentRef<WidgetInstanceEditor>> =>
setupComponent(
widgetComponentFactory,
host,
injector,
envInjector,
'editorComponentName'
) as Observable<ComponentRef<WidgetInstanceEditor>>;
const setupComponent = (
widgetComponentFactory: WidgetComponentFactory,
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector,
componentName: 'componentName' | 'editorComponentName'
): Observable<ComponentRef<WidgetInstance>> | Observable<ComponentRef<WidgetInstanceEditor>> => {
if (!widgetComponentFactory.factoryType || widgetComponentFactory.factoryType === 'default') {
return loadAndAttachComponent<WidgetInstance>(
widgetComponentFactory as WidgetComponentTypeFactory,
componentName,
host,
injector,
envInjector
);
E} else if (widgetComponentFactory.factoryType === 'web-component') {
return loadAndAttachWebComponentWrapper(widgetComponentFactory, componentName, host);
} else if (widgetFactoryRegistry.hasFactoryFn(widgetComponentFactory.factoryType)) {
const setupFn = widgetFactoryRegistry.getFactoryFn(widgetComponentFactory.factoryType);
return setupFn!<WidgetInstance>(
widgetComponentFactory as any,
componentName,
host,
injector,
envInjector
);
} else {
return throwError(
() => new Error(`Unknown widget factory type ${widgetComponentFactory.factoryType}.`)
);
}
};
const loadModuleBasedComponent = <T>(
factory: WidgetComponentTypeFactory,
componentName: 'componentName' | 'editorComponentName',
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector,
result: Subject<ComponentRef<T>>
): void => {
const loader = factory.moduleLoader!;
const componentKey = factory[componentName]!;
loader(componentKey).then(
moduleExports => {
const ngModuleRef = createNgModule(moduleExports[factory.moduleName!], envInjector);
const componentType = moduleExports[componentKey];
const widgetInstanceRef = host.createComponent<T>(componentType, { injector, ngModuleRef });
result.next(widgetInstanceRef);
result.complete();
},
rejection => {
const errorMsg = `Loading widget module ${factory.moduleName} failed`;
const msg = rejection ? `${errorMsg} with ${JSON.stringify(rejection.toString())}` : errorMsg;
result.error(msg);
result.complete();
}
);
};
const loadStandaloneComponent = <T>(
factory: WidgetComponentTypeFactory,
componentName: 'componentName' | 'editorComponentName',
host: ViewContainerRef,
injector: Injector,
result: Subject<ComponentRef<T>>
): void => {
const loader = factory.componentLoader!;
const componentKey = factory[componentName]!;
loader<T>(componentKey).then(
componentType => {
const widgetInstanceRef = host.createComponent<T>(componentType, { injector });
result.next(widgetInstanceRef);
result.complete();
},
rejection => {
const errorMsg = `Loading widget component ${componentKey} failed`;
const msg = rejection ? `${errorMsg} with ${JSON.stringify(rejection.toString())}` : errorMsg;
result.error(msg);
result.complete();
}
);
};
const loadAndAttachComponent = <T>(
factory: WidgetComponentTypeFactory,
componentName: 'componentName' | 'editorComponentName',
host: ViewContainerRef,
injector: Injector,
envInjector: EnvironmentInjector
): Observable<ComponentRef<T>> => {
const result = new Subject<ComponentRef<T>>();
if (!factory[componentName]) {
result.error(`Provided component factory has no ${componentName} component configuration`);
return result;
}
if (factory.moduleName) {
loadModuleBasedComponent(factory, componentName, host, injector, envInjector, result);
E} else if (factory.componentLoader) {
loadStandaloneComponent(factory, componentName, host, injector, result);
} else {
result.error('Invalid factory configuration: missing moduleLoader or componentLoader');
}
return result;
};
const loadAndAttachWebComponentWrapper = (
factory: WebComponent,
componentName: 'componentName' | 'editorComponentName',
host: ViewContainerRef
): Observable<ComponentRef<WidgetInstance>> => {
const result = new ReplaySubject<ComponentRef<any>>();
if (factory[componentName]) {
let widgetInstanceRef: ComponentRef<
SiWebComponentWrapperComponent | SiWebComponentEditorWrapperComponent
>;
if (componentName === 'componentName') {
widgetInstanceRef = host.createComponent(SiWebComponentWrapperComponent);
} else {
widgetInstanceRef = host.createComponent(SiWebComponentEditorWrapperComponent);
}
widgetInstanceRef.setInput('elementTagName', factory[componentName]);
widgetInstanceRef.setInput('url', factory.url);
result.next(widgetInstanceRef);
result.complete();
} else {
result.error(`Provided component factory has no ${componentName} component configuration`);
}
return result;
};
|