-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon-registry.js
381 lines (381 loc) · 18.2 KB
/
icon-registry.js
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
375
376
377
378
379
380
381
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var http_1 = require('@angular/http');
var error_1 = require('@angular2-material/core/errors/error');
var Observable_1 = require('rxjs/Observable');
require('rxjs/add/observable/forkJoin');
require('rxjs/add/observable/of');
require('rxjs/add/operator/map');
require('rxjs/add/operator/filter');
require('rxjs/add/operator/do');
require('rxjs/add/operator/share');
require('rxjs/add/operator/finally');
require('rxjs/add/operator/catch');
/** Exception thrown when attempting to load an icon with a name that cannot be found. */
var MdIconNameNotFoundError = (function (_super) {
__extends(MdIconNameNotFoundError, _super);
function MdIconNameNotFoundError(iconName) {
_super.call(this, "Unable to find icon with the name \"" + iconName + "\"");
}
return MdIconNameNotFoundError;
}(error_1.MdError));
exports.MdIconNameNotFoundError = MdIconNameNotFoundError;
/**
* Exception thrown when attempting to load SVG content that does not contain the expected
* <svg> tag.
*/
var MdIconSvgTagNotFoundError = (function (_super) {
__extends(MdIconSvgTagNotFoundError, _super);
function MdIconSvgTagNotFoundError() {
_super.call(this, '<svg> tag not found');
}
return MdIconSvgTagNotFoundError;
}(error_1.MdError));
exports.MdIconSvgTagNotFoundError = MdIconSvgTagNotFoundError;
/**
* Configuration for an icon, including the URL and possibly the cached SVG element.
* @internal
*/
var SvgIconConfig = (function () {
function SvgIconConfig(url) {
this.url = url;
this.svgElement = null;
}
return SvgIconConfig;
}());
/** Returns the cache key to use for an icon namespace and name. */
var iconKey = function (namespace, name) { return 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.
*/
var MdIconRegistry = (function () {
function MdIconRegistry(_http) {
this._http = _http;
/**
* URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
*/
this._svgIconConfigs = new Map();
/**
* SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
* Multiple icon sets can be registered under the same namespace.
*/
this._iconSetConfigs = new Map();
/** Cache for icons loaded by direct URLs. */
this._cachedIconsByUrl = new Map();
/** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
this._inProgressUrlFetches = new Map();
/** Map from font identifiers to their CSS class names. Used for icon fonts. */
this._fontCssClassesByAlias = new Map();
/**
* 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
*/
this._defaultFontSetClass = 'material-icons';
}
/** Registers an icon by URL in the default namespace. */
MdIconRegistry.prototype.addSvgIcon = function (iconName, url) {
return this.addSvgIconInNamespace('', iconName, url);
};
/** Registers an icon by URL in the specified namespace. */
MdIconRegistry.prototype.addSvgIconInNamespace = function (namespace, iconName, url) {
var key = iconKey(namespace, iconName);
this._svgIconConfigs.set(key, new SvgIconConfig(url));
return this;
};
/** Registers an icon set by URL in the default namespace. */
MdIconRegistry.prototype.addSvgIconSet = function (url) {
return this.addSvgIconSetInNamespace('', url);
};
/** Registers an icon set by URL in the specified namespace. */
MdIconRegistry.prototype.addSvgIconSetInNamespace = function (namespace, url) {
var 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.
*/
MdIconRegistry.prototype.registerFontClassAlias = function (alias, className) {
if (className === void 0) { className = alias; }
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.
*/
MdIconRegistry.prototype.classNameForFontAlias = function (alias) {
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.
*/
MdIconRegistry.prototype.setDefaultFontSetClass = function (className) {
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.
*/
MdIconRegistry.prototype.getDefaultFontSetClass = function () {
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).
*/
MdIconRegistry.prototype.getSvgIconFromUrl = function (url) {
var _this = this;
if (this._cachedIconsByUrl.has(url)) {
return Observable_1.Observable.of(cloneSvg(this._cachedIconsByUrl.get(url)));
}
return this._loadSvgIconFromConfig(new SvgIconConfig(url))
.do(function (svg) { return _this._cachedIconsByUrl.set(url, svg); })
.map(function (svg) { return 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.
*/
MdIconRegistry.prototype.getNamedSvgIcon = function (name, namespace) {
if (namespace === void 0) { namespace = ''; }
// Return (copy of) cached icon if possible.
var 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.
var iconSetConfigs = this._iconSetConfigs.get(namespace);
if (iconSetConfigs) {
return this._getSvgFromIconSetConfigs(name, iconSetConfigs);
}
return Observable_1.Observable.throw(new MdIconNameNotFoundError(key));
};
/**
* Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
*/
MdIconRegistry.prototype._getSvgFromConfig = function (config) {
if (config.svgElement) {
// We already have the SVG element for this icon, return a copy.
return Observable_1.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(function (svg) { return config.svgElement = svg; })
.map(function (svg) { return 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.
*/
MdIconRegistry.prototype._getSvgFromIconSetConfigs = function (name, iconSetConfigs) {
var _this = this;
// For all the icon set SVG elements we've fetched, see if any contain an icon with the
// requested name.
var 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_1.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.
var iconSetFetchRequests = iconSetConfigs
.filter(function (iconSetConfig) { return !iconSetConfig.svgElement; })
.map(function (iconSetConfig) {
return _this._loadSvgIconSetFromConfig(iconSetConfig)
.catch(function (err, caught) {
// 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_1.Observable.of(null);
})
.do(function (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_1.Observable.forkJoin(iconSetFetchRequests)
.map(function (ignoredResults) {
var 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.
*/
MdIconRegistry.prototype._extractIconWithNameFromAnySet = function (iconName, iconSetConfigs) {
// Iterate backwards, so icon sets added later have precedence.
for (var i = iconSetConfigs.length - 1; i >= 0; i--) {
var config = iconSetConfigs[i];
if (config.svgElement) {
var 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.
*/
MdIconRegistry.prototype._loadSvgIconFromConfig = function (config) {
var _this = this;
return this._fetchUrl(config.url)
.map(function (svgText) { return _this._createSvgElementForSingleIcon(svgText, config); });
};
/**
* Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
* from it.
*/
MdIconRegistry.prototype._loadSvgIconSetFromConfig = function (config) {
var _this = this;
// TODO: Document that icons should only be loaded from trusted sources.
return this._fetchUrl(config.url)
.map(function (svgText) { return _this._svgElementFromString(svgText); });
};
/**
* Creates a DOM element from the given SVG string, and adds default attributes.
*/
MdIconRegistry.prototype._createSvgElementForSingleIcon = function (responseText, config) {
var 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.
*/
MdIconRegistry.prototype._extractSvgIconFromSet = function (iconSet, iconName, config) {
var 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(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
var 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.
*/
MdIconRegistry.prototype._svgElementFromString = function (str) {
// TODO: Is there a better way than innerHTML? Renderer doesn't appear to have a method for
// creating an element from an HTML string.
var div = document.createElement('DIV');
div.innerHTML = str;
var svg = div.querySelector('svg');
if (!svg) {
throw new MdIconSvgTagNotFoundError();
}
return svg;
};
/**
* Sets the default attributes for an SVG element to be used as an icon.
*/
MdIconRegistry.prototype._setSvgAttributes = function (svg, config) {
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.
*/
MdIconRegistry.prototype._fetchUrl = function (url) {
var _this = this;
// 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.
var req = this._http.get(url)
.map(function (response) { return response.text(); })
.finally(function () {
_this._inProgressUrlFetches.delete(url);
})
.share();
this._inProgressUrlFetches.set(url, req);
return req;
};
MdIconRegistry = __decorate([
core_1.Injectable(),
__metadata('design:paramtypes', [http_1.Http])
], MdIconRegistry);
return MdIconRegistry;
}());
exports.MdIconRegistry = MdIconRegistry;
/** Clones an SVGElement while preserving type information. */
function cloneSvg(svg) {
return svg.cloneNode(true);
}
//# sourceMappingURL=icon-registry.js.map