Search in sources :

Example 1 with Component

use of com.enonic.xp.region.Component in project xp by enonic.

the class GetCurrentComponentScriptTest method currentComponent.

@Test
public void currentComponent() {
    final Component component = TestDataFixtures.newLayoutComponent();
    this.portalRequest.setComponent(component);
    runFunction("/test/getCurrentComponent-test.js", "currentComponent");
}
Also used : Component(com.enonic.xp.region.Component) Test(org.junit.jupiter.api.Test)

Example 2 with Component

use of com.enonic.xp.region.Component in project xp by enonic.

the class ComponentInstructionTest method testInstructionRenderByName.

@Test
public void testInstructionRenderByName() throws Exception {
    RendererDelegate rendererFactory = newRendererFactory("<b>part content</b>");
    ComponentService componentService = Mockito.mock(ComponentService.class);
    Component component = createPartComponent();
    doReturn(component).when(componentService).getByKey(isA(DescriptorKey.class));
    ComponentInstruction instruction = new ComponentInstruction();
    instruction.setRendererDelegate(rendererFactory);
    instruction.setComponentService(componentService);
    PortalRequest portalRequest = new PortalRequest();
    Content content = createPage("content-id", "content-name", "myapplication:content-type");
    portalRequest.setContent(content);
    Site site = createSite("site-id", "site-name", "myapplication:content-type");
    portalRequest.setSite(site);
    PageTemplate pageTemplate = createPageTemplate();
    portalRequest.setPageTemplate(pageTemplate);
    String outputHtml = instruction.evaluate(portalRequest, "COMPONENT module:myPartComponent").getAsString();
    assertEquals("<b>part content</b>", outputHtml);
}
Also used : Site(com.enonic.xp.site.Site) PageTemplate(com.enonic.xp.page.PageTemplate) Content(com.enonic.xp.content.Content) DescriptorKey(com.enonic.xp.page.DescriptorKey) ComponentService(com.enonic.xp.region.ComponentService) Component(com.enonic.xp.region.Component) PartComponent(com.enonic.xp.region.PartComponent) RendererDelegate(com.enonic.xp.portal.impl.rendering.RendererDelegate) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 3 with Component

use of com.enonic.xp.region.Component in project xp by enonic.

the class ComponentHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final ContentResolverResult resolvedContent = contentResolver.resolve(this.request);
    final Content content = resolvedContent.getContentOrElseThrow();
    final Site site = resolvedContent.getNearestSiteOrElseThrow();
    Page page = content.getPage();
    final PageResolverResult resolvedPage = pageResolver.resolve(request.getMode(), content, site);
    Component component = null;
    if (content.getType().isFragment()) {
        // fragment content, try resolving component path in Layout fragment
        final Component fragmentComponent = page.getFragment();
        if (this.componentPath.isEmpty()) {
            component = fragmentComponent;
        } else if (fragmentComponent instanceof LayoutComponent) {
            component = ((LayoutComponent) fragmentComponent).getComponent(this.componentPath);
        }
    }
    final Page effectivePage;
    if (component == null) {
        effectivePage = inlineFragments(resolvedPage.getEffectivePage(), this.componentPath);
        component = effectivePage.getRegions().getComponent(this.componentPath);
    } else {
        effectivePage = resolvedPage.getEffectivePage();
    }
    if (component == null) {
        throw WebException.notFound(String.format("Page component for [%s] not found", this.componentPath));
    }
    final Content effectiveContent = Content.create(content).page(effectivePage).build();
    final DescriptorKey controller = resolvedPage.getController();
    this.request.setSite(site);
    this.request.setContent(effectiveContent);
    this.request.setComponent(component);
    this.request.setApplicationKey(controller != null ? controller.getApplicationKey() : null);
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("componentPath", component.getPath());
        trace.put("type", component.getType().toString());
    }
    final PortalResponse response = rendererDelegate.render(component, this.request);
    return this.postProcessor.processResponseInstructions(this.request, response);
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) DescriptorKey(com.enonic.xp.page.DescriptorKey) Page(com.enonic.xp.page.Page) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component) LayoutComponent(com.enonic.xp.region.LayoutComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent)

Example 4 with Component

use of com.enonic.xp.region.Component in project xp by enonic.

the class ComponentInstruction method resolveComponent.

private Component resolveComponent(final PortalRequest portalRequest, final ComponentPath path) {
    final Content content = portalRequest.getContent();
    if (content == null) {
        return null;
    }
    final Page page = content.getPage();
    if (content.getType().isFragment()) {
        return resolveComponentInFragment(page, path);
    }
    final PageRegions pageRegions = page.getRegions();
    Component component = pageRegions.getComponent(path);
    if (component == null) {
        throw new RenderException("Component not found: [{0}]", path);
    }
    return component;
}
Also used : PageRegions(com.enonic.xp.page.PageRegions) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) Content(com.enonic.xp.content.Content) Page(com.enonic.xp.page.Page) LayoutComponent(com.enonic.xp.region.LayoutComponent) Component(com.enonic.xp.region.Component)

Example 5 with Component

use of com.enonic.xp.region.Component in project xp by enonic.

the class ComponentServiceTest method testGetPartByName.

@Test
public void testGetPartByName() throws Exception {
    final Component part1App1 = this.service.getByName(ApplicationKey.from("myapp1"), ComponentName.from("mypart"));
    final Component part1App2 = this.service.getByName(ApplicationKey.from("myapp2"), ComponentName.from("mypart"));
    assertNotNull(part1App1);
    assertNotNull(part1App2);
    assertTrue(part1App1 instanceof PartComponent);
    assertTrue(part1App2 instanceof PartComponent);
}
Also used : PartComponent(com.enonic.xp.region.PartComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent) PartComponent(com.enonic.xp.region.PartComponent) Component(com.enonic.xp.region.Component) Test(org.junit.jupiter.api.Test) AbstractDescriptorServiceTest(com.enonic.xp.core.impl.content.page.AbstractDescriptorServiceTest)

Aggregations

Component (com.enonic.xp.region.Component)25 LayoutComponent (com.enonic.xp.region.LayoutComponent)16 Test (org.junit.jupiter.api.Test)12 PartComponent (com.enonic.xp.region.PartComponent)11 AbstractDescriptorServiceTest (com.enonic.xp.core.impl.content.page.AbstractDescriptorServiceTest)8 Content (com.enonic.xp.content.Content)6 Page (com.enonic.xp.page.Page)5 Region (com.enonic.xp.region.Region)4 DescriptorKey (com.enonic.xp.page.DescriptorKey)3 PortalResponse (com.enonic.xp.portal.PortalResponse)3 ComponentPath (com.enonic.xp.region.ComponentPath)3 FragmentComponent (com.enonic.xp.region.FragmentComponent)3 Site (com.enonic.xp.site.Site)3 PageTemplate (com.enonic.xp.page.PageTemplate)2 PortalRequest (com.enonic.xp.portal.PortalRequest)2 RenderMode (com.enonic.xp.portal.RenderMode)2 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)2 RendererDelegate (com.enonic.xp.portal.impl.rendering.RendererDelegate)2 ComponentService (com.enonic.xp.region.ComponentService)2 DescriptorBasedComponent (com.enonic.xp.region.DescriptorBasedComponent)2