Java Code Examples for com.vaadin.flow.dom.Element#appendChild()

The following examples show how to use com.vaadin.flow.dom.Element#appendChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectLocaleChangeObserverFromElement_elementHasVirtualChildren()
        throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    node.appendVirtualChild(new Element("main-virtual"),
            new Element("menu-virtual"));
    Element nested = new Element("nested-locale");
    nested.appendVirtualChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendVirtualChild(nested);
    Component.from(nested, Locale.class);

    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil
            .collectLocaleChangeObservers(node);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 2
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectLocaleChangeObserverFromElement() throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested-locale");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, Locale.class);

    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil
            .collectLocaleChangeObservers(node);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 3
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void inspectMixedChildrenHierarchy() throws Exception {
    Element node = new Element("root");
    node.appendVirtualChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendVirtualChild(new Element("nested-virtual-child"),
            new Element("nested-virtual-child-2"));
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    nested.getStateProvider().appendVirtualChild(nested.getNode(),
            new Element("attached-by-id"), NodeProperties.INJECT_BY_ID,
            "id");
    nested.getStateProvider().appendVirtualChild(nested.getNode(),
            new Element("attached-by-id"),
            NodeProperties.TEMPLATE_IN_TEMPLATE, "");

    node.appendChild(nested);

    List<Element> elements = new ArrayList<>();

    EventUtil.inspectHierarchy(node, elements, element -> true);

    Assert.assertEquals("Missing elements from list.", 10, elements.size());
}
 
Example 4
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void inspectChildrenHierarchy_selective() throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);

    List<Element> elements = new ArrayList<>();

    EventUtil.inspectHierarchy(node, elements,
            element -> !nested.equals(element));

    Assert.assertEquals("Missing elements from list.", 3, elements.size());
}
 
Example 5
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void inspectChildrenHierarchy() throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);

    List<Element> elements = new ArrayList<>();

    EventUtil.inspectHierarchy(node, elements, element -> true);

    Assert.assertEquals("Missing elements from list.", 6, elements.size());
}
 
Example 6
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectAfterNavigationObservers() {
    UI ui = UI.getCurrent();

    Element menu = new Element("menu");
    menu.appendChild(new AfterObserver().getElement());

    Element node = ui.getElement();
    node.appendChild(new Element("main"), menu);
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, AfterObserver.class);

    List<AfterNavigationObserver> beforeNavigationObservers = EventUtil
            .collectAfterNavigationObservers(ui);

    Assert.assertEquals("Wrong amount of listener instances found", 2,
            beforeNavigationObservers.size());
}
 
Example 7
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectBeforeNavigationObserversFromChains() throws Exception {
    Foo foo = new Foo();
    EnterObserver toBeDetached = new EnterObserver();
    foo.getElement().appendChild(new EnterObserver().getElement(),
            toBeDetached.getElement());
    Bar bar = new Bar();

    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new EnterObserver().getElement());

    bar.getElement().appendChild(new Foo().getElement(), nested);

    EnterObserver toBeAttached = new EnterObserver();

    Collection<? extends HasElement> oldChain = Arrays.asList(foo,
            toBeDetached);
    Collection<? extends HasElement> newChain = Arrays.asList(foo,
            toBeAttached);
    List<BeforeEnterObserver> beforeNavigationObservers = EventUtil
            .collectBeforeEnterObservers(oldChain, newChain);

    Assert.assertEquals("Wrong amount of listener instances found", 2,
            beforeNavigationObservers.size());
}
 
Example 8
Source File: RouterLinkView.java    From flow with Apache License 2.0 6 votes vote down vote up
public RouterLinkView() {
    Element bodyElement = getElement();
    bodyElement.getStyle().set("margin", "1em");

    Element location = ElementFactory.createDiv("no location")
            .setAttribute("id", "location");

    Element queryParams = ElementFactory.createDiv("no queryParams")
            .setAttribute("id", "queryParams");

    bodyElement.appendChild(location, new Element("p"));
    bodyElement.appendChild(queryParams, new Element("p"));

    addLinks();

    getPage().getHistory().setHistoryStateChangeHandler(e -> {
        location.setText(e.getLocation().getPath());
        queryParams.setText(
                e.getLocation().getQueryParameters().getQueryString());
        if (e.getState().isPresent())
            UI.getCurrent().getPage().getHistory().pushState(null,
                    ((JsonObject) e.getState().get()).getString("href"));
    });

    addImageLink();
}
 
