1
1
import { Injectable , SecurityContext , Inject } from '@angular/core' ;
2
2
import { DomSanitizer } from '@angular/platform-browser' ;
3
- import { HttpClient } from '@angular/common/http' ;
4
3
import { DOCUMENT } from '@angular/common' ;
5
4
6
5
/**
@@ -19,13 +18,14 @@ import { DOCUMENT } from '@angular/common';
19
18
@Injectable ( {
20
19
providedIn : 'root'
21
20
} )
21
+
22
22
export class IgxIconService {
23
23
private _fontSet = 'material-icons' ;
24
24
private _fontSetAliases = new Map < string , string > ( ) ;
25
25
private _svgContainer : HTMLElement ;
26
26
private _cachedSvgIcons : Set < string > = new Set < string > ( ) ;
27
27
28
- constructor ( private _sanitizer : DomSanitizer , private _httpClient : HttpClient , @Inject ( DOCUMENT ) private _document : any ) { }
28
+ constructor ( private _sanitizer : DomSanitizer , @Inject ( DOCUMENT ) private _document : any ) { }
29
29
30
30
/**
31
31
* Returns the default font set.
@@ -108,7 +108,7 @@ export class IgxIconService {
108
108
}
109
109
110
110
/**
111
- * Returns wheather a given SVG image is present in the cache.
111
+ * Returns whether a given SVG image is present in the cache.
112
112
*```typescript
113
113
* const isSvgCached = this.iconService.isSvgIconCached('aruba', 'svg-flags');
114
114
* ```
@@ -132,14 +132,28 @@ export class IgxIconService {
132
132
* @hidden
133
133
*/
134
134
private fetchSvg ( iconName : string , url : string , fontSet : string = '' ) {
135
- const request = this . _httpClient . get ( url , { responseType : 'text' } ) ;
136
- const subscription = request . subscribe ( ( value : string ) => {
137
- this . cacheSvgIcon ( iconName , value , fontSet ) ;
138
- } , ( error ) => {
139
- throw new Error ( `Could not fetch SVG from url: ${ url } ; error: ${ error . message } ` ) ;
140
- } , ( ) => {
141
- subscription . unsubscribe ( ) ;
142
- } ) ;
135
+ const instance = this ;
136
+ const httpRequest = new XMLHttpRequest ( ) ;
137
+ httpRequest . open ( 'GET' , url , true ) ;
138
+ httpRequest . responseType = 'text' ;
139
+
140
+ // load – when the result is ready, that includes HTTP errors like 404.
141
+ httpRequest . onload = function ( event : ProgressEvent ) {
142
+ const request = event . target as XMLHttpRequest ;
143
+ if ( request . status === 200 ) {
144
+ instance . cacheSvgIcon ( iconName , request . responseText , fontSet ) ;
145
+ } else {
146
+ throw new Error ( `Could not fetch SVG from url: ${ url } ; error: ${ request . status } (${ request . statusText } )` ) ;
147
+ }
148
+ } ;
149
+
150
+ // error – when the request couldn’t be made, e.g.network down or invalid URL.
151
+ httpRequest . onerror = function ( event : ProgressEvent ) {
152
+ const request = event . target as XMLHttpRequest ;
153
+ throw new Error ( `Could not fetch SVG from url: ${ url } ; error status code: ${ request . status } (${ request . statusText } )` ) ;
154
+ } ;
155
+
156
+ httpRequest . send ( ) ;
143
157
}
144
158
145
159
/**
0 commit comments