Skip to content

Commit 7cbf440

Browse files
authored
fix(PLEX): instancias duplicadas de plex service. Elemento dinamico de navbar (#369)
1 parent b643f00 commit 7cbf440

6 files changed

Lines changed: 50 additions & 27 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@andes/plex",
3-
"version": "9.0.0-beta.2",
3+
"version": "9.0.0-beta.3",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/andes/plex.git"

src/lib/app/app.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { AfterViewInit, Component, Input, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
22
import { PlexVisualizadorService } from '../core/plex-visualizador.service';
33
import { Plex } from './../core/service';
44

@@ -11,7 +11,7 @@ import { Plex } from './../core/service';
1111
<ng-content select="[navIcon]"></ng-content>
1212
1313
<div class="menu-item">
14-
<ng-template #menuItem></ng-template>
14+
<ng-template #navbarItemHost></ng-template>
1515
</div>
1616
<div class="title">
1717
<ng-container *ngFor="let item of plex.title; let last = last">
@@ -84,7 +84,8 @@ import { Plex } from './../core/service';
8484
</div>`,
8585
})
8686

87-
export class PlexAppComponent implements OnInit {
87+
export class PlexAppComponent implements OnInit, AfterViewInit {
88+
@ViewChild('navbarItemHost', { read: ViewContainerRef }) navbarItemVcr!: ViewContainerRef;
8889
@Input() type = 'inverse';
8990

9091
public loginOpen = false;
@@ -134,4 +135,9 @@ export class PlexAppComponent implements OnInit {
134135
this.chart.dataset[0].data.push(1);
135136
}
136137
}
138+
139+
ngAfterViewInit() {
140+
// se pasamos al servicio de plex el host donde insertar componentes dinámicos
141+
this.plex.setNavbarHost(this.navbarItemVcr);
142+
}
137143
}

src/lib/app/nav-item.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export class NavItemComponent implements AfterViewInit {
3232
this.cd.detectChanges();
3333
}
3434

35-
@HostListener('click', ['event'])
36-
click() {
35+
@HostListener('click', ['$event'])
36+
click($event: Event) {
3737
if (!this.opened) {
38-
event.stopImmediatePropagation();
38+
$event.stopImmediatePropagation();
3939
this.plexHelp.toggle();
4040
this.opened = true;
4141
}

src/lib/core/service.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BreakpointObserver } from '@angular/cdk/layout';
22
import { TitleCasePipe } from '@angular/common';
3-
import { ComponentFactoryResolver, Injectable, ViewContainerRef } from '@angular/core';
3+
import { ComponentRef, Injectable, Type, ViewContainerRef } from '@angular/core';
44
import { Title } from '@angular/platform-browser';
55
import * as _introJs from 'intro.js';
66
import { 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
/**

src/lib/module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ import { SimpleNotificationsModule } from './toast/simple-notifications.module';
270270
providers: [
271271
TitleCasePipe,
272272
HelpService,
273-
Plex,
274273
{ provide: MAT_DATE_LOCALE, useValue: 'es-AR' },
275274
]
276275
})

0 commit comments

Comments
 (0)