code_junkie

ASP.NET MVC HtmlHelper extensions for YUI controls (Yahoo User Interfaces)

Nov 13th, 2011
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. public class MenuItem
  2.  
  3. {
  4. public string Text { get; set; }
  5. public string Description { get; set; }
  6. public string RouteURL { get; set; }
  7. public bool SeparatorBefore { get; set; }
  8.  
  9. public List<MenuItem> MenuItems { get; set; }
  10. }
  11.  
  12. public static class YUIExtensions
  13. {
  14. public static string RenderMenu(this HtmlHelper html, string id, List<MenuItem> menuItems)
  15. {
  16. // <div id="mnuTopNav" class="yuimenubar yuimenubarnav">
  17. // <div class="bd">
  18. // <ul class="first-of-type">
  19.  
  20. // <li class="yuimenubaritem first-of-type"><a class="yuimenubaritemlabel" href="#store">Store</a></li>
  21.  
  22. // <li class="yuimenubaritem"><a class="yuimenubaritemlabel" href="#products">Products</a>
  23.  
  24. // <div id="communication" class="yuimenu">
  25. // <div class="bd">
  26. // <ul>
  27. // <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://360.yahoo.com">360&#176;</a></li>
  28. // <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://mobile.yahoo.com">Mobile</a></li>
  29. // <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://www.flickr.com">Flickr Photo Sharing</a></li>
  30. // </ul>
  31. // </div>
  32. // </div>
  33. // </li>
  34.  
  35. // </ul>
  36. // </div>
  37. //</div>
  38.  
  39. int menuId = 0;
  40. HtmlGenericControl menuControl = CreateControl(html, id, 0, ref menuId, menuItems);
  41.  
  42. // render to string
  43. StringWriter sw = new StringWriter();
  44. HtmlTextWriter tw = new HtmlTextWriter(sw);
  45. tw.Indent = 1;
  46. menuControl.RenderControl(tw);
  47. return sw.ToString();
  48. }
  49.  
  50. private static HtmlGenericControl CreateControl(HtmlHelper html, string id, int level, ref int menuId, List<MenuItem> currentItems)
  51. {
  52. var menu = new HtmlGenericControl("div");
  53. menu.Attributes["class"] = (level == 0) ? "yuimenubar yuimenubarnav" : "yuimenu";
  54. menu.Attributes["id"] = id;
  55.  
  56. var div_bd = new HtmlGenericControl("div");
  57. menu.Controls.Add(div_bd);
  58. div_bd.Attributes["class"] = "bd";
  59.  
  60. HtmlGenericControl ul = null;
  61.  
  62. int i = 0;
  63. foreach (var menuItem in currentItems)
  64. {
  65. if (ul == null || menuItem.SeparatorBefore)
  66. {
  67. ul = new HtmlGenericControl("ul");
  68. div_bd.Controls.Add(ul);
  69.  
  70. if (i == 0)
  71. {
  72. ul.Attributes["class"] = "first-of-type";
  73. }
  74. }
  75.  
  76. var menuItem_li = new HtmlGenericControl("li");
  77. menuItem_li.Attributes["class"] = (level == 0) ? "yuimenubaritem" : "yuimenuitem";
  78. if (i == 0)
  79. {
  80. menuItem_li.Attributes["class"] += " first-of-type";
  81. }
  82. ul.Controls.Add(menuItem_li);
  83.  
  84. var href = new HtmlGenericControl("a");
  85. href.Attributes["class"] = (level == 0) ? "yuimenubaritemlabel" : "yuimenuitemlabel";
  86. href.Attributes["href"] = menuItem.RouteURL;
  87. href.InnerHtml = menuItem.Text;
  88. menuItem_li.Controls.Add(href);
  89.  
  90. if (menuItem.MenuItems != null && menuItem.MenuItems.Count > 0)
  91. {
  92. menuItem_li.Controls.Add(CreateControl(html, id + "_" + (menuId++), level + 1, ref menuId, menuItem.MenuItems));
  93. }
  94. i++;
  95. }
  96.  
  97. return menu;
  98. }
  99. }
  100.  
  101. <%= Html.RenderMenu("mnuTopNav", ViewData.Model.MainMenu.MenuOptions) %>
  102.  
  103. <!-- Combo-handled YUI CSS files: -->
  104. <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.6.0/build/menu/assets/skins/sam/menu.css">
  105. <!-- Combo-handled YUI JS files: -->
  106. <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&2.6.0/build/container/container_core-min.js&2.6.0/build/menu/menu-min.js"></script>
Add Comment
Please, Sign In to add comment