33 OnInit ,
44 Input ,
55 ElementRef ,
6- Renderer
6+ ViewChild
77} from '@angular/core'
8- import { HttpClient } from '@angular/common/http'
98
109@Component ( {
1110 selector : 'eqm-icon' ,
@@ -16,10 +15,11 @@ export class IconComponent implements OnInit {
1615 @Input ( ) width = 20
1716 @Input ( ) height = 20
1817
18+ @ViewChild ( 'icon' , { static : true } ) iconRef : ElementRef
19+
1920 @Input ( ) set size ( newSize ) {
2021 this . width = newSize
2122 this . height = newSize
22- this . loadSVG ( )
2323 }
2424 _color = '#979aa0'
2525 @Input ( )
@@ -29,7 +29,6 @@ export class IconComponent implements OnInit {
2929 } else {
3030 this . _color = '#979aa0'
3131 }
32- this . loadSVG ( )
3332 }
3433
3534 _strokeColor = this . _color
@@ -40,7 +39,6 @@ export class IconComponent implements OnInit {
4039 } else {
4140 this . _strokeColor = this . _color
4241 }
43- this . loadSVG ( )
4442 }
4543
4644 private _rotate = 0
@@ -50,59 +48,39 @@ export class IconComponent implements OnInit {
5048 }
5149 set rotate ( angle : number ) {
5250 this . _rotate = angle
53- this . loadSVG ( )
5451 }
5552
5653 _name = null
5754 @Input ( )
5855 set name ( iconName : string ) {
5956 this . _name = iconName
60- this . loadSVG ( )
6157 }
6258 get name ( ) { return this . _name }
6359
6460 @Input ( ) stroke : number = 0
65- constructor (
66- private elementRef : ElementRef ,
67- private renderer : Renderer ,
68- private http : HttpClient
69- ) { }
7061
7162 ngOnInit ( ) {
72- this . loadSVG ( )
7363 }
7464
75- loadSVG ( ) {
76- if ( ! this . name ) return
77- const responseType : any = 'text'
78- this . http . get < any > ( `/assets/icons/${ this . name } .svg` , {
79- responseType
80- } ) . subscribe ( response => {
65+ get style ( ) {
66+ const style : any = { }
8167
82- const element = this . elementRef . nativeElement
83- element . innerHTML = ''
68+ style . fill = `${ this . _color } `
69+ style . display = 'block'
70+ style . margin = '0 auto'
71+ if ( this . height >= 0 ) {
72+ style . height = `${ this . height } px`
73+ }
74+ if ( this . height >= 0 ) {
75+ style . width = `${ this . width } px`
76+ }
77+ style . transform = `rotate(${ this . rotate } deg)`
8478
85- const parser = new DOMParser ( )
86- const svg = parser . parseFromString ( response , 'image/svg+xml' ) . documentElement
87- svg . style . fill = `${ this . _color } `
88- svg . style . display = 'block'
89- svg . style . margin = '0 auto'
90- if ( this . height >= 0 ) {
91- svg . style . height = `${ this . height } px`
92- }
93- if ( this . height >= 0 ) {
94- svg . style . width = `${ this . width } px`
95- }
96- svg . style . transform = `rotate(${ this . rotate } deg)`
79+ if ( this . stroke ) {
80+ style [ 'stroke-width' ] = `${ this . stroke } px`
81+ style . stroke = `${ this . _strokeColor } `
82+ }
9783
98- if ( this . stroke ) {
99- svg . style [ 'stroke-width' ] = `${ this . stroke } px`
100- svg . style . stroke = `${ this . _strokeColor } `
101- }
102- this . renderer . projectNodes ( element , [ svg ] )
103- if ( this . height >= 0 ) {
104- svg . parentElement . style . height = `${ this . height } px`
105- }
106- } )
84+ return style
10785 }
10886}
0 commit comments