icon.es5.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { DOCUMENT } from '@angular/common';
  9. import { HttpClient } from '@angular/common/http';
  10. import { Inject, Injectable, Optional, SecurityContext, SkipSelf, NgModule, Attribute, ChangeDetectionStrategy, Component, ElementRef, Input, ViewEncapsulation, InjectionToken, inject, ɵɵdefineInjectable, ɵɵinject } from '@angular/core';
  11. import { DomSanitizer } from '@angular/platform-browser';
  12. import { forkJoin, of, throwError } from 'rxjs';
  13. import { catchError, finalize, map, share, tap, take } from 'rxjs/operators';
  14. import { __extends } from 'tslib';
  15. import { mixinColor, MatCommonModule } from '@angular/material/core';
  16. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  17. /**
  18. * @fileoverview added by tsickle
  19. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  20. */
  21. /**
  22. * Returns an exception to be thrown in the case when attempting to
  23. * load an icon with a name that cannot be found.
  24. * \@docs-private
  25. * @param {?} iconName
  26. * @return {?}
  27. */
  28. function getMatIconNameNotFoundError(iconName) {
  29. return Error("Unable to find icon with the name \"" + iconName + "\"");
  30. }
  31. /**
  32. * Returns an exception to be thrown when the consumer attempts to use
  33. * `<mat-icon>` without including \@angular/common/http.
  34. * \@docs-private
  35. * @return {?}
  36. */
  37. function getMatIconNoHttpProviderError() {
  38. return Error('Could not find HttpClient provider for use with Angular Material icons. ' +
  39. 'Please include the HttpClientModule from @angular/common/http in your ' +
  40. 'app imports.');
  41. }
  42. /**
  43. * Returns an exception to be thrown when a URL couldn't be sanitized.
  44. * \@docs-private
  45. * @param {?} url URL that was attempted to be sanitized.
  46. * @return {?}
  47. */
  48. function getMatIconFailedToSanitizeUrlError(url) {
  49. return Error("The URL provided to MatIconRegistry was not trusted as a resource URL " +
  50. ("via Angular's DomSanitizer. Attempted URL was \"" + url + "\"."));
  51. }
  52. /**
  53. * Returns an exception to be thrown when a HTML string couldn't be sanitized.
  54. * \@docs-private
  55. * @param {?} literal HTML that was attempted to be sanitized.
  56. * @return {?}
  57. */
  58. function getMatIconFailedToSanitizeLiteralError(literal) {
  59. return Error("The literal provided to MatIconRegistry was not trusted as safe HTML by " +
  60. ("Angular's DomSanitizer. Attempted literal was \"" + literal + "\"."));
  61. }
  62. /**
  63. * Configuration for an icon, including the URL and possibly the cached SVG element.
  64. * \@docs-private
  65. */
  66. var /**
  67. * Configuration for an icon, including the URL and possibly the cached SVG element.
  68. * \@docs-private
  69. */
  70. SvgIconConfig = /** @class */ (function () {
  71. function SvgIconConfig(data) {
  72. // Note that we can't use `instanceof SVGElement` here,
  73. // because it'll break during server-side rendering.
  74. if (!!((/** @type {?} */ (data))).nodeName) {
  75. this.svgElement = (/** @type {?} */ (data));
  76. }
  77. else {
  78. this.url = (/** @type {?} */ (data));
  79. }
  80. }
  81. return SvgIconConfig;
  82. }());
  83. /**
  84. * Service to register and display icons used by the `<mat-icon>` component.
  85. * - Registers icon URLs by namespace and name.
  86. * - Registers icon set URLs by namespace.
  87. * - Registers aliases for CSS classes, for use with icon fonts.
  88. * - Loads icons from URLs and extracts individual icons from icon sets.
  89. */
  90. var MatIconRegistry = /** @class */ (function () {
  91. function MatIconRegistry(_httpClient, _sanitizer, document) {
  92. this._httpClient = _httpClient;
  93. this._sanitizer = _sanitizer;
  94. /**
  95. * URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
  96. */
  97. this._svgIconConfigs = new Map();
  98. /**
  99. * SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
  100. * Multiple icon sets can be registered under the same namespace.
  101. */
  102. this._iconSetConfigs = new Map();
  103. /**
  104. * Cache for icons loaded by direct URLs.
  105. */
  106. this._cachedIconsByUrl = new Map();
  107. /**
  108. * In-progress icon fetches. Used to coalesce multiple requests to the same URL.
  109. */
  110. this._inProgressUrlFetches = new Map();
  111. /**
  112. * Map from font identifiers to their CSS class names. Used for icon fonts.
  113. */
  114. this._fontCssClassesByAlias = new Map();
  115. /**
  116. * The CSS class to apply when an `<mat-icon>` component has no icon name, url, or font specified.
  117. * The default 'material-icons' value assumes that the material icon font has been loaded as
  118. * described at http://google.github.io/material-design-icons/#icon-font-for-the-web
  119. */
  120. this._defaultFontSetClass = 'material-icons';
  121. this._document = document;
  122. }
  123. /**
  124. * Registers an icon by URL in the default namespace.
  125. * @param iconName Name under which the icon should be registered.
  126. * @param url
  127. */
  128. /**
  129. * Registers an icon by URL in the default namespace.
  130. * @template THIS
  131. * @this {THIS}
  132. * @param {?} iconName Name under which the icon should be registered.
  133. * @param {?} url
  134. * @return {THIS}
  135. */
  136. MatIconRegistry.prototype.addSvgIcon = /**
  137. * Registers an icon by URL in the default namespace.
  138. * @template THIS
  139. * @this {THIS}
  140. * @param {?} iconName Name under which the icon should be registered.
  141. * @param {?} url
  142. * @return {THIS}
  143. */
  144. function (iconName, url) {
  145. return (/** @type {?} */ (this)).addSvgIconInNamespace('', iconName, url);
  146. };
  147. /**
  148. * Registers an icon using an HTML string in the default namespace.
  149. * @param iconName Name under which the icon should be registered.
  150. * @param literal SVG source of the icon.
  151. */
  152. /**
  153. * Registers an icon using an HTML string in the default namespace.
  154. * @template THIS
  155. * @this {THIS}
  156. * @param {?} iconName Name under which the icon should be registered.
  157. * @param {?} literal SVG source of the icon.
  158. * @return {THIS}
  159. */
  160. MatIconRegistry.prototype.addSvgIconLiteral = /**
  161. * Registers an icon using an HTML string in the default namespace.
  162. * @template THIS
  163. * @this {THIS}
  164. * @param {?} iconName Name under which the icon should be registered.
  165. * @param {?} literal SVG source of the icon.
  166. * @return {THIS}
  167. */
  168. function (iconName, literal) {
  169. return (/** @type {?} */ (this)).addSvgIconLiteralInNamespace('', iconName, literal);
  170. };
  171. /**
  172. * Registers an icon by URL in the specified namespace.
  173. * @param namespace Namespace in which the icon should be registered.
  174. * @param iconName Name under which the icon should be registered.
  175. * @param url
  176. */
  177. /**
  178. * Registers an icon by URL in the specified namespace.
  179. * @template THIS
  180. * @this {THIS}
  181. * @param {?} namespace Namespace in which the icon should be registered.
  182. * @param {?} iconName Name under which the icon should be registered.
  183. * @param {?} url
  184. * @return {THIS}
  185. */
  186. MatIconRegistry.prototype.addSvgIconInNamespace = /**
  187. * Registers an icon by URL in the specified namespace.
  188. * @template THIS
  189. * @this {THIS}
  190. * @param {?} namespace Namespace in which the icon should be registered.
  191. * @param {?} iconName Name under which the icon should be registered.
  192. * @param {?} url
  193. * @return {THIS}
  194. */
  195. function (namespace, iconName, url) {
  196. return (/** @type {?} */ (this))._addSvgIconConfig(namespace, iconName, new SvgIconConfig(url));
  197. };
  198. /**
  199. * Registers an icon using an HTML string in the specified namespace.
  200. * @param namespace Namespace in which the icon should be registered.
  201. * @param iconName Name under which the icon should be registered.
  202. * @param literal SVG source of the icon.
  203. */
  204. /**
  205. * Registers an icon using an HTML string in the specified namespace.
  206. * @template THIS
  207. * @this {THIS}
  208. * @param {?} namespace Namespace in which the icon should be registered.
  209. * @param {?} iconName Name under which the icon should be registered.
  210. * @param {?} literal SVG source of the icon.
  211. * @return {THIS}
  212. */
  213. MatIconRegistry.prototype.addSvgIconLiteralInNamespace = /**
  214. * Registers an icon using an HTML string in the specified namespace.
  215. * @template THIS
  216. * @this {THIS}
  217. * @param {?} namespace Namespace in which the icon should be registered.
  218. * @param {?} iconName Name under which the icon should be registered.
  219. * @param {?} literal SVG source of the icon.
  220. * @return {THIS}
  221. */
  222. function (namespace, iconName, literal) {
  223. /** @type {?} */
  224. var sanitizedLiteral = (/** @type {?} */ (this))._sanitizer.sanitize(SecurityContext.HTML, literal);
  225. if (!sanitizedLiteral) {
  226. throw getMatIconFailedToSanitizeLiteralError(literal);
  227. }
  228. /** @type {?} */
  229. var svgElement = (/** @type {?} */ (this))._createSvgElementForSingleIcon(sanitizedLiteral);
  230. return (/** @type {?} */ (this))._addSvgIconConfig(namespace, iconName, new SvgIconConfig(svgElement));
  231. };
  232. /**
  233. * Registers an icon set by URL in the default namespace.
  234. * @param url
  235. */
  236. /**
  237. * Registers an icon set by URL in the default namespace.
  238. * @template THIS
  239. * @this {THIS}
  240. * @param {?} url
  241. * @return {THIS}
  242. */
  243. MatIconRegistry.prototype.addSvgIconSet = /**
  244. * Registers an icon set by URL in the default namespace.
  245. * @template THIS
  246. * @this {THIS}
  247. * @param {?} url
  248. * @return {THIS}
  249. */
  250. function (url) {
  251. return (/** @type {?} */ (this)).addSvgIconSetInNamespace('', url);
  252. };
  253. /**
  254. * Registers an icon set using an HTML string in the default namespace.
  255. * @param literal SVG source of the icon set.
  256. */
  257. /**
  258. * Registers an icon set using an HTML string in the default namespace.
  259. * @template THIS
  260. * @this {THIS}
  261. * @param {?} literal SVG source of the icon set.
  262. * @return {THIS}
  263. */
  264. MatIconRegistry.prototype.addSvgIconSetLiteral = /**
  265. * Registers an icon set using an HTML string in the default namespace.
  266. * @template THIS
  267. * @this {THIS}
  268. * @param {?} literal SVG source of the icon set.
  269. * @return {THIS}
  270. */
  271. function (literal) {
  272. return (/** @type {?} */ (this)).addSvgIconSetLiteralInNamespace('', literal);
  273. };
  274. /**
  275. * Registers an icon set by URL in the specified namespace.
  276. * @param namespace Namespace in which to register the icon set.
  277. * @param url
  278. */
  279. /**
  280. * Registers an icon set by URL in the specified namespace.
  281. * @template THIS
  282. * @this {THIS}
  283. * @param {?} namespace Namespace in which to register the icon set.
  284. * @param {?} url
  285. * @return {THIS}
  286. */
  287. MatIconRegistry.prototype.addSvgIconSetInNamespace = /**
  288. * Registers an icon set by URL in the specified namespace.
  289. * @template THIS
  290. * @this {THIS}
  291. * @param {?} namespace Namespace in which to register the icon set.
  292. * @param {?} url
  293. * @return {THIS}
  294. */
  295. function (namespace, url) {
  296. return (/** @type {?} */ (this))._addSvgIconSetConfig(namespace, new SvgIconConfig(url));
  297. };
  298. /**
  299. * Registers an icon set using an HTML string in the specified namespace.
  300. * @param namespace Namespace in which to register the icon set.
  301. * @param literal SVG source of the icon set.
  302. */
  303. /**
  304. * Registers an icon set using an HTML string in the specified namespace.
  305. * @template THIS
  306. * @this {THIS}
  307. * @param {?} namespace Namespace in which to register the icon set.
  308. * @param {?} literal SVG source of the icon set.
  309. * @return {THIS}
  310. */
  311. MatIconRegistry.prototype.addSvgIconSetLiteralInNamespace = /**
  312. * Registers an icon set using an HTML string in the specified namespace.
  313. * @template THIS
  314. * @this {THIS}
  315. * @param {?} namespace Namespace in which to register the icon set.
  316. * @param {?} literal SVG source of the icon set.
  317. * @return {THIS}
  318. */
  319. function (namespace, literal) {
  320. /** @type {?} */
  321. var sanitizedLiteral = (/** @type {?} */ (this))._sanitizer.sanitize(SecurityContext.HTML, literal);
  322. if (!sanitizedLiteral) {
  323. throw getMatIconFailedToSanitizeLiteralError(literal);
  324. }
  325. /** @type {?} */
  326. var svgElement = (/** @type {?} */ (this))._svgElementFromString(sanitizedLiteral);
  327. return (/** @type {?} */ (this))._addSvgIconSetConfig(namespace, new SvgIconConfig(svgElement));
  328. };
  329. /**
  330. * Defines an alias for a CSS class name to be used for icon fonts. Creating an matIcon
  331. * component with the alias as the fontSet input will cause the class name to be applied
  332. * to the `<mat-icon>` element.
  333. *
  334. * @param alias Alias for the font.
  335. * @param className Class name override to be used instead of the alias.
  336. */
  337. /**
  338. * Defines an alias for a CSS class name to be used for icon fonts. Creating an matIcon
  339. * component with the alias as the fontSet input will cause the class name to be applied
  340. * to the `<mat-icon>` element.
  341. *
  342. * @template THIS
  343. * @this {THIS}
  344. * @param {?} alias Alias for the font.
  345. * @param {?=} className Class name override to be used instead of the alias.
  346. * @return {THIS}
  347. */
  348. MatIconRegistry.prototype.registerFontClassAlias = /**
  349. * Defines an alias for a CSS class name to be used for icon fonts. Creating an matIcon
  350. * component with the alias as the fontSet input will cause the class name to be applied
  351. * to the `<mat-icon>` element.
  352. *
  353. * @template THIS
  354. * @this {THIS}
  355. * @param {?} alias Alias for the font.
  356. * @param {?=} className Class name override to be used instead of the alias.
  357. * @return {THIS}
  358. */
  359. function (alias, className) {
  360. if (className === void 0) { className = alias; }
  361. (/** @type {?} */ (this))._fontCssClassesByAlias.set(alias, className);
  362. return (/** @type {?} */ (this));
  363. };
  364. /**
  365. * Returns the CSS class name associated with the alias by a previous call to
  366. * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
  367. */
  368. /**
  369. * Returns the CSS class name associated with the alias by a previous call to
  370. * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
  371. * @param {?} alias
  372. * @return {?}
  373. */
  374. MatIconRegistry.prototype.classNameForFontAlias = /**
  375. * Returns the CSS class name associated with the alias by a previous call to
  376. * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
  377. * @param {?} alias
  378. * @return {?}
  379. */
  380. function (alias) {
  381. return this._fontCssClassesByAlias.get(alias) || alias;
  382. };
  383. /**
  384. * Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  385. * have a fontSet input value, and is not loading an icon by name or URL.
  386. *
  387. * @param className
  388. */
  389. /**
  390. * Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  391. * have a fontSet input value, and is not loading an icon by name or URL.
  392. *
  393. * @template THIS
  394. * @this {THIS}
  395. * @param {?} className
  396. * @return {THIS}
  397. */
  398. MatIconRegistry.prototype.setDefaultFontSetClass = /**
  399. * Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  400. * have a fontSet input value, and is not loading an icon by name or URL.
  401. *
  402. * @template THIS
  403. * @this {THIS}
  404. * @param {?} className
  405. * @return {THIS}
  406. */
  407. function (className) {
  408. (/** @type {?} */ (this))._defaultFontSetClass = className;
  409. return (/** @type {?} */ (this));
  410. };
  411. /**
  412. * Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  413. * have a fontSet input value, and is not loading an icon by name or URL.
  414. */
  415. /**
  416. * Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  417. * have a fontSet input value, and is not loading an icon by name or URL.
  418. * @return {?}
  419. */
  420. MatIconRegistry.prototype.getDefaultFontSetClass = /**
  421. * Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  422. * have a fontSet input value, and is not loading an icon by name or URL.
  423. * @return {?}
  424. */
  425. function () {
  426. return this._defaultFontSetClass;
  427. };
  428. /**
  429. * Returns an Observable that produces the icon (as an `<svg>` DOM element) from the given URL.
  430. * The response from the URL may be cached so this will not always cause an HTTP request, but
  431. * the produced element will always be a new copy of the originally fetched icon. (That is,
  432. * it will not contain any modifications made to elements previously returned).
  433. *
  434. * @param safeUrl URL from which to fetch the SVG icon.
  435. */
  436. /**
  437. * Returns an Observable that produces the icon (as an `<svg>` DOM element) from the given URL.
  438. * The response from the URL may be cached so this will not always cause an HTTP request, but
  439. * the produced element will always be a new copy of the originally fetched icon. (That is,
  440. * it will not contain any modifications made to elements previously returned).
  441. *
  442. * @param {?} safeUrl URL from which to fetch the SVG icon.
  443. * @return {?}
  444. */
  445. MatIconRegistry.prototype.getSvgIconFromUrl = /**
  446. * Returns an Observable that produces the icon (as an `<svg>` DOM element) from the given URL.
  447. * The response from the URL may be cached so this will not always cause an HTTP request, but
  448. * the produced element will always be a new copy of the originally fetched icon. (That is,
  449. * it will not contain any modifications made to elements previously returned).
  450. *
  451. * @param {?} safeUrl URL from which to fetch the SVG icon.
  452. * @return {?}
  453. */
  454. function (safeUrl) {
  455. var _this = this;
  456. /** @type {?} */
  457. var url = this._sanitizer.sanitize(SecurityContext.RESOURCE_URL, safeUrl);
  458. if (!url) {
  459. throw getMatIconFailedToSanitizeUrlError(safeUrl);
  460. }
  461. /** @type {?} */
  462. var cachedIcon = this._cachedIconsByUrl.get(url);
  463. if (cachedIcon) {
  464. return of(cloneSvg(cachedIcon));
  465. }
  466. return this._loadSvgIconFromConfig(new SvgIconConfig(safeUrl)).pipe(tap((/**
  467. * @param {?} svg
  468. * @return {?}
  469. */
  470. function (svg) { return _this._cachedIconsByUrl.set((/** @type {?} */ (url)), svg); })), map((/**
  471. * @param {?} svg
  472. * @return {?}
  473. */
  474. function (svg) { return cloneSvg(svg); })));
  475. };
  476. /**
  477. * Returns an Observable that produces the icon (as an `<svg>` DOM element) with the given name
  478. * and namespace. The icon must have been previously registered with addIcon or addIconSet;
  479. * if not, the Observable will throw an error.
  480. *
  481. * @param name Name of the icon to be retrieved.
  482. * @param namespace Namespace in which to look for the icon.
  483. */
  484. /**
  485. * Returns an Observable that produces the icon (as an `<svg>` DOM element) with the given name
  486. * and namespace. The icon must have been previously registered with addIcon or addIconSet;
  487. * if not, the Observable will throw an error.
  488. *
  489. * @param {?} name Name of the icon to be retrieved.
  490. * @param {?=} namespace Namespace in which to look for the icon.
  491. * @return {?}
  492. */
  493. MatIconRegistry.prototype.getNamedSvgIcon = /**
  494. * Returns an Observable that produces the icon (as an `<svg>` DOM element) with the given name
  495. * and namespace. The icon must have been previously registered with addIcon or addIconSet;
  496. * if not, the Observable will throw an error.
  497. *
  498. * @param {?} name Name of the icon to be retrieved.
  499. * @param {?=} namespace Namespace in which to look for the icon.
  500. * @return {?}
  501. */
  502. function (name, namespace) {
  503. if (namespace === void 0) { namespace = ''; }
  504. // Return (copy of) cached icon if possible.
  505. /** @type {?} */
  506. var key = iconKey(namespace, name);
  507. /** @type {?} */
  508. var config = this._svgIconConfigs.get(key);
  509. if (config) {
  510. return this._getSvgFromConfig(config);
  511. }
  512. // See if we have any icon sets registered for the namespace.
  513. /** @type {?} */
  514. var iconSetConfigs = this._iconSetConfigs.get(namespace);
  515. if (iconSetConfigs) {
  516. return this._getSvgFromIconSetConfigs(name, iconSetConfigs);
  517. }
  518. return throwError(getMatIconNameNotFoundError(key));
  519. };
  520. /**
  521. * @return {?}
  522. */
  523. MatIconRegistry.prototype.ngOnDestroy = /**
  524. * @return {?}
  525. */
  526. function () {
  527. this._svgIconConfigs.clear();
  528. this._iconSetConfigs.clear();
  529. this._cachedIconsByUrl.clear();
  530. };
  531. /**
  532. * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
  533. */
  534. /**
  535. * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
  536. * @private
  537. * @param {?} config
  538. * @return {?}
  539. */
  540. MatIconRegistry.prototype._getSvgFromConfig = /**
  541. * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
  542. * @private
  543. * @param {?} config
  544. * @return {?}
  545. */
  546. function (config) {
  547. if (config.svgElement) {
  548. // We already have the SVG element for this icon, return a copy.
  549. return of(cloneSvg(config.svgElement));
  550. }
  551. else {
  552. // Fetch the icon from the config's URL, cache it, and return a copy.
  553. return this._loadSvgIconFromConfig(config).pipe(tap((/**
  554. * @param {?} svg
  555. * @return {?}
  556. */
  557. function (svg) { return config.svgElement = svg; })), map((/**
  558. * @param {?} svg
  559. * @return {?}
  560. */
  561. function (svg) { return cloneSvg(svg); })));
  562. }
  563. };
  564. /**
  565. * Attempts to find an icon with the specified name in any of the SVG icon sets.
  566. * First searches the available cached icons for a nested element with a matching name, and
  567. * if found copies the element to a new `<svg>` element. If not found, fetches all icon sets
  568. * that have not been cached, and searches again after all fetches are completed.
  569. * The returned Observable produces the SVG element if possible, and throws
  570. * an error if no icon with the specified name can be found.
  571. */
  572. /**
  573. * Attempts to find an icon with the specified name in any of the SVG icon sets.
  574. * First searches the available cached icons for a nested element with a matching name, and
  575. * if found copies the element to a new `<svg>` element. If not found, fetches all icon sets
  576. * that have not been cached, and searches again after all fetches are completed.
  577. * The returned Observable produces the SVG element if possible, and throws
  578. * an error if no icon with the specified name can be found.
  579. * @private
  580. * @param {?} name
  581. * @param {?} iconSetConfigs
  582. * @return {?}
  583. */
  584. MatIconRegistry.prototype._getSvgFromIconSetConfigs = /**
  585. * Attempts to find an icon with the specified name in any of the SVG icon sets.
  586. * First searches the available cached icons for a nested element with a matching name, and
  587. * if found copies the element to a new `<svg>` element. If not found, fetches all icon sets
  588. * that have not been cached, and searches again after all fetches are completed.
  589. * The returned Observable produces the SVG element if possible, and throws
  590. * an error if no icon with the specified name can be found.
  591. * @private
  592. * @param {?} name
  593. * @param {?} iconSetConfigs
  594. * @return {?}
  595. */
  596. function (name, iconSetConfigs) {
  597. var _this = this;
  598. // For all the icon set SVG elements we've fetched, see if any contain an icon with the
  599. // requested name.
  600. /** @type {?} */
  601. var namedIcon = this._extractIconWithNameFromAnySet(name, iconSetConfigs);
  602. if (namedIcon) {
  603. // We could cache namedIcon in _svgIconConfigs, but since we have to make a copy every
  604. // time anyway, there's probably not much advantage compared to just always extracting
  605. // it from the icon set.
  606. return of(namedIcon);
  607. }
  608. // Not found in any cached icon sets. If there are icon sets with URLs that we haven't
  609. // fetched, fetch them now and look for iconName in the results.
  610. /** @type {?} */
  611. var iconSetFetchRequests = iconSetConfigs
  612. .filter((/**
  613. * @param {?} iconSetConfig
  614. * @return {?}
  615. */
  616. function (iconSetConfig) { return !iconSetConfig.svgElement; }))
  617. .map((/**
  618. * @param {?} iconSetConfig
  619. * @return {?}
  620. */
  621. function (iconSetConfig) {
  622. return _this._loadSvgIconSetFromConfig(iconSetConfig).pipe(catchError((/**
  623. * @param {?} err
  624. * @return {?}
  625. */
  626. function (err) {
  627. /** @type {?} */
  628. var url = _this._sanitizer.sanitize(SecurityContext.RESOURCE_URL, iconSetConfig.url);
  629. // Swallow errors fetching individual URLs so the
  630. // combined Observable won't necessarily fail.
  631. console.error("Loading icon set URL: " + url + " failed: " + err.message);
  632. return of(null);
  633. })));
  634. }));
  635. // Fetch all the icon set URLs. When the requests complete, every IconSet should have a
  636. // cached SVG element (unless the request failed), and we can check again for the icon.
  637. return forkJoin(iconSetFetchRequests).pipe(map((/**
  638. * @return {?}
  639. */
  640. function () {
  641. /** @type {?} */
  642. var foundIcon = _this._extractIconWithNameFromAnySet(name, iconSetConfigs);
  643. if (!foundIcon) {
  644. throw getMatIconNameNotFoundError(name);
  645. }
  646. return foundIcon;
  647. })));
  648. };
  649. /**
  650. * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
  651. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  652. * returns it. Returns null if no matching element is found.
  653. */
  654. /**
  655. * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
  656. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  657. * returns it. Returns null if no matching element is found.
  658. * @private
  659. * @param {?} iconName
  660. * @param {?} iconSetConfigs
  661. * @return {?}
  662. */
  663. MatIconRegistry.prototype._extractIconWithNameFromAnySet = /**
  664. * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
  665. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  666. * returns it. Returns null if no matching element is found.
  667. * @private
  668. * @param {?} iconName
  669. * @param {?} iconSetConfigs
  670. * @return {?}
  671. */
  672. function (iconName, iconSetConfigs) {
  673. // Iterate backwards, so icon sets added later have precedence.
  674. for (var i = iconSetConfigs.length - 1; i >= 0; i--) {
  675. /** @type {?} */
  676. var config = iconSetConfigs[i];
  677. if (config.svgElement) {
  678. /** @type {?} */
  679. var foundIcon = this._extractSvgIconFromSet(config.svgElement, iconName);
  680. if (foundIcon) {
  681. return foundIcon;
  682. }
  683. }
  684. }
  685. return null;
  686. };
  687. /**
  688. * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
  689. * from it.
  690. */
  691. /**
  692. * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
  693. * from it.
  694. * @private
  695. * @param {?} config
  696. * @return {?}
  697. */
  698. MatIconRegistry.prototype._loadSvgIconFromConfig = /**
  699. * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
  700. * from it.
  701. * @private
  702. * @param {?} config
  703. * @return {?}
  704. */
  705. function (config) {
  706. var _this = this;
  707. return this._fetchUrl(config.url)
  708. .pipe(map((/**
  709. * @param {?} svgText
  710. * @return {?}
  711. */
  712. function (svgText) { return _this._createSvgElementForSingleIcon(svgText); })));
  713. };
  714. /**
  715. * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
  716. * from it.
  717. */
  718. /**
  719. * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
  720. * from it.
  721. * @private
  722. * @param {?} config
  723. * @return {?}
  724. */
  725. MatIconRegistry.prototype._loadSvgIconSetFromConfig = /**
  726. * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
  727. * from it.
  728. * @private
  729. * @param {?} config
  730. * @return {?}
  731. */
  732. function (config) {
  733. var _this = this;
  734. // If the SVG for this icon set has already been parsed, do nothing.
  735. if (config.svgElement) {
  736. return of(config.svgElement);
  737. }
  738. return this._fetchUrl(config.url).pipe(map((/**
  739. * @param {?} svgText
  740. * @return {?}
  741. */
  742. function (svgText) {
  743. // It is possible that the icon set was parsed and cached by an earlier request, so parsing
  744. // only needs to occur if the cache is yet unset.
  745. if (!config.svgElement) {
  746. config.svgElement = _this._svgElementFromString(svgText);
  747. }
  748. return config.svgElement;
  749. })));
  750. };
  751. /**
  752. * Creates a DOM element from the given SVG string, and adds default attributes.
  753. */
  754. /**
  755. * Creates a DOM element from the given SVG string, and adds default attributes.
  756. * @private
  757. * @param {?} responseText
  758. * @return {?}
  759. */
  760. MatIconRegistry.prototype._createSvgElementForSingleIcon = /**
  761. * Creates a DOM element from the given SVG string, and adds default attributes.
  762. * @private
  763. * @param {?} responseText
  764. * @return {?}
  765. */
  766. function (responseText) {
  767. /** @type {?} */
  768. var svg = this._svgElementFromString(responseText);
  769. this._setSvgAttributes(svg);
  770. return svg;
  771. };
  772. /**
  773. * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
  774. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  775. * returns it. Returns null if no matching element is found.
  776. */
  777. /**
  778. * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
  779. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  780. * returns it. Returns null if no matching element is found.
  781. * @private
  782. * @param {?} iconSet
  783. * @param {?} iconName
  784. * @return {?}
  785. */
  786. MatIconRegistry.prototype._extractSvgIconFromSet = /**
  787. * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
  788. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  789. * returns it. Returns null if no matching element is found.
  790. * @private
  791. * @param {?} iconSet
  792. * @param {?} iconName
  793. * @return {?}
  794. */
  795. function (iconSet, iconName) {
  796. // Use the `id="iconName"` syntax in order to escape special
  797. // characters in the ID (versus using the #iconName syntax).
  798. /** @type {?} */
  799. var iconSource = iconSet.querySelector("[id=\"" + iconName + "\"]");
  800. if (!iconSource) {
  801. return null;
  802. }
  803. // Clone the element and remove the ID to prevent multiple elements from being added
  804. // to the page with the same ID.
  805. /** @type {?} */
  806. var iconElement = (/** @type {?} */ (iconSource.cloneNode(true)));
  807. iconElement.removeAttribute('id');
  808. // If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
  809. // the content of a new <svg> node.
  810. if (iconElement.nodeName.toLowerCase() === 'svg') {
  811. return this._setSvgAttributes((/** @type {?} */ (iconElement)));
  812. }
  813. // If the node is a <symbol>, it won't be rendered so we have to convert it into <svg>. Note
  814. // that the same could be achieved by referring to it via <use href="#id">, however the <use>
  815. // tag is problematic on Firefox, because it needs to include the current page path.
  816. if (iconElement.nodeName.toLowerCase() === 'symbol') {
  817. return this._setSvgAttributes(this._toSvgElement(iconElement));
  818. }
  819. // createElement('SVG') doesn't work as expected; the DOM ends up with
  820. // the correct nodes, but the SVG content doesn't render. Instead we
  821. // have to create an empty SVG node using innerHTML and append its content.
  822. // Elements created using DOMParser.parseFromString have the same problem.
  823. // http://stackoverflow.com/questions/23003278/svg-innerhtml-in-firefox-can-not-display
  824. /** @type {?} */
  825. var svg = this._svgElementFromString('<svg></svg>');
  826. // Clone the node so we don't remove it from the parent icon set element.
  827. svg.appendChild(iconElement);
  828. return this._setSvgAttributes(svg);
  829. };
  830. /**
  831. * Creates a DOM element from the given SVG string.
  832. */
  833. /**
  834. * Creates a DOM element from the given SVG string.
  835. * @private
  836. * @param {?} str
  837. * @return {?}
  838. */
  839. MatIconRegistry.prototype._svgElementFromString = /**
  840. * Creates a DOM element from the given SVG string.
  841. * @private
  842. * @param {?} str
  843. * @return {?}
  844. */
  845. function (str) {
  846. /** @type {?} */
  847. var div = this._document.createElement('DIV');
  848. div.innerHTML = str;
  849. /** @type {?} */
  850. var svg = (/** @type {?} */ (div.querySelector('svg')));
  851. if (!svg) {
  852. throw Error('<svg> tag not found');
  853. }
  854. return svg;
  855. };
  856. /**
  857. * Converts an element into an SVG node by cloning all of its children.
  858. */
  859. /**
  860. * Converts an element into an SVG node by cloning all of its children.
  861. * @private
  862. * @param {?} element
  863. * @return {?}
  864. */
  865. MatIconRegistry.prototype._toSvgElement = /**
  866. * Converts an element into an SVG node by cloning all of its children.
  867. * @private
  868. * @param {?} element
  869. * @return {?}
  870. */
  871. function (element) {
  872. /** @type {?} */
  873. var svg = this._svgElementFromString('<svg></svg>');
  874. for (var i = 0; i < element.childNodes.length; i++) {
  875. if (element.childNodes[i].nodeType === this._document.ELEMENT_NODE) {
  876. svg.appendChild(element.childNodes[i].cloneNode(true));
  877. }
  878. }
  879. return svg;
  880. };
  881. /**
  882. * Sets the default attributes for an SVG element to be used as an icon.
  883. */
  884. /**
  885. * Sets the default attributes for an SVG element to be used as an icon.
  886. * @private
  887. * @param {?} svg
  888. * @return {?}
  889. */
  890. MatIconRegistry.prototype._setSvgAttributes = /**
  891. * Sets the default attributes for an SVG element to be used as an icon.
  892. * @private
  893. * @param {?} svg
  894. * @return {?}
  895. */
  896. function (svg) {
  897. svg.setAttribute('fit', '');
  898. svg.setAttribute('height', '100%');
  899. svg.setAttribute('width', '100%');
  900. svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
  901. svg.setAttribute('focusable', 'false'); // Disable IE11 default behavior to make SVGs focusable.
  902. return svg;
  903. };
  904. /**
  905. * Returns an Observable which produces the string contents of the given URL. Results may be
  906. * cached, so future calls with the same URL may not cause another HTTP request.
  907. */
  908. /**
  909. * Returns an Observable which produces the string contents of the given URL. Results may be
  910. * cached, so future calls with the same URL may not cause another HTTP request.
  911. * @private
  912. * @param {?} safeUrl
  913. * @return {?}
  914. */
  915. MatIconRegistry.prototype._fetchUrl = /**
  916. * Returns an Observable which produces the string contents of the given URL. Results may be
  917. * cached, so future calls with the same URL may not cause another HTTP request.
  918. * @private
  919. * @param {?} safeUrl
  920. * @return {?}
  921. */
  922. function (safeUrl) {
  923. var _this = this;
  924. if (!this._httpClient) {
  925. throw getMatIconNoHttpProviderError();
  926. }
  927. if (safeUrl == null) {
  928. throw Error("Cannot fetch icon from URL \"" + safeUrl + "\".");
  929. }
  930. /** @type {?} */
  931. var url = this._sanitizer.sanitize(SecurityContext.RESOURCE_URL, safeUrl);
  932. if (!url) {
  933. throw getMatIconFailedToSanitizeUrlError(safeUrl);
  934. }
  935. // Store in-progress fetches to avoid sending a duplicate request for a URL when there is
  936. // already a request in progress for that URL. It's necessary to call share() on the
  937. // Observable returned by http.get() so that multiple subscribers don't cause multiple XHRs.
  938. /** @type {?} */
  939. var inProgressFetch = this._inProgressUrlFetches.get(url);
  940. if (inProgressFetch) {
  941. return inProgressFetch;
  942. }
  943. // TODO(jelbourn): for some reason, the `finalize` operator "loses" the generic type on the
  944. // Observable. Figure out why and fix it.
  945. /** @type {?} */
  946. var req = this._httpClient.get(url, { responseType: 'text' }).pipe(finalize((/**
  947. * @return {?}
  948. */
  949. function () { return _this._inProgressUrlFetches.delete(url); })), share());
  950. this._inProgressUrlFetches.set(url, req);
  951. return req;
  952. };
  953. /**
  954. * Registers an icon config by name in the specified namespace.
  955. * @param namespace Namespace in which to register the icon config.
  956. * @param iconName Name under which to register the config.
  957. * @param config Config to be registered.
  958. */
  959. /**
  960. * Registers an icon config by name in the specified namespace.
  961. * @private
  962. * @template THIS
  963. * @this {THIS}
  964. * @param {?} namespace Namespace in which to register the icon config.
  965. * @param {?} iconName Name under which to register the config.
  966. * @param {?} config Config to be registered.
  967. * @return {THIS}
  968. */
  969. MatIconRegistry.prototype._addSvgIconConfig = /**
  970. * Registers an icon config by name in the specified namespace.
  971. * @private
  972. * @template THIS
  973. * @this {THIS}
  974. * @param {?} namespace Namespace in which to register the icon config.
  975. * @param {?} iconName Name under which to register the config.
  976. * @param {?} config Config to be registered.
  977. * @return {THIS}
  978. */
  979. function (namespace, iconName, config) {
  980. (/** @type {?} */ (this))._svgIconConfigs.set(iconKey(namespace, iconName), config);
  981. return (/** @type {?} */ (this));
  982. };
  983. /**
  984. * Registers an icon set config in the specified namespace.
  985. * @param namespace Namespace in which to register the icon config.
  986. * @param config Config to be registered.
  987. */
  988. /**
  989. * Registers an icon set config in the specified namespace.
  990. * @private
  991. * @template THIS
  992. * @this {THIS}
  993. * @param {?} namespace Namespace in which to register the icon config.
  994. * @param {?} config Config to be registered.
  995. * @return {THIS}
  996. */
  997. MatIconRegistry.prototype._addSvgIconSetConfig = /**
  998. * Registers an icon set config in the specified namespace.
  999. * @private
  1000. * @template THIS
  1001. * @this {THIS}
  1002. * @param {?} namespace Namespace in which to register the icon config.
  1003. * @param {?} config Config to be registered.
  1004. * @return {THIS}
  1005. */
  1006. function (namespace, config) {
  1007. /** @type {?} */
  1008. var configNamespace = (/** @type {?} */ (this))._iconSetConfigs.get(namespace);
  1009. if (configNamespace) {
  1010. configNamespace.push(config);
  1011. }
  1012. else {
  1013. (/** @type {?} */ (this))._iconSetConfigs.set(namespace, [config]);
  1014. }
  1015. return (/** @type {?} */ (this));
  1016. };
  1017. MatIconRegistry.decorators = [
  1018. { type: Injectable, args: [{ providedIn: 'root' },] },
  1019. ];
  1020. /** @nocollapse */
  1021. MatIconRegistry.ctorParameters = function () { return [
  1022. { type: HttpClient, decorators: [{ type: Optional }] },
  1023. { type: DomSanitizer },
  1024. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] }] }
  1025. ]; };
  1026. /** @nocollapse */ MatIconRegistry.ngInjectableDef = ɵɵdefineInjectable({ factory: function MatIconRegistry_Factory() { return new MatIconRegistry(ɵɵinject(HttpClient, 8), ɵɵinject(DomSanitizer), ɵɵinject(DOCUMENT, 8)); }, token: MatIconRegistry, providedIn: "root" });
  1027. return MatIconRegistry;
  1028. }());
  1029. /**
  1030. * \@docs-private
  1031. * @param {?} parentRegistry
  1032. * @param {?} httpClient
  1033. * @param {?} sanitizer
  1034. * @param {?=} document
  1035. * @return {?}
  1036. */
  1037. function ICON_REGISTRY_PROVIDER_FACTORY(parentRegistry, httpClient, sanitizer, document) {
  1038. return parentRegistry || new MatIconRegistry(httpClient, sanitizer, document);
  1039. }
  1040. /**
  1041. * \@docs-private
  1042. * @type {?}
  1043. */
  1044. var ICON_REGISTRY_PROVIDER = {
  1045. // If there is already an MatIconRegistry available, use that. Otherwise, provide a new one.
  1046. provide: MatIconRegistry,
  1047. deps: [
  1048. [new Optional(), new SkipSelf(), MatIconRegistry],
  1049. [new Optional(), HttpClient],
  1050. DomSanitizer,
  1051. [new Optional(), (/** @type {?} */ (DOCUMENT))],
  1052. ],
  1053. useFactory: ICON_REGISTRY_PROVIDER_FACTORY,
  1054. };
  1055. /**
  1056. * Clones an SVGElement while preserving type information.
  1057. * @param {?} svg
  1058. * @return {?}
  1059. */
  1060. function cloneSvg(svg) {
  1061. return (/** @type {?} */ (svg.cloneNode(true)));
  1062. }
  1063. /**
  1064. * Returns the cache key to use for an icon namespace and name.
  1065. * @param {?} namespace
  1066. * @param {?} name
  1067. * @return {?}
  1068. */
  1069. function iconKey(namespace, name) {
  1070. return namespace + ':' + name;
  1071. }
  1072. /**
  1073. * @fileoverview added by tsickle
  1074. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1075. */
  1076. // Boilerplate for applying mixins to MatIcon.
  1077. /**
  1078. * \@docs-private
  1079. */
  1080. var
  1081. // Boilerplate for applying mixins to MatIcon.
  1082. /**
  1083. * \@docs-private
  1084. */
  1085. MatIconBase = /** @class */ (function () {
  1086. function MatIconBase(_elementRef) {
  1087. this._elementRef = _elementRef;
  1088. }
  1089. return MatIconBase;
  1090. }());
  1091. /** @type {?} */
  1092. var _MatIconMixinBase = mixinColor(MatIconBase);
  1093. /**
  1094. * Injection token used to provide the current location to `MatIcon`.
  1095. * Used to handle server-side rendering and to stub out during unit tests.
  1096. * \@docs-private
  1097. * @type {?}
  1098. */
  1099. var MAT_ICON_LOCATION = new InjectionToken('mat-icon-location', {
  1100. providedIn: 'root',
  1101. factory: MAT_ICON_LOCATION_FACTORY
  1102. });
  1103. /**
  1104. * \@docs-private
  1105. * @return {?}
  1106. */
  1107. function MAT_ICON_LOCATION_FACTORY() {
  1108. /** @type {?} */
  1109. var _document = inject(DOCUMENT);
  1110. /** @type {?} */
  1111. var _location = _document ? _document.location : null;
  1112. return {
  1113. // Note that this needs to be a function, rather than a property, because Angular
  1114. // will only resolve it once, but we want the current path on each call.
  1115. getPathname: (/**
  1116. * @return {?}
  1117. */
  1118. function () { return _location ? (_location.pathname + _location.search) : ''; })
  1119. };
  1120. }
  1121. /**
  1122. * SVG attributes that accept a FuncIRI (e.g. `url(<something>)`).
  1123. * @type {?}
  1124. */
  1125. var funcIriAttributes = [
  1126. 'clip-path',
  1127. 'color-profile',
  1128. 'src',
  1129. 'cursor',
  1130. 'fill',
  1131. 'filter',
  1132. 'marker',
  1133. 'marker-start',
  1134. 'marker-mid',
  1135. 'marker-end',
  1136. 'mask',
  1137. 'stroke'
  1138. ];
  1139. var ɵ0 = /**
  1140. * @param {?} attr
  1141. * @return {?}
  1142. */
  1143. function (attr) { return "[" + attr + "]"; };
  1144. /**
  1145. * Selector that can be used to find all elements that are using a `FuncIRI`.
  1146. * @type {?}
  1147. */
  1148. var funcIriAttributeSelector = funcIriAttributes.map((ɵ0)).join(', ');
  1149. /**
  1150. * Regex that can be used to extract the id out of a FuncIRI.
  1151. * @type {?}
  1152. */
  1153. var funcIriPattern = /^url\(['"]?#(.*?)['"]?\)$/;
  1154. /**
  1155. * Component to display an icon. It can be used in the following ways:
  1156. *
  1157. * - Specify the svgIcon input to load an SVG icon from a URL previously registered with the
  1158. * addSvgIcon, addSvgIconInNamespace, addSvgIconSet, or addSvgIconSetInNamespace methods of
  1159. * MatIconRegistry. If the svgIcon value contains a colon it is assumed to be in the format
  1160. * "[namespace]:[name]", if not the value will be the name of an icon in the default namespace.
  1161. * Examples:
  1162. * `<mat-icon svgIcon="left-arrow"></mat-icon>
  1163. * <mat-icon svgIcon="animals:cat"></mat-icon>`
  1164. *
  1165. * - Use a font ligature as an icon by putting the ligature text in the content of the `<mat-icon>`
  1166. * component. By default the Material icons font is used as described at
  1167. * http://google.github.io/material-design-icons/#icon-font-for-the-web. You can specify an
  1168. * alternate font by setting the fontSet input to either the CSS class to apply to use the
  1169. * desired font, or to an alias previously registered with MatIconRegistry.registerFontClassAlias.
  1170. * Examples:
  1171. * `<mat-icon>home</mat-icon>
  1172. * <mat-icon fontSet="myfont">sun</mat-icon>`
  1173. *
  1174. * - Specify a font glyph to be included via CSS rules by setting the fontSet input to specify the
  1175. * font, and the fontIcon input to specify the icon. Typically the fontIcon will specify a
  1176. * CSS class which causes the glyph to be displayed via a :before selector, as in
  1177. * https://fortawesome.github.io/Font-Awesome/examples/
  1178. * Example:
  1179. * `<mat-icon fontSet="fa" fontIcon="alarm"></mat-icon>`
  1180. */
  1181. var MatIcon = /** @class */ (function (_super) {
  1182. __extends(MatIcon, _super);
  1183. function MatIcon(elementRef, _iconRegistry, ariaHidden, _location) {
  1184. var _this = _super.call(this, elementRef) || this;
  1185. _this._iconRegistry = _iconRegistry;
  1186. _this._location = _location;
  1187. _this._inline = false;
  1188. // If the user has not explicitly set aria-hidden, mark the icon as hidden, as this is
  1189. // the right thing to do for the majority of icon use-cases.
  1190. if (!ariaHidden) {
  1191. elementRef.nativeElement.setAttribute('aria-hidden', 'true');
  1192. }
  1193. return _this;
  1194. }
  1195. Object.defineProperty(MatIcon.prototype, "inline", {
  1196. /**
  1197. * Whether the icon should be inlined, automatically sizing the icon to match the font size of
  1198. * the element the icon is contained in.
  1199. */
  1200. get: /**
  1201. * Whether the icon should be inlined, automatically sizing the icon to match the font size of
  1202. * the element the icon is contained in.
  1203. * @return {?}
  1204. */
  1205. function () {
  1206. return this._inline;
  1207. },
  1208. set: /**
  1209. * @param {?} inline
  1210. * @return {?}
  1211. */
  1212. function (inline) {
  1213. this._inline = coerceBooleanProperty(inline);
  1214. },
  1215. enumerable: true,
  1216. configurable: true
  1217. });
  1218. Object.defineProperty(MatIcon.prototype, "fontSet", {
  1219. /** Font set that the icon is a part of. */
  1220. get: /**
  1221. * Font set that the icon is a part of.
  1222. * @return {?}
  1223. */
  1224. function () { return this._fontSet; },
  1225. set: /**
  1226. * @param {?} value
  1227. * @return {?}
  1228. */
  1229. function (value) {
  1230. this._fontSet = this._cleanupFontValue(value);
  1231. },
  1232. enumerable: true,
  1233. configurable: true
  1234. });
  1235. Object.defineProperty(MatIcon.prototype, "fontIcon", {
  1236. /** Name of an icon within a font set. */
  1237. get: /**
  1238. * Name of an icon within a font set.
  1239. * @return {?}
  1240. */
  1241. function () { return this._fontIcon; },
  1242. set: /**
  1243. * @param {?} value
  1244. * @return {?}
  1245. */
  1246. function (value) {
  1247. this._fontIcon = this._cleanupFontValue(value);
  1248. },
  1249. enumerable: true,
  1250. configurable: true
  1251. });
  1252. /**
  1253. * Splits an svgIcon binding value into its icon set and icon name components.
  1254. * Returns a 2-element array of [(icon set), (icon name)].
  1255. * The separator for the two fields is ':'. If there is no separator, an empty
  1256. * string is returned for the icon set and the entire value is returned for
  1257. * the icon name. If the argument is falsy, returns an array of two empty strings.
  1258. * Throws an error if the name contains two or more ':' separators.
  1259. * Examples:
  1260. * `'social:cake' -> ['social', 'cake']
  1261. * 'penguin' -> ['', 'penguin']
  1262. * null -> ['', '']
  1263. * 'a:b:c' -> (throws Error)`
  1264. */
  1265. /**
  1266. * Splits an svgIcon binding value into its icon set and icon name components.
  1267. * Returns a 2-element array of [(icon set), (icon name)].
  1268. * The separator for the two fields is ':'. If there is no separator, an empty
  1269. * string is returned for the icon set and the entire value is returned for
  1270. * the icon name. If the argument is falsy, returns an array of two empty strings.
  1271. * Throws an error if the name contains two or more ':' separators.
  1272. * Examples:
  1273. * `'social:cake' -> ['social', 'cake']
  1274. * 'penguin' -> ['', 'penguin']
  1275. * null -> ['', '']
  1276. * 'a:b:c' -> (throws Error)`
  1277. * @private
  1278. * @param {?} iconName
  1279. * @return {?}
  1280. */
  1281. MatIcon.prototype._splitIconName = /**
  1282. * Splits an svgIcon binding value into its icon set and icon name components.
  1283. * Returns a 2-element array of [(icon set), (icon name)].
  1284. * The separator for the two fields is ':'. If there is no separator, an empty
  1285. * string is returned for the icon set and the entire value is returned for
  1286. * the icon name. If the argument is falsy, returns an array of two empty strings.
  1287. * Throws an error if the name contains two or more ':' separators.
  1288. * Examples:
  1289. * `'social:cake' -> ['social', 'cake']
  1290. * 'penguin' -> ['', 'penguin']
  1291. * null -> ['', '']
  1292. * 'a:b:c' -> (throws Error)`
  1293. * @private
  1294. * @param {?} iconName
  1295. * @return {?}
  1296. */
  1297. function (iconName) {
  1298. if (!iconName) {
  1299. return ['', ''];
  1300. }
  1301. /** @type {?} */
  1302. var parts = iconName.split(':');
  1303. switch (parts.length) {
  1304. case 1: return ['', parts[0]]; // Use default namespace.
  1305. case 2: return (/** @type {?} */ (parts));
  1306. default: throw Error("Invalid icon name: \"" + iconName + "\"");
  1307. }
  1308. };
  1309. /**
  1310. * @param {?} changes
  1311. * @return {?}
  1312. */
  1313. MatIcon.prototype.ngOnChanges = /**
  1314. * @param {?} changes
  1315. * @return {?}
  1316. */
  1317. function (changes) {
  1318. var _this = this;
  1319. // Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
  1320. /** @type {?} */
  1321. var svgIconChanges = changes['svgIcon'];
  1322. if (svgIconChanges) {
  1323. if (this.svgIcon) {
  1324. var _a = this._splitIconName(this.svgIcon), namespace = _a[0], iconName = _a[1];
  1325. this._iconRegistry.getNamedSvgIcon(iconName, namespace).pipe(take(1)).subscribe((/**
  1326. * @param {?} svg
  1327. * @return {?}
  1328. */
  1329. function (svg) { return _this._setSvgElement(svg); }), (/**
  1330. * @param {?} err
  1331. * @return {?}
  1332. */
  1333. function (err) { return console.log("Error retrieving icon: " + err.message); }));
  1334. }
  1335. else if (svgIconChanges.previousValue) {
  1336. this._clearSvgElement();
  1337. }
  1338. }
  1339. if (this._usingFontIcon()) {
  1340. this._updateFontIconClasses();
  1341. }
  1342. };
  1343. /**
  1344. * @return {?}
  1345. */
  1346. MatIcon.prototype.ngOnInit = /**
  1347. * @return {?}
  1348. */
  1349. function () {
  1350. // Update font classes because ngOnChanges won't be called if none of the inputs are present,
  1351. // e.g. <mat-icon>arrow</mat-icon> In this case we need to add a CSS class for the default font.
  1352. if (this._usingFontIcon()) {
  1353. this._updateFontIconClasses();
  1354. }
  1355. };
  1356. /**
  1357. * @return {?}
  1358. */
  1359. MatIcon.prototype.ngAfterViewChecked = /**
  1360. * @return {?}
  1361. */
  1362. function () {
  1363. /** @type {?} */
  1364. var cachedElements = this._elementsWithExternalReferences;
  1365. if (cachedElements && this._location && cachedElements.size) {
  1366. /** @type {?} */
  1367. var newPath = this._location.getPathname();
  1368. // We need to check whether the URL has changed on each change detection since
  1369. // the browser doesn't have an API that will let us react on link clicks and
  1370. // we can't depend on the Angular router. The references need to be updated,
  1371. // because while most browsers don't care whether the URL is correct after
  1372. // the first render, Safari will break if the user navigates to a different
  1373. // page and the SVG isn't re-rendered.
  1374. if (newPath !== this._previousPath) {
  1375. this._previousPath = newPath;
  1376. this._prependPathToReferences(newPath);
  1377. }
  1378. }
  1379. };
  1380. /**
  1381. * @return {?}
  1382. */
  1383. MatIcon.prototype.ngOnDestroy = /**
  1384. * @return {?}
  1385. */
  1386. function () {
  1387. if (this._elementsWithExternalReferences) {
  1388. this._elementsWithExternalReferences.clear();
  1389. }
  1390. };
  1391. /**
  1392. * @private
  1393. * @return {?}
  1394. */
  1395. MatIcon.prototype._usingFontIcon = /**
  1396. * @private
  1397. * @return {?}
  1398. */
  1399. function () {
  1400. return !this.svgIcon;
  1401. };
  1402. /**
  1403. * @private
  1404. * @param {?} svg
  1405. * @return {?}
  1406. */
  1407. MatIcon.prototype._setSvgElement = /**
  1408. * @private
  1409. * @param {?} svg
  1410. * @return {?}
  1411. */
  1412. function (svg) {
  1413. this._clearSvgElement();
  1414. // Workaround for IE11 and Edge ignoring `style` tags inside dynamically-created SVGs.
  1415. // See: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10898469/
  1416. // Do this before inserting the element into the DOM, in order to avoid a style recalculation.
  1417. /** @type {?} */
  1418. var styleTags = (/** @type {?} */ (svg.querySelectorAll('style')));
  1419. for (var i = 0; i < styleTags.length; i++) {
  1420. styleTags[i].textContent += ' ';
  1421. }
  1422. // Note: we do this fix here, rather than the icon registry, because the
  1423. // references have to point to the URL at the time that the icon was created.
  1424. if (this._location) {
  1425. /** @type {?} */
  1426. var path = this._location.getPathname();
  1427. this._previousPath = path;
  1428. this._cacheChildrenWithExternalReferences(svg);
  1429. this._prependPathToReferences(path);
  1430. }
  1431. this._elementRef.nativeElement.appendChild(svg);
  1432. };
  1433. /**
  1434. * @private
  1435. * @return {?}
  1436. */
  1437. MatIcon.prototype._clearSvgElement = /**
  1438. * @private
  1439. * @return {?}
  1440. */
  1441. function () {
  1442. /** @type {?} */
  1443. var layoutElement = this._elementRef.nativeElement;
  1444. /** @type {?} */
  1445. var childCount = layoutElement.childNodes.length;
  1446. if (this._elementsWithExternalReferences) {
  1447. this._elementsWithExternalReferences.clear();
  1448. }
  1449. // Remove existing non-element child nodes and SVGs, and add the new SVG element. Note that
  1450. // we can't use innerHTML, because IE will throw if the element has a data binding.
  1451. while (childCount--) {
  1452. /** @type {?} */
  1453. var child = layoutElement.childNodes[childCount];
  1454. // 1 corresponds to Node.ELEMENT_NODE. We remove all non-element nodes in order to get rid
  1455. // of any loose text nodes, as well as any SVG elements in order to remove any old icons.
  1456. if (child.nodeType !== 1 || child.nodeName.toLowerCase() === 'svg') {
  1457. layoutElement.removeChild(child);
  1458. }
  1459. }
  1460. };
  1461. /**
  1462. * @private
  1463. * @return {?}
  1464. */
  1465. MatIcon.prototype._updateFontIconClasses = /**
  1466. * @private
  1467. * @return {?}
  1468. */
  1469. function () {
  1470. if (!this._usingFontIcon()) {
  1471. return;
  1472. }
  1473. /** @type {?} */
  1474. var elem = this._elementRef.nativeElement;
  1475. /** @type {?} */
  1476. var fontSetClass = this.fontSet ?
  1477. this._iconRegistry.classNameForFontAlias(this.fontSet) :
  1478. this._iconRegistry.getDefaultFontSetClass();
  1479. if (fontSetClass != this._previousFontSetClass) {
  1480. if (this._previousFontSetClass) {
  1481. elem.classList.remove(this._previousFontSetClass);
  1482. }
  1483. if (fontSetClass) {
  1484. elem.classList.add(fontSetClass);
  1485. }
  1486. this._previousFontSetClass = fontSetClass;
  1487. }
  1488. if (this.fontIcon != this._previousFontIconClass) {
  1489. if (this._previousFontIconClass) {
  1490. elem.classList.remove(this._previousFontIconClass);
  1491. }
  1492. if (this.fontIcon) {
  1493. elem.classList.add(this.fontIcon);
  1494. }
  1495. this._previousFontIconClass = this.fontIcon;
  1496. }
  1497. };
  1498. /**
  1499. * Cleans up a value to be used as a fontIcon or fontSet.
  1500. * Since the value ends up being assigned as a CSS class, we
  1501. * have to trim the value and omit space-separated values.
  1502. */
  1503. /**
  1504. * Cleans up a value to be used as a fontIcon or fontSet.
  1505. * Since the value ends up being assigned as a CSS class, we
  1506. * have to trim the value and omit space-separated values.
  1507. * @private
  1508. * @param {?} value
  1509. * @return {?}
  1510. */
  1511. MatIcon.prototype._cleanupFontValue = /**
  1512. * Cleans up a value to be used as a fontIcon or fontSet.
  1513. * Since the value ends up being assigned as a CSS class, we
  1514. * have to trim the value and omit space-separated values.
  1515. * @private
  1516. * @param {?} value
  1517. * @return {?}
  1518. */
  1519. function (value) {
  1520. return typeof value === 'string' ? value.trim().split(' ')[0] : value;
  1521. };
  1522. /**
  1523. * Prepends the current path to all elements that have an attribute pointing to a `FuncIRI`
  1524. * reference. This is required because WebKit browsers require references to be prefixed with
  1525. * the current path, if the page has a `base` tag.
  1526. */
  1527. /**
  1528. * Prepends the current path to all elements that have an attribute pointing to a `FuncIRI`
  1529. * reference. This is required because WebKit browsers require references to be prefixed with
  1530. * the current path, if the page has a `base` tag.
  1531. * @private
  1532. * @param {?} path
  1533. * @return {?}
  1534. */
  1535. MatIcon.prototype._prependPathToReferences = /**
  1536. * Prepends the current path to all elements that have an attribute pointing to a `FuncIRI`
  1537. * reference. This is required because WebKit browsers require references to be prefixed with
  1538. * the current path, if the page has a `base` tag.
  1539. * @private
  1540. * @param {?} path
  1541. * @return {?}
  1542. */
  1543. function (path) {
  1544. /** @type {?} */
  1545. var elements = this._elementsWithExternalReferences;
  1546. if (elements) {
  1547. elements.forEach((/**
  1548. * @param {?} attrs
  1549. * @param {?} element
  1550. * @return {?}
  1551. */
  1552. function (attrs, element) {
  1553. attrs.forEach((/**
  1554. * @param {?} attr
  1555. * @return {?}
  1556. */
  1557. function (attr) {
  1558. element.setAttribute(attr.name, "url('" + path + "#" + attr.value + "')");
  1559. }));
  1560. }));
  1561. }
  1562. };
  1563. /**
  1564. * Caches the children of an SVG element that have `url()`
  1565. * references that we need to prefix with the current path.
  1566. */
  1567. /**
  1568. * Caches the children of an SVG element that have `url()`
  1569. * references that we need to prefix with the current path.
  1570. * @private
  1571. * @param {?} element
  1572. * @return {?}
  1573. */
  1574. MatIcon.prototype._cacheChildrenWithExternalReferences = /**
  1575. * Caches the children of an SVG element that have `url()`
  1576. * references that we need to prefix with the current path.
  1577. * @private
  1578. * @param {?} element
  1579. * @return {?}
  1580. */
  1581. function (element) {
  1582. /** @type {?} */
  1583. var elementsWithFuncIri = element.querySelectorAll(funcIriAttributeSelector);
  1584. /** @type {?} */
  1585. var elements = this._elementsWithExternalReferences =
  1586. this._elementsWithExternalReferences || new Map();
  1587. var _loop_1 = function (i) {
  1588. funcIriAttributes.forEach((/**
  1589. * @param {?} attr
  1590. * @return {?}
  1591. */
  1592. function (attr) {
  1593. /** @type {?} */
  1594. var elementWithReference = elementsWithFuncIri[i];
  1595. /** @type {?} */
  1596. var value = elementWithReference.getAttribute(attr);
  1597. /** @type {?} */
  1598. var match = value ? value.match(funcIriPattern) : null;
  1599. if (match) {
  1600. /** @type {?} */
  1601. var attributes = elements.get(elementWithReference);
  1602. if (!attributes) {
  1603. attributes = [];
  1604. elements.set(elementWithReference, attributes);
  1605. }
  1606. (/** @type {?} */ (attributes)).push({ name: attr, value: match[1] });
  1607. }
  1608. }));
  1609. };
  1610. for (var i = 0; i < elementsWithFuncIri.length; i++) {
  1611. _loop_1(i);
  1612. }
  1613. };
  1614. MatIcon.decorators = [
  1615. { type: Component, args: [{template: '<ng-content></ng-content>',
  1616. selector: 'mat-icon',
  1617. exportAs: 'matIcon',
  1618. styles: [".mat-icon{background-repeat:no-repeat;display:inline-block;fill:currentColor;height:24px;width:24px}.mat-icon.mat-icon-inline{font-size:inherit;height:inherit;line-height:inherit;width:inherit}[dir=rtl] .mat-icon-rtl-mirror{transform:scale(-1,1)}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon{display:block}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button .mat-icon{margin:auto}"],
  1619. inputs: ['color'],
  1620. host: {
  1621. 'role': 'img',
  1622. 'class': 'mat-icon notranslate',
  1623. '[class.mat-icon-inline]': 'inline',
  1624. '[class.mat-icon-no-color]': 'color !== "primary" && color !== "accent" && color !== "warn"',
  1625. },
  1626. encapsulation: ViewEncapsulation.None,
  1627. changeDetection: ChangeDetectionStrategy.OnPush,
  1628. },] },
  1629. ];
  1630. /** @nocollapse */
  1631. MatIcon.ctorParameters = function () { return [
  1632. { type: ElementRef },
  1633. { type: MatIconRegistry },
  1634. { type: String, decorators: [{ type: Attribute, args: ['aria-hidden',] }] },
  1635. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_ICON_LOCATION,] }] }
  1636. ]; };
  1637. MatIcon.propDecorators = {
  1638. inline: [{ type: Input }],
  1639. svgIcon: [{ type: Input }],
  1640. fontSet: [{ type: Input }],
  1641. fontIcon: [{ type: Input }]
  1642. };
  1643. return MatIcon;
  1644. }(_MatIconMixinBase));
  1645. /**
  1646. * @fileoverview added by tsickle
  1647. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1648. */
  1649. var MatIconModule = /** @class */ (function () {
  1650. function MatIconModule() {
  1651. }
  1652. MatIconModule.decorators = [
  1653. { type: NgModule, args: [{
  1654. imports: [MatCommonModule],
  1655. exports: [MatIcon, MatCommonModule],
  1656. declarations: [MatIcon],
  1657. },] },
  1658. ];
  1659. return MatIconModule;
  1660. }());
  1661. /**
  1662. * @fileoverview added by tsickle
  1663. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1664. */
  1665. /**
  1666. * @fileoverview added by tsickle
  1667. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1668. */
  1669. export { MatIconModule, MAT_ICON_LOCATION_FACTORY, MAT_ICON_LOCATION, MatIcon, getMatIconNameNotFoundError, getMatIconNoHttpProviderError, getMatIconFailedToSanitizeUrlError, getMatIconFailedToSanitizeLiteralError, ICON_REGISTRY_PROVIDER_FACTORY, MatIconRegistry, ICON_REGISTRY_PROVIDER };
  1670. //# sourceMappingURL=icon.es5.js.map