1- import { Injectable , SecurityContext } from '@angular/core' ;
1+ import { Injectable , SecurityContext , Optional , SkipSelf } from '@angular/core' ;
22import { SafeResourceUrl , DomSanitizer } from '@angular/platform-browser' ;
33import { Http } from '@angular/http' ;
4- import { MdError } from '../core' ;
54import { Observable } from 'rxjs/Observable' ;
5+ import {
6+ MdIconNameNotFoundError ,
7+ MdIconSvgTagNotFoundError ,
8+ MdIconNoHttpProviderError ,
9+ } from './icon-errors' ;
610import 'rxjs/add/observable/forkJoin' ;
711import 'rxjs/add/observable/of' ;
812import 'rxjs/add/operator/map' ;
@@ -14,27 +18,6 @@ import 'rxjs/add/operator/catch';
1418import 'rxjs/add/observable/throw' ;
1519
1620
17- /**
18- * Exception thrown when attempting to load an icon with a name that cannot be found.
19- * @docs -private
20- */
21- export class MdIconNameNotFoundError extends MdError {
22- constructor ( iconName : string ) {
23- super ( `Unable to find icon with the name "${ iconName } "` ) ;
24- }
25- }
26-
27- /**
28- * Exception thrown when attempting to load SVG content that does not contain the expected
29- * <svg> tag.
30- * @docs -private
31- */
32- export class MdIconSvgTagNotFoundError extends MdError {
33- constructor ( ) {
34- super ( '<svg> tag not found' ) ;
35- }
36- }
37-
3821/**
3922 * Configuration for an icon, including the URL and possibly the cached SVG element.
4023 * @docs -private
@@ -44,9 +27,6 @@ class SvgIconConfig {
4427 constructor ( public url : SafeResourceUrl ) { }
4528}
4629
47- /** Returns the cache key to use for an icon namespace and name. */
48- const iconKey = ( namespace : string , name : string ) => namespace + ':' + name ;
49-
5030/**
5131 * Service to register and display icons used by the <md-icon> component.
5232 * - Registers icon URLs by namespace and name.
@@ -83,7 +63,7 @@ export class MdIconRegistry {
8363 */
8464 private _defaultFontSetClass = 'material-icons' ;
8565
86- constructor ( private _http : Http , private _sanitizer : DomSanitizer ) { }
66+ constructor ( @ Optional ( ) private _http : Http , private _sanitizer : DomSanitizer ) { }
8767
8868 /**
8969 * Registers an icon by URL in the default namespace.
@@ -386,7 +366,11 @@ export class MdIconRegistry {
386366 * cached, so future calls with the same URL may not cause another HTTP request.
387367 */
388368 private _fetchUrl ( safeUrl : SafeResourceUrl ) : Observable < string > {
389- let url = this . _sanitizer . sanitize ( SecurityContext . RESOURCE_URL , safeUrl ) ;
369+ if ( ! this . _http ) {
370+ throw new MdIconNoHttpProviderError ( ) ;
371+ }
372+
373+ const url = this . _sanitizer . sanitize ( SecurityContext . RESOURCE_URL , safeUrl ) ;
390374
391375 // Store in-progress fetches to avoid sending a duplicate request for a URL when there is
392376 // already a request in progress for that URL. It's necessary to call share() on the
@@ -408,8 +392,24 @@ export class MdIconRegistry {
408392 }
409393}
410394
395+ export function ICON_REGISTRY_PROVIDER_FACTORY (
396+ parentRegistry : MdIconRegistry , http : Http , sanitizer : DomSanitizer ) {
397+ return parentRegistry || new MdIconRegistry ( http , sanitizer ) ;
398+ }
399+
400+ export const ICON_REGISTRY_PROVIDER = {
401+ // If there is already an MdIconRegistry available, use that. Otherwise, provide a new one.
402+ provide : MdIconRegistry ,
403+ deps : [ [ new Optional ( ) , new SkipSelf ( ) , MdIconRegistry ] , [ new Optional ( ) , Http ] , DomSanitizer ] ,
404+ useFactory : ICON_REGISTRY_PROVIDER_FACTORY
405+ } ;
411406
412407/** Clones an SVGElement while preserving type information. */
413408function cloneSvg ( svg : SVGElement ) : SVGElement {
414409 return svg . cloneNode ( true ) as SVGElement ;
415410}
411+
412+ /** Returns the cache key to use for an icon namespace and name. */
413+ function iconKey ( namespace : string , name : string ) {
414+ return namespace + ':' + name ;
415+ }
0 commit comments