Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Created October 5, 2011 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahjohannessen/1264592 to your computer and use it in GitHub Desktop.
Save ahjohannessen/1264592 to your computer and use it in GitHub Desktop.
For Dru
// Experimental stuff until we get a better picture of how to approach page tree, levels, subpages and such
public class MenuBuilder : IMenuBuilder
{
private readonly IPages _pages;
private readonly IContextualMenu<Page> _pagesMenu;
private readonly Page _currentPage;
public MenuBuilder(IPages pages, IContextualMenu<Page> pagesMenu, Page currentPage)
{
_pages = pages;
_pagesMenu = pagesMenu;
_currentPage = currentPage;
}
public HtmlTag BuildMenu()
{
var menu = new HtmlTag("ul").AddClass("menu");
var pages = _pages.GetAll();
pages.Each(p => _pagesMenu.MenuItemsFor(p)
.Where(m => m.EnabledAndShown())
.Each(m => menu.Append("li", h =>
{
h.Append(new LinkTag(m.Text, m.Url));
if (_currentPage.Id == p.Id)
{
h.AddClass("selected");
}
})));
return menu;
}
}
// Not really generally appropriate, but has some very nice ideas.
public class PagesMenu : IContextualMenu<Page>
{
private readonly IEndpointService _endpoints;
private readonly IEnumerable<IContextualAction<Page>> _definitions;
public PagesMenu(IEndpointService endpoints, IEnumerable<IContextualAction<Page>> definitions)
{
_endpoints = endpoints;
_definitions = definitions;
}
public IEnumerable<MenuItemToken> MenuItemsFor(Page page)
{
return _definitions.Select(x => BuildMenuItem(page, x));
}
public IEnumerable<MenuItemToken> MenuItemsFor(Page page, string category)
{
return _definitions.Where(x => x.Category == category).Select(x => BuildMenuItem(page, x));
}
public MenuItemToken BuildMenuItem(Page page, IContextualAction<Page> definition)
{
var endpoint = definition.FindEndpoint(_endpoints, page);
var menuItemState = determineAvailability(page, endpoint, definition);
return new MenuItemToken
{
Key = definition.Key,
MenuItemState = menuItemState,
Text = page.Name,
Url = endpoint.Url
};
}
private static MenuItemState determineAvailability(Page page, Endpoint endpoint, IContextualAction<Page> definition)
{
var authorized = endpoint.IsAuthorized ? MenuItemState.Available : definition.UnauthorizedState;
var available = definition.IsAvailable(page);
return MenuItemState.Least(authorized, available);
}
}
public class PageMenuDefinition : IContextualAction<Page>
{
public string Text()
{
return "Default PageMenu Definition";
}
public MenuItemState IsAvailable(Page target)
{
return MenuItemState.Available;
}
public Endpoint FindEndpoint(IEndpointService endpoints, Page target)
{
return endpoints.EndpointFor(new PageRequest
{
Slug = target.Slug
});
}
public string Key { get; set; }
public string Category { get; set; }
public MenuItemState UnauthorizedState
{
get { return MenuItemState.Hidden; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment