11import { CdkCopyToClipboard } from '@angular/cdk/clipboard' ;
22import { CommonModule } from '@angular/common' ;
3- import { Component , ComponentRef , DoCheck , NgZone , OnDestroy , OnInit , Type , ViewChild , ViewContainerRef } from '@angular/core' ;
3+ import { Component , DoCheck , NgZone , OnInit , Type } from '@angular/core' ;
44import { FormsModule , NgForm } from '@angular/forms' ;
55import { MatButtonModule } from '@angular/material/button' ;
66import { MatButtonToggleModule } from '@angular/material/button-toggle' ;
@@ -13,6 +13,7 @@ import { MatSlideToggleModule } from '@angular/material/slide-toggle';
1313import { MatTooltipModule } from '@angular/material/tooltip' ;
1414import { Title } from '@angular/platform-browser' ;
1515import { Router , RouterModule } from '@angular/router' ;
16+ import { DynamicAttributesDirective , DynamicModule } from 'ng-dynamic-component' ;
1617import { Angulartics2 , Angulartics2Module } from 'angulartics2' ;
1718import * as ipaddr from 'ipaddr.js' ;
1819import posthog from 'posthog-js' ;
@@ -70,13 +71,13 @@ import { RedisCredentialsFormComponent } from './db-credentials-forms/redis-cred
7071 AlertComponent ,
7172 Angulartics2Module ,
7273 ConnectionStringValidatorDirective ,
74+ DynamicModule ,
75+ DynamicAttributesDirective ,
7376 ] ,
7477} )
75- export class ConnectDBComponent implements OnInit , DoCheck , OnDestroy {
78+ export class ConnectDBComponent implements OnInit , DoCheck {
7679 protected posthog = posthog ;
7780
78- @ViewChild ( 'credentialsFormContainer' , { read : ViewContainerRef } ) credentialsFormContainer : ViewContainerRef ;
79-
8081 public isSaas = ( environment as any ) . saas ;
8182 public connectionID : string | null = null ;
8283 public masterKey : string ;
@@ -458,11 +459,7 @@ export class ConnectDBComponent implements OnInit, DoCheck, OnDestroy {
458459 }
459460
460461 ngDoCheck ( ) {
461- this . _updateCredentialsForm ( ) ;
462- }
463-
464- ngOnDestroy ( ) {
465- this . _destroyCredentialsForm ( ) ;
462+ this . _updateCredentialsFormInputs ( ) ;
466463 }
467464
468465 applyConnectionString ( ) {
@@ -518,59 +515,28 @@ export class ConnectDBComponent implements OnInit, DoCheck, OnDestroy {
518515 return provider ;
519516 }
520517
521- private _credentialsFormRef : ComponentRef < BaseCredentialsFormComponent > | null = null ;
522- private _credentialsFormType : Type < BaseCredentialsFormComponent > | null = null ;
523- private _outputSubscriptions : { switchToAgent ?: any ; masterKeyChange ?: any } = { } ;
524-
525- private _updateCredentialsForm ( ) {
526- if ( ! this . credentialsFormContainer ) {
527- return ;
528- }
518+ public credentialsFormComponent : Type < BaseCredentialsFormComponent > | null = null ;
519+ public credentialsFormInputs : Record < string , any > = { } ;
520+ public credentialsFormOutputs : Record < string , any > = {
521+ switchToAgent : ( ) => this . switchToAgent ( ) ,
522+ masterKeyChange : ( key : string ) => this . handleMasterKeyChange ( key ) ,
523+ } ;
524+ public credentialsFormAttributes : Record < string , string > = { class : 'credentials-fieldset' } ;
529525
526+ private _updateCredentialsFormInputs ( ) {
530527 const isConnectionStringMode = this . connectionInputMode === 'connectionString' && this . db . connectionType === 'direct' && ! this . db . id ;
531528 const targetType = ( ! isConnectionStringMode && this . db . connectionType === 'direct' && this . credentialsFormMap [ this . db . type ] ) || null ;
532529
533- if ( targetType !== this . _credentialsFormType ) {
534- this . _destroyCredentialsForm ( ) ;
535-
536- if ( targetType ) {
537- this . _credentialsFormRef = this . credentialsFormContainer . createComponent ( targetType ) ;
538- this . _credentialsFormType = targetType ;
539-
540- const instance = this . _credentialsFormRef . instance ;
541- this . _outputSubscriptions . switchToAgent = instance . switchToAgent . subscribe ( ( ) => this . switchToAgent ( ) ) ;
542- this . _outputSubscriptions . masterKeyChange = instance . masterKeyChange . subscribe ( ( key : string ) =>
543- this . handleMasterKeyChange ( key ) ,
544- ) ;
545- }
546- }
547-
548- if ( this . _credentialsFormRef ) {
549- const instance = this . _credentialsFormRef . instance ;
550- instance . connection = this . db ;
551- instance . submitting = this . submitting ;
552- instance . accessLevel = this . accessLevel ;
553- instance . masterKey = this . masterKey ;
554- instance . readonly = ! ! ( ( this . accessLevel === 'readonly' || this . db . isTestConnection ) && this . db . id ) ;
555-
556- const hostEl = this . _credentialsFormRef . location . nativeElement as HTMLElement ;
557- hostEl . classList . add ( 'credentials-fieldset' ) ;
558- }
559- }
560-
561- private _destroyCredentialsForm ( ) {
562- this . _outputSubscriptions . switchToAgent ?. unsubscribe ( ) ;
563- this . _outputSubscriptions . masterKeyChange ?. unsubscribe ( ) ;
564- this . _outputSubscriptions = { } ;
530+ this . credentialsFormComponent = targetType ;
565531
566- if ( this . _credentialsFormRef ) {
567- this . _credentialsFormRef . destroy ( ) ;
568- this . _credentialsFormRef = null ;
569- this . _credentialsFormType = null ;
570- }
571-
572- if ( this . credentialsFormContainer ) {
573- this . credentialsFormContainer . clear ( ) ;
532+ if ( targetType ) {
533+ this . credentialsFormInputs = {
534+ connection : this . db ,
535+ submitting : this . submitting ,
536+ accessLevel : this . accessLevel ,
537+ masterKey : this . masterKey ,
538+ readonly : ! ! ( ( this . accessLevel === 'readonly' || this . db . isTestConnection ) && this . db . id ) ,
539+ } ;
574540 }
575541 }
576542}
0 commit comments