Merge branch 'v8/8.12' into v8/dev

This commit is contained in:
Sebastiaan Janssen
2021-03-12 09:29:44 +01:00
14 changed files with 190 additions and 193 deletions

View File

@@ -75,9 +75,8 @@ Icon with additional attribute. It can be treated like any other dom element
iconHelper.getIcon(icon)
.then(data => {
if (data !== null && data.svgString !== undefined) {
if (data && data.svgString) {
// Watch source SVG string
//icon.svgString.$$unwrapTrustedValue();
scope.svgString = data.svgString;
}
});

View File

@@ -3,9 +3,9 @@
* @name umbraco.services.iconHelper
* @description A helper service for dealing with icons, mostly dealing with legacy tree icons
**/
function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) {
function iconHelper($http, $q, $sce, $timeout) {
var converter = [
const converter = [
{ oldIcon: ".sprNew", newIcon: "add" },
{ oldIcon: ".sprDelete", newIcon: "remove" },
{ oldIcon: ".sprMove", newIcon: "enter" },
@@ -85,15 +85,61 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) {
{ oldIcon: ".sprTreeDeveloperPython", newIcon: "icon-linux" }
];
var collectedIcons;
let collectedIcons;
var imageConverter = [
{oldImage: "contour.png", newIcon: "icon-umb-contour"}
];
let imageConverter = [
{oldImage: "contour.png", newIcon: "icon-umb-contour"}
];
var iconCache = [];
var liveRequests = [];
var allIconsRequested = false;
const iconCache = [];
const promiseQueue = [];
let resourceLoadStatus = "none";
/**
* This is the same approach as use for loading the localized text json
* We don't want multiple requests for the icon collection, so need to track
* the current request state, and resolve the queued requests once the icons arrive
* Subsequent requests are returned immediately as the icons are cached into
*/
function init() {
const deferred = $q.defer();
if (resourceLoadStatus === "loaded") {
deferred.resolve(iconCache);
return deferred.promise;
}
if (resourceLoadStatus === "loading") {
promiseQueue.push(deferred);
return deferred.promise;
}
resourceLoadStatus = "loading";
$http({ method: "GET", url: Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetIcons' })
.then(function (response) {
resourceLoadStatus = "loaded";
for (const [key, value] of Object.entries(response.data.Data)) {
iconCache.push({name: key, svgString: $sce.trustAsHtml(value)})
}
deferred.resolve(iconCache);
//ensure all other queued promises are resolved
for (let p in promiseQueue) {
promiseQueue[p].resolve(iconCache);
}
}, function (err) {
deferred.reject("Something broke");
//ensure all other queued promises are resolved
for (let p in promiseQueue) {
promiseQueue[p].reject("Something broke");
}
});
return deferred.promise;
}
return {
@@ -187,67 +233,12 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) {
/** Gets a single IconModel */
getIcon: function(iconName) {
return $q((resolve, reject) => {
var icon = this._getIconFromCache(iconName);
if(icon !== undefined) {
resolve(icon);
} else {
var iconRequestPath = Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetIcon?iconName=' + iconName;
// 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.
// This is a bit rough and ready and could probably be improved used an event based system
if(liveRequests.indexOf(iconRequestPath) >= 0) {
setTimeout(() => {
resolve(this.getIcon(iconName));
}, 10);
} else {
liveRequests.push(iconRequestPath);
// TODO - fix bug where Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl is undefinied when help icon
umbRequestHelper.resourcePromise(
$http.get(iconRequestPath)
,'Failed to retrieve icon: ' + iconName)
.then(icon => {
if(icon) {
var trustedIcon = this.defineIcon(icon.Name, icon.SvgString);
liveRequests = _.filter(liveRequests, iconRequestPath);
resolve(trustedIcon);
}
})
.catch(err => {
console.warn(err);
});
};
}
});
return init().then(icons => icons.find(i => i.name === iconName));
},
/** Gets all the available icons in the backoffice icon folder and returns them as an array of IconModels */
getAllIcons: function() {
return $q((resolve, reject) => {
if(allIconsRequested === false) {
allIconsRequested = true;
umbRequestHelper.resourcePromise(
$http.get(Umbraco.Sys.ServerVariables.umbracoUrls.iconApiBaseUrl + 'GetAllIcons')
,'Failed to retrieve icons')
.then(icons => {
icons.forEach(icon => {
this.defineIcon(icon.Name, icon.SvgString);
});
resolve(iconCache);
})
.catch(err => {
console.warn(err);
});;
} else {
resolve(iconCache);
}
});
return init().then(icons => icons);
},
/** LEGACY - Return a list of icons from icon fonts, optionally filter them */
@@ -312,9 +303,8 @@ function iconHelper($http, $q, $sce, $timeout, umbRequestHelper) {
},
/** Returns the cached icon or undefined */
_getIconFromCache: function(iconName) {
return _.find(iconCache, {name: iconName});
}
_getIconFromCache: iconName => iconCache.find(icon => icon.name === iconName)
};
}
angular.module('umbraco.services').factory('iconHelper', iconHelper);