-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon.js
238 lines (238 loc) · 11.2 KB
/
icon.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
"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 error_1 = require('@angular2-material/core/errors/error');
var icon_registry_1 = require('./icon-registry');
var icon_registry_2 = require('./icon-registry');
exports.MdIconRegistry = icon_registry_2.MdIconRegistry;
/** Exception thrown when an invalid icon name is passed to an md-icon component. */
var MdIconInvalidNameError = (function (_super) {
__extends(MdIconInvalidNameError, _super);
function MdIconInvalidNameError(iconName) {
_super.call(this, "Invalid icon name: \"" + iconName + "\"");
}
return MdIconInvalidNameError;
}(error_1.MdError));
exports.MdIconInvalidNameError = MdIconInvalidNameError;
/**
* 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>
*/
var MdIcon = (function () {
function MdIcon(_element, _renderer, _mdIconRegistry) {
this._element = _element;
this._renderer = _renderer;
this._mdIconRegistry = _mdIconRegistry;
this.hostAriaLabel = '';
}
/**
* 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 a MdIconInvalidNameError if the name contains two or more ':' separators.
* Examples:
* 'social:cake' -> ['social', 'cake']
* 'penguin' -> ['', 'penguin']
* null -> ['', '']
* 'a:b:c' -> (throws MdIconInvalidNameError)
*/
MdIcon.prototype._splitIconName = function (iconName) {
if (!iconName) {
return ['', ''];
}
var parts = iconName.split(':');
switch (parts.length) {
case 1:
// Use default namespace.
return ['', parts[0]];
case 2:
return parts;
default:
throw new MdIconInvalidNameError(iconName);
}
};
/** TODO: internal */
MdIcon.prototype.ngOnChanges = function (changes) {
var _this = this;
var 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) {
var _a = this._splitIconName(this.svgIcon), namespace = _a[0], iconName = _a[1];
this._mdIconRegistry.getNamedSvgIcon(iconName, namespace).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
}
else if (this.svgSrc) {
this._mdIconRegistry.getSvgIconFromUrl(this.svgSrc).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
}
}
if (this._usingFontIcon()) {
this._updateFontIconClasses();
}
this._updateAriaLabel();
};
/** TODO: internal */
MdIcon.prototype.ngOnInit = function () {
// 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();
}
};
/** TODO: internal */
MdIcon.prototype.ngAfterViewChecked = function () {
// Update aria label here because it may depend on the projected text content.
// (e.g. <md-icon>home</md-icon> should use 'home').
this._updateAriaLabel();
};
MdIcon.prototype._updateAriaLabel = function () {
var ariaLabel = this._getAriaLabel();
if (ariaLabel) {
this._renderer.setElementAttribute(this._element.nativeElement, 'aria-label', ariaLabel);
}
};
MdIcon.prototype._getAriaLabel = function () {
// If the parent provided an aria-label attribute value, use it as-is. Otherwise look for a
// reasonable value from the alt attribute, font icon name, SVG icon name, or (for ligatures)
// the text content of the directive.
var label = this.hostAriaLabel ||
this.alt ||
this.fontIcon ||
this._splitIconName(this.svgIcon)[1];
if (label) {
return label;
}
// The "content" of an SVG icon is not a useful label.
if (this._usingFontIcon()) {
var text = this._element.nativeElement.textContent;
if (text) {
return text;
}
}
// TODO: Warn here in dev mode.
return null;
};
MdIcon.prototype._usingFontIcon = function () {
return !(this.svgIcon || this.svgSrc);
};
MdIcon.prototype._setSvgElement = function (svg) {
var layoutElement = this._element.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.projectNodes(layoutElement, [svg]);
};
MdIcon.prototype._updateFontIconClasses = function () {
if (!this._usingFontIcon()) {
return;
}
var elem = this._element.nativeElement;
var fontSetClass = this.fontSet ?
this._mdIconRegistry.classNameForFontAlias(this.fontSet) :
this._mdIconRegistry.getDefaultFontSetClass();
if (fontSetClass != this._previousFontSetClass) {
if (this._previousFontSetClass) {
this._renderer.setElementClass(elem, this._previousFontSetClass, false);
}
if (fontSetClass) {
this._renderer.setElementClass(elem, fontSetClass, true);
}
this._previousFontSetClass = fontSetClass;
}
if (this.fontIcon != this._previousFontIconClass) {
if (this._previousFontIconClass) {
this._renderer.setElementClass(elem, this._previousFontIconClass, false);
}
if (this.fontIcon) {
this._renderer.setElementClass(elem, this.fontIcon, true);
}
this._previousFontIconClass = this.fontIcon;
}
};
__decorate([
core_1.Input(),
__metadata('design:type', String)
], MdIcon.prototype, "svgSrc", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], MdIcon.prototype, "svgIcon", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], MdIcon.prototype, "fontSet", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], MdIcon.prototype, "fontIcon", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], MdIcon.prototype, "alt", void 0);
__decorate([
core_1.Input('aria-label'),
__metadata('design:type', String)
], MdIcon.prototype, "hostAriaLabel", void 0);
MdIcon = __decorate([
core_1.Component({
moduleId: module.id,
template: '<ng-content></ng-content>',
selector: 'md-icon',
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; } "],
host: {
'role': 'img',
},
encapsulation: core_1.ViewEncapsulation.None,
changeDetection: core_1.ChangeDetectionStrategy.OnPush,
}),
__metadata('design:paramtypes', [core_1.ElementRef, core_1.Renderer, icon_registry_1.MdIconRegistry])
], MdIcon);
return MdIcon;
}());
exports.MdIcon = MdIcon;
exports.MD_ICON_DIRECTIVES = [MdIcon];
//# sourceMappingURL=icon.js.map