import {Component} from '@angular/core';
+import {Router, NavigationEnd} from '@angular/router';
+import {DialogComponent} from '@eg/share/dialog/dialog.component';
@Component({
selector: 'eg-root',
})
export class BaseComponent {
+
+ constructor(private router: Router) {
+ this.router.events.subscribe(routeEvent => {
+ if (routeEvent instanceof NavigationEnd) {
+ // Prevent dialogs from persisting across navigation.
+ DialogComponent.closeAll();
+ }
+ });
+ }
+
}
})
export class DialogComponent implements OnInit {
+ // Track instances so we can refer to them later in closeAll()
+ // NOTE this could also be done by importing router and subscribing
+ // to route events here, but that would require all subclassed
+ // components to import and pass the router via the constructor.
+ static counter = 0;
+ static instances: {[ident: number]: any} = {};
+
// Assume all dialogs support a title attribute.
@Input() public dialogTitle: string;
@ViewChild('dialogContent')
private dialogContent: TemplateRef<any>;
+ identifier: number = DialogComponent.counter++;
+
// Emitted after open() is called on the ngbModal.
// Note when overriding open(), this will not fire unless also
// called in the overridding method.
constructor(private modalService: NgbModal) {}
+ // Close all active dialogs
+ static closeAll() {
+ Object.keys(DialogComponent.instances).forEach(id => {
+ if (DialogComponent.instances[id]) {
+ DialogComponent.instances[id].close();
+ delete DialogComponent.instances[id];
+ }
+ });
+ }
+
ngOnInit() {
this.onOpen$ = new EventEmitter<any>();
}
}
this.modalRef = this.modalService.open(this.dialogContent, options);
+ DialogComponent.instances[this.identifier] = this;
if (this.onOpen$) {
// Let the digest cycle complete
this.observer = null;
}
this.modalRef = null;
+ delete DialogComponent.instances[this.identifier];
}
+
}