icon-registry.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 { HttpClient } from '@angular/common/http';
  9. import { Optional, OnDestroy } from '@angular/core';
  10. import { DomSanitizer, SafeResourceUrl, SafeHtml } from '@angular/platform-browser';
  11. import { Observable } from 'rxjs';
  12. /**
  13. * Returns an exception to be thrown in the case when attempting to
  14. * load an icon with a name that cannot be found.
  15. * @docs-private
  16. */
  17. export declare function getMatIconNameNotFoundError(iconName: string): Error;
  18. /**
  19. * Returns an exception to be thrown when the consumer attempts to use
  20. * `<mat-icon>` without including @angular/common/http.
  21. * @docs-private
  22. */
  23. export declare function getMatIconNoHttpProviderError(): Error;
  24. /**
  25. * Returns an exception to be thrown when a URL couldn't be sanitized.
  26. * @param url URL that was attempted to be sanitized.
  27. * @docs-private
  28. */
  29. export declare function getMatIconFailedToSanitizeUrlError(url: SafeResourceUrl): Error;
  30. /**
  31. * Returns an exception to be thrown when a HTML string couldn't be sanitized.
  32. * @param literal HTML that was attempted to be sanitized.
  33. * @docs-private
  34. */
  35. export declare function getMatIconFailedToSanitizeLiteralError(literal: SafeHtml): Error;
  36. /**
  37. * Service to register and display icons used by the `<mat-icon>` component.
  38. * - Registers icon URLs by namespace and name.
  39. * - Registers icon set URLs by namespace.
  40. * - Registers aliases for CSS classes, for use with icon fonts.
  41. * - Loads icons from URLs and extracts individual icons from icon sets.
  42. */
  43. export declare class MatIconRegistry implements OnDestroy {
  44. private _httpClient;
  45. private _sanitizer;
  46. private _document;
  47. /**
  48. * URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
  49. */
  50. private _svgIconConfigs;
  51. /**
  52. * SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
  53. * Multiple icon sets can be registered under the same namespace.
  54. */
  55. private _iconSetConfigs;
  56. /** Cache for icons loaded by direct URLs. */
  57. private _cachedIconsByUrl;
  58. /** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
  59. private _inProgressUrlFetches;
  60. /** Map from font identifiers to their CSS class names. Used for icon fonts. */
  61. private _fontCssClassesByAlias;
  62. /**
  63. * The CSS class to apply when an `<mat-icon>` component has no icon name, url, or font specified.
  64. * The default 'material-icons' value assumes that the material icon font has been loaded as
  65. * described at http://google.github.io/material-design-icons/#icon-font-for-the-web
  66. */
  67. private _defaultFontSetClass;
  68. constructor(_httpClient: HttpClient, _sanitizer: DomSanitizer, document: any);
  69. /**
  70. * Registers an icon by URL in the default namespace.
  71. * @param iconName Name under which the icon should be registered.
  72. * @param url
  73. */
  74. addSvgIcon(iconName: string, url: SafeResourceUrl): this;
  75. /**
  76. * Registers an icon using an HTML string in the default namespace.
  77. * @param iconName Name under which the icon should be registered.
  78. * @param literal SVG source of the icon.
  79. */
  80. addSvgIconLiteral(iconName: string, literal: SafeHtml): this;
  81. /**
  82. * Registers an icon by URL in the specified namespace.
  83. * @param namespace Namespace in which the icon should be registered.
  84. * @param iconName Name under which the icon should be registered.
  85. * @param url
  86. */
  87. addSvgIconInNamespace(namespace: string, iconName: string, url: SafeResourceUrl): this;
  88. /**
  89. * Registers an icon using an HTML string in the specified namespace.
  90. * @param namespace Namespace in which the icon should be registered.
  91. * @param iconName Name under which the icon should be registered.
  92. * @param literal SVG source of the icon.
  93. */
  94. addSvgIconLiteralInNamespace(namespace: string, iconName: string, literal: SafeHtml): this;
  95. /**
  96. * Registers an icon set by URL in the default namespace.
  97. * @param url
  98. */
  99. addSvgIconSet(url: SafeResourceUrl): this;
  100. /**
  101. * Registers an icon set using an HTML string in the default namespace.
  102. * @param literal SVG source of the icon set.
  103. */
  104. addSvgIconSetLiteral(literal: SafeHtml): this;
  105. /**
  106. * Registers an icon set by URL in the specified namespace.
  107. * @param namespace Namespace in which to register the icon set.
  108. * @param url
  109. */
  110. addSvgIconSetInNamespace(namespace: string, url: SafeResourceUrl): this;
  111. /**
  112. * Registers an icon set using an HTML string in the specified namespace.
  113. * @param namespace Namespace in which to register the icon set.
  114. * @param literal SVG source of the icon set.
  115. */
  116. addSvgIconSetLiteralInNamespace(namespace: string, literal: SafeHtml): this;
  117. /**
  118. * Defines an alias for a CSS class name to be used for icon fonts. Creating an matIcon
  119. * component with the alias as the fontSet input will cause the class name to be applied
  120. * to the `<mat-icon>` element.
  121. *
  122. * @param alias Alias for the font.
  123. * @param className Class name override to be used instead of the alias.
  124. */
  125. registerFontClassAlias(alias: string, className?: string): this;
  126. /**
  127. * Returns the CSS class name associated with the alias by a previous call to
  128. * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
  129. */
  130. classNameForFontAlias(alias: string): string;
  131. /**
  132. * Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  133. * have a fontSet input value, and is not loading an icon by name or URL.
  134. *
  135. * @param className
  136. */
  137. setDefaultFontSetClass(className: string): this;
  138. /**
  139. * Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
  140. * have a fontSet input value, and is not loading an icon by name or URL.
  141. */
  142. getDefaultFontSetClass(): string;
  143. /**
  144. * Returns an Observable that produces the icon (as an `<svg>` DOM element) from the given URL.
  145. * The response from the URL may be cached so this will not always cause an HTTP request, but
  146. * the produced element will always be a new copy of the originally fetched icon. (That is,
  147. * it will not contain any modifications made to elements previously returned).
  148. *
  149. * @param safeUrl URL from which to fetch the SVG icon.
  150. */
  151. getSvgIconFromUrl(safeUrl: SafeResourceUrl): Observable<SVGElement>;
  152. /**
  153. * Returns an Observable that produces the icon (as an `<svg>` DOM element) with the given name
  154. * and namespace. The icon must have been previously registered with addIcon or addIconSet;
  155. * if not, the Observable will throw an error.
  156. *
  157. * @param name Name of the icon to be retrieved.
  158. * @param namespace Namespace in which to look for the icon.
  159. */
  160. getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
  161. ngOnDestroy(): void;
  162. /**
  163. * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
  164. */
  165. private _getSvgFromConfig;
  166. /**
  167. * Attempts to find an icon with the specified name in any of the SVG icon sets.
  168. * First searches the available cached icons for a nested element with a matching name, and
  169. * if found copies the element to a new `<svg>` element. If not found, fetches all icon sets
  170. * that have not been cached, and searches again after all fetches are completed.
  171. * The returned Observable produces the SVG element if possible, and throws
  172. * an error if no icon with the specified name can be found.
  173. */
  174. private _getSvgFromIconSetConfigs;
  175. /**
  176. * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
  177. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  178. * returns it. Returns null if no matching element is found.
  179. */
  180. private _extractIconWithNameFromAnySet;
  181. /**
  182. * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
  183. * from it.
  184. */
  185. private _loadSvgIconFromConfig;
  186. /**
  187. * Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
  188. * from it.
  189. */
  190. private _loadSvgIconSetFromConfig;
  191. /**
  192. * Creates a DOM element from the given SVG string, and adds default attributes.
  193. */
  194. private _createSvgElementForSingleIcon;
  195. /**
  196. * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
  197. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  198. * returns it. Returns null if no matching element is found.
  199. */
  200. private _extractSvgIconFromSet;
  201. /**
  202. * Creates a DOM element from the given SVG string.
  203. */
  204. private _svgElementFromString;
  205. /**
  206. * Converts an element into an SVG node by cloning all of its children.
  207. */
  208. private _toSvgElement;
  209. /**
  210. * Sets the default attributes for an SVG element to be used as an icon.
  211. */
  212. private _setSvgAttributes;
  213. /**
  214. * Returns an Observable which produces the string contents of the given URL. Results may be
  215. * cached, so future calls with the same URL may not cause another HTTP request.
  216. */
  217. private _fetchUrl;
  218. /**
  219. * Registers an icon config by name in the specified namespace.
  220. * @param namespace Namespace in which to register the icon config.
  221. * @param iconName Name under which to register the config.
  222. * @param config Config to be registered.
  223. */
  224. private _addSvgIconConfig;
  225. /**
  226. * Registers an icon set config in the specified namespace.
  227. * @param namespace Namespace in which to register the icon config.
  228. * @param config Config to be registered.
  229. */
  230. private _addSvgIconSetConfig;
  231. }
  232. /** @docs-private */
  233. export declare function ICON_REGISTRY_PROVIDER_FACTORY(parentRegistry: MatIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer, document?: any): MatIconRegistry;
  234. /** @docs-private */
  235. export declare const ICON_REGISTRY_PROVIDER: {
  236. provide: typeof MatIconRegistry;
  237. deps: (Optional[] | typeof DomSanitizer)[];
  238. useFactory: typeof ICON_REGISTRY_PROVIDER_FACTORY;
  239. };