NavigationScriptManager.cs 4.6 KB

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