Search in sources :

Example 11 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class DependencyListTest method relativeUrlBecomesFrontend.

@Test
public void relativeUrlBecomesFrontend() {
    addSimpleDependency("foo.js");
    Dependency dependency = deps.getPendingSendToClient().iterator().next();
    assertEquals("frontend://foo.js", dependency.getUrl());
}
Also used : Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 12 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class DependencyListTest method testAddingDuplicateDependencies.

private void testAddingDuplicateDependencies(LoadMode first, LoadMode second, LoadMode expected) {
    String url = "foo/bar.js";
    Type type = Type.JAVASCRIPT;
    // need to clear so that there is no leftovers
    deps = new DependencyList();
    deps.add(new Dependency(type, url, first));
    deps.add(new Dependency(type, url, second));
    Collection<Dependency> pendingSendToClient = deps.getPendingSendToClient();
    assertEquals("Expected to have only one dependency", 1, pendingSendToClient.size());
    assertEquals("Wrong load mode resolved", pendingSendToClient.iterator().next().getLoadMode(), expected);
}
Also used : DependencyList(com.vaadin.flow.component.internal.DependencyList) Type(com.vaadin.flow.shared.ui.Dependency.Type) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 13 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class DependencyListTest method ensureDependenciesSentToClientHaveTheSameOrderAsAdded.

@Test
public void ensureDependenciesSentToClientHaveTheSameOrderAsAdded() {
    Dependency eagerHtml = new Dependency(Type.HTML_IMPORT, "eager.html", LoadMode.EAGER);
    Dependency eagerJs = new Dependency(Type.JAVASCRIPT, "eager.js", LoadMode.EAGER);
    Dependency eagerCss = new Dependency(Type.STYLESHEET, "eager.css", LoadMode.EAGER);
    Dependency lazyHtml = new Dependency(Type.HTML_IMPORT, "lazy.html", LoadMode.LAZY);
    Dependency lazyJs = new Dependency(Type.JAVASCRIPT, "lazy.js", LoadMode.LAZY);
    Dependency lazyCss = new Dependency(Type.STYLESHEET, "lazy.css", LoadMode.LAZY);
    assertEquals("Expected the dependency to be eager", LoadMode.EAGER, eagerHtml.getLoadMode());
    assertEquals("Expected the dependency to be eager", LoadMode.EAGER, eagerJs.getLoadMode());
    assertEquals("Expected the dependency to be eager", LoadMode.EAGER, eagerCss.getLoadMode());
    assertEquals("Expected the dependency to be lazy", LoadMode.LAZY, lazyHtml.getLoadMode());
    assertEquals("Expected the dependency to be lazy", LoadMode.LAZY, lazyJs.getLoadMode());
    assertEquals("Expected the dependency to be lazy", LoadMode.LAZY, lazyCss.getLoadMode());
    List<Dependency> dependencies = new ArrayList<>(Arrays.asList(eagerHtml, eagerJs, eagerCss, lazyHtml, lazyJs, lazyCss));
    assertEquals("Expected to have 6 dependencies", 6, dependencies.size());
    Collections.shuffle(dependencies);
    dependencies.forEach(deps::add);
    List<Dependency> pendingSendToClient = new ArrayList<>(deps.getPendingSendToClient());
    for (int i = 0; i < pendingSendToClient.size(); i++) {
        Dependency actualDependency = pendingSendToClient.get(i);
        Dependency expectedDependency = dependencies.get(i);
        assertEquals("Expected to have the same dependency on the same position for list, but urls do not match", expectedDependency.getUrl(), actualDependency.getUrl());
        assertEquals("Expected to have the same dependency on the same position for list, but load modes do not match", expectedDependency.getLoadMode(), actualDependency.getLoadMode());
    }
}
Also used : ArrayList(java.util.ArrayList) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 14 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class BootstrapHandler method handleInitialPageSettings.

