icon-registry.d-BVwP8t9_.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { HttpClient } from '@angular/common/http';
  2. import * as i0 from '@angular/core';
  3. import { OnDestroy, ErrorHandler, Optional } from '@angular/core';
  4. import { SafeResourceUrl, SafeHtml, DomSanitizer } from '@angular/platform-browser';
  5. import { Observable } from 'rxjs';
  6. /**
  7. * Returns an exception to be thrown in the case when attempting to
  8. * load an icon with a name that cannot be found.
  9. * @docs-private
  10. */
  11. declare function getMatIconNameNotFoundError(iconName: string): Error;
  12. /**
  13. * Returns an exception to be thrown when the consumer attempts to use
  14. * `<mat-icon>` without including @angular/common/http.
  15. * @docs-private
  16. */
  17. declare function getMatIconNoHttpProviderError(): Error;
  18. /**
  19. * Returns an exception to be thrown when a URL couldn't be sanitized.
  20. * @param url URL that was attempted to be sanitized.
  21. * @docs-private
  22. */
  23. declare function getMatIconFailedToSanitizeUrlError(url: SafeResourceUrl): Error;
  24. /**
  25. * Returns an exception to be thrown when a HTML string couldn't be sanitized.
  26. * @param literal HTML that was attempted to be sanitized.
  27. * @docs-private
  28. */
  29. declare function getMatIconFailedToSanitizeLiteralError(literal: SafeHtml): Error;
  30. /** Options that can be used to configure how an icon or the icons in an icon set are presented. */
  31. interface IconOptions {
  32. /** View box to set on the icon. */
  33. viewBox?: string;
  34. /** Whether or not to fetch the icon or icon set using HTTP credentials. */
  35. withCredentials?: boolean;
  36. }
  37. /**
  38. * Function that will be invoked by the icon registry when trying to resolve the
  39. * URL from which to fetch an icon. The returned URL will be used to make a request for the icon.
  40. */
  41. type IconResolver = (name: string, namespace: string) => SafeResourceUrl | SafeResourceUrlWithIconOptions | null;
  42. /** Object that specifies a URL from which to fetch an icon and the options to use for it. */
  43. interface SafeResourceUrlWithIconOptions {
  44. url: SafeResourceUrl;
  45. options: IconOptions;
  46. }
  47. /**
  48. * Service to register and display icons used by the `<mat-icon>` component.
  49. * - Registers icon URLs by namespace and name.
  50. * - Registers icon set URLs by namespace.
  51. * - Registers aliases for CSS classes, for use with icon fonts.
  52. * - Loads icons from URLs and extracts individual icons from icon sets.
  53. */
  54. declare class MatIconRegistry implements OnDestroy {
  55. private _httpClient;
  56. private _sanitizer;
  57. private readonly _errorHandler;
  58. private _document;
  59. /**
  60. * URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
  61. */
  62. private _svgIconConfigs;
  63. /**
  64. * SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
  65. * Multiple icon sets can be registered under the same namespace.
  66. */
  67. private _iconSetConfigs;
  68. /** Cache for icons loaded by direct URLs. */
  69. private _cachedIconsByUrl;
  70. /** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
  71. private _inProgressUrlFetches;
  72. /** Map from font identifiers to their CSS class names. Used for icon fonts. */
  73. private _fontCssClassesByAlias;
  74. /** Registered icon resolver functions. */
  75. private _resolvers;
  76. /**
  77. * The CSS classes to apply when an `<mat-icon>` component has no icon name, url, or font
  78. * specified. The default 'material-icons' value assumes that the material icon font has been
  79. * loaded as described at https://google.github.io/material-design-icons/#icon-font-for-the-web
  80. */
  81. private _defaultFontSetClass;
  82. constructor(_httpClient: HttpClient, _sanitizer: DomSanitizer, document: any, _errorHandler: ErrorHandler);
  83. /**
  84. * Registers an icon by URL in the default namespace.
  85. * @param iconName Name under which the icon should be registered.
  86. * @param url
  87. */
  88. addSvgIcon(iconName: string, url: SafeResourceUrl, options?: IconOptions): this;
  89. /**
  90. * Registers an icon using an HTML string in the default namespace.
  91. * @param iconName Name under which the icon should be registered.
  92. * @param literal SVG source of the icon.
  93. */
  94. addSvgIconLiteral(iconName: string, literal: SafeHtml, options?: IconOptions): this;
  95. /**
  96. * Registers an icon by URL in the specified namespace.
  97. * @param namespace Namespace in which the icon should be registered.
  98. * @param iconName Name under which the icon should be registered.
  99. * @param url
  100. */
  101. addSvgIconInNamespace(namespace: string, iconName: string, url: SafeResourceUrl, options?: IconOptions): this;
  102. /**
  103. * Registers an icon resolver function with the registry. The function will be invoked with the
  104. * name and namespace of an icon when the registry tries to resolve the URL from which to fetch
  105. * the icon. The resolver is expected to return a `SafeResourceUrl` that points to the icon,
  106. * an object with the icon URL and icon options, or `null` if the icon is not supported. Resolvers
  107. * will be invoked in the order in which they have been registered.
  108. * @param resolver Resolver function to be registered.
  109. */
  110. addSvgIconResolver(resolver: IconResolver): this;
  111. /**
  112. * Registers an icon using an HTML string in the specified namespace.
  113. * @param namespace Namespace in which the icon should be registered.
  114. * @param iconName Name under which the icon should be registered.
  115. * @param literal SVG source of the icon.
  116. */
  117. addSvgIconLiteralInNamespace(namespace: string, iconName: string, literal: SafeHtml, options?: IconOptions): this;
  118. /**
  119. * Registers an icon set by URL in the default namespace.
  120. * @param url
  121. */
  122. addSvgIconSet(url: SafeResourceUrl, options?: IconOptions): this;
  123. /**
  124. * Registers an icon set using an HTML string in the default namespace.
  125. * @param literal SVG source of the icon set.
  126. */
  127. addSvgIconSetLiteral(literal: SafeHtml, options?: IconOptions): this;
  128. /**
  129. * Registers an icon set by URL in the specified namespace.
  130. * @param namespace Namespace in which to register the icon set.
  131. * @param url
  132. */
  133. addSvgIconSetInNamespace(namespace: string, url: SafeResourceUrl, options?: IconOptions): this;
  134. /**
  135. * Registers an icon set using an HTML string in the specified namespace.
  136. * @param namespace Namespace in which to register the icon set.
  137. * @param literal SVG source of the icon set.
  138. */
  139. addSvgIconSetLiteralInNamespace(namespace: string, literal: SafeHtml, options?: IconOptions): this;
  140. /**
  141. * Defines an alias for CSS class names to be used for icon fonts. Creating an matIcon
  142. * component with the alias as the fontSet input will cause the class name to be applied
  143. * to the `<mat-icon>` element.
  144. *
  145. * If the registered font is a ligature font, then don't forget to also include the special
  146. * class `mat-ligature-font` to allow the usage via attribute. So register like this:
  147. *
  148. * ```ts
  149. * iconRegistry.registerFontClassAlias('f1', 'font1 mat-ligature-font');
  150. * ```
  151. *
  152. * And use like this:
  153. *
  154. * ```html
  155. * <mat-icon fontSet="f1" fontIcon="home"></mat-icon>
  156. * ```
  157. *
  158. * @param alias Alias for the font.
  159. * @param classNames Class names override to be used instead of the alias.
  160. */
  161. registerFontClassAlias(alias: string, classNames?: string): this;
  162. /**
  163. * Returns the CSS class name associated with the alias by a previous call to
  164. * registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
  165. */
  166. classNameForFontAlias(alias: string): string;
  167. /**
  168. * Sets the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
  169. * have a fontSet input value, and is not loading an icon by name or URL.
  170. */
  171. setDefaultFontSetClass(...classNames: string[]): this;
  172. /**
  173. * Returns the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
  174. * have a fontSet input value, and is not loading an icon by name or URL.
  175. */
  176. getDefaultFontSetClass(): string[];
  177. /**
  178. * Returns an Observable that produces the icon (as an `<svg>` DOM element) from the given URL.
  179. * The response from the URL may be cached so this will not always cause an HTTP request, but
  180. * the produced element will always be a new copy of the originally fetched icon. (That is,
  181. * it will not contain any modifications made to elements previously returned).
  182. *
  183. * @param safeUrl URL from which to fetch the SVG icon.
  184. */
  185. getSvgIconFromUrl(safeUrl: SafeResourceUrl): Observable<SVGElement>;
  186. /**
  187. * Returns an Observable that produces the icon (as an `<svg>` DOM element) with the given name
  188. * and namespace. The icon must have been previously registered with addIcon or addIconSet;
  189. * if not, the Observable will throw an error.
  190. *
  191. * @param name Name of the icon to be retrieved.
  192. * @param namespace Namespace in which to look for the icon.
  193. */
  194. getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
  195. ngOnDestroy(): void;
  196. /**
  197. * Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
  198. */
  199. private _getSvgFromConfig;
  200. /**
  201. * Attempts to find an icon with the specified name in any of the SVG icon sets.
  202. * First searches the available cached icons for a nested element with a matching name, and
  203. * if found copies the element to a new `<svg>` element. If not found, fetches all icon sets
  204. * that have not been cached, and searches again after all fetches are completed.
  205. * The returned Observable produces the SVG element if possible, and throws
  206. * an error if no icon with the specified name can be found.
  207. */
  208. private _getSvgFromIconSetConfigs;
  209. /**
  210. * Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
  211. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  212. * returns it. Returns null if no matching element is found.
  213. */
  214. private _extractIconWithNameFromAnySet;
  215. /**
  216. * Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
  217. * from it.
  218. */
  219. private _loadSvgIconFromConfig;
  220. /**
  221. * Loads the content of the icon set URL specified in the
  222. * SvgIconConfig and attaches it to the config.
  223. */
  224. private _loadSvgIconSetFromConfig;
  225. /**
  226. * Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
  227. * tag matches the specified name. If found, copies the nested element to a new SVG element and
  228. * returns it. Returns null if no matching element is found.
  229. */
  230. private _extractSvgIconFromSet;
  231. /**
  232. * Creates a DOM element from the given SVG string.
  233. */
  234. private _svgElementFromString;
  235. /**
  236. * Converts an element into an SVG node by cloning all of its children.
  237. */
  238. private _toSvgElement;
  239. /**
  240. * Sets the default attributes for an SVG element to be used as an icon.
  241. */
  242. private _setSvgAttributes;
  243. /**
  244. * Returns an Observable which produces the string contents of the given icon. Results may be
  245. * cached, so future calls with the same URL may not cause another HTTP request.
  246. */
  247. private _fetchIcon;
  248. /**
  249. * Registers an icon config by name in the specified namespace.
  250. * @param namespace Namespace in which to register the icon config.
  251. * @param iconName Name under which to register the config.
  252. * @param config Config to be registered.
  253. */
  254. private _addSvgIconConfig;
  255. /**
  256. * Registers an icon set config in the specified namespace.
  257. * @param namespace Namespace in which to register the icon config.
  258. * @param config Config to be registered.
  259. */
  260. private _addSvgIconSetConfig;
  261. /** Parses a config's text into an SVG element. */
  262. private _svgElementFromConfig;
  263. /** Tries to create an icon config through the registered resolver functions. */
  264. private _getIconConfigFromResolvers;
  265. static ɵfac: i0.ɵɵFactoryDeclaration<MatIconRegistry, [{ optional: true; }, null, { optional: true; }, null]>;
  266. static ɵprov: i0.ɵɵInjectableDeclaration<MatIconRegistry>;
  267. }
  268. /**
  269. * @docs-private
  270. * @deprecated No longer used, will be removed.
  271. * @breaking-change 21.0.0
  272. */
  273. declare function ICON_REGISTRY_PROVIDER_FACTORY(parentRegistry: MatIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer, errorHandler: ErrorHandler, document?: any): MatIconRegistry;
  274. /**
  275. * @docs-private
  276. * @deprecated No longer used, will be removed.
  277. * @breaking-change 21.0.0
  278. */
  279. declare const ICON_REGISTRY_PROVIDER: {
  280. provide: typeof MatIconRegistry;
  281. deps: (Optional[] | typeof DomSanitizer | typeof ErrorHandler)[];
  282. useFactory: typeof ICON_REGISTRY_PROVIDER_FACTORY;
  283. };
  284. export { MatIconRegistry as M, getMatIconNoHttpProviderError as a, getMatIconFailedToSanitizeUrlError as b, getMatIconFailedToSanitizeLiteralError as c, ICON_REGISTRY_PROVIDER_FACTORY as e, ICON_REGISTRY_PROVIDER as f, getMatIconNameNotFoundError as g };
  285. export type { IconOptions as I, SafeResourceUrlWithIconOptions as S, IconResolver as d };