| 1 | /* |
| 2 | * Copyright (c) 2024 sovity GmbH |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Apache License, Version 2.0 which is available at |
| 6 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * |
| 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Contributors: |
| 11 | * sovity GmbH - initial implementation |
| 12 | */ |
| 13 | import { |
| 14 | Component, |
| 15 | EventEmitter, |
| 16 | Injector, |
| 17 | Input, |
| 18 | OnChanges, |
| 19 | OnDestroy, |
| 20 | Output, |
| 21 | } from '@angular/core'; |
| 22 | import {Subject, takeUntil} from 'rxjs'; |
| 23 | import {SlideOverService} from 'src/app/core/services/slide-over.service'; |
| 24 | import { |
| 25 | DEFAULT_SLIDE_OVER_STATE, |
| 26 | NavigationType, |
| 27 | SlideOverAction, |
| 28 | } from './slide-over.model'; |
| 29 | |
| 30 | @Component({ |
| 31 | selector: 'app-slide-over', |
| 32 | templateUrl: './slide-over.component.html', |
| 33 | }) |
| 34 | export class SlideOverComponent implements OnChanges, OnDestroy { |
| 35 | @Input() component: any; |
| 36 | |
| 37 | @Output() close = new EventEmitter(); |
| 38 | @Output() navigate = new EventEmitter(); |
| 39 | |
| 40 | state = DEFAULT_SLIDE_OVER_STATE; |
| 41 | childComponentInputId!: string; |
| 42 | dynamicComponentInjector!: Injector; |
| 43 | |
| 44 | private ngOnDestroy$ = new Subject(); |
| 45 | |
| 46 | constructor( |
| 47 | private injector: Injector, |
| 48 | private slideOverService: SlideOverService, |
| 49 | ) {} |
| 50 | |
| 51 | ngOnChanges() { |
| 52 | this.startListeningToState(); |
| 53 | } |
| 54 | |
| 55 | private startListeningToState() { |
| 56 | this.slideOverService.slideOverState$ |
| 57 | .pipe(takeUntil(this.ngOnDestroy$)) |
| 58 | .subscribe((state) => { |
| 59 | this.state = state; |
| 60 | if ( |
| 61 | this.state.config.childComponentInput.id !== |
| 62 | this.childComponentInputId |
| 63 | ) { |
| 64 | this.childComponentInputId = this.state.config.childComponentInput.id; |
| 65 | this.dynamicComponentInjector = Injector.create({ |
| 66 | parent: this.injector, |
| 67 | providers: [ |
| 68 | { |
| 69 | provide: 'childComponentInput', |
| 70 | useValue: this.state.config.childComponentInput, |
| 71 | }, |
| 72 | ], |
| 73 | }); |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Handle the slide over close |
| 80 | */ |
| 81 | onClose() { |
| 82 | this.close.emit(); |
| 83 | this.slideOverService.slideOverReset(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Handles the navigation of the slide over |
| 88 | * @param direction |
| 89 | */ |
| 90 | onNavigate(direction: string) { |
| 91 | this.navigate.emit(direction as SlideOverAction); |
| 92 | } |
| 93 | |
| 94 | onGoBack() { |
| 95 | this.slideOverService.setSlideOverViewToPreviousView(); |
| 96 | this.slideOverService.setSlideOverNavigationType(NavigationType.STEPPER); |
| 97 | } |
| 98 | |
| 99 | ngOnDestroy(): void { |
| 100 | this.ngOnDestroy$.next(null); |
| 101 | this.ngOnDestroy$.complete(); |
| 102 | } |
| 103 | } |