// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Squidex.Domain.Apps.Core.Rules; using Squidex.Infrastructure; namespace Squidex.Extensions.Actions { public static class RuleElementRegistry { private const string Suffix = "Action"; private static readonly HashSet ActionHandlerTypes = new HashSet(); private static readonly Dictionary ActionTypes = new Dictionary(); private static readonly Dictionary TriggerTypes = new Dictionary { ["ContentChanged"] = new RuleElement { IconImage = "", IconColor = "#3389ff", Display = "Content changed", Description = "Content changed like created, updated, published, unpublished..." }, ["AssetChanged"] = new RuleElement { IconImage = "", IconColor = "#3389ff", Display = "Asset changed", Description = "Asset changed like created, updated, renamed..." } }; public static IReadOnlyDictionary Triggers { get { return TriggerTypes; } } public static IReadOnlyDictionary Actions { get { return ActionTypes; } } public static IReadOnlyCollection ActionHandlers { get { return ActionHandlerTypes; } } static RuleElementRegistry() { var actionTypes = typeof(RuleElementRegistry).Assembly .GetTypes() .Where(x => typeof(RuleAction).IsAssignableFrom(x)) .Where(x => x.GetCustomAttribute() != null) .Where(x => x.GetCustomAttribute() != null) .ToList(); foreach (var actionType in actionTypes) { var name = actionType.Name; if (name.EndsWith(Suffix, StringComparison.Ordinal)) { name = name.Substring(0, name.Length - Suffix.Length); } var metadata = actionType.GetCustomAttribute(); ActionTypes[name] = new RuleElement { Type = actionType, Display = metadata.Display, Description = metadata.Description, IconColor = metadata.IconColor, IconImage = metadata.IconImage, ReadMore = metadata.ReadMore }; ActionHandlerTypes.Add(actionType.GetCustomAttribute().HandlerType); } } public static void RegisterTypes(TypeNameRegistry typeNameRegistry) { foreach (var actionType in ActionTypes.Values) { typeNameRegistry.Map(actionType.Type, actionType.Type.Name); } } } }