-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNavMenu.razor
More file actions
365 lines (326 loc) · 15.2 KB
/
NavMenu.razor
File metadata and controls
365 lines (326 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
@implements IDisposable
@inject NavigationManager NavigationManager
@inject UserContextService UserContext
@inject ThemeService ThemeService
@inject IJSRuntime JSRuntime
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid d-flex justify-content-between align-items-center">
<a class="navbar-brand" href="">Aquiis.SimpleStart</a>
<div class="d-flex gap-2">
<button class="btn btn-link p-0" @onclick="ToggleTheme" title="Toggle theme">
<i class="bi @GetThemeIcon()" style="font-size: 1.4rem;"></i>
</button>
</div>
</div>
</div>
<input type="checkbox" title="Navigation menu" class="navbar-toggler" />
<div class="nav-scrollable">
<nav class="nav flex-column">
<OrganizationAuthorizeView Roles="Owner,Administrator">
<div class="nav-item px-3">
<NavLink class="nav-link" href="administration/settings/application" Match="NavLinkMatch.All">
<div><span class="bi bi-window-dock" aria-hidden="true"></span>Application</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="@($"administration/organizations/{OrganizationId}")">
<div><span class="bi bi-building" aria-hidden="true"></span>Organization</div>
</NavLink>
</div>
</OrganizationAuthorizeView>
<OrganizationAuthorizeView Roles="Owner,Administrator,PropertyManager">
<hr class="nav-separator" />
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<div><span class="bi bi-speedometer" aria-hidden="true"></span>Dashboard</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="calendar">
<div><span class="bi bi-calendar-fill" aria-hidden="true"></span>Calendar</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="propertymanagement/properties">
<div><span class="bi bi-house-fill" aria-hidden="true"></span>Properties</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="propertymanagement/repairs">
<div><span class="bi bi-tools" aria-hidden="true"></span>Repairs</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="reports">
<div><span class="bi bi-file-earmark-bar-graph" aria-hidden="true"></span>Reports</div>
</NavLink>
</div>
</OrganizationAuthorizeView>
<OrganizationAuthorizeView Roles="User">
<hr class="nav-separator" />
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<div><span class="bi bi-speedometer" aria-hidden="true"></span>Dashboard</div>
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="propertymanagement/properties">
<div><span class="bi bi-house-fill" aria-hidden="true"></span>Properties</div>
</NavLink>
</div>
</OrganizationAuthorizeView>
<AuthorizeView>
<Authorized>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Account/Manage">
<span class="bi bi-person-fill-nav-menu" aria-hidden="true"></span> @context.User.Identity?.Name
</NavLink>
</div>
<div class="nav-item px-3">
<form action="Account/Logout" method="post">
<AntiforgeryToken />
<input type="hidden" name="ReturnUrl" value="@currentUrl" />
<button type="submit" class="nav-link">
<span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout
</button>
</form>
</div>
<div class="nav-item px-3">
<div class="dropdown">
<button class="nav-link dropdown-toggle" type="button" id="brandThemeDropdown"
@onclick="ToggleBrandThemeDropdown"
@onclick:stopPropagation="true">
<span class="bi bi-palette-fill-nav-menu" aria-hidden="true"></span> Theme: @FormatBrandThemeName(ThemeService.CurrentBrandTheme)
</button>
@if (showBrandThemeDropdown)
{
<ul class="dropdown-menu show" style="position: relative;">
@foreach (var theme in AvailableBrandThemes)
{
<li>
<button class="dropdown-item @(ThemeService.CurrentBrandTheme == theme ? "active" : "")"
type="button"
@onclick="() => SelectBrandTheme(theme)"
@onclick:stopPropagation="true">
@FormatBrandThemeName(theme)
</button>
</li>
}
</ul>
}
</div>
</div>
</Authorized>
<NotAuthorized>
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Account/Register">
<span class="bi bi-person-nav-menu" aria-hidden="true"></span> Register
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Account/Login">
<span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span> Login
</NavLink>
</div>
<div class="nav-item px-3">
<div class="dropdown">
<button class="nav-link dropdown-toggle" type="button"
@onclick="ToggleBrandThemeDropdown"
@onclick:stopPropagation="true">
<span class="bi bi-palette-fill-nav-menu" aria-hidden="true"></span> Theme: @FormatBrandThemeName(ThemeService.CurrentBrandTheme)
</button>
@if (showBrandThemeDropdown)
{
<ul class="dropdown-menu show" style="position: relative;">
@foreach (var theme in AvailableBrandThemes)
{
<li>
<button class="dropdown-item @(ThemeService.CurrentBrandTheme == theme ? "active" : "")"
type="button"
@onclick="() => SelectBrandTheme(theme)"
@onclick:stopPropagation="true">
@FormatBrandThemeName(theme)
</button>
</li>
}
</ul>
}
</div>
</div>
</NotAuthorized>
</AuthorizeView>
</nav>
</div>
@code {
private string? currentUrl;
private string currentTheme = "light";
private bool showBrandThemeDropdown = false;
private string OrganizationId = string.Empty;
// Available brand themes - add new themes here
private readonly List<string> AvailableBrandThemes = new()
{
"bootstrap",
"obsidian",
"teal"
};
protected override async Task OnInitializedAsync()
{
currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.LocationChanged += OnLocationChanged;
ThemeService.OnThemeChanged += OnThemeChanged;
ThemeService.OnBrandThemeChanged += OnBrandThemeChanged;
currentTheme = ThemeService.CurrentTheme;
var orgId = await UserContext.GetActiveOrganizationIdAsync();
OrganizationId = orgId?.ToString() ?? string.Empty;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
try
{
// Read current DOM attributes that were set by theme.js initialization
var domTheme = await JSRuntime.InvokeAsync<string>("eval",
"document.documentElement.getAttribute('data-bs-theme') || localStorage.getItem('theme') || 'light'");
var domBrandTheme = await JSRuntime.InvokeAsync<string>("eval",
"document.documentElement.getAttribute('data-brand-theme') || localStorage.getItem('brandTheme') || 'bootstrap'");
// Sync the service with the DOM state (which was set by theme.js)
if (ThemeService.CurrentTheme != domTheme)
{
ThemeService.SetTheme(domTheme);
}
if (ThemeService.CurrentBrandTheme != domBrandTheme)
{
ThemeService.SetBrandTheme(domBrandTheme);
}
// Force UI refresh to reflect synced theme
StateHasChanged();
// Also ensure DOM attributes are still set (in case Blazor cleared them)
await JSRuntime.InvokeVoidAsync("themeManager.setTheme", ThemeService.CurrentTheme);
await JSRuntime.InvokeVoidAsync("themeManager.setBrandTheme", ThemeService.CurrentBrandTheme);
}
catch (Exception ex)
{
Console.WriteLine($"Error loading themes: {ex.Message}");
// Fallback if localStorage not available
ThemeService.SetTheme("light");
ThemeService.SetBrandTheme("bootstrap");
await JSRuntime.InvokeVoidAsync("themeManager.setTheme", "light");
await JSRuntime.InvokeVoidAsync("themeManager.setBrandTheme", "bootstrap");
}
}
}
private async Task ToggleTheme()
{
try
{
ThemeService.ToggleTheme();
currentTheme = ThemeService.CurrentTheme;
// Save to localStorage and update DOM immediately
await JSRuntime.InvokeVoidAsync("themeManager.setTheme", ThemeService.CurrentTheme);
// Force a complete page refresh to apply theme changes immediately
NavigationManager.Refresh(forceReload: false);
Console.WriteLine($"Theme toggled to: {ThemeService.CurrentTheme}");
}
catch (Exception ex)
{
Console.WriteLine($"Error toggling theme: {ex.Message}");
}
}
private async Task SelectBrandTheme(string brandTheme)
{
try
{
Console.WriteLine($"SelectBrandTheme called with: {brandTheme}");
// Close dropdown first
showBrandThemeDropdown = false;
StateHasChanged();
// Update service and JavaScript
ThemeService.SetBrandTheme(brandTheme);
await JSRuntime.InvokeVoidAsync("themeManager.setBrandTheme", brandTheme);
Console.WriteLine($"Brand theme changed to: {ThemeService.CurrentBrandTheme}");
// Force UI refresh
NavigationManager.Refresh(forceReload: false);
}
catch (Exception ex)
{
Console.WriteLine($"Error changing brand theme: {ex.Message}");
showBrandThemeDropdown = false;
StateHasChanged();
}
}
private void ToggleBrandThemeDropdown()
{
showBrandThemeDropdown = !showBrandThemeDropdown;
Console.WriteLine($"Dropdown toggled: {showBrandThemeDropdown}");
StateHasChanged();
}
private string FormatBrandThemeName(string theme)
{
return theme switch
{
"bootstrap" => "Bootstrap",
"obsidian" => "Obsidian",
"teal" => "Teal",
_ => theme.Substring(0, 1).ToUpper() + theme.Substring(1)
};
}
private string GetThemeIcon()
{
return currentTheme == "light" ? "bi-moon-fill" : "bi-sun-fill";
}
private void OnThemeChanged()
{
InvokeAsync(async () =>
{
try
{
currentTheme = ThemeService.CurrentTheme;
// Re-apply theme when service notifies of change
await JSRuntime.InvokeVoidAsync("themeManager.setTheme", ThemeService.CurrentTheme);
}
catch { }
StateHasChanged();
});
}
private void OnBrandThemeChanged()
{
InvokeAsync(async () =>
{
try
{
// Re-apply brand theme when service notifies of change
await JSRuntime.InvokeVoidAsync("themeManager.setBrandTheme", ThemeService.CurrentBrandTheme);
}
catch { }
StateHasChanged();
});
}
private async void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
var newUrl = NavigationManager.ToBaseRelativePath(e.Location);
// Only update if URL actually changed (prevents visual glitch on same-link clicks)
if (newUrl == currentUrl)
return;
currentUrl = newUrl;
// Re-apply current theme and brand theme after navigation
try
{
await JSRuntime.InvokeVoidAsync("themeManager.setTheme", ThemeService.CurrentTheme);
await JSRuntime.InvokeVoidAsync("themeManager.setBrandTheme", ThemeService.CurrentBrandTheme);
}
catch { }
StateHasChanged();
}
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
ThemeService.OnThemeChanged -= OnThemeChanged;
ThemeService.OnBrandThemeChanged -= OnBrandThemeChanged;
}
}