// gets the top-level parent public static T GetParent <T>(T _element, List <T> _element_record) where T : DisplayableProductDefinition { if (_element == null || _element_record == null) { return(null); } if (_element_record.Count == 0) { return(null); } // top level if (_element_record.Contains(_element)) { return(null); } foreach (T item in _element_record) { List <T> itemContent = DisplayableProductDefinition.GetFlatSubElementList(item); if (itemContent.Contains(_element)) { return(item); } } return(null); }
public static List <T> FlattenHierarchicalRecord <T>(List <T> _record, bool _include_invisible = true) where T : DisplayableProductDefinition { if (_record == null) { return(null); } List <T> record_flat = new List <T>(); foreach (T item in _record) { item.IsMarkable = true; if (!_include_invisible && item.IsExcludedFromDisplay) { continue; } record_flat.Add(item); List <T> subItems = DisplayableProductDefinition.GetFlatSubElementList(item); foreach (T si in subItems) { si.IsMarkable = false; } record_flat.AddRange(subItems); } return(record_flat); }
public static T SelectElement <T>(long _id, List <T> _element_record, ref List <T> _element_record_flat) where T : DisplayableProductDefinition { if (_id < 0) { return(null); } if (_element_record == null) { return(null); } if (_element_record.Count == 0) { return(null); } if (_element_record_flat == null) { _element_record_flat = DisplayableProductDefinition.FlattenHierarchicalRecord(_element_record); } T selected = null; foreach (T item in _element_record_flat) { if (item == null || item.ID != _id) { item.IsSelected = false; item.IsParentOfSelected = false; } else { item.IsSelected = true; selected = item; List <T> parents = DisplayableProductDefinition.GetParentChain(item, _element_record); if (parents != null && parents.Count > 0) { foreach (T parent in parents) { if (parent.ID == _id) { continue; } parent.IsParentOfSelected = true; parent.IsExpanded = true; } } } } return(selected); }
public static List <T> GetFlatSubElementList <T>(T _element) where T : DisplayableProductDefinition { if (_element.ContainedOfSameType == null || _element.ContainedOfSameType.Count < 1) { return(new List <T>()); } List <T> list = new List <T>(); foreach (T item in _element.ContainedOfSameType) { if (item == null) { continue; } list.Add(item); if (item.ContainedOfSameType.Count > 0) { list.AddRange(DisplayableProductDefinition.GetFlatSubElementList(item)); } } return(list); }
public static List <T> GetParentChain <T>(T _element, List <T> _element_record) where T : DisplayableProductDefinition { if (_element == null || _element_record == null) { return(null); } if (_element_record.Count == 0) { return(null); } // if at top level if (_element_record.Contains(_element)) { return new List <T> { _element } } ; List <T> chain = new List <T>(); // get top-level parent T topParent = null; foreach (T item in _element_record) { List <T> itemContent = DisplayableProductDefinition.GetFlatSubElementList(item); if (itemContent.Contains(_element)) { topParent = item; break; } } if (topParent == null) { return new List <T> { _element } } ; // proceed from the top parent down chain.Add(topParent); T currentParent = topParent; while (currentParent.ID != _element.ID) { foreach (T item in currentParent.ContainedOfSameType) { if (item == null) { continue; } if (item.ID == _element.ID) { currentParent = item; break; } List <T> subNodeContent = DisplayableProductDefinition.GetFlatSubElementList(item); if (subNodeContent.Contains(_element)) { currentParent = item; break; } } chain.Add(currentParent); } return(chain); }