UNPKG

30.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/http'), require('@angular2-material/core'), 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')) :
3 typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/http', '@angular2-material/core', 'rxjs/Observable', 'rxjs/add/observable/forkJoin', 'rxjs/add/observable/of', 'rxjs/add/operator/map', 'rxjs/add/operator/filter', 'rxjs/add/operator/do', 'rxjs/add/operator/share', 'rxjs/add/operator/finally', 'rxjs/add/operator/catch'], factory) :
4 (factory((global.md = global.md || {}, global.md.icon = global.md.icon || {}),global.ng.core,global.ng.http,global.md.core,global.Rx,global.Rx.Observable,global.Rx.Observable,global.Rx.Observable.prototype,global.Rx.Observable.prototype,global.Rx.Observable.prototype,global.Rx.Observable.prototype,global.Rx.Observable.prototype,global.Rx.Observable.prototype));
5}(this, (function (exports,_angular_core,_angular_http,_angular2Material_core,rxjs_Observable,rxjs_add_observable_forkJoin,rxjs_add_observable_of,rxjs_add_operator_map,rxjs_add_operator_filter,rxjs_add_operator_do,rxjs_add_operator_share,rxjs_add_operator_finally,rxjs_add_operator_catch) { 'use strict';
6
7var __extends$1 = (window && window.__extends) || function (d, b) {
8 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
9 function __() { this.constructor = d; }
10 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
11};
12var __decorate$1 = (window && window.__decorate) || function (decorators, target, key, desc) {
13 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
14 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
15 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;
16 return c > 3 && r && Object.defineProperty(target, key, r), r;
17};
18var __metadata$1 = (window && window.__metadata) || function (k, v) {
19 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
20};
21/** Exception thrown when attempting to load an icon with a name that cannot be found. */
22var MdIconNameNotFoundError = (function (_super) {
23 __extends$1(MdIconNameNotFoundError, _super);
24 function MdIconNameNotFoundError(iconName) {
25 _super.call(this, "Unable to find icon with the name \"" + iconName + "\"");
26 }
27 return MdIconNameNotFoundError;
28}(_angular2Material_core.MdError));
29/**
30 * Exception thrown when attempting to load SVG content that does not contain the expected
31 * <svg> tag.
32 */
33var MdIconSvgTagNotFoundError = (function (_super) {
34 __extends$1(MdIconSvgTagNotFoundError, _super);
35 function MdIconSvgTagNotFoundError() {
36 _super.call(this, '<svg> tag not found');
37 }
38 return MdIconSvgTagNotFoundError;
39}(_angular2Material_core.MdError));
40/** Configuration for an icon, including the URL and possibly the cached SVG element. */
41var SvgIconConfig = (function () {
42 function SvgIconConfig(url) {
43 this.url = url;
44 this.svgElement = null;
45 }
46 return SvgIconConfig;
47}());
48/** Returns the cache key to use for an icon namespace and name. */
49var iconKey = function (namespace, name) { return namespace + ':' + name; };
50/**
51 * Service to register and display icons used by the <md-icon> component.
52 * - Registers icon URLs by namespace and name.
53 * - Registers icon set URLs by namespace.
54 * - Registers aliases for CSS classes, for use with icon fonts.
55 * - Loads icons from URLs and extracts individual icons from icon sets.
56 */
57var MdIconRegistry = (function () {
58 function MdIconRegistry(_http) {
59 this._http = _http;
60 /**
61 * URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
62 */
63 this._svgIconConfigs = new Map();
64 /**
65 * SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
66 * Multiple icon sets can be registered under the same namespace.
67 */
68 this._iconSetConfigs = new Map();
69 /** Cache for icons loaded by direct URLs. */
70 this._cachedIconsByUrl = new Map();
71 /** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
72 this._inProgressUrlFetches = new Map();
73 /** Map from font identifiers to their CSS class names. Used for icon fonts. */
74 this._fontCssClassesByAlias = new Map();
75 /**
76 * The CSS class to apply when an <md-icon> component has no icon name, url, or font specified.
77 * The default 'material-icons' value assumes that the material icon font has been loaded as
78 * described at http://google.github.io/material-design-icons/#icon-font-for-the-web
79 */
80 this._defaultFontSetClass = 'material-icons';
81 }
82 /** Registers an icon by URL in the default namespace. */
83 MdIconRegistry.prototype.addSvgIcon = function (iconName, url) {
84 return this.addSvgIconInNamespace('', iconName, url);
85 };
86 /** Registers an icon by URL in the specified namespace. */
87 MdIconRegistry.prototype.addSvgIconInNamespace = function (namespace, iconName, url) {
88 var key = iconKey(namespace, iconName);
89 this._svgIconConfigs.set(key, new SvgIconConfig(url));
90 return this;
91 };
92 /** Registers an icon set by URL in the default namespace. */
93 MdIconRegistry.prototype.addSvgIconSet = function (url) {
94 return this.addSvgIconSetInNamespace('', url);
95 };
96 /** Registers an icon set by URL in the specified namespace. */
97 MdIconRegistry.prototype.addSvgIconSetInNamespace = function (namespace, url) {
98 var config = new SvgIconConfig(url);
99 if (this._iconSetConfigs.has(namespace)) {
100 this._iconSetConfigs.get(namespace).push(config);
101 }
102 else {
103 this._iconSetConfigs.set(namespace, [config]);
104 }
105 return this;
106 };
107 /**
108 * Defines an alias for a CSS class name to be used for icon fonts. Creating an mdIcon
109 * component with the alias as the fontSet input will cause the class name to be applied
110 * to the <md-icon> element.
111 */
112 MdIconRegistry.prototype.registerFontClassAlias = function (alias, className) {
113 if (className === void 0) { className = alias; }
114 this._fontCssClassesByAlias.set(alias, className);
115 return this;
116 };
117 /**
118 * Returns the CSS class name associated with the alias by a previous call to
119 * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
120 */
121 MdIconRegistry.prototype.classNameForFontAlias = function (alias) {
122 return this._fontCssClassesByAlias.get(alias) || alias;
123 };
124 /**
125 * Sets the CSS class name to be used for icon fonts when an <md-icon> component does not
126 * have a fontSet input value, and is not loading an icon by name or URL.
127 */
128 MdIconRegistry.prototype.setDefaultFontSetClass = function (className) {
129 this._defaultFontSetClass = className;
130 return this;
131 };
132 /**
133 * Returns the CSS class name to be used for icon fonts when an <md-icon> component does not
134 * have a fontSet input value, and is not loading an icon by name or URL.
135 */
136 MdIconRegistry.prototype.getDefaultFontSetClass = function () {
137 return this._defaultFontSetClass;
138 };
139 /**
140 * Returns an Observable that produces the icon (as an <svg> DOM element) from the given URL.
141 * The response from the URL may be cached so this will not always cause an HTTP request, but
142 * the produced element will always be a new copy of the originally fetched icon. (That is,
143 * it will not contain any modifications made to elements previously returned).
144 */
145 MdIconRegistry.prototype.getSvgIconFromUrl = function (url) {
146 var _this = this;
147 if (this._cachedIconsByUrl.has(url)) {
148 return rxjs_Observable.Observable.of(cloneSvg(this._cachedIconsByUrl.get(url)));
149 }
150 return this._loadSvgIconFromConfig(new SvgIconConfig(url))
151 .do(function (svg) { return _this._cachedIconsByUrl.set(url, svg); })
152 .map(function (svg) { return cloneSvg(svg); });
153 };
154 /**
155 * Returns an Observable that produces the icon (as an <svg> DOM element) with the given name
156 * and namespace. The icon must have been previously registered with addIcon or addIconSet;
157 * if not, the Observable will throw an MdIconNameNotFoundError.
158 */
159 MdIconRegistry.prototype.getNamedSvgIcon = function (name, namespace) {
160 if (namespace === void 0) { namespace = ''; }
161 // Return (copy of) cached icon if possible.
162 var key = iconKey(namespace, name);
163 if (this._svgIconConfigs.has(key)) {
164 return this._getSvgFromConfig(this._svgIconConfigs.get(key));
165 }
166 // See if we have any icon sets registered for the namespace.
167 var iconSetConfigs = this._iconSetConfigs.get(namespace);
168 if (iconSetConfigs) {
169 return this._getSvgFromIconSetConfigs(name, iconSetConfigs);
170 }
171 return rxjs_Observable.Observable.throw(new MdIconNameNotFoundError(key));
172 };
173 /**
174 * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
175 */
176 MdIconRegistry.prototype._getSvgFromConfig = function (config) {
177 if (config.svgElement) {
178 // We already have the SVG element for this icon, return a copy.
179 return rxjs_Observable.Observable.of(cloneSvg(config.svgElement));
180 }
181 else {
182 // Fetch the icon from the config's URL, cache it, and return a copy.
183 return this._loadSvgIconFromConfig(config)
184 .do(function (svg) { return config.svgElement = svg; })
185 .map(function (svg) { return cloneSvg(svg); });
186 }
187 };
188 /**
189 * Attempts to find an icon with the specified name in any of the SVG icon sets.
190 * First searches the available cached icons for a nested element with a matching name, and
191 * if found copies the element to a new <svg> element. If not found, fetches all icon sets
192 * that have not been cached, and searches again after all fetches are completed.
193 * The returned Observable produces the SVG element if possible, and throws
194 * MdIconNameNotFoundError if no icon with the specified name can be found.
195 */
196 MdIconRegistry.prototype._getSvgFromIconSetConfigs = function (name, iconSetConfigs) {
197 var _this = this;
198 // For all the icon set SVG elements we've fetched, see if any contain an icon with the
199 // requested name.
200 var namedIcon = this._extractIconWithNameFromAnySet(name, iconSetConfigs);
201 if (namedIcon) {
202 // We could cache namedIcon in _svgIconConfigs, but since we have to make a copy every
203 // time anyway, there's probably not much advantage compared to just always extracting
204 // it from the icon set.
205 return rxjs_Observable.Observable.of(namedIcon);
206 }
207 // Not found in any cached icon sets. If there are icon sets with URLs that we haven't
208 // fetched, fetch them now and look for iconName in the results.
209 var iconSetFetchRequests = iconSetConfigs
210 .filter(function (iconSetConfig) { return !iconSetConfig.svgElement; })
211 .map(function (iconSetConfig) {
212 return _this._loadSvgIconSetFromConfig(iconSetConfig)
213 .catch(function (err, caught) {
214 // Swallow errors fetching individual URLs so the combined Observable won't
215 // necessarily fail.
216 console.log("Loading icon set URL: " + iconSetConfig.url + " failed: " + err);
217 return rxjs_Observable.Observable.of(null);
218 })
219 .do(function (svg) {
220 // Cache SVG element.
221 if (svg) {
222 iconSetConfig.svgElement = svg;
223 }
224 });
225 });
226 // Fetch all the icon set URLs. When the requests complete, every IconSet should have a
227 // cached SVG element (unless the request failed), and we can check again for the icon.
228 return rxjs_Observable.Observable.forkJoin(iconSetFetchRequests)
229 .map(function (ignoredResults) {
230 var foundIcon = _this._extractIconWithNameFromAnySet(name, iconSetConfigs);
231 if (!foundIcon) {
232 throw new MdIconNameNotFoundError(name);
233 }
234 return foundIcon;
235 });
236 };
237 /**
238 * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
239 * tag matches the specified name. If found, copies the nested element to a new SVG element and
240 * returns it. Returns null if no matching element is found.
241 */
242 MdIconRegistry.prototype._extractIconWithNameFromAnySet = function (iconName, iconSetConfigs) {
243 // Iterate backwards, so icon sets added later have precedence.
244 for (var i = iconSetConfigs.length - 1; i >= 0; i--) {
245 var config = iconSetConfigs[i];
246 if (config.svgElement) {
247 var foundIcon = this._extractSvgIconFromSet(config.svgElement, iconName, config);
248 if (foundIcon) {
249 return foundIcon;
250 }
251 }
252 }
253 return null;
254 };
255 /**
256 * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
257 * from it.
258 */
259 MdIconRegistry.prototype._loadSvgIconFromConfig = function (config) {
260 var _this = this;
261 return this._fetchUrl(config.url)
262 .map(function (svgText) { return _this._createSvgElementForSingleIcon(svgText, config); });
263 };
264 /**
265 * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
266 * from it.
267 */
268 MdIconRegistry.prototype._loadSvgIconSetFromConfig = function (config) {
269 var _this = this;
270 // TODO: Document that icons should only be loaded from trusted sources.
271 return this._fetchUrl(config.url)
272 .map(function (svgText) { return _this._svgElementFromString(svgText); });
273 };
274 /**
275 * Creates a DOM element from the given SVG string, and adds default attributes.
276 */
277 MdIconRegistry.prototype._createSvgElementForSingleIcon = function (responseText, config) {
278 var svg = this._svgElementFromString(responseText);
279 this._setSvgAttributes(svg, config);
280 return svg;
281 };
282 /**
283 * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
284 * tag matches the specified name. If found, copies the nested element to a new SVG element and
285 * returns it. Returns null if no matching element is found.
286 */
287 MdIconRegistry.prototype._extractSvgIconFromSet = function (iconSet, iconName, config) {
288 var iconNode = iconSet.querySelector('#' + iconName);
289 if (!iconNode) {
290 return null;
291 }
292 // If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
293 // the content of a new <svg> node.
294 if (iconNode.tagName.toLowerCase() == 'svg') {
295 return this._setSvgAttributes(iconNode.cloneNode(true), config);
296 }
297 // createElement('SVG') doesn't work as expected; the DOM ends up with
298 // the correct nodes, but the SVG content doesn't render. Instead we
299 // have to create an empty SVG node using innerHTML and append its content.
300 // Elements created using DOMParser.parseFromString have the same problem.
301 // http://stackoverflow.com/questions/23003278/svg-innerhtml-in-firefox-can-not-display
302 var svg = this._svgElementFromString('<svg></svg>');
303 // Clone the node so we don't remove it from the parent icon set element.
304 svg.appendChild(iconNode.cloneNode(true));
305 return this._setSvgAttributes(svg, config);
306 };
307 /**
308 * Creates a DOM element from the given SVG string.
309 */
310 MdIconRegistry.prototype._svgElementFromString = function (str) {
311 // TODO: Is there a better way than innerHTML? Renderer doesn't appear to have a method for
312 // creating an element from an HTML string.
313 var div = document.createElement('DIV');
314 div.innerHTML = str;
315 var svg = div.querySelector('svg');
316 if (!svg) {
317 throw new MdIconSvgTagNotFoundError();
318 }
319 return svg;
320 };
321 /**
322 * Sets the default attributes for an SVG element to be used as an icon.
323 */
324 MdIconRegistry.prototype._setSvgAttributes = function (svg, config) {
325 if (!svg.getAttribute('xmlns')) {
326 svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
327 }
328 svg.setAttribute('fit', '');
329 svg.setAttribute('height', '100%');
330 svg.setAttribute('width', '100%');
331 svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
332 svg.setAttribute('focusable', 'false'); // Disable IE11 default behavior to make SVGs focusable.
333 return svg;
334 };
335 /**
336 * Returns an Observable which produces the string contents of the given URL. Results may be
337 * cached, so future calls with the same URL may not cause another HTTP request.
338 */
339 MdIconRegistry.prototype._fetchUrl = function (url) {
340 var _this = this;
341 // Store in-progress fetches to avoid sending a duplicate request for a URL when there is
342 // already a request in progress for that URL. It's necessary to call share() on the
343 // Observable returned by http.get() so that multiple subscribers don't cause multiple XHRs.
344 if (this._inProgressUrlFetches.has(url)) {
345 return this._inProgressUrlFetches.get(url);
346 }
347 // TODO(jelbourn): for some reason, the `finally` operator "loses" the generic type on the
348 // Observable. Figure out why and fix it.
349 var req = this._http.get(url)
350 .map(function (response) { return response.text(); })
351 .finally(function () {
352 _this._inProgressUrlFetches.delete(url);
353 })
354 .share();
355 this._inProgressUrlFetches.set(url, req);
356 return req;
357 };
358 MdIconRegistry = __decorate$1([
359 _angular_core.Injectable(),
360 __metadata$1('design:paramtypes', [_angular_http.Http])
361 ], MdIconRegistry);
362 return MdIconRegistry;
363}());
364/** Clones an SVGElement while preserving type information. */
365function cloneSvg(svg) {
366 return svg.cloneNode(true);
367}
368
369var __extends = (window && window.__extends) || function (d, b) {
370 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
371 function __() { this.constructor = d; }
372 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
373};
374var __decorate = (window && window.__decorate) || function (decorators, target, key, desc) {
375 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
376 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
377 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;
378 return c > 3 && r && Object.defineProperty(target, key, r), r;
379};
380var __metadata = (window && window.__metadata) || function (k, v) {
381 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
382};
383/** Exception thrown when an invalid icon name is passed to an md-icon component. */
384var MdIconInvalidNameError = (function (_super) {
385 __extends(MdIconInvalidNameError, _super);
386 function MdIconInvalidNameError(iconName) {
387 _super.call(this, "Invalid icon name: \"" + iconName + "\"");
388 }
389 return MdIconInvalidNameError;
390}(_angular2Material_core.MdError));
391/**
392 * Component to display an icon. It can be used in the following ways:
393 * - Specify the svgSrc input to load an SVG icon from a URL. The SVG content is directly inlined
394 * as a child of the <md-icon> component, so that CSS styles can easily be applied to it.
395 * The URL is loaded via an XMLHttpRequest, so it must be on the same domain as the page or its
396 * server must be configured to allow cross-domain requests.
397 * Example:
398 * <md-icon svgSrc="assets/arrow.svg"></md-icon>
399 *
400 * - Specify the svgIcon input to load an SVG icon from a URL previously registered with the
401 * addSvgIcon, addSvgIconInNamespace, addSvgIconSet, or addSvgIconSetInNamespace methods of
402 * MdIconRegistry. If the svgIcon value contains a colon it is assumed to be in the format
403 * "[namespace]:[name]", if not the value will be the name of an icon in the default namespace.
404 * Examples:
405 * <md-icon svgIcon="left-arrow"></md-icon>
406 * <md-icon svgIcon="animals:cat"></md-icon>
407 *
408 * - Use a font ligature as an icon by putting the ligature text in the content of the <md-icon>
409 * component. By default the Material icons font is used as described at
410 * http://google.github.io/material-design-icons/#icon-font-for-the-web. You can specify an
411 * alternate font by setting the fontSet input to either the CSS class to apply to use the
412 * desired font, or to an alias previously registered with MdIconRegistry.registerFontClassAlias.
413 * Examples:
414 * <md-icon>home</md-icon>
415 * <md-icon fontSet="myfont">sun</md-icon>
416 *
417 * - Specify a font glyph to be included via CSS rules by setting the fontSet input to specify the
418 * font, and the fontIcon input to specify the icon. Typically the fontIcon will specify a
419 * CSS class which causes the glyph to be displayed via a :before selector, as in
420 * https://fortawesome.github.io/Font-Awesome/examples/
421 * Example:
422 * <md-icon fontSet="fa" fontIcon="alarm"></md-icon>
423 */
424var MdIcon = (function () {
425 function MdIcon(_element, _renderer, _mdIconRegistry) {
426 this._element = _element;
427 this._renderer = _renderer;
428 this._mdIconRegistry = _mdIconRegistry;
429 this.hostAriaLabel = '';
430 }
431 /**
432 * Splits an svgIcon binding value into its icon set and icon name components.
433 * Returns a 2-element array of [(icon set), (icon name)].
434 * The separator for the two fields is ':'. If there is no separator, an empty
435 * string is returned for the icon set and the entire value is returned for
436 * the icon name. If the argument is falsy, returns an array of two empty strings.
437 * Throws a MdIconInvalidNameError if the name contains two or more ':' separators.
438 * Examples:
439 * 'social:cake' -> ['social', 'cake']
440 * 'penguin' -> ['', 'penguin']
441 * null -> ['', '']
442 * 'a:b:c' -> (throws MdIconInvalidNameError)
443 */
444 MdIcon.prototype._splitIconName = function (iconName) {
445 if (!iconName) {
446 return ['', ''];
447 }
448 var parts = iconName.split(':');
449 switch (parts.length) {
450 case 1:
451 // Use default namespace.
452 return ['', parts[0]];
453 case 2:
454 return parts;
455 default:
456 throw new MdIconInvalidNameError(iconName);
457 }
458 };
459 /** TODO: internal */
460 MdIcon.prototype.ngOnChanges = function (changes) {
461 var _this = this;
462 var changedInputs = Object.keys(changes);
463 // Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
464 if (changedInputs.indexOf('svgIcon') != -1 || changedInputs.indexOf('svgSrc') != -1) {
465 if (this.svgIcon) {
466 var _a = this._splitIconName(this.svgIcon), namespace = _a[0], iconName = _a[1];
467 this._mdIconRegistry.getNamedSvgIcon(iconName, namespace).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
468 }
469 else if (this.svgSrc) {
470 this._mdIconRegistry.getSvgIconFromUrl(this.svgSrc).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
471 }
472 }
473 if (this._usingFontIcon()) {
474 this._updateFontIconClasses();
475 }
476 this._updateAriaLabel();
477 };
478 /** TODO: internal */
479 MdIcon.prototype.ngOnInit = function () {
480 // Update font classes because ngOnChanges won't be called if none of the inputs are present,
481 // e.g. <md-icon>arrow</md-icon>. In this case we need to add a CSS class for the default font.
482 if (this._usingFontIcon()) {
483 this._updateFontIconClasses();
484 }
485 };
486 /** TODO: internal */
487 MdIcon.prototype.ngAfterViewChecked = function () {
488 // Update aria label here because it may depend on the projected text content.
489 // (e.g. <md-icon>home</md-icon> should use 'home').
490 this._updateAriaLabel();
491 };
492 MdIcon.prototype._updateAriaLabel = function () {
493 var ariaLabel = this._getAriaLabel();
494 if (ariaLabel) {
495 this._renderer.setElementAttribute(this._element.nativeElement, 'aria-label', ariaLabel);
496 }
497 };
498 MdIcon.prototype._getAriaLabel = function () {
499 // If the parent provided an aria-label attribute value, use it as-is. Otherwise look for a
500 // reasonable value from the alt attribute, font icon name, SVG icon name, or (for ligatures)
501 // the text content of the directive.
502 var label = this.hostAriaLabel ||
503 this.alt ||
504 this.fontIcon ||
505 this._splitIconName(this.svgIcon)[1];
506 if (label) {
507 return label;
508 }
509 // The "content" of an SVG icon is not a useful label.
510 if (this._usingFontIcon()) {
511 var text = this._element.nativeElement.textContent;
512 if (text) {
513 return text;
514 }
515 }
516 // TODO: Warn here in dev mode.
517 return null;
518 };
519 MdIcon.prototype._usingFontIcon = function () {
520 return !(this.svgIcon || this.svgSrc);
521 };
522 MdIcon.prototype._setSvgElement = function (svg) {
523 var layoutElement = this._element.nativeElement;
524 // Remove existing child nodes and add the new SVG element.
525 // We would use renderer.detachView(Array.from(layoutElement.childNodes)) here,
526 // but it fails in IE11: https://github.com/angular/angular/issues/6327
527 layoutElement.innerHTML = '';
528 this._renderer.projectNodes(layoutElement, [svg]);
529 };
530 MdIcon.prototype._updateFontIconClasses = function () {
531 if (!this._usingFontIcon()) {
532 return;
533 }
534 var elem = this._element.nativeElement;
535 var fontSetClass = this.fontSet ?
536 this._mdIconRegistry.classNameForFontAlias(this.fontSet) :
537 this._mdIconRegistry.getDefaultFontSetClass();
538 if (fontSetClass != this._previousFontSetClass) {
539 if (this._previousFontSetClass) {
540 this._renderer.setElementClass(elem, this._previousFontSetClass, false);
541 }
542 if (fontSetClass) {
543 this._renderer.setElementClass(elem, fontSetClass, true);
544 }
545 this._previousFontSetClass = fontSetClass;
546 }
547 if (this.fontIcon != this._previousFontIconClass) {
548 if (this._previousFontIconClass) {
549 this._renderer.setElementClass(elem, this._previousFontIconClass, false);
550 }
551 if (this.fontIcon) {
552 this._renderer.setElementClass(elem, this.fontIcon, true);
553 }
554 this._previousFontIconClass = this.fontIcon;
555 }
556 };
557 __decorate([
558 _angular_core.Input(),
559 __metadata('design:type', String)
560 ], MdIcon.prototype, "svgSrc", void 0);
561 __decorate([
562 _angular_core.Input(),
563 __metadata('design:type', String)
564 ], MdIcon.prototype, "svgIcon", void 0);
565 __decorate([
566 _angular_core.Input(),
567 __metadata('design:type', String)
568 ], MdIcon.prototype, "fontSet", void 0);
569 __decorate([
570 _angular_core.Input(),
571 __metadata('design:type', String)
572 ], MdIcon.prototype, "fontIcon", void 0);
573 __decorate([
574 _angular_core.Input(),
575 __metadata('design:type', String)
576 ], MdIcon.prototype, "alt", void 0);
577 __decorate([
578 _angular_core.Input('aria-label'),
579 __metadata('design:type', String)
580 ], MdIcon.prototype, "hostAriaLabel", void 0);
581 MdIcon = __decorate([
582 _angular_core.Component({template: '<ng-content></ng-content>',
583 selector: 'md-icon',
584 styles: ["/** The width/height of the icon element. */ /** This works because we're using ViewEncapsulation.None. If we used the default encapsulation, the selector would need to be \":host\". */ md-icon { background-repeat: no-repeat; display: inline-block; fill: currentColor; height: 24px; width: 24px; } /*# sourceMappingURL=icon.css.map */ "],
585 host: {
586 'role': 'img',
587 },
588 encapsulation: _angular_core.ViewEncapsulation.None,
589 changeDetection: _angular_core.ChangeDetectionStrategy.OnPush,
590 }),
591 __metadata('design:paramtypes', [_angular_core.ElementRef, _angular_core.Renderer, MdIconRegistry])
592 ], MdIcon);
593 return MdIcon;
594}());
595var MdIconModule = (function () {
596 function MdIconModule() {
597 }
598 MdIconModule.forRoot = function () {
599 return {
600 ngModule: MdIconModule,
601 providers: [MdIconRegistry],
602 };
603 };
604 MdIconModule = __decorate([
605 _angular_core.NgModule({
606 imports: [_angular_http.HttpModule],
607 exports: [MdIcon],
608 declarations: [MdIcon],
609 }),
610 __metadata('design:paramtypes', [])
611 ], MdIconModule);
612 return MdIconModule;
613}());
614
615exports.MdIconInvalidNameError = MdIconInvalidNameError;
616exports.MdIcon = MdIcon;
617exports.MdIconModule = MdIconModule;
618exports.MdIconRegistry = MdIconRegistry;
619
620Object.defineProperty(exports, '__esModule', { value: true });
621
622})));
\No newline at end of file