| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using InABox.Core;
- namespace InABox.Avalonia
- {
- public class ShellColumn<TEntity>
- where TEntity : Entity
- {
- public int Index { get; }
- public Expression<Func<TEntity, object?>> Expression { get; }
- public string ColumnName { get; }
- public ShellColumn(int index, Expression<Func<TEntity, object?>> expression)
- {
- Index = index;
- Expression = expression;
- ColumnName = CoreUtils.GetFullPropertyName(expression, ".");
- }
- }
- public class ShellColumns<TParent,TEntity> : IShellColumns<TEntity> where TEntity : Entity
- {
-
- private static Dictionary<string, ShellColumn<TEntity>> _columns = new();
- public int IndexOf(string name) => _columns[name].Index;
-
- public string this[string name] => _columns[name].ColumnName;
- public ShellColumns<TParent,TEntity> Map(string property, Expression<Func<TEntity, object?>> expression)
- {
- int iCol = _columns.Keys.Count;
- if(_columns.TryGetValue(property, out var column))
- {
- iCol = column.Index;
- }
-
- _columns[property] = new(iCol, expression);
- return this;
- }
- public string? FindShellProperty(string entityProperty)
- {
- foreach(var (property, column) in _columns)
- {
- if(column.ColumnName == entityProperty)
- {
- return property;
- }
- }
- return null;
- }
- public Columns<TEntity> Columns
- {
- get
- {
- var columns = new Columns<TEntity>(ColumnTypeFlags.None).Add(_columns.Values.Select(x => x.ColumnName));
- columns.Add(new Columns<TEntity>(ColumnTypeFlags.Required)); // Have to be added after the other columns to preserve column indexing.
- return columns;
- }
- }
-
- public int Count => _columns.Count;
- }
- }
|