UNPKG

6.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var utilities_1 = require("@uifabric/utilities");
5var merge_styles_1 = require("@uifabric/merge-styles");
6var ICON_SETTING_NAME = 'icons';
7var _iconSettings = utilities_1.GlobalSettings.getValue(ICON_SETTING_NAME, {
8 __options: {
9 disableWarnings: false,
10 warnOnMissingIcons: true,
11 },
12 __remapped: {},
13});
14// Reset icon registration on stylesheet resets.
15var stylesheet = merge_styles_1.Stylesheet.getInstance();
16if (stylesheet && stylesheet.onReset) {
17 stylesheet.onReset(function () {
18 for (var name_1 in _iconSettings) {
19 if (_iconSettings.hasOwnProperty(name_1) && !!_iconSettings[name_1].subset) {
20 _iconSettings[name_1].subset.className = undefined;
21 }
22 }
23 });
24}
25/**
26 * Normalizes an icon name for consistent mapping.
27 * Current implementation is to convert the icon name to lower case.
28 *
29 * @param name - Icon name to normalize.
30 * @returns {string} Normalized icon name to use for indexing and mapping.
31 */
32var normalizeIconName = function (name) { return name.toLowerCase(); };
33/**
34 * Registers a given subset of icons.
35 *
36 * @param iconSubset - the icon subset definition.
37 */
38function registerIcons(iconSubset, options) {
39 var subset = tslib_1.__assign(tslib_1.__assign({}, iconSubset), { isRegistered: false, className: undefined });
40 var icons = iconSubset.icons;
41 // Grab options, optionally mix user provided ones on top.
42 options = options ? tslib_1.__assign(tslib_1.__assign({}, _iconSettings.__options), options) : _iconSettings.__options;
43 for (var iconName in icons) {
44 if (icons.hasOwnProperty(iconName)) {
45 var code = icons[iconName];
46 var normalizedIconName = normalizeIconName(iconName);
47 if (_iconSettings[normalizedIconName]) {
48 _warnDuplicateIcon(iconName);
49 }
50 else {
51 _iconSettings[normalizedIconName] = {
52 code: code,
53 subset: subset,
54 };
55 }
56 }
57 }
58}
59exports.registerIcons = registerIcons;
60/**
61 * Unregisters icons by name.
62 *
63 * @param iconNames - List of icons to unregister.
64 */
65function unregisterIcons(iconNames) {
66 var options = _iconSettings.__options;
67 var _loop_1 = function (iconName) {
68 var normalizedIconName = normalizeIconName(iconName);
69 if (_iconSettings[normalizedIconName]) {
70 delete _iconSettings[normalizedIconName];
71 }
72 else {
73 // Warn that we are trying to delete an icon that doesn't exist
74 if (!options.disableWarnings) {
75 utilities_1.warn("The icon \"" + iconName + "\" tried to unregister but was not registered.");
76 }
77 }
78 // Delete any aliases for this iconName
79 if (_iconSettings.__remapped[normalizedIconName]) {
80 delete _iconSettings.__remapped[normalizedIconName];
81 }
82 // Delete any items that were an alias for this iconName
83 Object.keys(_iconSettings.__remapped).forEach(function (key) {
84 if (_iconSettings.__remapped[key] === normalizedIconName) {
85 delete _iconSettings.__remapped[key];
86 }
87 });
88 };
89 for (var _i = 0, iconNames_1 = iconNames; _i < iconNames_1.length; _i++) {
90 var iconName = iconNames_1[_i];
91 _loop_1(iconName);
92 }
93}
94exports.unregisterIcons = unregisterIcons;
95/**
96 * Remaps one icon name to another.
97 */
98function registerIconAlias(iconName, mappedToName) {
99 _iconSettings.__remapped[normalizeIconName(iconName)] = normalizeIconName(mappedToName);
100}
101exports.registerIconAlias = registerIconAlias;
102/**
103 * Gets an icon definition. If an icon is requested but the subset has yet to be registered,
104 * it will get registered immediately.
105 *
106 * @public
107 * @param name - Name of icon.
108 */
109function getIcon(name) {
110 var icon = undefined;
111 var options = _iconSettings.__options;
112 name = name ? normalizeIconName(name) : '';
113 name = _iconSettings.__remapped[name] || name;
114 if (name) {
115 icon = _iconSettings[name];
116 if (icon) {
117 var subset = icon.subset;
118 if (subset && subset.fontFace) {
119 if (!subset.isRegistered) {
120 merge_styles_1.fontFace(subset.fontFace);
121 subset.isRegistered = true;
122 }
123 if (!subset.className) {
124 subset.className = merge_styles_1.mergeStyles(subset.style, {
125 fontFamily: subset.fontFace.fontFamily,
126 fontWeight: subset.fontFace.fontWeight || 'normal',
127 fontStyle: subset.fontFace.fontStyle || 'normal',
128 });
129 }
130 }
131 }
132 else {
133 // eslint-disable-next-line deprecation/deprecation
134 if (!options.disableWarnings && options.warnOnMissingIcons) {
135 utilities_1.warn("The icon \"" + name + "\" was used but not registered. See https://github.com/microsoft/fluentui/wiki/Using-icons for more information.");
136 }
137 }
138 }
139 return icon;
140}
141exports.getIcon = getIcon;
142/**
143 * Sets the icon options.
144 *
145 * @public
146 */
147function setIconOptions(options) {
148 _iconSettings.__options = tslib_1.__assign(tslib_1.__assign({}, _iconSettings.__options), options);
149}
150exports.setIconOptions = setIconOptions;
151var _missingIcons = [];
152var _missingIconsTimer = undefined;
153function _warnDuplicateIcon(iconName) {
154 var options = _iconSettings.__options;
155 var warningDelay = 2000;
156 var maxIconsInMessage = 10;
157 if (!options.disableWarnings) {
158 _missingIcons.push(iconName);
159 if (_missingIconsTimer === undefined) {
160 _missingIconsTimer = setTimeout(function () {
161 utilities_1.warn("Some icons were re-registered. Applications should only call registerIcons for any given " +
162 "icon once. Redefining what an icon is may have unintended consequences. Duplicates " +
163 "include: \n" +
164 _missingIcons.slice(0, maxIconsInMessage).join(', ') +
165 (_missingIcons.length > maxIconsInMessage ? " (+ " + (_missingIcons.length - maxIconsInMessage) + " more)" : ''));
166 _missingIconsTimer = undefined;
167 _missingIcons = [];
168 }, warningDelay);
169 }
170 }
171}
172//# sourceMappingURL=icons.js.map
\No newline at end of file