1 | import { __assign } from "tslib";
|
2 | import { GlobalSettings, warn } from '@uifabric/utilities';
|
3 | import { fontFace, mergeStyles, Stylesheet } from '@uifabric/merge-styles';
|
4 | var ICON_SETTING_NAME = 'icons';
|
5 | var _iconSettings = GlobalSettings.getValue(ICON_SETTING_NAME, {
|
6 | __options: {
|
7 | disableWarnings: false,
|
8 | warnOnMissingIcons: true,
|
9 | },
|
10 | __remapped: {},
|
11 | });
|
12 |
|
13 | var stylesheet = Stylesheet.getInstance();
|
14 | if (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 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var normalizeIconName = function (name) { return name.toLowerCase(); };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export function registerIcons(iconSubset, options) {
|
37 | var subset = __assign(__assign({}, iconSubset), { isRegistered: false, className: undefined });
|
38 | var icons = iconSubset.icons;
|
39 |
|
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 |
|
59 |
|
60 |
|
61 |
|
62 | export 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 |
|
71 | if (!options.disableWarnings) {
|
72 | warn("The icon \"" + iconName + "\" tried to unregister but was not registered.");
|
73 | }
|
74 | }
|
75 |
|
76 | if (_iconSettings.__remapped[normalizedIconName]) {
|
77 | delete _iconSettings.__remapped[normalizedIconName];
|
78 | }
|
79 |
|
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 |
|
93 |
|
94 | export function registerIconAlias(iconName, mappedToName) {
|
95 | _iconSettings.__remapped[normalizeIconName(iconName)] = normalizeIconName(mappedToName);
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | export 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 |
|
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 |
|
138 |
|
139 |
|
140 |
|
141 | export function setIconOptions(options) {
|
142 | _iconSettings.__options = __assign(__assign({}, _iconSettings.__options), options);
|
143 | }
|
144 | var _missingIcons = [];
|
145 | var _missingIconsTimer = undefined;
|
146 | function _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 |
|
\ | No newline at end of file |