using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using InABox.Configuration; using InABox.Core; namespace InABox.DynamicGrid; public class DynamicGridColumns : List, IGlobalConfigurationSettings, IUserConfigurationSettings { public string GridName { get; set; } private static Dictionary> _columnscache = new Dictionary>(); public DynamicGridColumns ExtractColumns(Type type) { if (!_columnscache.TryGetValue(type, out List? value)) { value = []; _columnscache[type] = value; var defaults = DefaultColumns.GetDefaultColumns(type); var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence); foreach (var prop in properties) { if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required) { var defaultCol = defaults.FirstOrDefault(x => x.Property == prop); if(defaultCol is not null) { value.Add(DynamicGridColumn.FromCoreGridColumn(defaultCol)); } else { var col = new DynamicGridColumn { ColumnName = prop.Name, Width = prop.Editor.Width, Alignment = prop.Editor.Alignment, Format = prop.Editor.Format, Editor = prop.Editor.CloneEditor(), Caption = prop.Caption }; value.Add(col); } } } } AddRange(value); return this; } public static DynamicGridColumn CreateColumn( Expression> member, int? width = null, string? caption = null, string? format = null, Alignment? alignment = null) { var prop = DatabaseSchema.Property(member); var col = new DynamicGridColumn { ColumnName = prop.Name, Width = width ?? prop.Editor.Width, Alignment = alignment ?? prop.Editor.Alignment, Format = format ?? prop.Editor.Format, Editor = prop.Editor.CloneEditor(), Caption = caption ?? prop.Caption }; return col; } public DynamicGridColumn Add( Expression> member, int? width = null, string? caption = null, string? format = null, Alignment? alignment = null ) { var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment); Add(col); return col; } } public class DynamicGridColumns : DynamicGridColumns { public DynamicGridColumn Add(Expression> member, int width, string caption, string format, Alignment alignment) { return Add(member, width, caption, format, alignment); } public DynamicGridColumn Add( Expression> member, int? width = null, string? caption = null, string? format = null, Alignment? alignment = null ) { var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment); Add(col); return col; } } public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null) { public string Header { get; set; } = header; public DynamicColumnBase StartColumn { get; set; } = start; public DynamicColumnBase EndColumn { get; set; } = end; public object? Tag { get; set; } = tag; } public class DynamicGridColumnGrouping { public List Groups = new(); public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null) { Groups.Add(new(header, start, end, tag: tag)); return this; } } public class DynamicGridColumnGroupings : List { }