TreeViewSettingsExtension.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using DevExpress.Web.Mvc;
  2. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GreenTree.Nachtragsmanagement.Web.Framework.Extension.DevExpress
  9. {
  10. public static class TreeViewSettingsExtension
  11. {
  12. /// <summary>
  13. /// Extension method to build up a tree view of the available functions
  14. /// </summary>
  15. /// <param name="settings">The current TreeViewSettings.</param>
  16. /// <param name="functionList">Available functions.</param>
  17. public static void CreateGroupedFunctionList(this TreeViewSettings settings, IList<Function> functionList)
  18. {
  19. if (settings == null || functionList == null) return;
  20. var dynFunctions = functionList
  21. .Select(f => new
  22. {
  23. Function = f,
  24. SubFunctions = new List<dynamic>()
  25. })
  26. .ToList();
  27. var dictFunctions = dynFunctions.ToDictionary(g => g.Function.Name);
  28. foreach (var item in dynFunctions.Where(g => g.Function.GroupName != null))
  29. {
  30. dictFunctions[item.Function.GroupName].SubFunctions.Add(item);
  31. }
  32. var groupFunctions = dynFunctions.Where(g => g.Function.GroupName == null);
  33. foreach (var mainFunction in groupFunctions)
  34. {
  35. var node = settings.Nodes.Add(mainFunction.Function.Description, mainFunction.Function.Id.ToString());
  36. foreach (var subFunction in mainFunction.SubFunctions)
  37. IterateGroupedFunction(node, subFunction);
  38. }
  39. }
  40. /// <summary>
  41. /// Recursive method to iterate each subFunction of a dynamic function tree and creates a corresponding node
  42. /// </summary>
  43. /// <param name="node">The current parent to be iterated.</param>
  44. /// <param name="functionContainer">The corresponding function tree.</param>
  45. private static void IterateGroupedFunction(MVCxTreeViewNode node, dynamic functionContainer)
  46. {
  47. var subNode = node.Nodes.Add(functionContainer.Function.Description, functionContainer.Function.Id.ToString());
  48. foreach (var subFunction in functionContainer.SubFunctions)
  49. IterateGroupedFunction(subNode, subFunction);
  50. }
  51. }
  52. }