-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
icon-registry.ts
374 lines (339 loc) · 14.8 KB
/
icon-registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {MdError} from '../core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/catch';
/** Exception thrown when attempting to load an icon with a name that cannot be found. */
export class MdIconNameNotFoundError extends MdError {
constructor(iconName: string) {
super(`Unable to find icon with the name "${iconName}"`);
}
}
/**
* Exception thrown when attempting to load SVG content that does not contain the expected
* <svg> tag.
*/
export class MdIconSvgTagNotFoundError extends MdError {
constructor() {
super('<svg> tag not found');
}
}
/** Configuration for an icon, including the URL and possibly the cached SVG element. */
class SvgIconConfig {
svgElement: SVGElement = null;
constructor(public url: string) { }
}
/** Returns the cache key to use for an icon namespace and name. */
const iconKey = (namespace: string, name: string) => namespace + ':' + name;
/**
* Service to register and display icons used by the <md-icon> component.
* - Registers icon URLs by namespace and name.
* - Registers icon set URLs by namespace.
* - Registers aliases for CSS classes, for use with icon fonts.
* - Loads icons from URLs and extracts individual icons from icon sets.
*/
@Injectable()
export class MdIconRegistry {
/**
* URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
*/
private _svgIconConfigs = new Map<string, SvgIconConfig>();
/**
* SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
* Multiple icon sets can be registered under the same namespace.
*/
private _iconSetConfigs = new Map<string, SvgIconConfig[]>();
/** Cache for icons loaded by direct URLs. */
private _cachedIconsByUrl = new Map<string, SVGElement>();
/** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
private _inProgressUrlFetches = new Map<string, Observable<string>>();
/** Map from font identifiers to their CSS class names. Used for icon fonts. */
private _fontCssClassesByAlias = new Map<string, string>();
/**
* The CSS class to apply when an <md-icon> component has no icon name, url, or font specified.
* The default 'material-icons' value assumes that the material icon font has been loaded as
* described at http://google.github.io/material-design-icons/#icon-font-for-the-web
*/
private _defaultFontSetClass = 'material-icons';
constructor(private _http: Http) {}
/** Registers an icon by URL in the default namespace. */
addSvgIcon(iconName: string, url: string): this {
return this.addSvgIconInNamespace('', iconName, url);
}
/** Registers an icon by URL in the specified namespace. */
addSvgIconInNamespace(namespace: string, iconName: string, url: string): this {
const key = iconKey(namespace, iconName);
this._svgIconConfigs.set(key, new SvgIconConfig(url));
return this;
}
/** Registers an icon set by URL in the default namespace. */
addSvgIconSet(url: string): this {
return this.addSvgIconSetInNamespace('', url);
}
/** Registers an icon set by URL in the specified namespace. */
addSvgIconSetInNamespace(namespace: string, url: string): this {
const config = new SvgIconConfig(url);
if (this._iconSetConfigs.has(namespace)) {
this._iconSetConfigs.get(namespace).push(config);
} else {
this._iconSetConfigs.set(namespace, [config]);
}
return this;
}
/**
* Defines an alias for a CSS class name to be used for icon fonts. Creating an mdIcon
* component with the alias as the fontSet input will cause the class name to be applied
* to the <md-icon> element.
*/
registerFontClassAlias(alias: string, className = alias): this {
this._fontCssClassesByAlias.set(alias, className);
return this;
}
/**
* Returns the CSS class name associated with the alias by a previous call to
* registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
*/
classNameForFontAlias(alias: string): string {
return this._fontCssClassesByAlias.get(alias) || alias;
}
/**
* Sets the CSS class name to be used for icon fonts when an <md-icon> component does not
* have a fontSet input value, and is not loading an icon by name or URL.
*/
setDefaultFontSetClass(className: string): this {
this._defaultFontSetClass = className;
return this;
}
/**
* Returns the CSS class name to be used for icon fonts when an <md-icon> component does not
* have a fontSet input value, and is not loading an icon by name or URL.
*/
getDefaultFontSetClass(): string {
return this._defaultFontSetClass;
}
/**
* Returns an Observable that produces the icon (as an <svg> DOM element) from the given URL.
* The response from the URL may be cached so this will not always cause an HTTP request, but
* the produced element will always be a new copy of the originally fetched icon. (That is,
* it will not contain any modifications made to elements previously returned).
*/
getSvgIconFromUrl(url: string): Observable<SVGElement> {
if (this._cachedIconsByUrl.has(url)) {
return Observable.of(cloneSvg(this._cachedIconsByUrl.get(url)));
}
return this._loadSvgIconFromConfig(new SvgIconConfig(url))
.do(svg => this._cachedIconsByUrl.set(url, svg))
.map(svg => cloneSvg(svg));
}
/**
* Returns an Observable that produces the icon (as an <svg> DOM element) with the given name
* and namespace. The icon must have been previously registered with addIcon or addIconSet;
* if not, the Observable will throw an MdIconNameNotFoundError.
*/
getNamedSvgIcon(name: string, namespace = ''): Observable<SVGElement> {
// Return (copy of) cached icon if possible.
const key = iconKey(namespace, name);
if (this._svgIconConfigs.has(key)) {
return this._getSvgFromConfig(this._svgIconConfigs.get(key));
}
// See if we have any icon sets registered for the namespace.
const iconSetConfigs = this._iconSetConfigs.get(namespace);
if (iconSetConfigs) {
return this._getSvgFromIconSetConfigs(name, iconSetConfigs);
}
return Observable.throw(new MdIconNameNotFoundError(key));
}
/**
* Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
*/
private _getSvgFromConfig(config: SvgIconConfig): Observable<SVGElement> {
if (config.svgElement) {
// We already have the SVG element for this icon, return a copy.
return Observable.of(cloneSvg(config.svgElement));
} else {
// Fetch the icon from the config's URL, cache it, and return a copy.
return this._loadSvgIconFromConfig(config)
.do(svg => config.svgElement = svg)
.map(svg => cloneSvg(svg));
}
}
/**
* Attempts to find an icon with the specified name in any of the SVG icon sets.
* First searches the available cached icons for a nested element with a matching name, and
* if found copies the element to a new <svg> element. If not found, fetches all icon sets
* that have not been cached, and searches again after all fetches are completed.
* The returned Observable produces the SVG element if possible, and throws
* MdIconNameNotFoundError if no icon with the specified name can be found.
*/
private _getSvgFromIconSetConfigs(name: string, iconSetConfigs: SvgIconConfig[]):
Observable<SVGElement> {
// For all the icon set SVG elements we've fetched, see if any contain an icon with the
// requested name.
const namedIcon = this._extractIconWithNameFromAnySet(name, iconSetConfigs);
if (namedIcon) {
// We could cache namedIcon in _svgIconConfigs, but since we have to make a copy every
// time anyway, there's probably not much advantage compared to just always extracting
// it from the icon set.
return Observable.of(namedIcon);
}
// Not found in any cached icon sets. If there are icon sets with URLs that we haven't
// fetched, fetch them now and look for iconName in the results.
const iconSetFetchRequests: Observable<SVGElement>[] = iconSetConfigs
.filter(iconSetConfig => !iconSetConfig.svgElement)
.map(iconSetConfig =>
this._loadSvgIconSetFromConfig(iconSetConfig)
.catch((err: any, caught: Observable<SVGElement>): Observable<SVGElement> => {
// Swallow errors fetching individual URLs so the combined Observable won't
// necessarily fail.
console.log(`Loading icon set URL: ${iconSetConfig.url} failed: ${err}`);
return Observable.of(null);
})
.do(svg => {
// Cache SVG element.
if (svg) {
iconSetConfig.svgElement = svg;
}
}));
// Fetch all the icon set URLs. When the requests complete, every IconSet should have a
// cached SVG element (unless the request failed), and we can check again for the icon.
return Observable.forkJoin(iconSetFetchRequests)
.map((ignoredResults: any) => {
const foundIcon = this._extractIconWithNameFromAnySet(name, iconSetConfigs);
if (!foundIcon) {
throw new MdIconNameNotFoundError(name);
}
return foundIcon;
});
}
/**
* Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
* tag matches the specified name. If found, copies the nested element to a new SVG element and
* returns it. Returns null if no matching element is found.
*/
private _extractIconWithNameFromAnySet(iconName: string, iconSetConfigs: SvgIconConfig[]):
SVGElement {
// Iterate backwards, so icon sets added later have precedence.
for (let i = iconSetConfigs.length - 1; i >= 0; i--) {
const config = iconSetConfigs[i];
if (config.svgElement) {
const foundIcon = this._extractSvgIconFromSet(config.svgElement, iconName, config);
if (foundIcon) {
return foundIcon;
}
}
}
return null;
}
/**
* Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
* from it.
*/
private _loadSvgIconFromConfig(config: SvgIconConfig): Observable<SVGElement> {
return this._fetchUrl(config.url)
.map(svgText => this._createSvgElementForSingleIcon(svgText, config));
}
/**
* Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
* from it.
*/
private _loadSvgIconSetFromConfig(config: SvgIconConfig): Observable<SVGElement> {
// TODO: Document that icons should only be loaded from trusted sources.
return this._fetchUrl(config.url)
.map((svgText) => this._svgElementFromString(svgText));
}
/**
* Creates a DOM element from the given SVG string, and adds default attributes.
*/
private _createSvgElementForSingleIcon(responseText: string, config: SvgIconConfig): SVGElement {
const svg = this._svgElementFromString(responseText);
this._setSvgAttributes(svg, config);
return svg;
}
/**
* Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
* tag matches the specified name. If found, copies the nested element to a new SVG element and
* returns it. Returns null if no matching element is found.
*/
private _extractSvgIconFromSet(
iconSet: SVGElement, iconName: string, config: SvgIconConfig): SVGElement {
const iconNode = iconSet.querySelector('#' + iconName);
if (!iconNode) {
return null;
}
// If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
// the content of a new <svg> node.
if (iconNode.tagName.toLowerCase() == 'svg') {
return this._setSvgAttributes(<SVGElement>iconNode.cloneNode(true), config);
}
// createElement('SVG') doesn't work as expected; the DOM ends up with
// the correct nodes, but the SVG content doesn't render. Instead we
// have to create an empty SVG node using innerHTML and append its content.
// Elements created using DOMParser.parseFromString have the same problem.
// http://stackoverflow.com/questions/23003278/svg-innerhtml-in-firefox-can-not-display
const svg = this._svgElementFromString('<svg></svg>');
// Clone the node so we don't remove it from the parent icon set element.
svg.appendChild(iconNode.cloneNode(true));
return this._setSvgAttributes(svg, config);
}
/**
* Creates a DOM element from the given SVG string.
*/
private _svgElementFromString(str: string): SVGElement {
// TODO: Is there a better way than innerHTML? Renderer doesn't appear to have a method for
// creating an element from an HTML string.
const div = document.createElement('DIV');
div.innerHTML = str;
const svg = <SVGElement>div.querySelector('svg');
if (!svg) {
throw new MdIconSvgTagNotFoundError();
}
return svg;
}
/**
* Sets the default attributes for an SVG element to be used as an icon.
*/
private _setSvgAttributes(svg: SVGElement, config: SvgIconConfig): SVGElement {
if (!svg.getAttribute('xmlns')) {
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
}
svg.setAttribute('fit', '');
svg.setAttribute('height', '100%');
svg.setAttribute('width', '100%');
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
svg.setAttribute('focusable', 'false'); // Disable IE11 default behavior to make SVGs focusable.
return svg;
}
/**
* Returns an Observable which produces the string contents of the given URL. Results may be
* cached, so future calls with the same URL may not cause another HTTP request.
*/
private _fetchUrl(url: string): Observable<string> {
// Store in-progress fetches to avoid sending a duplicate request for a URL when there is
// already a request in progress for that URL. It's necessary to call share() on the
// Observable returned by http.get() so that multiple subscribers don't cause multiple XHRs.
if (this._inProgressUrlFetches.has(url)) {
return this._inProgressUrlFetches.get(url);
}
// TODO(jelbourn): for some reason, the `finally` operator "loses" the generic type on the
// Observable. Figure out why and fix it.
const req = <Observable<string>> this._http.get(url)
.map(response => response.text())
.finally(() => {
this._inProgressUrlFetches.delete(url);
})
.share();
this._inProgressUrlFetches.set(url, req);
return req;
}
}
/** Clones an SVGElement while preserving type information. */
function cloneSvg(svg: SVGElement) {
return <SVGElement> svg.cloneNode(true);
}