1
1
import { Component , ContentChildren , HostListener , Input , OnInit , QueryList } from '@angular/core' ;
2
2
import { DevuiCommonsService } from '../devui-commons.service' ;
3
3
import { LogoComponent } from './logo/logo.component' ;
4
+ import { DomSanitizer , SafeResourceUrl } from '@angular/platform-browser' ;
5
+ import { I18nUtil } from '../i18n/i18n.util' ;
4
6
5
7
@Component ( {
6
8
selector : 'd-common-header' ,
@@ -15,6 +17,9 @@ export class HeaderComponent implements OnInit {
15
17
@Input ( ) showSlideButton = true ;
16
18
@Input ( ) showSearch = false ;
17
19
@Input ( ) showAvatar = false ;
20
+ @Input ( ) repoName = 'ng-devui' ;
21
+
22
+ repoLink : SafeResourceUrl ;
18
23
19
24
@ContentChildren ( LogoComponent ) subLogo : QueryList < LogoComponent > = new QueryList < LogoComponent > ( ) ;
20
25
@@ -27,45 +32,49 @@ export class HeaderComponent implements OnInit {
27
32
28
33
29
34
@HostListener ( 'window:resize' )
30
- resize ( ) {
35
+ resize ( ) : void {
31
36
this . showSlideMenu = document . body . clientWidth < 1024 ? false : true ;
32
37
this . setSlideBarStyle ( ) ;
33
38
}
34
39
35
- constructor ( private commonsService : DevuiCommonsService ) {
40
+ constructor ( private commonsService : DevuiCommonsService , private sanitizer : DomSanitizer ) {
36
41
}
37
42
38
- ngOnInit ( ) {
39
- this . curLanguage = localStorage . getItem ( 'lang' ) || 'zh-cn' ;
43
+ ngOnInit ( ) : void {
44
+ this . curLanguage = I18nUtil . getCurrentLanguage ( ) ;
40
45
this . commonsService . on ( 'languageEvent' ) . subscribe ( term => this . changeLanguage ( term ) ) ;
41
46
this . searchPlaceholder = this . curLanguage === 'zh-cn' ? '请输入你想查找的组件' : 'Enter the component' ;
42
47
this . showSlideMenu = document . body . clientWidth < 1024 ? false : true ;
43
48
this . setSlideBarStyle ( ) ;
49
+ this . repoLink = this . sanitizer . bypassSecurityTrustResourceUrl ( `https://ghbtns.com/github-btn.html?user=DevCloudFE&repo=${ this . repoName } &type=star&count=true` ) ;
44
50
}
45
51
46
- onSearch ( term ) {
52
+ onSearch ( term ) : void {
47
53
this . commonsService . broadcast ( 'searchEvent' , term ) ;
54
+
55
+ if ( term ) {
56
+ this . clickSlideMenu ( true ) ;
57
+ }
48
58
}
49
59
50
60
toggleMenu ( $event ) : void {
51
61
this . collapseMenuActive = ! this . collapseMenuActive ;
52
62
}
53
63
54
- clickSlideMenu ( $event ) {
55
- this . showSlideMenu = ! this . showSlideMenu ;
64
+ clickSlideMenu ( showMenu ?: boolean ) : void {
65
+ this . showSlideMenu = typeof showMenu === 'boolean' ? showMenu : ! this . showSlideMenu ;
56
66
this . setSlideBarStyle ( ) ;
57
67
}
58
68
59
- setSlideBarStyle ( ) {
69
+ setSlideBarStyle ( ) : void {
60
70
const ele = document . querySelector ( '.sidebar-wrapper' ) ;
61
71
if ( ele ) {
62
72
ele . setAttribute ( 'style' , `max-width: ${ this . showSlideMenu ? '260px' : '0' } ` ) ;
63
73
}
64
74
}
65
75
66
- changeLanguage ( lang ) {
76
+ changeLanguage ( lang ) : void {
67
77
this . curLanguage = lang ;
68
78
this . searchPlaceholder = this . curLanguage === 'zh-cn' ? '请输入你想查找的组件' : 'Enter the component' ;
69
79
}
70
-
71
80
}
0 commit comments