|
| 1 | +@inherits LayoutComponentBase |
| 2 | +@inject NavigationManager _navMan |
| 3 | + |
| 4 | +<style> |
| 5 | + /* the size of the containers will fill up their parents up to the viewport */ |
| 6 | + html, body { |
| 7 | + width: 100%; |
| 8 | + height: 100%; |
| 9 | + max-height: 100%; |
| 10 | + } |
| 11 | + body { |
| 12 | + margin: 0; |
| 13 | + display: flex; |
| 14 | + flex-direction: column; |
| 15 | + } |
| 16 | +
|
| 17 | + /* vertical scroll should happen in the main portion of the content - where the Body is |
| 18 | + This keeps the header sticky at the top */ |
| 19 | + .main { |
| 20 | + padding: var(--kendo-spacing-4, 1rem); |
| 21 | + overflow: auto; |
| 22 | + } |
| 23 | +
|
| 24 | + .k-drawer-container { |
| 25 | + flex:1; |
| 26 | + overflow:auto; |
| 27 | + } |
| 28 | +
|
| 29 | + /* horizontal scroll happens in the drawer content to keep the drawer on the left side of the screen */ |
| 30 | + .k-drawer-content { |
| 31 | + height:100%; |
| 32 | + overflow-x: auto; |
| 33 | + } |
| 34 | +
|
| 35 | +</style> |
| 36 | + |
| 37 | +<TelerikRootComponent> |
| 38 | + <TelerikAppBar ThemeColor="@(ThemeConstants.AppBar.ThemeColor.Tertiary)" > |
| 39 | + <AppBarSection> |
| 40 | + <TelerikButton FillMode="flat" |
| 41 | + Icon="@SvgIcon.Menu" |
| 42 | + OnClick="@(() => DrawerRef.ToggleAsync())"/> |
| 43 | + </AppBarSection> |
| 44 | + <AppBarSpacer Size="var(--kendo-spacing-1)" /> |
| 45 | + <AppBarSection> |
| 46 | + <h1>ML Dashboard</h1> |
| 47 | + </AppBarSection> |
| 48 | +</TelerikAppBar> |
| 49 | + <TelerikDrawer Data="@NavigablePages" |
| 50 | + @bind-Expanded="@DrawerExpanded" |
| 51 | + MiniMode="true" |
| 52 | + Mode="@DrawerMode.Push" |
| 53 | + @ref="@DrawerRef" |
| 54 | + @bind-SelectedItem="@SelectedItem"> |
| 55 | + <DrawerContent> |
| 56 | + @* This layout is now in the drawer Content, and is very similar to the default template layout *@ |
| 57 | + <div class="main"> |
| 58 | + @Body |
| 59 | + </div> |
| 60 | + </DrawerContent> |
| 61 | + </TelerikDrawer> |
| 62 | + |
| 63 | +</TelerikRootComponent> |
| 64 | + |
| 65 | +<div id="blazor-error-ui"> |
| 66 | + An unhandled error has occurred. |
| 67 | + <a href="" class="reload">Reload</a> |
| 68 | + <a class="dismiss">🗙</a> |
| 69 | +</div> |
| 70 | + |
| 71 | +@code { |
| 72 | + bool DrawerExpanded { get; set; } = true; // drawer expanded by default |
| 73 | + DrawerItem SelectedItem { get; set; } = DrawerItem.None(); // set in OnInitialized |
| 74 | + TelerikDrawer<DrawerItem> DrawerRef { get; set; } = default!; // set by framework |
| 75 | +
|
| 76 | + // in this sample we hardcode the existing pages, in your case you can |
| 77 | + // create the list based on your business logic (e.g., based on user roles/access) |
| 78 | + List<DrawerItem> NavigablePages { get; set; } = new List<DrawerItem> |
| 79 | + { |
| 80 | + new DrawerItem { Text = "Home", Url = "/", Icon = SvgIcon.Home.Name }, |
| 81 | + new DrawerItem { Separator = true }, |
| 82 | + new DrawerItem { Text = "Counter", Url = "counter", Icon = SvgIcon.Plus.Name }, |
| 83 | + new DrawerItem { Text = "Weather", Url = "weather", Icon = SvgIcon.Cloud.Name }, |
| 84 | + }; |
| 85 | + |
| 86 | + protected override void OnInitialized() |
| 87 | + { |
| 88 | + // pre-select the page the user lands on |
| 89 | + // as the user clicks items, the DOM changes only in the Body and so the selected item stays active |
| 90 | + SelectedItem = NavigablePages.Where(hasUrlEqualToPageRoute).FirstOrDefault(DrawerItem.None()); |
| 91 | + |
| 92 | + base.OnInitialized(); |
| 93 | + } |
| 94 | + |
| 95 | + bool hasUrlEqualToPageRoute(DrawerItem item) { |
| 96 | + if(item.Url is null) return false; |
| 97 | + |
| 98 | + string navItemAsAbsoluteUri = _navMan.ToAbsoluteUri(item.Url).AbsoluteUri; |
| 99 | + if (string.Equals(_navMan.Uri, navItemAsAbsoluteUri, StringComparison.OrdinalIgnoreCase)) |
| 100 | + { |
| 101 | + return true; |
| 102 | + } |
| 103 | + // Special case: highlight links to http://host/path/ even if you're |
| 104 | + // at http://host/path (with no trailing slash) |
| 105 | + // |
| 106 | + // This is because the router accepts an absolute URI value of "same |
| 107 | + // as base URI but without trailing slash" as equivalent to "base URI", |
| 108 | + // which in turn is because it's common for servers to return the same page |
| 109 | + // for http://host/vdir as they do for host://host/vdir/ as it's no |
| 110 | + // good to display a blank page in that case. |
| 111 | + bool isNotRoot = item.Url != "/"; |
| 112 | + bool hasTrailingSlash = _navMan.Uri[_navMan.Uri.Length - 1] == '/'; |
| 113 | + if(isNotRoot && hasTrailingSlash && |
| 114 | + _navMan.Uri.StartsWith(navItemAsAbsoluteUri, StringComparison.OrdinalIgnoreCase)) |
| 115 | + { |
| 116 | + return true; |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + // generally, this should go into its own file, but it is here to keep all the drawer-related code in one place |
| 122 | + public class DrawerItem |
| 123 | + { |
| 124 | + public string Text { get; set; } = ""; |
| 125 | + public string? Url { get; set; } |
| 126 | + public string? Icon { get; set; } |
| 127 | + public bool Separator { get; set; } |
| 128 | + public static DrawerItem None() => new(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
0 commit comments