NTP Analyzer  0.8.2
Analyze the operation of time servers
BootstrapMenuRender.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Linq;
24 using System.Text;
26 using Ntp.Common.Web;
27 
28 namespace Ntp.Analyzer.Render
29 {
30  public sealed class BootstrapMenuRender : HtmlObjectRender
31  {
32  public BootstrapMenuRender(Uri webPath, MenuConfiguration menu, ILinkable page)
33  : base(webPath)
34  {
35  this.menu = menu;
36  this.page = page;
37  }
38 
39  private readonly MenuConfiguration menu;
40  private readonly ILinkable page;
41 
42  public override string Render()
43  {
44  var b = new StringBuilder();
45 
46  b.AppendLine(@" <div class=""navbar navbar-inverse navbar-fixed-top"">");
47  b.AppendLine(@" <div class=""container"">");
48  b.AppendLine(@" <div class=""navbar-header"">");
49  b.AppendLine(
50  @" <button type=""button"" class=""navbar-toggle"" data-toggle=""collapse"" data-target="".navbar-collapse"">");
51  b.AppendLine(@" <span class=""icon-bar""></span>");
52  b.AppendLine(@" <span class=""icon-bar""></span>");
53  b.AppendLine(@" <span class=""icon-bar""></span>");
54  b.AppendLine(@" </button>");
55 
56  var headItem =
57  menu.MenuItems.SingleOrDefault(i => i is HeadMenuItemConfiguration) as HeadMenuItemConfiguration;
58 
59  if (headItem != null)
60  {
61  b.AppendLine(
62  $@" <a class=""navbar-brand"" href=""{headItem.Link.ToHtmlString()}"">{headItem.Caption}</a>");
63  }
64 
65  b.AppendLine(@" </div>");
66  b.AppendLine(@" <div class=""navbar-collapse collapse"">");
67  b.AppendLine(@" <ul class=""nav navbar-nav"">");
68 
69  foreach (var item in menu.MenuItems.Where(i => !(i is HeadMenuItemConfiguration)))
70  {
71  string active = item.Page == page ? @" class=""active""" : string.Empty;
72 
74  {
75  b.AppendLine(
76  $@" <li{active}><a href=""{item.Link.ToHtmlString()}"">{item.Caption}</a></li>");
77  }
78  else if (item is DropdownItemConfiguration)
79  {
80  b.AppendLine(@" <li class=""dropdown"">");
81  b.AppendLine(
82  @" <a href=""#"" class=""dropdown-toggle"" data-toggle=""dropdown"">Peers <b class=""caret""></b></a>");
83  b.AppendLine(@" <ul class=""dropdown-menu"">");
84 
85  var dropdownItem = item as DropdownItemConfiguration;
86 
87  foreach (var subItem in dropdownItem.MenuItems)
88  {
89  if (subItem is LinkMenuItemConfiguration || subItem is PageMenuItemConfiguration)
90  {
91  b.AppendLine(
92  $@" <li><a href=""{subItem.Link.ToHtmlString()}"">{subItem.Caption}</a></li>");
93  }
94  else if (subItem is SpacerMenuItemConfiguration)
95  {
96  b.AppendLine(@" <li class=""divider""></li>");
97  }
98  else if (subItem is HeaderMenuItemConfiguration)
99  {
100  b.AppendLine($@" <li class=""dropdown-header"">{subItem.Caption}</li>");
101  }
102  else
103  {
104  throw new NotSupportedException("Unknown item type.");
105  }
106  }
107 
108  b.AppendLine(@" </ul>");
109  }
110  else
111  {
112  throw new NotSupportedException("Unknown item type.");
113  }
114  }
115 
116  b.AppendLine(@" </ul>");
117  b.AppendLine(@" </div>");
118  b.AppendLine(@" </div>");
119  b.AppendLine(@" </div>");
120 
121  return b.ToString();
122  }
123 
124  public override string RenderFooter()
125  {
126  return string.Empty;
127  }
128 
129  public override string RenderHead()
130  {
131  return string.Empty;
132  }
133  }
134 }
BootstrapMenuRender(Uri webPath, MenuConfiguration menu, ILinkable page)
var b
Definition: bootstrap.min.js:6