1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var utilities_1 = require("@uifabric/utilities");
|
5 | var merge_styles_1 = require("@uifabric/merge-styles");
|
6 | var ICON_SETTING_NAME = 'icons';
|
7 | var _iconSettings = utilities_1.GlobalSettings.getValue(ICON_SETTING_NAME, {
|
8 | __options: {
|
9 | disableWarnings: false,
|
10 | warnOnMissingIcons: true,
|
11 | },
|
12 | __remapped: {},
|
13 | });
|
14 |
|
15 | var stylesheet = merge_styles_1.Stylesheet.getInstance();
|
16 | if (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 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var normalizeIconName = function (name) { return name.toLowerCase(); };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function registerIcons(iconSubset, options) {
|
39 | var subset = tslib_1.__assign(tslib_1.__assign({}, iconSubset), { isRegistered: false, className: undefined });
|
40 | var icons = iconSubset.icons;
|
41 |
|
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 | }
|
59 | exports.registerIcons = registerIcons;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function 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 |
|
74 | if (!options.disableWarnings) {
|
75 | utilities_1.warn("The icon \"" + iconName + "\" tried to unregister but was not registered.");
|
76 | }
|
77 | }
|
78 |
|
79 | if (_iconSettings.__remapped[normalizedIconName]) {
|
80 | delete _iconSettings.__remapped[normalizedIconName];
|
81 | }
|
82 |
|
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 | }
|
94 | exports.unregisterIcons = unregisterIcons;
|
95 |
|
96 |
|
97 |
|
98 | function registerIconAlias(iconName, mappedToName) {
|
99 | _iconSettings.__remapped[normalizeIconName(iconName)] = normalizeIconName(mappedToName);
|
100 | }
|
101 | exports.registerIconAlias = registerIconAlias;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | function 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 |
|
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 | }
|
141 | exports.getIcon = getIcon;
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | function setIconOptions(options) {
|
148 | _iconSettings.__options = tslib_1.__assign(tslib_1.__assign({}, _iconSettings.__options), options);
|
149 | }
|
150 | exports.setIconOptions = setIconOptions;
|
151 | var _missingIcons = [];
|
152 | var _missingIconsTimer = undefined;
|
153 | function _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 |
|
\ | No newline at end of file |