UNPKG

11.6 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || function (d, b) {
2 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3 function __() { this.constructor = d; }
4 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
5};
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 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;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12var __metadata = (this && this.__metadata) || function (k, v) {
13 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
14};
15import { NgModule, ChangeDetectionStrategy, Component, ElementRef, Input, Renderer, ViewEncapsulation } from '@angular/core';
16import { HttpModule } from '@angular/http';
17import { MdError } from '@angular2-material/core';
18import { MdIconRegistry } from './icon-registry';
19export { MdIconRegistry } from './icon-registry';
20/** Exception thrown when an invalid icon name is passed to an md-icon component. */
21export var MdIconInvalidNameError = (function (_super) {
22 __extends(MdIconInvalidNameError, _super);
23 function MdIconInvalidNameError(iconName) {
24 _super.call(this, "Invalid icon name: \"" + iconName + "\"");
25 }
26 return MdIconInvalidNameError;
27}(MdError));
28/**
29 * Component to display an icon. It can be used in the following ways:
30 * - Specify the svgSrc input to load an SVG icon from a URL. The SVG content is directly inlined
31 * as a child of the <md-icon> component, so that CSS styles can easily be applied to it.
32 * The URL is loaded via an XMLHttpRequest, so it must be on the same domain as the page or its
33 * server must be configured to allow cross-domain requests.
34 * Example:
35 * <md-icon svgSrc="assets/arrow.svg"></md-icon>
36 *
37 * - Specify the svgIcon input to load an SVG icon from a URL previously registered with the
38 * addSvgIcon, addSvgIconInNamespace, addSvgIconSet, or addSvgIconSetInNamespace methods of
39 * MdIconRegistry. If the svgIcon value contains a colon it is assumed to be in the format
40 * "[namespace]:[name]", if not the value will be the name of an icon in the default namespace.
41 * Examples:
42 * <md-icon svgIcon="left-arrow"></md-icon>
43 * <md-icon svgIcon="animals:cat"></md-icon>
44 *
45 * - Use a font ligature as an icon by putting the ligature text in the content of the <md-icon>
46 * component. By default the Material icons font is used as described at
47 * http://google.github.io/material-design-icons/#icon-font-for-the-web. You can specify an
48 * alternate font by setting the fontSet input to either the CSS class to apply to use the
49 * desired font, or to an alias previously registered with MdIconRegistry.registerFontClassAlias.
50 * Examples:
51 * <md-icon>home</md-icon>
52 * <md-icon fontSet="myfont">sun</md-icon>
53 *
54 * - Specify a font glyph to be included via CSS rules by setting the fontSet input to specify the
55 * font, and the fontIcon input to specify the icon. Typically the fontIcon will specify a
56 * CSS class which causes the glyph to be displayed via a :before selector, as in
57 * https://fortawesome.github.io/Font-Awesome/examples/
58 * Example:
59 * <md-icon fontSet="fa" fontIcon="alarm"></md-icon>
60 */
61export var MdIcon = (function () {
62 function MdIcon(_element, _renderer, _mdIconRegistry) {
63 this._element = _element;
64 this._renderer = _renderer;
65 this._mdIconRegistry = _mdIconRegistry;
66 this.hostAriaLabel = '';
67 }
68 /**
69 * Splits an svgIcon binding value into its icon set and icon name components.
70 * Returns a 2-element array of [(icon set), (icon name)].
71 * The separator for the two fields is ':'. If there is no separator, an empty
72 * string is returned for the icon set and the entire value is returned for
73 * the icon name. If the argument is falsy, returns an array of two empty strings.
74 * Throws a MdIconInvalidNameError if the name contains two or more ':' separators.
75 * Examples:
76 * 'social:cake' -> ['social', 'cake']
77 * 'penguin' -> ['', 'penguin']
78 * null -> ['', '']
79 * 'a:b:c' -> (throws MdIconInvalidNameError)
80 */
81 MdIcon.prototype._splitIconName = function (iconName) {
82 if (!iconName) {
83 return ['', ''];
84 }
85 var parts = iconName.split(':');
86 switch (parts.length) {
87 case 1:
88 // Use default namespace.
89 return ['', parts[0]];
90 case 2:
91 return parts;
92 default:
93 throw new MdIconInvalidNameError(iconName);
94 }
95 };
96 /** TODO: internal */
97 MdIcon.prototype.ngOnChanges = function (changes) {
98 var _this = this;
99 var changedInputs = Object.keys(changes);
100 // Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
101 if (changedInputs.indexOf('svgIcon') != -1 || changedInputs.indexOf('svgSrc') != -1) {
102 if (this.svgIcon) {
103 var _a = this._splitIconName(this.svgIcon), namespace = _a[0], iconName = _a[1];
104 this._mdIconRegistry.getNamedSvgIcon(iconName, namespace).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
105 }
106 else if (this.svgSrc) {
107 this._mdIconRegistry.getSvgIconFromUrl(this.svgSrc).subscribe(function (svg) { return _this._setSvgElement(svg); }, function (err) { return console.log("Error retrieving icon: " + err); });
108 }
109 }
110 if (this._usingFontIcon()) {
111 this._updateFontIconClasses();
112 }
113 this._updateAriaLabel();
114 };
115 /** TODO: internal */
116 MdIcon.prototype.ngOnInit = function () {
117 // Update font classes because ngOnChanges won't be called if none of the inputs are present,
118 // e.g. <md-icon>arrow</md-icon>. In this case we need to add a CSS class for the default font.
119 if (this._usingFontIcon()) {
120 this._updateFontIconClasses();
121 }
122 };
123 /** TODO: internal */
124 MdIcon.prototype.ngAfterViewChecked = function () {
125 // Update aria label here because it may depend on the projected text content.
126 // (e.g. <md-icon>home</md-icon> should use 'home').
127 this._updateAriaLabel();
128 };
129 MdIcon.prototype._updateAriaLabel = function () {
130 var ariaLabel = this._getAriaLabel();
131 if (ariaLabel) {
132 this._renderer.setElementAttribute(this._element.nativeElement, 'aria-label', ariaLabel);
133 }
134 };
135 MdIcon.prototype._getAriaLabel = function () {
136 // If the parent provided an aria-label attribute value, use it as-is. Otherwise look for a
137 // reasonable value from the alt attribute, font icon name, SVG icon name, or (for ligatures)
138 // the text content of the directive.
139 var label = this.hostAriaLabel ||
140 this.alt ||
141 this.fontIcon ||
142 this._splitIconName(this.svgIcon)[1];
143 if (label) {
144 return label;
145 }
146 // The "content" of an SVG icon is not a useful label.
147 if (this._usingFontIcon()) {
148 var text = this._element.nativeElement.textContent;
149 if (text) {
150 return text;
151 }
152 }
153 // TODO: Warn here in dev mode.
154 return null;
155 };
156 MdIcon.prototype._usingFontIcon = function () {
157 return !(this.svgIcon || this.svgSrc);
158 };
159 MdIcon.prototype._setSvgElement = function (svg) {
160 var layoutElement = this._element.nativeElement;
161 // Remove existing child nodes and add the new SVG element.
162 // We would use renderer.detachView(Array.from(layoutElement.childNodes)) here,
163 // but it fails in IE11: https://github.com/angular/angular/issues/6327
164 layoutElement.innerHTML = '';
165 this._renderer.projectNodes(layoutElement, [svg]);
166 };
167 MdIcon.prototype._updateFontIconClasses = function () {
168 if (!this._usingFontIcon()) {
169 return;
170 }
171 var elem = this._element.nativeElement;
172 var fontSetClass = this.fontSet ?
173 this._mdIconRegistry.classNameForFontAlias(this.fontSet) :
174 this._mdIconRegistry.getDefaultFontSetClass();
175 if (fontSetClass != this._previousFontSetClass) {
176 if (this._previousFontSetClass) {
177 this._renderer.setElementClass(elem, this._previousFontSetClass, false);
178 }
179 if (fontSetClass) {
180 this._renderer.setElementClass(elem, fontSetClass, true);
181 }
182 this._previousFontSetClass = fontSetClass;
183 }
184 if (this.fontIcon != this._previousFontIconClass) {
185 if (this._previousFontIconClass) {
186 this._renderer.setElementClass(elem, this._previousFontIconClass, false);
187 }
188 if (this.fontIcon) {
189 this._renderer.setElementClass(elem, this.fontIcon, true);
190 }
191 this._previousFontIconClass = this.fontIcon;
192 }
193 };
194 __decorate([
195 Input(),
196 __metadata('design:type', String)
197 ], MdIcon.prototype, "svgSrc", void 0);
198 __decorate([
199 Input(),
200 __metadata('design:type', String)
201 ], MdIcon.prototype, "svgIcon", void 0);
202 __decorate([
203 Input(),
204 __metadata('design:type', String)
205 ], MdIcon.prototype, "fontSet", void 0);
206 __decorate([
207 Input(),
208 __metadata('design:type', String)
209 ], MdIcon.prototype, "fontIcon", void 0);
210 __decorate([
211 Input(),
212 __metadata('design:type', String)
213 ], MdIcon.prototype, "alt", void 0);
214 __decorate([
215 Input('aria-label'),
216 __metadata('design:type', String)
217 ], MdIcon.prototype, "hostAriaLabel", void 0);
218 MdIcon = __decorate([
219 Component({template: '<ng-content></ng-content>',
220 selector: 'md-icon',
221 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 */ "],
222 host: {
223 'role': 'img',
224 },
225 encapsulation: ViewEncapsulation.None,
226 changeDetection: ChangeDetectionStrategy.OnPush,
227 }),
228 __metadata('design:paramtypes', [ElementRef, Renderer, MdIconRegistry])
229 ], MdIcon);
230 return MdIcon;
231}());
232export var MdIconModule = (function () {
233 function MdIconModule() {
234 }
235 MdIconModule.forRoot = function () {
236 return {
237 ngModule: MdIconModule,
238 providers: [MdIconRegistry],
239 };
240 };
241 MdIconModule = __decorate([
242 NgModule({
243 imports: [HttpModule],
244 exports: [MdIcon],
245 declarations: [MdIcon],
246 }),
247 __metadata('design:paramtypes', [])
248 ], MdIconModule);
249 return MdIconModule;
250}());
251
252//# sourceMappingURL=icon.js.map