import { Component, OnInit, ElementRef, Input, ViewChild, TemplateRef, ViewContainerRef, ViewRef, ComponentRef, OnDestroy, AfterViewInit } from '@angular/core'; import { CfCoreBusinessComponent } from '../core-business/core-business.component'; import { Customer } from '../../models/customer-list/customer-list.model'; import { CustomerListModel } from '../../models/customer-list/customer-list.model'; import { CfComponentTemplateService } from '../../services/component-template-service/component-template.service'; import { CfAnimationService } from '../../services/animation-service/animation.service'; @Component({ selector: 'cf-customer-list', template: " ", styles: [".customers { border: solid 1px; min-height: 100px; } .customer-avatar { width: 40px; height: 40px; } .customer-details { min-height: 120px; } "], animations: [ CfAnimationService.fadeInRight(), CfAnimationService.zoomIn() ] }) export class CfCustomerListComponent extends CfCoreBusinessComponent implements OnInit, OnDestroy { @ViewChild('customerViewContainer', { read: ViewContainerRef }) customerViewContainer: ViewContainerRef; @ViewChild('customersListTemplate', { read: TemplateRef }) customersListTemplate: TemplateRef; /** *

Customer details template must be named as cfComponentSidenavTemplate * if we want it to show in sidenav from CfCoreBusinessComponent.

*/ @ViewChild('cfComponentSidenavTemplate', { read: TemplateRef }) customerDetailsTemplate: TemplateRef; selectedCustomer: Customer; @Input() settings: CustomerListModel; @Input() properties: any; constructor(public elementRef: ElementRef, private cfComponentTemplateService: CfComponentTemplateService) { super(elementRef); } ngOnInit() { super.ngOnInit(); } ngOnDestroy() { super.ngOnDestroy(); } /** *

Method that creates customer list embedded view and adds it to container.

*

Should be done like that, otherwise, it will be impossible to dynamically replace list view * in customer container for another one, i.e. customer details.

*/ ngAfterViewInit() { setTimeout(() => { let listViewRef = this.customersListTemplate.createEmbeddedView(null); this.customerViewContainer.insert(listViewRef); }, 0); } /** *

Method to decide based on properties, where and how to show customers list or customer details.

*/ showCustomerDetails($event, cfComponentSidenav, switchToList?: boolean) { this.selectedCustomer = (!switchToList) ? $event.cfRow : this.selectedCustomer; if (this.properties) { let detailsTmplProps = this.properties.templates.customerDetailsTmpl; switch (detailsTmplProps.templateType) { case 'listInComponent': this.cfComponentTemplateService.showTemplateInContainer(this.customersListTemplate, this.customerViewContainer); break; case 'detailsInComponent': this.cfComponentTemplateService.showTemplateInContainer(this.customerDetailsTemplate, this.customerViewContainer); break; case 'dialog': this.cfComponentTemplateService.showInDialog({ template: this.customerDetailsTemplate, dialogOptions: detailsTmplProps }); break; case 'sidenav': this.cfComponentTemplateService.showTemplateInSidenav(cfComponentSidenav, detailsTmplProps); break; } } } }