Search in sources :

Example 1 with WebComponentConfigurationRegistry

use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.

the class WebComponentConfigurationRegistryInitializer method initialize.

@Override
@SuppressWarnings("rawtypes")
public void initialize(Set<Class<?>> classSet, VaadinContext context) throws VaadinInitializerException {
    WebComponentConfigurationRegistry instance = WebComponentConfigurationRegistry.getInstance(context);
    if (classSet == null || classSet.isEmpty()) {
        instance.setConfigurations(Collections.emptySet());
        return;
    }
    try {
        Set<WebComponentExporterFactory> factories = WebComponentExporterUtils.getFactories(classSet);
        Set<WebComponentConfiguration<? extends Component>> configurations = constructConfigurations(factories);
        validateTagNames(configurations);
        validateDistinctTagNames(configurations);
        instance.setConfigurations(configurations);
    } catch (Exception e) {
        throw new VaadinInitializerException(String.format("%s failed to collect %s implementations!", WebComponentConfigurationRegistryInitializer.class.getSimpleName(), WebComponentExporter.class.getSimpleName()), e);
    }
}
Also used : WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) Component(com.vaadin.flow.component.Component) WebComponentExporter(com.vaadin.flow.component.WebComponentExporter) InvalidCustomElementNameException(com.vaadin.flow.server.InvalidCustomElementNameException) WebComponentExporterFactory(com.vaadin.flow.component.WebComponentExporterFactory)

Example 2 with WebComponentConfigurationRegistry

use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.

the class WebComponentProviderTest method setExporters_exportersHasOnePush_pushIsSet.

@Test
public void setExporters_exportersHasOnePush_pushIsSet() {
    WebComponentConfigurationRegistry registry = setupConfigurations(ThemedComponentExporter.class, MyComponentExporter.class);
    Assert.assertTrue(registry.getEmbeddedApplicationAnnotation(Push.class).isPresent());
}
Also used : WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) Test(org.junit.Test)

Example 3 with WebComponentConfigurationRegistry

use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.

the class WebComponentBootstrapHandler method createAndInitUI.

