NavigationScriptManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using Abp.Application.Navigation;
  4. using Abp.Dependency;
  5. using Abp.Json;
  6. using Abp.Runtime.Session;
  7. namespace Abp.Web.Navigation
  8. {
  9. internal class NavigationScriptManager : INavigationScriptManager, ITransientDependency
  10. {
  11. public IAbpSession AbpSession { get; set; }
  12. private readonly IUserNavigationManager _userNavigationManager;
  13. public NavigationScriptManager(IUserNavigationManager userNavigationManager)
  14. {
  15. _userNavigationManager = userNavigationManager;
  16. AbpSession = NullAbpSession.Instance;
  17. }
  18. public async Task<string> GetScriptAsync()
  19. {
  20. var userMenus = await _userNavigationManager.GetMenusAsync(AbpSession.ToUserIdentifier());
  21. var sb = new StringBuilder();
  22. sb.AppendLine("(function() {");
  23. sb.AppendLine(" abp.nav = {};");
  24. sb.AppendLine(" abp.nav.menus = {");
  25. for (int i = 0; i < userMenus.Count; i++)
  26. {
  27. AppendMenu(sb, userMenus[i]);
  28. if (userMenus.Count - 1 > i)
  29. {
  30. sb.Append(" , ");
  31. }
  32. }
  33. sb.AppendLine(" };");
  34. sb.AppendLine("})();");
  35. return sb.ToString();
  36. }
  37. private static void AppendMenu(StringBuilder sb, UserMenu menu)
  38. {
  39. sb.AppendLine(" '" + menu.Name + "': {");
  40. sb.AppendLine(" name: '" + menu.Name + "',");
  41. if (menu.DisplayName != null)
  42. {
  43. sb.AppendLine(" displayName: '" + menu.DisplayName + "',");
  44. }
  45. if (menu.CustomData != null)
  46. {
  47. sb.AppendLine(" customData: " + menu.CustomData.ToJsonString(true) + ",");
  48. }
  49. sb.Append(" items: ");
  50. if (menu.Items.Count <= 0)
  51. {
  52. sb.AppendLine("[]");
  53. }
  54. else
  55. {
  56. sb.Append("[");
  57. for (int i = 0; i < menu.Items.Count; i++)
  58. {
  59. AppendMenuItem(16, sb, menu.Items[i]);
  60. if (menu.Items.Count - 1 > i)
  61. {
  62. sb.Append(" , ");
  63. }
  64. }
  65. sb.AppendLine("]");
  66. }
  67. sb.AppendLine(" }");
  68. }
  69. private static void AppendMenuItem(int indentLength, StringBuilder sb, UserMenuItem menuItem)
  70. {
  71. sb.AppendLine("{");
  72. sb.AppendLine(new string(' ', indentLength + 4) + "name: '" + menuItem.Name + "',");
  73. sb.AppendLine(new string(' ', indentLength + 4) + "order: " + menuItem.Order + ",");
  74. if (!string.IsNullOrEmpty(menuItem.Icon))
  75. {
  76. sb.AppendLine(new string(' ', indentLength + 4) + "icon: '" + menuItem.Icon.Replace("'", @"\'") + "',");
  77. }
  78. if (!string.IsNullOrEmpty(menuItem.Url))
  79. {
  80. sb.AppendLine(new string(' ', indentLength + 4) + "url: '" + menuItem.Url.Replace("'", @"\'") + "',");
  81. }
  82. if (menuItem.DisplayName != null)
  83. {
  84. sb.AppendLine(new string(' ', indentLength + 4) + "displayName: '" + menuItem.DisplayName.Replace("'", @"\'") + "',");
  85. }
  86. if (menuItem.CustomData != null)
  87. {
  88. sb.AppendLine(new string(' ', indentLength + 4) + "customData: " + menuItem.CustomData.ToJsonString(true) + ",");
  89. }
  90. if (menuItem.Target != null)
  91. {
  92. sb.AppendLine(new string(' ', indentLength + 4) + "target: '" + menuItem.Target.Replace("'", @"\'") + "',");
  93. }
  94. sb.AppendLine(new string(' ', indentLength + 4) + "isEnabled: " + menuItem.IsEnabled.ToString().ToLowerInvariant() + ",");
  95. sb.AppendLine(new string(' ', indentLength + 4) + "isVisible: " + menuItem.IsVisible.ToString().ToLowerInvariant() + ",");
  96. sb.Append(new string(' ', indentLength + 4) + "items: [");
  97. for (int i = 0; i < menuItem.Items.Count; i++)
  98. {
  99. AppendMenuItem(24, sb, menuItem.Items[i]);
  100. if (menuItem.Items.Count - 1 > i)
  101. {
  102. sb.Append(" , ");
  103. }
  104. }
  105. sb.AppendLine("]");
  106. sb.Append(new string(' ', indentLength) + "}");
  107. }
  108. }
  109. }