|
3 | 3 | * @name umbraco.services.iconHelper |
4 | 4 | * @description A helper service for dealing with icons, mostly dealing with legacy tree icons |
5 | 5 | **/ |
6 | | -function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) { |
| 6 | +function iconHelper($http, $q, $sce, $timeout) { |
7 | 7 |
|
8 | | - var converter = [ |
| 8 | + const converter = [ |
9 | 9 | { oldIcon: ".sprNew", newIcon: "add" }, |
10 | 10 | { oldIcon: ".sprDelete", newIcon: "remove" }, |
11 | 11 | { oldIcon: ".sprMove", newIcon: "enter" }, |
@@ -85,15 +85,61 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) { |
85 | 85 | { oldIcon: ".sprTreeDeveloperPython", newIcon: "icon-linux" } |
86 | 86 | ]; |
87 | 87 |
|
88 | | - var collectedIcons; |
| 88 | + let collectedIcons; |
89 | 89 |
|
90 | | - var imageConverter = [ |
91 | | - {oldImage: "contour.png", newIcon: "icon-umb-contour"} |
92 | | - ]; |
| 90 | + let imageConverter = [ |
| 91 | + {oldImage: "contour.png", newIcon: "icon-umb-contour"} |
| 92 | + ]; |
| 93 | + |
| 94 | + const iconCache = []; |
| 95 | + const promiseQueue = []; |
| 96 | + let resourceLoadStatus = "none"; |
| 97 | + |
| 98 | + /** |
| 99 | + * This is the same approach as use for loading the localized text json |
| 100 | + * We don't want multiple requests for the icon collection, so need to track |
| 101 | + * the current request state, and resolve the queued requests once the icons arrive |
| 102 | + * Subsequent requests are returned immediately as the icons are cached into |
| 103 | + */ |
| 104 | + function init() { |
| 105 | + const deferred = $q.defer(); |
| 106 | + |
| 107 | + if (resourceLoadStatus === "loaded") { |
| 108 | + deferred.resolve(iconCache); |
| 109 | + return deferred.promise; |
| 110 | + } |
| 111 | + |
| 112 | + if (resourceLoadStatus === "loading") { |
| 113 | + promiseQueue.push(deferred); |
| 114 | + return deferred.promise; |
| 115 | + } |
93 | 116 |
|
94 | | - var iconCache = []; |
95 | | - var liveRequests = []; |
96 | | - var allIconsRequested = false; |
| 117 | + resourceLoadStatus = "loading"; |
| 118 | + |
| 119 | + $http({ method: "GET", url: Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetIcons' }) |
| 120 | + .then(function (response) { |
| 121 | + resourceLoadStatus = "loaded"; |
| 122 | + |
| 123 | + for (const [key, value] of Object.entries(response.data.Data)) { |
| 124 | + iconCache.push({name: key, svgString: $sce.trustAsHtml(value)}) |
| 125 | + } |
| 126 | + |
| 127 | + deferred.resolve(iconCache); |
| 128 | + |
| 129 | + //ensure all other queued promises are resolved |
| 130 | + for (let p in promiseQueue) { |
| 131 | + promiseQueue[p].resolve(iconCache); |
| 132 | + } |
| 133 | + }, function (err) { |
| 134 | + deferred.reject("Something broke"); |
| 135 | + //ensure all other queued promises are resolved |
| 136 | + for (let p in promiseQueue) { |
| 137 | + promiseQueue[p].reject("Something broke"); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + return deferred.promise; |
| 142 | + } |
97 | 143 |
|
98 | 144 | return { |
99 | 145 |
|
@@ -187,67 +233,12 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) { |
187 | 233 |
|
188 | 234 | /** Gets a single IconModel */ |
189 | 235 | getIcon: function(iconName) { |
190 | | - return $q((resolve, reject) => { |
191 | | - var icon = this._getIconFromCache(iconName); |
192 | | - |
193 | | - if(icon !== undefined) { |
194 | | - resolve(icon); |
195 | | - } else { |
196 | | - var iconRequestPath = Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetIcon?iconName=' + iconName; |
197 | | - |
198 | | - // If the current icon is being requested, wait a bit so that we don't have to make another http request and can instead get the icon from the cache. |
199 | | - // This is a bit rough and ready and could probably be improved used an event based system |
200 | | - if(liveRequests.indexOf(iconRequestPath) >= 0) { |
201 | | - setTimeout(() => { |
202 | | - resolve(this.getIcon(iconName)); |
203 | | - }, 10); |
204 | | - } else { |
205 | | - liveRequests.push(iconRequestPath); |
206 | | - // TODO - fix bug where Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl is undefinied when help icon |
207 | | - umbRequestHelper.resourcePromise( |
208 | | - $http.get(iconRequestPath) |
209 | | - ,'Failed to retrieve icon: ' + iconName) |
210 | | - .then(icon => { |
211 | | - if(icon) { |
212 | | - var trustedIcon = this.defineIcon(icon.Name, icon.SvgString); |
213 | | - |
214 | | - liveRequests = _.filter(liveRequests, iconRequestPath); |
215 | | - |
216 | | - resolve(trustedIcon); |
217 | | - } |
218 | | - }) |
219 | | - .catch(err => { |
220 | | - console.warn(err); |
221 | | - }); |
222 | | - }; |
223 | | - |
224 | | - } |
225 | | - }); |
| 236 | + return init().then(icons => icons.find(i => i.name === iconName)); |
226 | 237 | }, |
227 | 238 |
|
228 | 239 | /** Gets all the available icons in the backoffice icon folder and returns them as an array of IconModels */ |
229 | 240 | getAllIcons: function() { |
230 | | - return $q((resolve, reject) => { |
231 | | - if(allIconsRequested === false) { |
232 | | - allIconsRequested = true; |
233 | | - |
234 | | - umbRequestHelper.resourcePromise( |
235 | | - $http.get(Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetAllIcons') |
236 | | - ,'Failed to retrieve icons') |
237 | | - .then(icons => { |
238 | | - icons.forEach(icon => { |
239 | | - this.defineIcon(icon.Name, icon.SvgString); |
240 | | - }); |
241 | | - |
242 | | - resolve(iconCache); |
243 | | - }) |
244 | | - .catch(err => { |
245 | | - console.warn(err); |
246 | | - });; |
247 | | - } else { |
248 | | - resolve(iconCache); |
249 | | - } |
250 | | - }); |
| 241 | + return init().then(icons => icons); |
251 | 242 | }, |
252 | 243 |
|
253 | 244 | /** LEGACY - Return a list of icons from icon fonts, optionally filter them */ |
@@ -312,9 +303,8 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) { |
312 | 303 | }, |
313 | 304 |
|
314 | 305 | /** Returns the cached icon or undefined */ |
315 | | - _getIconFromCache: function(iconName) { |
316 | | - return _.find(iconCache, {name: iconName}); |
317 | | - } |
| 306 | + _getIconFromCache: iconName => iconCache.find(icon => icon.name === iconName) |
| 307 | + |
318 | 308 | }; |
319 | 309 | } |
320 | 310 | angular.module('umbraco.services').factory('iconHelper', iconHelper); |
0 commit comments