@studiohyperdrive/ngx-gov-flanders
Version:
An integrated Angular approach to common use-cases for applications for the Flemish Government.
1 lines • 13 kB
Source Map (JSON)
{"version":3,"file":"studiohyperdrive-ngx-gov-flanders.mjs","sources":["../../../../../../libs/angular/gov/flanders/src/lib/services/acm-component-injection.service.ts","../../../../../../libs/angular/gov/flanders/src/studiohyperdrive-ngx-gov-flanders.ts"],"sourcesContent":["import {\n\tinject,\n\tInjectable,\n\tRenderer2,\n\tRendererFactory2,\n\tSignal,\n\tsignal,\n\tWritableSignal,\n} from '@angular/core';\nimport { GlobalFooterClient } from '@govflanders/vl-widget-global-footer-types';\nimport { GlobalHeaderClient } from '@govflanders/vl-widget-global-header-types';\nimport { NgxWindowService } from '@studiohyperdrive/ngx-core';\nimport { first, from, fromEvent, Observable, of, retry, switchMap, tap, throwError } from 'rxjs';\nimport { map } from 'rxjs/operators';\nimport {\n\tNgxAcmComponentConfiguration,\n\tNgxAcmComponentInjectionConfiguration,\n\tNgxAcmHeaderComponentConfiguration,\n} from '../types';\n\n/**\n * The `NgxAcmComponentInjectionService` is a service that provides methods to inject either a GlobalHeader or a GlobalFooter by ACM to your application.\n */\n@Injectable({\n\tprovidedIn: 'root',\n})\nexport class NgxAcmComponentInjectionService {\n\tpublic globalHeaderClient: GlobalHeaderClient;\n\tprivate readonly _globalHeaderClientInitialized: WritableSignal<boolean> =\n\t\tsignal<boolean>(false);\n\tpublic readonly globalHeaderClientInitialized: Signal<boolean> =\n\t\tthis._globalHeaderClientInitialized.asReadonly();\n\n\tpublic globalFooterClient: GlobalFooterClient;\n\tprivate readonly _globalFooterClientInitialized: WritableSignal<boolean> =\n\t\tsignal<boolean>(false);\n\tpublic readonly globalFooterClientInitialized: Signal<boolean> =\n\t\tthis._globalFooterClientInitialized.asReadonly();\n\t/**\n\t * An instance of the RendererFactory2\n\t */\n\tprivate rendererFactory: RendererFactory2 = inject(RendererFactory2);\n\t/**\n\t * An instance of the Renderer2\n\t */\n\tprivate renderer: Renderer2 = this.rendererFactory.createRenderer(null, null);\n\t/**\n\t * An instance of the NgxWindowService\n\t */\n\tprivate windowService: NgxWindowService = inject(NgxWindowService);\n\n\t/**\n\t * Initializes the VO Global Header widget and sets optional applicationMenuLinks.\n\t *\n\t * @param selector - The selector of the element where the header widget should be mounted.\n\t * @param configuration - The configuration object\n\t * @param configuration.url - The URL of the widget (e.g. 'https://<environment>.widgets.burgerprofiel.vlaanderen.be/')\n\t * @param configuration.id - The ID of the widget (e.g. '7502b557-5d49-4ab3-9995-168718b81be5')\n\t * @param configuration.profile - The profile configuration object\n\t * @param links - Optional application menu links\n\t *\n\t * @returns The GlobalHeaderClient object will be returned to allow for custom implementations that are not supported out of the box, see: https://test.widgets.burgerprofiel.dev-vlaanderen.be/docs/global-header/ for more information.\n\t */\n\tpublic injectGlobalHeaderComponent({\n\t\tselector,\n\t\tconfiguration,\n\t\tlinks,\n\t}: NgxAcmComponentInjectionConfiguration<NgxAcmHeaderComponentConfiguration>): Observable<GlobalHeaderClient> {\n\t\t// Denis: The following code should only run in the browser.\n\t\treturn this.windowService.runInBrowser<Observable<GlobalHeaderClient>>(\n\t\t\t({ browserWindow, browserDocument }) => {\n\t\t\t\t// Denis: Create a new script tag\n\t\t\t\treturn this.injectComponent(\n\t\t\t\t\tbrowserDocument,\n\t\t\t\t\tselector,\n\t\t\t\t\tconfiguration,\n\t\t\t\t\t'GlobalHeaderComponent'\n\t\t\t\t).pipe(\n\t\t\t\t\tswitchMap((headerElement: HTMLElement) => {\n\t\t\t\t\t\t// Denis: Get the GlobalHeaderClient object\n\t\t\t\t\t\tconst headerClient: GlobalHeaderClient = browserWindow.globalHeaderClient;\n\n\t\t\t\t\t\treturn from(headerClient.mount(headerElement));\n\t\t\t\t\t}),\n\t\t\t\t\tswitchMap(() => {\n\t\t\t\t\t\t// Denis: If there are no links, fallback to the default links\n\t\t\t\t\t\tif (!Array.isArray(links) || !links.length) {\n\t\t\t\t\t\t\treturn of(null);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Denis: Get the GlobalHeaderClient object\n\t\t\t\t\t\tconst headerClient: GlobalHeaderClient = browserWindow.globalHeaderClient;\n\n\t\t\t\t\t\t// Denis: Set the optional application menu links\n\t\t\t\t\t\treturn from(headerClient.accessMenu.setApplicationMenuLinks(links));\n\t\t\t\t\t}),\n\t\t\t\t\tswitchMap(() => {\n\t\t\t\t\t\t// Denis: Get the GlobalHeaderClient object\n\t\t\t\t\t\tconst headerClient: GlobalHeaderClient = browserWindow.globalHeaderClient;\n\n\t\t\t\t\t\t// Denis: Set the provided ProfileConfig\n\t\t\t\t\t\treturn headerClient.accessMenu.setProfile(configuration.profile);\n\t\t\t\t\t}),\n\t\t\t\t\tmap(() => browserWindow.globalHeaderClient),\n\t\t\t\t\ttap((client: GlobalHeaderClient) => {\n\t\t\t\t\t\tif (client) {\n\t\t\t\t\t\t\tthis.globalHeaderClient = client;\n\n\t\t\t\t\t\t\tthis._globalHeaderClientInitialized.set(true);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t}\n\n\t/**\n\t * Initializes the VO Global Footer widget and sets optional navigation links.\n\t *\n\t * @param selector - The selector of the element where the footer widget should be mounted.\n\t * @param configuration - The configuration object\n\t * @param configuration.url - The URL of the widget (e.g. 'https://<environment>.widgets.burgerprofiel.vlaanderen.be/')\n\t * @param configuration.id - The ID of the widget (e.g. '7502b557-5d49-4ab3-9995-168718b81be5')\n\t * @param links - Optional navigation links\n\t *\n\t * @returns The GlobalFooterClient object will be returned to allow for custom implementations that are not supported out of the box, see: https://test.widgets.burgerprofiel.dev-vlaanderen.be/docs/global-footer/ for more information.\n\t */\n\tpublic injectGlobalFooterComponent({\n\t\tselector,\n\t\tconfiguration,\n\t\tlinks,\n\t}: NgxAcmComponentInjectionConfiguration): Observable<GlobalFooterClient> {\n\t\t// Denis: The following code should only run in the browser.\n\t\treturn this.windowService.runInBrowser<Observable<GlobalFooterClient>>(\n\t\t\t({ browserWindow, browserDocument }) => {\n\t\t\t\t// Denis: Create a new script tag\n\t\t\t\treturn this.injectComponent(\n\t\t\t\t\tbrowserDocument,\n\t\t\t\t\tselector,\n\t\t\t\t\tconfiguration,\n\t\t\t\t\t'GlobalFooterComponent'\n\t\t\t\t).pipe(\n\t\t\t\t\tswitchMap((footerElement: HTMLElement) => {\n\t\t\t\t\t\t// Denis: When the script is loaded, get the GlobalHeaderClient object\n\t\t\t\t\t\tconst footerClient: GlobalFooterClient = browserWindow.globalFooterClient;\n\n\t\t\t\t\t\treturn from(footerClient.mount(footerElement));\n\t\t\t\t\t}),\n\t\t\t\t\tswitchMap(() => {\n\t\t\t\t\t\t// Denis: If there are no links, fallback to the default links\n\t\t\t\t\t\tif (!Array.isArray(links) || !links.length) {\n\t\t\t\t\t\t\treturn of(null);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Denis: Get the GlobalHeaderClient object\n\t\t\t\t\t\tconst footerClient: GlobalFooterClient = browserWindow.globalFooterClient;\n\n\t\t\t\t\t\t// Denis: Set the provided navigation links\n\t\t\t\t\t\treturn from(footerClient.setNavigationLinks(links));\n\t\t\t\t\t}),\n\t\t\t\t\tmap(() => browserWindow.globalFooterClient),\n\t\t\t\t\ttap((client: GlobalFooterClient) => {\n\t\t\t\t\t\tif (client) {\n\t\t\t\t\t\t\tthis.globalFooterClient = client;\n\n\t\t\t\t\t\t\tthis._globalFooterClientInitialized.set(true);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t}\n\n\t/**\n\t * Creates a script tag for the ACMComponent and appends it to the browser\n\t *\n\t * @private\n\t * @param browserDocument - An instance of the browser document\n\t * @param configuration - The configuration of the ACM component\n\t * @param selector - The selector of the component\n\t * @param component - The name of the component we wish to append\n\t */\n\tprivate injectComponent(\n\t\tbrowserDocument: Document,\n\t\tselector: string,\n\t\tconfiguration: NgxAcmComponentConfiguration,\n\t\tcomponent: string\n\t): Observable<HTMLElement> {\n\t\t// Denis: Create a new script tag\n\t\tconst script = this.renderer.createElement('script');\n\n\t\t// Denis: Set up the script to load the header widget\n\t\tscript.src = configuration.url + 'api/v2/widget/' + configuration.id + '/entry';\n\t\tscript.type = 'text/javascript';\n\n\t\t// Denis: Append the script to the DOM to load it.\n\t\tthis.renderer.appendChild(browserDocument.head, script);\n\n\t\treturn fromEvent(script, 'load').pipe(\n\t\t\tfirst(),\n\t\t\tswitchMap(() => {\n\t\t\t\t// Denis: Mount the provided widget script\n\t\t\t\tconst element: HTMLElement = browserDocument.querySelector(selector);\n\n\t\t\t\t// Iben: If no element was found, we throw an error\n\t\t\t\tif (typeof element === 'undefined' || element === null) {\n\t\t\t\t\treturn throwError(\n\t\t\t\t\t\t() =>\n\t\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t\t`NGXGovFlanders: An error occurred when trying to mount the ${component}, the provided targetElement was not found.`\n\t\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// Iben: Return the element\n\t\t\t\treturn of(element);\n\t\t\t}),\n\t\t\tretry({\n\t\t\t\tcount: 5,\n\t\t\t\tdelay: 100,\n\t\t\t})\n\t\t);\n\t}\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAoBA;;AAEG;MAIU,+BAA+B,CAAA;AAH5C,IAAA,WAAA,GAAA;AAKkB,QAAA,IAAA,CAAA,8BAA8B,GAC9C,MAAM,CAAU,KAAK,0EAAC;AACP,QAAA,IAAA,CAAA,6BAA6B,GAC5C,IAAI,CAAC,8BAA8B,CAAC,UAAU,EAAE;AAGhC,QAAA,IAAA,CAAA,8BAA8B,GAC9C,MAAM,CAAU,KAAK,0EAAC;AACP,QAAA,IAAA,CAAA,6BAA6B,GAC5C,IAAI,CAAC,8BAA8B,CAAC,UAAU,EAAE;AACjD;;AAEG;AACK,QAAA,IAAA,CAAA,eAAe,GAAqB,MAAM,CAAC,gBAAgB,CAAC;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