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");
}
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);
}
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);
}
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;
}
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);
}
Aggregations