Example 9
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectLocaleChangeObserverFromComponentList_elementHasVirtualChildren()
        throws Exception {
    Foo foo = new Foo();
    foo.getElement().appendChild(new Locale().getElement());
    Bar bar = new Bar();

    Element nested = new Element("nested-locale");
    nested.appendChild(new Element("nested-child"),
            new Locale().getElement());

    bar.getElement().appendChild(new Foo().getElement(), nested);

    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil
            .collectLocaleChangeObservers(Arrays.asList(foo, bar));

    Assert.assertEquals("Wrong amount of listener instances found", 2,
            beforeNavigationObservers.size());
}
 
Example 10
Source File: ComponentTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultGetChildrenMultiple() {
    // parent
    // * level1
    // ** child1
    // ** child2

    Element level1 = ElementFactory.createDiv("Level1");

    parentDivComponent.getElement().appendChild(level1);
    level1.appendChild(child1SpanComponent.getElement());
    level1.appendChild(child2InputComponent.getElement());

    assertChildren(parentDivComponent, child1SpanComponent,
            child2InputComponent);

}
 
Example 11
Source File: InternalServerError.java    From flow with Apache License 2.0 6 votes vote down vote up
private void checkLogBinding() {
    if (!hasLogBinding()) {
        Element logInfo = ElementFactory
                .createDiv("Your application doesn't have SLF4J binding. "
                        + "As a result the logger doesn't do any real logging. "
                        + "Add some binding as a dependency to your project. "
                        + "See details ");
        logInfo.getStyle().set("marginTop", "10px");
        logInfo.getStyle().set("marginBottom", "10px");
        logInfo.getStyle().set("fontWeight", "bold");
        logInfo.getStyle().set("color", "#6495ED");
        logInfo.appendChild(ElementFactory.createAnchor(
                "https://www.slf4j.org/manual.html#swapping", "here"));
        getElement().appendChild(logInfo);
    }
}
 
Example 12
Source File: JavaScriptBootstrapUI.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public void updateRoot(UI ui, HasElement oldRoot, HasElement newRoot) {
    JavaScriptBootstrapUI jsUI = castToJavaScriptUI(ui);
    Element wrapperElement = jsUI.wrapperElement;
    // server-side routing
    if (wrapperElement == null) {
        UIInternalUpdater.super.updateRoot(ui, oldRoot, newRoot);
        return;
    }

    // client-side routing
    Element rootElement = newRoot.getElement();
    if (newRoot instanceof ClientViewPlaceholder) {
        // only need to remove all children when newRoot is a
        // placeholder
        wrapperElement.removeAllChildren();
    } else if (!wrapperElement.equals(rootElement.getParent())) {
        if (oldRoot != null) {
            oldRoot.getElement().removeFromParent();
        }
        rootElement.removeFromParent();
        wrapperElement.appendChild(rootElement);
    }
}
 
Example 13
Source File: SelectView.java    From flow with Apache License 2.0 6 votes vote down vote up
public SelectView() {
    Div log = new Div();
    log.setId("log");

    Element select = new Element("select");
    for (int i = 1; i < 10; i++) {
        select.appendChild(
                new Element("option").setAttribute("id", "id" + i)
                        .setAttribute("value", "value" + i)
                        .setText("Visible text " + i));
    }
    select.setAttribute("id", "input");
    select.addEventListener("change", e -> {
        log.setText("Value is '"
                + e.getEventData().getString("element.value") + "'");
    }).synchronizeProperty("element.value");
    add(log);
    getElement().appendChild(select);
}
 
Example 14
Source File: ComponentTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultGetChildrenDirectlyDeepElementHierarchy() {
    // parent
    // * level1
    // ** level2
    // *** child1
    // * child2
    // * level1b
    // ** child3

    TestComponent parent = new TestComponent(ElementFactory.createDiv());
    TestComponent child1 = new TestComponent(
            ElementFactory.createDiv("Child1"));
    TestComponent child2 = new TestComponent(
            ElementFactory.createDiv("Child2"));
    TestComponent child3 = new TestComponent(
            ElementFactory.createDiv("Child2"));

    Element parentElement = parent.getElement();
    parentElement.appendChild(
            new Element("level1").appendChild(
                    new Element("level2").appendChild(child1.getElement())),
            child2.getElement(),
            new Element("level1b").appendChild(child3.getElement()));

    List<Component> children = parent.getChildren()
            .collect(Collectors.toList());
    Assert.assertArrayEquals(new Component[] { child1, child2, child3 },
            children.toArray());

}
 
