// Copyright (c) MudBlazor 2021 // MudBlazor licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Diagnostics.CodeAnalysis; namespace Connected.Components; public class TableGroupDefinition<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T> { public TableGroupDefinition() { } public TableGroupDefinition(Func selector, TableGroupDefinition innerGroup = null) { Selector = selector; InnerGroup = innerGroup; } /// /// Gets or Sets the Group Name. It's useful for use on Header, for example. /// public string GroupName { get; set; } /// /// The selector func to be used on .GroupBy() with LINQ. /// public Func Selector { get; set; } private TableGroupDefinition _innerGroup; public TableGroupDefinition InnerGroup { get => _innerGroup; set { if (_innerGroup != null) { _innerGroup.Parent = null; _innerGroup.Context = null; } _innerGroup = value; if (_innerGroup != null) { _innerGroup.Parent = this; _innerGroup.Indentation = Indentation; _innerGroup.Context = Context; } } } internal TableGroupDefinition Parent { get; private set; } private bool _indentation; /// /// Gets or Sets if First Column cell must have Indentation. /// It must be set on First grouping level and works recursively. /// public bool Indentation { get => _indentation; set { _indentation = value; if (InnerGroup != null) InnerGroup.Indentation = value; } } private bool _expandable = false; /// /// Gets or Sets is group header can Expand and Collapse its children. /// public bool Expandable { get => _expandable; set { _expandable = value; if (_expandable == false) Context?.GroupRows.Where(gr => gr.GroupDefinition == this).ToList().ForEach(gr => gr.IsExpanded = IsInitiallyExpanded); } } private bool _isInitiallyExpanded = true; /// /// Gets or Sets if expandable group header is collapsed or expanded on initialization. /// public bool IsInitiallyExpanded { get => _isInitiallyExpanded; set { _isInitiallyExpanded = value; } } internal bool IsParentExpandable { get { if (Parent?.Expandable ?? false) return true; else return Parent?.IsParentExpandable ?? false; } } internal bool IsThisOrParentExpandable { get { if (Expandable) return Expandable; else return Parent?.IsThisOrParentExpandable ?? false; } } internal int Level { get { if (Parent == null) return 1; else return Parent.Level + 1; } } private TableContext _context; internal TableContext Context { get => _context; set { _context = value; if (_innerGroup != null) { _innerGroup.Context = _context; } } } }