PWA Fundvelo der Caritas.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NavMenu.razor 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. @inject AppState AppState;
  2. @inject NavigationManager NavigationManager;
  3. @inject IStringLocalizer<Resources> i18n
  4. @inject IJSRuntime jsRuntime;
  5. @inject PageHistoryManager PageHistoryManager;
  6. @implements IDisposable;
  7. @if (HandleAppBarContainer()) {
  8. <div class="sidebar">
  9. <MatAppBarContainer>
  10. <MatAppBar Fixed="true">
  11. <MatAppBarRow>
  12. <MatAppBarSection>
  13. <MatIconButton Icon="menu" OnClick="@((e) => ButtonClicked())"></MatIconButton>
  14. @if (PageHistoryManager.CanGoBack()) {
  15. <MatIconButton Icon="keyboard_backspace" OnClick="@((e) => ButtonBackClicked())" Disabled="@BackButtonDisabled()"></MatIconButton>
  16. }
  17. <MatAppBarTitle Style="padding-left:0px; font-size:90%; font-weight:500">@LocationUrl</MatAppBarTitle>
  18. </MatAppBarSection>
  19. <MatAppBarSection align="@MatAppBarSectionAlign.End" Style="max-width:min-content">
  20. <NavLink target="_blank" class="text-white small" href="https://www.caritas.ch" align="@MatAppBarSectionAlign.End" style="padding-right:1em">@i18n["Learnmore"]</NavLink>
  21. </MatAppBarSection>
  22. </MatAppBarRow>
  23. </MatAppBar>
  24. <MatAppBarContent>
  25. <MatDrawerContainer>
  26. <MatDrawer @bind-Opened="@Opened" Mode="@MatDrawerMode.Modal">
  27. <MatList>
  28. <MatListItem Class="@((Index == 1) ? "bg-primary-color text-white" : "")"
  29. Href="caritas_services"
  30. @onclick="@((e) => ButtonClicked(1))">
  31. <MatIcon Icon="@MatIconNames.Apps"></MatIcon>
  32. <MatListItemText Style="padding-left:0.5em">@i18n["CaritasServices"]</MatListItemText>
  33. </MatListItem>
  34. <MatListItem Class="@((Index == 2) ? "bg-primary-color text-white" : "")"
  35. Href="account"
  36. @onclick="@((e) => ButtonClicked(2))">
  37. <MatIcon Icon="@MatIconNames.Person_outline"></MatIcon>
  38. <MatListItemText Style="padding-left:0.5em">@i18n["Account"]</MatListItemText>
  39. </MatListItem>
  40. <MatListItem Class="@((Index == 3) ? "bg-primary-color text-white" : "")"
  41. Href="info"
  42. @onclick="@((e) => ButtonClicked(3))">
  43. <MatIcon Icon="@MatIconNames.Error_outline" Style="transform: rotate(180deg)"></MatIcon>
  44. <MatListItemText Style="padding-left:0.5em">@i18n["Info"]</MatListItemText>
  45. </MatListItem>
  46. <MatListItem Class="@((Index == 4) ? "bg-primary-color text-white" : "")"
  47. href=""
  48. Match="NavLinkMatch.All"
  49. @onclick="@((e) => ButtonClicked(4))">
  50. <MatIcon Icon="@MatIconNames.Exit_to_app"></MatIcon>
  51. <MatListItemText Style="padding-left:0.5em">@i18n["Logout"]</MatListItemText>
  52. </MatListItem>
  53. </MatList>
  54. </MatDrawer>
  55. </MatDrawerContainer>
  56. </MatAppBarContent>
  57. </MatAppBarContainer>
  58. </div>
  59. }
  60. @code {
  61. private bool Opened = false;
  62. private static int Index = 1;
  63. private string locUrl;
  64. private string LocationUrl {
  65. get => locUrl;
  66. set {
  67. locUrl = value;
  68. StateHasChanged();
  69. }
  70. }
  71. public void Dispose() {
  72. AppState.OnChange -= StateHasChanged;
  73. NavigationManager.LocationChanged -= LocationChanged;
  74. }
  75. protected override void OnInitialized() {
  76. AppState.OnChange += StateHasChanged;
  77. NavigationManager.LocationChanged += LocationChanged;
  78. PageHistoryManager.AddPageToHistory(NavigationManager.Uri);
  79. base.OnInitialized();
  80. }
  81. private void ButtonClicked() {
  82. Opened = !Opened;
  83. }
  84. private void ButtonClicked(int _Index) {
  85. Index = _Index;
  86. ButtonClicked();
  87. if (_Index == 4) {
  88. AppState.LoggedIn = false;
  89. }
  90. }
  91. private void ButtonBackClicked() {
  92. NavigationManager.NavigateTo(PageHistoryManager.GetPreviousPage());
  93. }
  94. private void LocationChanged(object sender, LocationChangedEventArgs e) {
  95. locUrl = i18n.GetString(e.Location.Replace(NavigationManager.BaseUri, ""));
  96. if (IsInServicesUrl(e)) {
  97. Index = 1;
  98. } else if (e.Location.Contains("account")) {
  99. Index = 2;
  100. } else if (e.Location.Contains("info")) {
  101. Index = 3;
  102. } else {
  103. Index = 4;
  104. }
  105. StateHasChanged();
  106. }
  107. private bool IsInServicesUrl(LocationChangedEventArgs e) {
  108. return (e.Location.Contains("caritas_services") || e.Location.Contains("lost_found") || e.Location.Contains("keydata")
  109. || e.Location.Contains("account/") || e.Location.Contains("conclusion_"));
  110. }
  111. private bool HandleAppBarContainer() {
  112. string uri = NavigationManager.Uri;
  113. string baseUri = NavigationManager.BaseUri;
  114. string delta = uri.Replace(baseUri, "");
  115. if (delta == null || delta.Equals("")) {
  116. Index = 4;
  117. return false;
  118. } else {
  119. if (delta.Equals("caritas_services")) {
  120. Index = 1;
  121. } else if (delta.Equals("account")) {
  122. Index = 2;
  123. } else if (delta.Equals("info")) {
  124. Index = 3;
  125. }
  126. return true;
  127. }
  128. }
  129. private bool BackButtonDisabled() {
  130. return !PageHistoryManager.CanGoBack();
  131. }
  132. }