import { Component, Input, ViewEncapsulation, ElementRef, TemplateRef, ContentChild, OnInit, AfterViewInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MenuService } from '@core/services/menu.service';
import { TranslatorService } from '@core/translator/translator.service';
@Component({
selector: 'pro-header',
template: `
{{i.title}}
{{i.title}}
`,
styleUrls: [ './pro-header.less' ],
encapsulation: ViewEncapsulation.None,
// tslint:disable-next-line:use-host-property-decorator
host: {
'[class.content__title]': 'true',
'[class.pro-header]': 'true'
}
})
export class ProHeaderComponent implements OnInit {
// region fields
@Input() title: string;
/**
* 自动生成导航,以当前路由从主菜单中定位
*/
@Input() autoBreadcrumb = true;
paths: any[] = [];
@ContentChild('breadcrumb') breadcrumb: TemplateRef;
@ContentChild('logo') logo: TemplateRef;
@ContentChild('action') action: TemplateRef;
@ContentChild('content') content: TemplateRef;
@ContentChild('extra') extra: TemplateRef;
@ContentChild('tab') tab: TemplateRef;
// endregion
constructor(
private route: Router,
private menuSrv: MenuService,
private translatorSrv: TranslatorService) {}
private genBreadcrumb() {
if (this.breadcrumb || !this.autoBreadcrumb) return;
const menus = this.menuSrv.getPathByUrl(this.route.url);
if (menus.length <= 0) return ;
const paths: any[] = [];
menus.forEach(item => {
let title;
if (item.translate) title = this.translatorSrv.fanyi(item.translate);
paths.push({ title: title || item.text, link: item.link && [ item.link ] });
});
// add home
paths.splice(0, 0, {
title: this.translatorSrv.fanyi('home') || 'Home',
link: [ '/' ]
});
this.paths = paths;
}
ngOnInit() {
this.genBreadcrumb();
}
}