forked from myBadges-org/badgr-ui
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate-webcomponent.ts
More file actions
76 lines (67 loc) · 2.9 KB
/
create-webcomponent.ts
File metadata and controls
76 lines (67 loc) · 2.9 KB
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
import { ApplicationConfig, enableProdMode, Injectable, Type } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { createApplication } from '@angular/platform-browser';
import {
ActivatedRoute,
ActivatedRouteSnapshot,
convertToParamMap,
Event,
NavigationBehaviorOptions,
NavigationEnd,
NavigationExtras,
Router,
UrlTree,
} from '@angular/router';
import { Observable, Subject } from 'rxjs';
import { LanguageService, lngs } from '~/common/services/language.service';
/**
* Creates a web component from the given details and registers it with the browser.
* @param component The Angular component to be wrapped into a web component, e.g. `VersionComponent`
* @param tagName The tag name under which the web component will be usable, e.g. `oeb-version` (used for customElements.define)
* @param options ApplicationConfig passed to the web component, used to setup providers and such
* @returns A promise for creating and registering the web component with the browser
*/
export const createWebcomponent = (component: Type<unknown>, tagName: string, options: ApplicationConfig) => {
enableProdMode();
return createApplication(options)
.then((app) => {
const elem = createCustomElement(component, { injector: app.injector });
customElements.define(tagName, elem);
})
.catch((err) => console.error(`Error bootstrapping custom element ${tagName}: ${err}`));
};
/**
* A reusable configuration method to make a web component aware of the language setting
* that is set in a configuration object of the embedding window.
* @param lang The language service configured in the app initializer
*/
export const useWebComponentLanguageSetting = (lang: LanguageService) => {
if (!window || !window['OEBWebComponentSettings'] || !window['OEBWebComponentSettings'].language) return;
const configuredLanguage = window['OEBWebComponentSettings'].language as string;
if (lngs.indexOf(configuredLanguage) >= 0) lang.setLanguage(configuredLanguage);
else lang.setInitialAppLanguage();
};
@Injectable()
export class WebComponentRouter extends Router {
private readonly _eventsSubject = new Subject<Event>();
override get events(): Observable<Event> {
return this._eventsSubject;
}
navigate(commands: any[], extras?: NavigationExtras): Promise<boolean> {
const urlTree = super.createUrlTree(commands, extras);
this._eventsSubject.next(new NavigationEnd(Math.random(), urlTree.toString(), urlTree.toString()));
return Promise.resolve(true);
}
navigateByUrl(url: string | UrlTree, extras?: NavigationBehaviorOptions): Promise<boolean> {
this._eventsSubject.next(new NavigationEnd(Math.random(), url.toString(), url.toString()));
return Promise.resolve(true);
}
}
@Injectable()
export class WebComponentActivatedRoute extends ActivatedRoute {
override snapshot: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
constructor() {
super();
this.snapshot.params = convertToParamMap({});
}
}