private static void handleInitialPageSettings(BootstrapContext context, Element head, InitialPageSettings initialPageSettings) {
    if (initialPageSettings.getViewport() != null) {
        Elements viewport = head.getElementsByAttributeValue("name", VIEWPORT);
        if (!viewport.isEmpty() && viewport.size() == 1) {
            viewport.get(0).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
        } else {
            head.appendElement(META_TAG).attr("name", VIEWPORT).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
        }
    }
    initialPageSettings.getInline(InitialPageSettings.Position.PREPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::prependChild));
    initialPageSettings.getInline(InitialPageSettings.Position.APPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::appendChild));
    initialPageSettings.getElement(InitialPageSettings.Position.PREPEND).forEach(element -> insertElements(element, head::prependChild));
    initialPageSettings.getElement(InitialPageSettings.Position.APPEND).forEach(element -> insertElements(element, head::appendChild));
}
Also used : Inline(com.vaadin.flow.component.page.Inline) TargetElement(com.vaadin.flow.component.page.TargetElement) DocumentType(org.jsoup.nodes.DocumentType) LoggerFactory(org.slf4j.LoggerFactory) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Parser(org.jsoup.parser.Parser) JsonValue(elemental.json.JsonValue) WebComponents(com.vaadin.flow.component.WebComponents) PushMode(com.vaadin.flow.shared.communication.PushMode) JsonUtil(elemental.json.impl.JsonUtil) Locale(java.util.Locale) Map(java.util.Map) Element(org.jsoup.nodes.Element) UI(com.vaadin.flow.component.UI) EnumMap(java.util.EnumMap) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) PushConfiguration(com.vaadin.flow.component.PushConfiguration) Tag(org.jsoup.parser.Tag) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Stream(java.util.stream.Stream) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Jsoup(org.jsoup.Jsoup) DataNode(org.jsoup.nodes.DataNode) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) Properties(java.util.Properties) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BufferedWriter(java.io.BufferedWriter) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) AtmospherePushConnection(com.vaadin.flow.server.communication.AtmospherePushConnection) JsonObject(elemental.json.JsonObject) BufferedReader(java.io.BufferedReader) UidlWriter(com.vaadin.flow.server.communication.UidlWriter) Collections(java.util.Collections) InputStream(java.io.InputStream) Push(com.vaadin.flow.component.page.Push) Elements(org.jsoup.select.Elements)

Example 15 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class DefaultTemplateParser method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag) {
    VaadinServlet servlet = VaadinServlet.getCurrent();
    boolean logEnabled = LOG_CACHE.get(clazz).compareAndSet(false, true);
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, HtmlImport.class).stream().map(htmlImport -> new Dependency(Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode())).collect(Collectors.toList());
    FilterContext filterContext = new FilterContext(VaadinSession.getCurrent());
    for (DependencyFilter filter : VaadinService.getCurrent().getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), filterContext);
    }
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Type.HTML_IMPORT) {
            continue;
        }
        String url = dependency.getUrl();
        String path = servlet.resolveResource(url);
        if (logEnabled) {
            getLogger().debug("Html import path '{}' is resolved to '{}'", url, path);
        }
        try (InputStream content = servlet.getResourceAsStream(path)) {
            if (content == null) {
                throw new IllegalStateException(String.format("Can't find resource '%s' " + "via the servlet context", url));
            }
            Element templateElement = parseHtmlImport(content, url, tag);
            if (logEnabled && templateElement != null) {
                getLogger().debug("Found a template file containing template " + "definition for the tag '{}' by the path '{}'", tag, url);
            }
            if (templateElement != null) {
                return new TemplateData(url, templateElement);
            }
        } catch (IOException exception) {
            // ignore exception on close()
            if (logEnabled) {
                getLogger().warn("Couldn't close template input stream", exception);
            }
        }
    }
    throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using @'%s' annotations. " + "Check the availability of the template files in your WAR " + "file or provide alternative implementation of the " + "method getTemplateContent() which should return an element " + "representing the content of the template file", tag, HtmlImport.class.getSimpleName()));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Comment(org.jsoup.nodes.Comment) Dependency(com.vaadin.flow.shared.ui.Dependency) ArrayList(java.util.ArrayList) Type(com.vaadin.flow.shared.ui.Dependency.Type) Element(org.jsoup.nodes.Element) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext) DependencyFilter(com.vaadin.flow.server.DependencyFilter) ReflectionCache(com.vaadin.flow.internal.ReflectionCache) VaadinSession(com.vaadin.flow.server.VaadinSession) Logger(org.slf4j.Logger) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) VaadinServlet(com.vaadin.flow.server.VaadinServlet) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) List(java.util.List) Document(org.jsoup.nodes.Document) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Jsoup(org.jsoup.Jsoup) InputStream(java.io.InputStream) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) InputStream(java.io.InputStream) Element(org.jsoup.nodes.Element) VaadinServlet(com.vaadin.flow.server.VaadinServlet) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) IOException(java.io.IOException) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext)

Aggregations

Dependency (com.vaadin.flow.shared.ui.Dependency)30 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)8 LoadMode (com.vaadin.flow.shared.ui.LoadMode)7 List (java.util.List)7 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)6 JsonObject (elemental.json.JsonObject)6 DependencyFilter (com.vaadin.flow.server.DependencyFilter)5 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Collections (java.util.Collections)5 Element (org.jsoup.nodes.Element)5 Tag (com.vaadin.flow.component.Tag)4 UI (com.vaadin.flow.component.UI)4 VaadinService (com.vaadin.flow.server.VaadinService)3 VaadinServlet (com.vaadin.flow.server.VaadinServlet)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 Type (com.vaadin.flow.shared.ui.Dependency.Type)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3