@Override
protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass, VaadinRequest request, VaadinResponse response, VaadinSession session) {
    if (!canHandleRequest(request)) {
        throw new IllegalStateException("Unexpected request URL '" + getRequestUrl(request) + "' in the bootstrap handler for web " + "component UI which should handle path " + PATH_PATTERN.toString());
    }
    final String serviceUrl = getServiceUrl(request, response);
    BootstrapContext context = super.createAndInitUI(WebComponentUI.class, request, response, session);
    JsonObject config = context.getApplicationParameters();
    String pushURL = context.getSession().getConfiguration().getPushURL();
    if (pushURL == null) {
        pushURL = serviceUrl;
    } else {
        try {
            URI uri = new URI(serviceUrl);
            pushURL = uri.resolve(new URI(pushURL)).toASCIIString();
        } catch (URISyntaxException exception) {
            throw new IllegalStateException(String.format("Can't resolve pushURL '%s' based on the service URL '%s'", pushURL, serviceUrl), exception);
        }
    }
    PushConfiguration pushConfiguration = context.getUI().getPushConfiguration();
    pushConfiguration.setPushUrl(pushURL);
    assert serviceUrl.endsWith("/");
    config.put(ApplicationConstants.SERVICE_URL, serviceUrl);
    config.put(ApplicationConstants.APP_WC_MODE, true);
    WebComponentConfigurationRegistry registry = WebComponentConfigurationRegistry.getInstance(request.getService().getContext());
    JsonArray tags = registry.getConfigurations().stream().map(conf -> Json.create(conf.getTag())).collect(JsonUtils.asArray());
    config.put("webcomponents", tags);
    config.put(ApplicationConstants.DEVMODE_GIZMO_ENABLED, false);
    return context;
}
Also used : JsonArray(elemental.json.JsonArray) PwaRegistry(com.vaadin.flow.server.PwaRegistry) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) URISyntaxException(java.net.URISyntaxException) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8(com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8) WebComponentUI(com.vaadin.flow.component.webcomponent.WebComponentUI) EXPORT_CHUNK(com.vaadin.flow.server.frontend.FrontendUtils.EXPORT_CHUNK) Function(java.util.function.Function) ArrayList(java.util.ArrayList) BootstrapHandler(com.vaadin.flow.server.BootstrapHandler) ElementUtil(com.vaadin.flow.dom.ElementUtil) Element(org.jsoup.nodes.Element) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) OutputStreamWriter(java.io.OutputStreamWriter) Constants(com.vaadin.flow.server.Constants) URI(java.net.URI) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) HandlerHelper(com.vaadin.flow.server.HandlerHelper) VaadinResponse(com.vaadin.flow.server.VaadinResponse) BufferedWriter(java.io.BufferedWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) PushConfiguration(com.vaadin.flow.component.PushConfiguration) IOException(java.io.IOException) VaadinRequest(com.vaadin.flow.server.VaadinRequest) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) Attribute(org.jsoup.nodes.Attribute) Document(org.jsoup.nodes.Document) Writer(java.io.Writer) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) PushConfiguration(com.vaadin.flow.component.PushConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) JsonObject(elemental.json.JsonObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 4 with WebComponentConfigurationRegistry

use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.

the class WebComponentBootstrapHandlerTest method mockRequest.

private VaadinRequest mockRequest(boolean hasConfig) {
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(service.getContext()).thenReturn(context);
    WebComponentConfigurationRegistry registry = Mockito.mock(WebComponentConfigurationRegistry.class);
    Mockito.when(context.getAttribute(Mockito.eq(WebComponentConfigurationRegistry.class), Mockito.any())).thenReturn(registry);
    Mockito.when(registry.hasConfigurations()).thenReturn(hasConfig);
    Mockito.when(request.getPathInfo()).thenReturn("/web-component/web-component-ui.js");
    return request;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest)

Example 5 with WebComponentConfigurationRegistry

use of com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry in project flow by vaadin.

the class WebComponentUI method assignThemeVariant.

private void assignThemeVariant() {
    WebComponentConfigurationRegistry registry = getConfigurationRegistry();
    Optional<Theme> theme = registry.getEmbeddedApplicationAnnotation(Theme.class);
    if (!theme.isPresent() || theme.get().themeClass().equals(AbstractTheme.class)) {
        return;
    }
    AbstractTheme themeInstance = Instantiator.get(this).getOrCreate(theme.get().themeClass());
    ThemeDefinition definition = new ThemeDefinition(theme.get());
    Map<String, String> attributes = themeInstance.getHtmlAttributes(definition.getVariant());
    registry.getConfigurations().forEach(config -> addAttributes(config.getTag(), attributes));
}
Also used : WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Theme(com.vaadin.flow.theme.Theme) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition)

Aggregations

WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)10 VaadinRequest (com.vaadin.flow.server.VaadinRequest)4 Component (com.vaadin.flow.component.Component)3 WebComponentConfiguration (com.vaadin.flow.component.webcomponent.WebComponentConfiguration)3 WebComponentExporter (com.vaadin.flow.component.WebComponentExporter)2 VaadinContext (com.vaadin.flow.server.VaadinContext)2 VaadinResponse (com.vaadin.flow.server.VaadinResponse)2 VaadinService (com.vaadin.flow.server.VaadinService)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8 (com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8)2 IOException (java.io.IOException)2 PushConfiguration (com.vaadin.flow.component.PushConfiguration)1 UI (com.vaadin.flow.component.UI)1 WebComponentExporterFactory (com.vaadin.flow.component.WebComponentExporterFactory)1 DefaultWebComponentExporterFactory (com.vaadin.flow.component.WebComponentExporterFactory.DefaultWebComponentExporterFactory)1 Push (com.vaadin.flow.component.page.Push)1 WebComponent (com.vaadin.flow.component.webcomponent.WebComponent)1 WebComponentUI (com.vaadin.flow.component.webcomponent.WebComponentUI)1 ElementUtil (com.vaadin.flow.dom.ElementUtil)1 JsonUtils (com.vaadin.flow.internal.JsonUtils)1