UNPKG

6.99 kBTypeScriptView Raw
1import { Http } from '@angular/http';
2import { MdError } from '@angular2-material/core';
3import { Observable } from 'rxjs/Observable';
4import 'rxjs/add/observable/forkJoin';
5import 'rxjs/add/observable/of';
6import 'rxjs/add/operator/map';
7import 'rxjs/add/operator/filter';
8import 'rxjs/add/operator/do';
9import 'rxjs/add/operator/share';
10import 'rxjs/add/operator/finally';
11import 'rxjs/add/operator/catch';
12/** Exception thrown when attempting to load an icon with a name that cannot be found. */
13export declare class MdIconNameNotFoundError extends MdError {
14 constructor(iconName: string);
15}
16/**
17 * Exception thrown when attempting to load SVG content that does not contain the expected
18 * <svg> tag.
19 */
20export declare class MdIconSvgTagNotFoundError extends MdError {
21 constructor();
22}
23/**
24 * Service to register and display icons used by the <md-icon> component.
25 * - Registers icon URLs by namespace and name.
26 * - Registers icon set URLs by namespace.
27 * - Registers aliases for CSS classes, for use with icon fonts.
28 * - Loads icons from URLs and extracts individual icons from icon sets.
29 */
30export declare class MdIconRegistry {
31 private _http;
32 /**
33 * URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
34 */
35 private _svgIconConfigs;
36 /**
37 * SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
38 * Multiple icon sets can be registered under the same namespace.
39 */
40 private _iconSetConfigs;
41 /** Cache for icons loaded by direct URLs. */
42 private _cachedIconsByUrl;
43 /** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
44 private _inProgressUrlFetches;
45 /** Map from font identifiers to their CSS class names. Used for icon fonts. */
46 private _fontCssClassesByAlias;
47 /**
48 * The CSS class to apply when an <md-icon> component has no icon name, url, or font specified.
49 * The default 'material-icons' value assumes that the material icon font has been loaded as
50 * described at http://google.github.io/material-design-icons/#icon-font-for-the-web
51 */
52 private _defaultFontSetClass;
53 constructor(_http: Http);
54 /** Registers an icon by URL in the default namespace. */
55 addSvgIcon(iconName: string, url: string): this;
56 /** Registers an icon by URL in the specified namespace. */
57 addSvgIconInNamespace(namespace: string, iconName: string, url: string): this;
58 /** Registers an icon set by URL in the default namespace. */
59 addSvgIconSet(url: string): this;
60 /** Registers an icon set by URL in the specified namespace. */
61 addSvgIconSetInNamespace(namespace: string, url: string): this;
62 /**
63 * Defines an alias for a CSS class name to be used for icon fonts. Creating an mdIcon
64 * component with the alias as the fontSet input will cause the class name to be applied
65 * to the <md-icon> element.
66 */
67 registerFontClassAlias(alias: string, className?: string): this;
68 /**
69 * Returns the CSS class name associated with the alias by a previous call to
70 * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
71 */
72 classNameForFontAlias(alias: string): string;
73 /**
74 * Sets the CSS class name to be used for icon fonts when an <md-icon> component does not
75 * have a fontSet input value, and is not loading an icon by name or URL.
76 */
77 setDefaultFontSetClass(className: string): this;
78 /**
79 * Returns the CSS class name to be used for icon fonts when an <md-icon> component does not
80 * have a fontSet input value, and is not loading an icon by name or URL.
81 */
82 getDefaultFontSetClass(): string;
83 /**
84 * Returns an Observable that produces the icon (as an <svg> DOM element) from the given URL.
85 * The response from the URL may be cached so this will not always cause an HTTP request, but
86 * the produced element will always be a new copy of the originally fetched icon. (That is,
87 * it will not contain any modifications made to elements previously returned).
88 */
89 getSvgIconFromUrl(url: string): Observable<SVGElement>;
90 /**
91 * Returns an Observable that produces the icon (as an <svg> DOM element) with the given name
92 * and namespace. The icon must have been previously registered with addIcon or addIconSet;
93 * if not, the Observable will throw an MdIconNameNotFoundError.
94 */
95 getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
96 /**
97 * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
98 */
99 private _getSvgFromConfig(config);
100 /**
101 * Attempts to find an icon with the specified name in any of the SVG icon sets.
102 * First searches the available cached icons for a nested element with a matching name, and
103 * if found copies the element to a new <svg> element. If not found, fetches all icon sets
104 * that have not been cached, and searches again after all fetches are completed.
105 * The returned Observable produces the SVG element if possible, and throws
106 * MdIconNameNotFoundError if no icon with the specified name can be found.
107 */
108 private _getSvgFromIconSetConfigs(name, iconSetConfigs);
109 /**
110 * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
111 * tag matches the specified name. If found, copies the nested element to a new SVG element and
112 * returns it. Returns null if no matching element is found.
113 */
114 private _extractIconWithNameFromAnySet(iconName, iconSetConfigs);
115 /**
116 * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
117 * from it.
118 */
119 private _loadSvgIconFromConfig(config);
120 /**
121 * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
122 * from it.
123 */
124 private _loadSvgIconSetFromConfig(config);
125 /**
126 * Creates a DOM element from the given SVG string, and adds default attributes.
127 */
128 private _createSvgElementForSingleIcon(responseText, config);
129 /**
130 * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
131 * tag matches the specified name. If found, copies the nested element to a new SVG element and
132 * returns it. Returns null if no matching element is found.
133 */
134 private _extractSvgIconFromSet(iconSet, iconName, config);
135 /**
136 * Creates a DOM element from the given SVG string.
137 */
138 private _svgElementFromString(str);
139 /**
140 * Sets the default attributes for an SVG element to be used as an icon.
141 */
142 private _setSvgAttributes(svg, config);
143 /**
144 * Returns an Observable which produces the string contents of the given URL. Results may be
145 * cached, so future calls with the same URL may not cause another HTTP request.
146 */
147 private _fetchUrl(url);
148}