Search in sources :

Example 1 with NavItem

use of org.craftercms.engine.navigation.NavItem in project engine by craftercms.

the class NavBreadcrumbBuilderImpl method getBreadcrumb.

@Override
public List<NavItem> getBreadcrumb(String url, String root, Converter<SiteItem, NavItem> itemConverter) {
    if (itemConverter == null) {
        itemConverter = defaultItemConverter;
    }
    List<NavItem> breadcrumb = new ArrayList<>();
    String breadcrumbUrl = extractBreadcrumbUrl(url, root);
    String[] breadcrumbUrlComponents = breadcrumbUrl.split("/");
    String currentUrl = root;
    for (String breadcrumbUrlComponent : breadcrumbUrlComponents) {
        currentUrl = UrlUtils.concat(currentUrl, breadcrumbUrlComponent);
        SiteItem siteItem = siteItemService.getSiteItem(currentUrl, processor);
        if (siteItem != null) {
            NavItem navItem = itemConverter.convert(siteItem);
            if (navItem != null) {
                navItem.setActive(url.equals(siteItem.getItem().getDescriptorUrl()));
                breadcrumb.add(navItem);
            }
        }
    }
    return breadcrumb;
}
Also used : ArrayList(java.util.ArrayList) NavItem(org.craftercms.engine.navigation.NavItem) SiteItem(org.craftercms.engine.model.SiteItem)

Example 2 with NavItem

use of org.craftercms.engine.navigation.NavItem in project engine by craftercms.

the class DefaultItemConverter method convert.

@Override
public NavItem convert(SiteItem siteItem) {
    NavItem navItem = null;
    if (siteItem.getDom() != null) {
        navItem = new NavItem();
        navItem.setLabel(getNavigationLabel(siteItem));
        navItem.setUrl(getNavigationUrl(siteItem));
        navItem.setAttributes(getAdditionalAttributes(siteItem));
    }
    return navItem;
}
Also used : NavItem(org.craftercms.engine.navigation.NavItem)

Example 3 with NavItem

use of org.craftercms.engine.navigation.NavItem in project engine by craftercms.

the class NavTreeBuilderImplTest method getItemConverter.

private Converter<SiteItem, NavItem> getItemConverter() {
    Converter<SiteItem, NavItem> converter = mock(Converter.class);
    doAnswer((Answer<NavItem>) invocation -> {
        SiteItem siteItem = (SiteItem) invocation.getArguments()[0];
        NavItem navItem = new NavItem();
        navItem.setLabel(siteItem.getStoreName());
        navItem.setUrl(siteItem.getStoreUrl());
        return navItem;
    }).when(converter).convert(any(SiteItem.class));
    return converter;
}
Also used : ItemFilter(org.craftercms.core.service.ItemFilter) Answer(org.mockito.stubbing.Answer) Mockito(org.mockito.Mockito) Arrays(java.util.Arrays) Converter(org.craftercms.commons.converters.Converter) SiteItem(org.craftercms.engine.model.SiteItem) SiteItemService(org.craftercms.engine.service.SiteItemService) Test(org.junit.Test) NavItem(org.craftercms.engine.navigation.NavItem) Assert(org.junit.Assert) FilenameUtils(org.apache.commons.io.FilenameUtils) Before(org.junit.Before) NavItem(org.craftercms.engine.navigation.NavItem) SiteItem(org.craftercms.engine.model.SiteItem)

Example 4 with NavItem

use of org.craftercms.engine.navigation.NavItem in project engine by craftercms.

the class NavTreeBuilderImplTest method testGetNavTree.

@Test
public void testGetNavTree() {
    NavItem navItem = navTreeBuilder.getNavTree(ROOT_URL, -1, URL3);
    assertNotNull(navItem);
    assertEquals(ROOT_URL, navItem.getUrl());
    assertTrue(navItem.isActive());
    assertEquals(2, navItem.getSubItems().size());
    assertEquals(URL1, navItem.getSubItems().get(0).getUrl());
    assertFalse(navItem.getSubItems().get(0).isActive());
    assertEquals(URL2, navItem.getSubItems().get(1).getUrl());
    assertTrue(navItem.getSubItems().get(1).isActive());
    assertEquals(1, navItem.getSubItems().get(1).getSubItems().size());
    assertEquals(URL3, navItem.getSubItems().get(1).getSubItems().get(0).getUrl());
    assertTrue(navItem.getSubItems().get(1).getSubItems().get(0).isActive());
}
Also used : NavItem(org.craftercms.engine.navigation.NavItem) Test(org.junit.Test)

Example 5 with NavItem

use of org.craftercms.engine.navigation.NavItem in project engine by craftercms.

the class NavTreeBuilderImpl method getNavItem.

protected NavItem getNavItem(SiteItem siteItem, String currentPageUrl, Converter<SiteItem, NavItem> itemConverter) {
    NavItem navItem = itemConverter.convert(siteItem);
    if (navItem != null) {
        navItem.setSubItems(getNavSubItems(siteItem, currentPageUrl, itemConverter));
        navItem.setActive(isActive(currentPageUrl, siteItem.getStoreUrl()));
        return navItem;
    } else {
        return null;
    }
}
Also used : NavItem(org.craftercms.engine.navigation.NavItem)

Aggregations

NavItem (org.craftercms.engine.navigation.NavItem)5 SiteItem (org.craftercms.engine.model.SiteItem)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 Converter (org.craftercms.commons.converters.Converter)1 ItemFilter (org.craftercms.core.service.ItemFilter)1 SiteItemService (org.craftercms.engine.service.SiteItemService)1 Assert (org.junit.Assert)1 Before (org.junit.Before)1 Mockito (org.mockito.Mockito)1 Answer (org.mockito.stubbing.Answer)1