11import { BreakpointObserver } from '@angular/cdk/layout' ;
22import { TitleCasePipe } from '@angular/common' ;
3- import { ComponentFactoryResolver , Injectable , ViewContainerRef } from '@angular/core' ;
3+ import { ComponentRef , Injectable , Type , ViewContainerRef } from '@angular/core' ;
44import { Title } from '@angular/platform-browser' ;
55import * as _introJs from 'intro.js' ;
66import { BehaviorSubject , Subject } from 'rxjs' ;
@@ -18,6 +18,9 @@ export class Plex {
1818 public appStatus : Subject < any > = new Subject ( ) ;
1919 public userInfo : any ;
2020 public navbarVisible = true ;
21+ private navbarHost ?: ViewContainerRef ;
22+ private navbarCmpRef ?: ComponentRef < any > ;
23+ private pending ?: { component : Type < any > ; inputs ?: any } ;
2124
2225 /**
2326 * Cuenta los POST, PATCH, PUT, DELETE
@@ -31,10 +34,10 @@ export class Plex {
3134 constructor (
3235 private titleService : Title ,
3336 private noficationService : NotificationsService ,
34- private componentFactoryResolver : ComponentFactoryResolver ,
3537 private breakpointObserver : BreakpointObserver ,
3638 private titlecasePipe : TitleCasePipe
37- ) { }
39+ ) {
40+ }
3841
3942 collapse ( ) {
4043 this . menu = this . menu . map ( ( item ) => ( { ...item , collapsed : true } ) ) ;
@@ -296,9 +299,7 @@ export class Plex {
296299 imageHeight : 250 ,
297300 confirmButtonText : 'Siguiente' ,
298301 cancelButtonText : 'Cancelar' ,
299- showCancelButton : true ,
300- // animation: false,
301- // customClass: 'animated fadeInLeft'
302+ showCancelButton : true
302303 } ) ;
303304 }
304305
@@ -344,7 +345,6 @@ export class Plex {
344345 const steps : introJs . Step [ ] = [ ] ;
345346 for ( const i in config . steps ) {
346347 steps . push ( {
347- // title: config.steps[i].title,
348348 intro : ( config . steps [ i ] . title ? `<h3>${ config . steps [ i ] . title } </h3>` : '' ) + config . steps [ i ] . content ,
349349 element : document . querySelector ( `[plex-wizard-ref="${ i } "]` ) ,
350350 position : 'right'
@@ -383,21 +383,39 @@ export class Plex {
383383 this . viewContainerRef = viewContainerRef ;
384384 }
385385
386+ setNavbarHost ( vcr : ViewContainerRef ) {
387+ this . navbarHost = vcr ;
388+
389+ if ( this . pending ) {
390+ const p = this . pending ;
391+ this . pending = undefined ;
392+ this . setNavbarItem ( p . component , p . inputs ) ;
393+ }
394+ }
395+
386396 /**
387397 * Instancia una componente y la injecta en la parte dinamica del plex-app
388398 * @param componentRef
389399 * @param inputs
390400 */
391- setNavbarItem ( componentRef , inputs ) {
392- // el setTimeout resuelve el error ExpressionChangedAfterItHasBeenCheckedError.
393- // La componente dinamica se estaba creando antes de que finalize la componente padre, lo que generaba ese error.
394- // Por eso encolamos la creación de la componente al proximo tick del navegador.
395- setTimeout ( ( ) => {
396- this . viewContainerRef ?. clear ( ) ;
397- const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( componentRef ) ;
398- const component = this . viewContainerRef ?. createComponent ( componentFactory ) ;
399- Object . assign ( component ?. instance ?? { } , inputs ) ;
400- } , 0 ) ;
401+ setNavbarItem < T > ( component : Type < T > , inputs ?: Partial < T > ) {
402+
403+ if ( ! this . navbarHost ) {
404+ this . pending = { component, inputs } ;
405+ return ; // evita reintentos infinitos
406+ }
407+
408+ this . navbarHost . clear ( ) ;
409+ const cmpRef = this . navbarHost . createComponent ( component ) ;
410+ if ( inputs ) { Object . assign ( cmpRef . instance , inputs ) ; }
411+ cmpRef . changeDetectorRef . detectChanges ( ) ;
412+ this . navbarCmpRef = cmpRef ;
413+ }
414+
415+ clearNavbarItem ( ) {
416+ this . navbarCmpRef ?. destroy ( ) ;
417+ this . navbarCmpRef = undefined ;
418+ this . navbarHost ?. clear ( ) ;
401419 }
402420
403421 /**
0 commit comments