11import { Component , ContentChildren , HostListener , Input , OnInit , QueryList } from '@angular/core' ;
22import { DevuiCommonsService } from '../devui-commons.service' ;
33import { LogoComponent } from './logo/logo.component' ;
4+ import { DomSanitizer , SafeResourceUrl } from '@angular/platform-browser' ;
5+ import { I18nUtil } from '../i18n/i18n.util' ;
46
57@Component ( {
68 selector : 'd-common-header' ,
@@ -15,6 +17,9 @@ export class HeaderComponent implements OnInit {
1517 @Input ( ) showSlideButton = true ;
1618 @Input ( ) showSearch = false ;
1719 @Input ( ) showAvatar = false ;
20+ @Input ( ) repoName = 'ng-devui' ;
21+
22+ repoLink : SafeResourceUrl ;
1823
1924 @ContentChildren ( LogoComponent ) subLogo : QueryList < LogoComponent > = new QueryList < LogoComponent > ( ) ;
2025
@@ -27,45 +32,49 @@ export class HeaderComponent implements OnInit {
2732
2833
2934 @HostListener ( 'window:resize' )
30- resize ( ) {
35+ resize ( ) : void {
3136 this . showSlideMenu = document . body . clientWidth < 1024 ? false : true ;
3237 this . setSlideBarStyle ( ) ;
3338 }
3439
35- constructor ( private commonsService : DevuiCommonsService ) {
40+ constructor ( private commonsService : DevuiCommonsService , private sanitizer : DomSanitizer ) {
3641 }
3742
38- ngOnInit ( ) {
39- this . curLanguage = localStorage . getItem ( 'lang' ) || 'zh-cn' ;
43+ ngOnInit ( ) : void {
44+ this . curLanguage = I18nUtil . getCurrentLanguage ( ) ;
4045 this . commonsService . on ( 'languageEvent' ) . subscribe ( term => this . changeLanguage ( term ) ) ;
4146 this . searchPlaceholder = this . curLanguage === 'zh-cn' ? '请输入你想查找的组件' : 'Enter the component' ;
4247 this . showSlideMenu = document . body . clientWidth < 1024 ? false : true ;
4348 this . setSlideBarStyle ( ) ;
49+ this . repoLink = this . sanitizer . bypassSecurityTrustResourceUrl ( `https://ghbtns.com/github-btn.html?user=DevCloudFE&repo=${ this . repoName } &type=star&count=true` ) ;
4450 }
4551
46- onSearch ( term ) {
52+ onSearch ( term ) : void {
4753 this . commonsService . broadcast ( 'searchEvent' , term ) ;
54+
55+ if ( term ) {
56+ this . clickSlideMenu ( true ) ;
57+ }
4858 }
4959
5060 toggleMenu ( $event ) : void {
5161 this . collapseMenuActive = ! this . collapseMenuActive ;
5262 }
5363
54- clickSlideMenu ( $event ) {
55- this . showSlideMenu = ! this . showSlideMenu ;
64+ clickSlideMenu ( showMenu ?: boolean ) : void {
65+ this . showSlideMenu = typeof showMenu === 'boolean' ? showMenu : ! this . showSlideMenu ;
5666 this . setSlideBarStyle ( ) ;
5767 }
5868
59- setSlideBarStyle ( ) {
69+ setSlideBarStyle ( ) : void {
6070 const ele = document . querySelector ( '.sidebar-wrapper' ) ;
6171 if ( ele ) {
6272 ele . setAttribute ( 'style' , `max-width: ${ this . showSlideMenu ? '260px' : '0' } ` ) ;
6373 }
6474 }
6575
66- changeLanguage ( lang ) {
76+ changeLanguage ( lang ) : void {
6777 this . curLanguage = lang ;
6878 this . searchPlaceholder = this . curLanguage === 'zh-cn' ? '请输入你想查找的组件' : 'Enter the component' ;
6979 }
70-
7180}
0 commit comments