@c8y/ngx-components
Version:
Angular modules for Cumulocity IoT applications
1 lines • 6.72 kB
Source Map (JSON)
{"version":3,"file":"c8y-ngx-components-query-param-modal-handler.mjs","sources":["../../query-param-modal-handler/query-param-modal-handler.service.ts","../../query-param-modal-handler/c8y-ngx-components-query-param-modal-handler.ts"],"sourcesContent":["import { inject, Injectable } from '@angular/core';\nimport { ActivatedRoute, ParamMap, Router } from '@angular/router';\nimport { filter, tap } from 'rxjs/operators';\nimport { BsModalService } from 'ngx-bootstrap/modal';\nimport { ModalOptions } from 'ngx-bootstrap/modal/modal-options.class';\nimport { ComponentType } from '@angular/cdk/overlay';\nimport { Observable, Subscription } from 'rxjs';\n\n/**\n * Interface representing the configuration for handling query parameter-based modals.\n */\nexport interface QueryParamModalConfig {\n /**\n * The name of the query parameter to observe.\n */\n queryParam: string;\n\n /**\n * The component to display in the modal.\n */\n component: ComponentType<any>;\n\n /**\n * Optional configuration settings for the modal.\n */\n modalConfig?: ModalOptions;\n}\n\n/**\n * Service to manage the display of modals based on URL query parameter.\n *\n * This service listens for specific query parameters in the URL and opens corresponding modals when detected.\n * It automatically cleans up the query parameters from the URL when the modal is closed or query parameter value passed as 'false'.\n */\n@Injectable({\n providedIn: 'root'\n})\nexport class QueryParamModalHandlerService {\n private readonly activatedRoute = inject(ActivatedRoute);\n private readonly bsModalService = inject(BsModalService);\n private readonly router = inject(Router);\n\n /**\n * Subscription to track modal close events.\n */\n private modalCloseSubscription: Subscription | null = null;\n\n /**\n * Handles the display of a modal based on a specific query parameter.\n *\n * @param config - Configuration object specifying the query parameter, component, and optional modal settings.\n * @returns An observable that listens for changes in the query parameter.\n */\n handleQueryParamModal(config: QueryParamModalConfig): Observable<ParamMap> {\n return this.activatedRoute.queryParamMap.pipe(\n filter(params => params.has(config.queryParam)),\n tap(params => {\n const paramValue = this.parseQueryParam(params.get(config.queryParam));\n\n if (paramValue) {\n this.showModal(config.component, config.modalConfig);\n\n this.modalCloseSubscription = this.bsModalService.onHidden.subscribe(() => {\n this.removeQueryParam(config.queryParam);\n this.cleanup();\n });\n } else {\n this.removeQueryParam(config.queryParam);\n }\n })\n );\n }\n\n /**\n * Removes the specified query parameter from the URL.\n *\n * @param paramKey - The key of the query parameter to remove.\n */\n private removeQueryParam(paramKey: string): void {\n const queryParams = {\n [paramKey]: null\n };\n\n this.router.navigate([], {\n relativeTo: this.activatedRoute,\n queryParams,\n queryParamsHandling: 'merge'\n });\n }\n\n /**\n * Displays a modal with the specified component and configuration.\n *\n * @param component - The component to display in the modal.\n * @param modalConfig - Optional settings for the modal appearance and behavior.\n */\n private showModal(component: ComponentType<any>, modalConfig?: any): void {\n this.bsModalService.show(component, modalConfig);\n }\n\n /**\n * Cleans up subscriptions related to modal events.\n * Ensures no memory leaks by unsubscribing from modal close events.\n */\n private cleanup(): void {\n if (this.modalCloseSubscription) {\n this.modalCloseSubscription.unsubscribe();\n this.modalCloseSubscription = null;\n }\n }\n\n /**\n * Parses a string representation of a query parameter into a boolean or null.\n *\n * Converts the provided `queryParam` string into a boolean value or null based on its content:\n * - Returns `true` if `queryParam` is 'true'.\n * - Returns `false` if `queryParam` is 'false'.\n * - Returns `null` if `queryParam` is neither 'true' nor 'false'.\n *\n * @param queryParam - The string representation of the query parameter.\n * @returns A boolean value corresponding to the parsed state, or null if the input is not 'true' or 'false'.\n */\n private parseQueryParam(queryParam: string): boolean | null {\n if (queryParam === 'false') {\n return false;\n } else if (queryParam === 'true') {\n return true;\n } else {\n return null;\n }\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AA4BA;;;;;AAKG;MAIU,6BAA6B,CAAA;AAH1C,IAAA,WAAA,GAAA;AAImB,QAAA,IAAA,CAAA,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;AACvC,QAAA,IAAA,CAAA,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;AACvC,QAAA,IAAA,CAAA,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;A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