Example 15
Source File: MenuTemplate.java    From radman with MIT License 5 votes vote down vote up
private void addCategoryName(String name) {
    Element li = ElementFactory.createListItem();
    Element span = new Span(name).getElement();
    span.getClassList().add("category");
    li.appendChild(span);
    linksContainer.appendChild(li);
}
 
Example 16
Source File: ComponentTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void componentFromHierarchy() {
    Element div = new Element("div");
    Element button = new Element("button");
    div.appendChild(button);

    TestDiv testDiv = Component.from(div, TestDiv.class);
    TestButton testButton = Component.from(button, TestButton.class);
    Assert.assertEquals(testButton.getParent().get(), testDiv);
    Assert.assertTrue(testDiv.getChildren().anyMatch(c -> c == testButton));
}
 
Example 17
Source File: EventHandlerView.java    From flow with Apache License 2.0 5 votes vote down vote up
@EventHandler
private void sendData(@EventData("event.button") int button,
        @EventData("event.type") String type,
        @EventData("event.srcElement.tagName") String tag) {
    Element container = ElementFactory.createDiv();
    container.appendChild(ElementFactory
            .createDiv("Received event from the client with the data:"));
    container.appendChild(ElementFactory.createDiv("button: " + button));
    container.appendChild(ElementFactory.createDiv("type: " + type));
    container.appendChild(ElementFactory
            .createDiv("tag: " + tag.toLowerCase(Locale.ENGLISH)));
    container.setAttribute("id", "event-data");
    getParent().get().getElement().appendChild(container);
}
 
Example 18
Source File: FragmentLinkView.java    From flow with Apache License 2.0 5 votes vote down vote up
public FragmentLinkView() {
    Element bodyElement = getElement();
    bodyElement.getStyle().set("margin", "1em");

    Element scrollLocator = ElementFactory.createDiv()
            .setAttribute("id", "scrollLocator").setText("Scroll locator");
    scrollLocator.getStyle().set("position", "fixed").set("top", "0")
            .set("right", "0");

    Element placeholder = ElementFactory.createDiv("Hash Change Events")
            .setAttribute("id", "placeholder");

    bodyElement.appendChild(scrollLocator, placeholder, new Element("p"));

    Element scrollToLink = ElementFactory.createRouterLink(
            "/view/com.vaadin.flow.uitest.ui.FragmentLinkView#Scroll_Target",
            "Scroller link");
    Element scrollToLink2 = ElementFactory.createRouterLink(
            "/view/com.vaadin.flow.uitest.ui.FragmentLinkView#Scroll_Target2",
            "Scroller link 2");
    Element scrollToLinkAnotherView = ElementFactory.createRouterLink(
            "/view/com.vaadin.flow.uitest.ui.FragmentLinkView2#Scroll_Target",
            "Scroller link with different view");
    Element linkThatIsOverridden = ElementFactory.createRouterLink(
            "./override#Scroll_Target", "Link that server overrides");

    Element scrollTarget = ElementFactory.createHeading1("Scroll Target")
            .setAttribute("id", "Scroll_Target");
    Element scrollTarget2 = ElementFactory.createHeading2("Scroll Target 2")
            .setAttribute("id", "Scroll_Target2");

    bodyElement.appendChild(scrollToLink, new Element("p"), scrollToLink2,
            new Element("p"), scrollToLinkAnotherView, new Element("p"),
            linkThatIsOverridden, new Element("p"), createSpacer(),
            scrollTarget, createSpacer(), scrollTarget2, createSpacer());

}
 
Example 19
Source File: UIInternalUpdater.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Update root element of the given UI.
 * 
 * @param ui
 *            the UI to be updated
 * @param oldRoot
 *            the old root to be removed
 * @param newRoot
 *            the new root to be added
 */
default void updateRoot(UI ui, HasElement oldRoot, HasElement newRoot) {
    Element uiElement = ui.getElement();
    Element rootElement = newRoot.getElement();

    if (!uiElement.equals(rootElement.getParent())) {
        if (oldRoot != null) {
            oldRoot.getElement().removeFromParent();
        }
        rootElement.removeFromParent();
        uiElement.appendChild(rootElement);
    }
}
 
Example 20
Source File: ElementInitOrderView.java    From flow with Apache License 2.0 5 votes vote down vote up
private static Element createElement(String tag) {
    Element element = new Element(tag);
    element.appendChild(new Element("span"));
    element.getStyle().set("animationName", "style");
    element.getClassList().add("class");
    element.setAttribute("attribute", "attribute");
    element.setProperty("property", "property");
    return element;
}