-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
icon.ts
202 lines (184 loc) · 7.2 KB
/
icon.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
ChangeDetectionStrategy,
Component,
ElementRef,
Input,
OnChanges,
OnInit,
Renderer2,
SimpleChange,
ViewEncapsulation,
Attribute,
} from '@angular/core';
import {MdIconRegistry} from './icon-registry';
import {CanColor, mixinColor} from '../core/common-behaviors/color';
import {first} from '../core/rxjs/index';
// Boilerplate for applying mixins to MdIcon.
/** @docs-private */
export class MdIconBase {
constructor(public _renderer: Renderer2, public _elementRef: ElementRef) {}
}
export const _MdIconMixinBase = mixinColor(MdIconBase);
/**
* Component to display an icon. It can be used in the following ways:
* - Specify the svgSrc input to load an SVG icon from a URL. The SVG content is directly inlined
* as a child of the <md-icon> component, so that CSS styles can easily be applied to it.
* The URL is loaded via an XMLHttpRequest, so it must be on the same domain as the page or its
* server must be configured to allow cross-domain requests.
* Example:
* <md-icon svgSrc="assets/arrow.svg"></md-icon>
*
* - Specify the svgIcon input to load an SVG icon from a URL previously registered with the
* addSvgIcon, addSvgIconInNamespace, addSvgIconSet, or addSvgIconSetInNamespace methods of
* MdIconRegistry. If the svgIcon value contains a colon it is assumed to be in the format
* "[namespace]:[name]", if not the value will be the name of an icon in the default namespace.
* Examples:
* <md-icon svgIcon="left-arrow"></md-icon>
* <md-icon svgIcon="animals:cat"></md-icon>
*
* - Use a font ligature as an icon by putting the ligature text in the content of the <md-icon>
* component. By default the Material icons font is used as described at
* http://google.github.io/material-design-icons/#icon-font-for-the-web. You can specify an
* alternate font by setting the fontSet input to either the CSS class to apply to use the
* desired font, or to an alias previously registered with MdIconRegistry.registerFontClassAlias.
* Examples:
* <md-icon>home</md-icon>
* <md-icon fontSet="myfont">sun</md-icon>
*
* - Specify a font glyph to be included via CSS rules by setting the fontSet input to specify the
* font, and the fontIcon input to specify the icon. Typically the fontIcon will specify a
* CSS class which causes the glyph to be displayed via a :before selector, as in
* https://fortawesome.github.io/Font-Awesome/examples/
* Example:
* <md-icon fontSet="fa" fontIcon="alarm"></md-icon>
*/
@Component({
moduleId: module.id,
template: '<ng-content></ng-content>',
selector: 'md-icon, mat-icon',
styleUrls: ['icon.css'],
inputs: ['color'],
host: {
'role': 'img',
'class': 'mat-icon',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdIcon extends _MdIconMixinBase implements OnChanges, OnInit, CanColor {
/** Name of the icon in the SVG icon set. */
@Input() svgIcon: string;
/** Font set that the icon is a part of. */
@Input() fontSet: string;
/** Name of an icon within a font set. */
@Input() fontIcon: string;
private _previousFontSetClass: string;
private _previousFontIconClass: string;
constructor(
renderer: Renderer2,
elementRef: ElementRef,
private _mdIconRegistry: MdIconRegistry,
@Attribute('aria-hidden') ariaHidden: string) {
super(renderer, elementRef);
// If the user has not explicitly set aria-hidden, mark the icon as hidden, as this is
// the right thing to do for the majority of icon use-cases.
if (!ariaHidden) {
renderer.setAttribute(elementRef.nativeElement, 'aria-hidden', 'true');
}
}
/**
* Splits an svgIcon binding value into its icon set and icon name components.
* Returns a 2-element array of [(icon set), (icon name)].
* The separator for the two fields is ':'. If there is no separator, an empty
* string is returned for the icon set and the entire value is returned for
* the icon name. If the argument is falsy, returns an array of two empty strings.
* Throws an error if the name contains two or more ':' separators.
* Examples:
* 'social:cake' -> ['social', 'cake']
* 'penguin' -> ['', 'penguin']
* null -> ['', '']
* 'a:b:c' -> (throws Error)
*/
private _splitIconName(iconName: string): [string, string] {
if (!iconName) {
return ['', ''];
}
const parts = iconName.split(':');
switch (parts.length) {
case 1:
// Use default namespace.
return ['', parts[0]];
case 2:
return <[string, string]>parts;
default:
throw Error(`Invalid icon name: "${iconName}"`);
}
}
ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
const changedInputs = Object.keys(changes);
// Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
if (changedInputs.indexOf('svgIcon') != -1 || changedInputs.indexOf('svgSrc') != -1) {
if (this.svgIcon) {
const [namespace, iconName] = this._splitIconName(this.svgIcon);
first.call(this._mdIconRegistry.getNamedSvgIcon(iconName, namespace)).subscribe(
svg => this._setSvgElement(svg),
(err: Error) => console.log(`Error retrieving icon: ${err.message}`));
}
}
if (this._usingFontIcon()) {
this._updateFontIconClasses();
}
}
ngOnInit() {
// Update font classes because ngOnChanges won't be called if none of the inputs are present,
// e.g. <md-icon>arrow</md-icon>. In this case we need to add a CSS class for the default font.
if (this._usingFontIcon()) {
this._updateFontIconClasses();
}
}
private _usingFontIcon(): boolean {
return !this.svgIcon;
}
private _setSvgElement(svg: SVGElement) {
const layoutElement = this._elementRef.nativeElement;
// Remove existing child nodes and add the new SVG element.
// We would use renderer.detachView(Array.from(layoutElement.childNodes)) here,
// but it fails in IE11: https://github.com/angular/angular/issues/6327
layoutElement.innerHTML = '';
this._renderer.appendChild(layoutElement, svg);
}
private _updateFontIconClasses() {
if (!this._usingFontIcon()) {
return;
}
const elem = this._elementRef.nativeElement;
const fontSetClass = this.fontSet ?
this._mdIconRegistry.classNameForFontAlias(this.fontSet) :
this._mdIconRegistry.getDefaultFontSetClass();
if (fontSetClass != this._previousFontSetClass) {
if (this._previousFontSetClass) {
this._renderer.removeClass(elem, this._previousFontSetClass);
}
if (fontSetClass) {
this._renderer.addClass(elem, fontSetClass);
}
this._previousFontSetClass = fontSetClass;
}
if (this.fontIcon != this._previousFontIconClass) {
if (this._previousFontIconClass) {
this._renderer.removeClass(elem, this._previousFontIconClass);
}
if (this.fontIcon) {
this._renderer.addClass(elem, this.fontIcon);
}
this._previousFontIconClass = this.fontIcon;
}
}
}