UNPKG

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