소스 검색

Form small random functions

Kenric Nugteren 1 년 전
부모
커밋
c6f84a3d4c

+ 1 - 1
InABox.Core/Column.cs

@@ -149,7 +149,7 @@ namespace InABox.Core
             var result = Activator.CreateInstance(type);
             return (result as IColumns)!;
         }
-        
+
         public static IColumns Create(Type concrete)
         {
             var type = typeof(Columns<>).MakeGenericType(concrete);

+ 38 - 0
InABox.Core/DigitalForms/DFUtils.cs

@@ -112,6 +112,44 @@ namespace InABox.Core
         }
 
 
+        private static Dictionary<string, Tuple<Type, Type>>? _formInstanceTypes;
+        private static Dictionary<string, Tuple<Type, Type>> FormInstanceTypes
+        {
+            get
+            {
+                _formInstanceTypes ??= CoreUtils.TypeList(
+                    AppDomain.CurrentDomain.GetAssemblies(),
+                    x => !x.IsAbstract && x.GetInterfaces().Contains(typeof(IDigitalFormInstance))
+                ).Select(x =>
+                {
+                    var inter = x.GetInterfaces()
+                        .Where(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IDigitalFormInstance<>))).FirstOrDefault();
+                    if (inter != null)
+                    {
+                        var link = inter.GenericTypeArguments[0];
+                        var entityLinkDef = link.GetSuperclassDefinition(typeof(EntityLink<>));
+                        if (entityLinkDef != null)
+                        {
+                            var entityType = entityLinkDef.GenericTypeArguments[0];
+                            return new Tuple<string, Type, Type>(entityType.Name, x, entityType);
+                        }
+                    }
+                    return null;
+                }).Where(x => x != null).ToDictionary(x => x!.Item1, x => new Tuple<Type, Type>(x!.Item2, x!.Item3));
+                return _formInstanceTypes;
+            }
+        }
+
+        public static Type? GetFormInstanceType(string appliesTo)
+        {
+            if (FormInstanceTypes.TryGetValue(appliesTo, out var result))
+            {
+                return result.Item1;
+            }
+            return null;
+        }
+
+
         private static Dictionary<Type, IEntityFormUtils> _formUtils = new Dictionary<Type, IEntityFormUtils>();
 
         public static void AddFormUtils<TForm, TEntity, TEntityLink>(

+ 16 - 9
InABox.Core/DigitalForms/Forms/DigitalFormCategoryLookups.cs

@@ -6,22 +6,29 @@ namespace InABox.Core
 {
     public class DigitalFormCategoryLookups : LookupGenerator<object>
     {
+        private static Type[]? _formTypes;
+        public static IEnumerable<Type> FormTypes
+        {
+            get
+            {
+                _formTypes ??= CoreUtils.TypeList(
+                    AppDomain.CurrentDomain.GetAssemblies(),
+                    x => !x.IsAbstract && x.GetInterfaces().Contains(typeof(IDigitalForm)) // &&  !x.GetInterfaces().Contains(typeof(IQAInstance))
+                ).ToArray();
+                return _formTypes;
+            }
+        }
 
-        public Dictionary<String, String> Lookups()
+        public Dictionary<string, string> Lookups()
         {
             var result = new Dictionary<string, string>(); // { { "", "Select Category" } };
 
-            var types = CoreUtils.TypeList(
-                AppDomain.CurrentDomain.GetAssemblies(),
-                x => !x.IsAbstract && x.GetInterfaces().Contains(typeof(IDigitalForm)) // &&  !x.GetInterfaces().Contains(typeof(IQAInstance))
-            ).ToArray();
-
-            foreach (var type in types)
+            foreach (var type in FormTypes)
             {
                 var forms = type.GetInterfaces().Where(x => x.GetInterfaces().Contains(typeof(IDigitalForm)) && x.IsGenericType);
                 foreach (var form in forms)
-                foreach (var generic in form.GenericTypeArguments)
-                    result[generic.EntityName().Split('.').Last()] = generic.GetCaption();
+                    foreach (var generic in form.GenericTypeArguments)
+                        result[generic.EntityName().Split('.').Last()] = generic.GetCaption();
             }
 
             return result;

+ 2 - 0
InABox.Core/DigitalForms/Forms/DigitalFormVariable.cs

@@ -115,6 +115,8 @@ namespace InABox.Core
             return "{0:D3}";
         }
 
+        public DFLayoutFieldProperties GetProperties() => _properties;
+
         public string FormatValue(object? value)
         {
             return _properties.FormatValue(value);

+ 8 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutEmbeddedImage/DFLayoutEmbeddedImageProperties.cs

@@ -42,7 +42,14 @@ namespace InABox.Core
 
         public override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(DFLayoutEmbeddedMediaValue value)
         {
-            yield return new KeyValuePair<string, object?>(Code, DigitalFormDocumentFactory.LoadDocument(value.ID));
+            if(value.Data != null && value.Data.Length > 0)
+            {
+                yield return new KeyValuePair<string, object?>(Code, value.Data);
+            }
+            else
+            {
+                yield return new KeyValuePair<string, object?>(Code, DigitalFormDocumentFactory.LoadDocument(value.ID));
+            }
             yield return new KeyValuePair<string, object?>($"{Code}.ID", value.ID);
             yield return new KeyValuePair<string, object?>($"{Code}.Thumbnail", value.Thumbnail);
         }