Search in sources :

Example 11 with LayoutComponent

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

the class FragmentPageResolver method replaceComponentInPage.

private PageRegions replaceComponentInPage(final PageRegions pageRegions, final ImmutableList<ComponentPath.RegionAndComponent> pathItems, final Component component) {
    final ComponentPath.RegionAndComponent regionCmp = pathItems.isEmpty() ? null : pathItems.get(0);
    final Region region = regionCmp == null ? null : pageRegions.getRegion(regionCmp.getRegionName());
    if (region == null) {
        return pageRegions;
    }
    final int componentIndex = regionCmp.getComponentIndex();
    final Component existingCmp = region.getComponent(componentIndex);
    if (existingCmp == null) {
        return pageRegions;
    }
    final int numberOfLevels = pathItems.size();
    if (numberOfLevels == 1) {
        final Region updatedRegion = Region.create(region).set(componentIndex, component).build();
        return replaceRegionInPage(pageRegions, region, updatedRegion);
    } else {
        if (!(existingCmp instanceof LayoutComponent)) {
            return pageRegions;
        }
        final LayoutComponent layoutComponent = (LayoutComponent) existingCmp;
        final LayoutRegions layoutRegions = replaceComponentInLayout(layoutComponent.getRegions(), removeFirstLevel(pathItems), component);
        final LayoutComponent updatedLayout = LayoutComponent.create(layoutComponent).regions(layoutRegions).build();
        final Region updatedRegion = Region.create(region).set(componentIndex, updatedLayout).build();
        return replaceRegionInPage(pageRegions, region, updatedRegion);
    }
}
Also used : ComponentPath(com.enonic.xp.region.ComponentPath) Region(com.enonic.xp.region.Region) LayoutComponent(com.enonic.xp.region.LayoutComponent) Component(com.enonic.xp.region.Component) LayoutComponent(com.enonic.xp.region.LayoutComponent) LayoutRegions(com.enonic.xp.region.LayoutRegions)

Example 12 with LayoutComponent

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

the class HtmlAreaContentProcessorTest method inner_component_data.

@Test
public void inner_component_data() throws IOException {
    final PropertyTree data1 = new PropertyTree();
    data1.addProperty("htmlData", ValueFactory.newString("<img data-src=\"image://image-id1\"/>"));
    final PropertyTree data2 = new PropertyTree();
    data2.addProperty("htmlData", ValueFactory.newString("<img data-src=\"image://image-id2\"/>"));
    final Form form = Form.create().addFormItem(Input.create().name("htmlData").label("htmlData").inputType(InputTypeName.HTML_AREA).build()).build();
    final PartDescriptor partDescriptor = PartDescriptor.create().key(DescriptorKey.from("app:part")).config(form).build();
    Mockito.when(partDescriptorService.getByKey(partDescriptor.getKey())).thenReturn(partDescriptor);
    final PartComponent partComponent = PartComponent.create().descriptor("myapp:partest").descriptor(partDescriptor.getKey()).config(data2).build();
    final LayoutDescriptor layoutDescriptor = LayoutDescriptor.create().key(DescriptorKey.from("app:layout")).config(form).regions(RegionDescriptors.create().add(RegionDescriptor.create().name("part").build()).build()).build();
    Mockito.when(layoutDescriptorService.getByKey(layoutDescriptor.getKey())).thenReturn(layoutDescriptor);
    final LayoutComponent layoutComponent = LayoutComponent.create().descriptor("myapp:layout").descriptor(layoutDescriptor.getKey()).regions(LayoutRegions.create().add(Region.create().name("part").add(partComponent).build()).build()).config(data1).build();
    final PageDescriptor pageDescriptor = PageDescriptor.create().regions(RegionDescriptors.create().add(RegionDescriptor.create().name("region").build()).build()).key(DescriptorKey.from("app:page")).config(Form.create().build()).build();
    Mockito.when(pageDescriptorService.getByKey(pageDescriptor.getKey())).thenReturn(pageDescriptor);
    final Page page = Page.create().config(new PropertyTree()).descriptor(pageDescriptor.getKey()).regions(PageRegions.create().add(Region.create().name("region").add(layoutComponent).build()).build()).build();
    final EditableContent editableContent = new EditableContent(Media.create().name("myContentName").type(contentTypeName).page(page).parentPath(ContentPath.ROOT).data(new PropertyTree()).build());
    result.getEditor().edit(editableContent);
    assertEquals(2, editableContent.processedReferences.build().getSize());
    assertTrue(editableContent.processedReferences.build().contains(ContentId.from("image-id1")));
    assertTrue(editableContent.processedReferences.build().contains(ContentId.from("image-id2")));
}
Also used : PageDescriptor(com.enonic.xp.page.PageDescriptor) LayoutDescriptor(com.enonic.xp.region.LayoutDescriptor) Form(com.enonic.xp.form.Form) PartDescriptor(com.enonic.xp.region.PartDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) EditableContent(com.enonic.xp.content.EditableContent) Page(com.enonic.xp.page.Page) PartComponent(com.enonic.xp.region.PartComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent) Test(org.junit.jupiter.api.Test)

Aggregations

LayoutComponent (com.enonic.xp.region.LayoutComponent)12 Component (com.enonic.xp.region.Component)6 PartComponent (com.enonic.xp.region.PartComponent)6 LayoutDescriptor (com.enonic.xp.region.LayoutDescriptor)4 Page (com.enonic.xp.page.Page)3 LayoutRegions (com.enonic.xp.region.LayoutRegions)3 PartDescriptor (com.enonic.xp.region.PartDescriptor)3 Test (org.junit.jupiter.api.Test)3 AbstractDescriptorServiceTest (com.enonic.xp.core.impl.content.page.AbstractDescriptorServiceTest)2 PropertyTree (com.enonic.xp.data.PropertyTree)2 Form (com.enonic.xp.form.Form)2 DescriptorKey (com.enonic.xp.page.DescriptorKey)2 PageDescriptor (com.enonic.xp.page.PageDescriptor)2 Region (com.enonic.xp.region.Region)2 Content (com.enonic.xp.content.Content)1 EditableContent (com.enonic.xp.content.EditableContent)1 IndexConfigVisitor (com.enonic.xp.core.impl.content.index.IndexConfigVisitor)1 PropertySet (com.enonic.xp.data.PropertySet)1 FormDefaultValuesProcessor (com.enonic.xp.form.FormDefaultValuesProcessor)1 PageDescriptorService (com.enonic.xp.page.PageDescriptorService)1