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