Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.data.renderer;
import java.util.Objects;
import java.util.regex.Pattern;
import org.slf4j.LoggerFactory;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.internal.JsonSerializer;
import com.vaadin.flow.internal.UsageStatistics;
/**
* Helper class to create {@link Renderer} instances, with fluent API.
*
* @author Vaadin Ltd
*
* @param
* the type of the input object used inside the template
*
* @see #of(String)
* @see https://www.polymer-project.org/2.0/docs/devguide/templates
*
* @deprecated since Vaadin 22, {@code TemplateRenderer} is deprecated in favor
* of {@link LitRenderer}
*/
@Deprecated
public final class TemplateRenderer extends Renderer {
static {
UsageStatistics.markAsUsed("flow-components/TemplateRenderer", null);
}
private static final Pattern BINDING_MISSING_DOLLAR = Pattern
.compile("\\s(class|style)\\s*=\\s*['\"]?[{\\[]{2}");
/**
* Creates a new TemplateRenderer based on the provided template. The
* template accepts anything that is allowed inside a {@code }
* element, and works with Polymer data binding syntax.
*
* Examples:
*
*
* {@code
* // Prints the index of the item inside a repeating list
* TemplateRenderer.of("[[index]]");
*
* // Prints the property of an item
* TemplateRenderer.of("
Property: [[item.property]]
");
* }
*
*
* @param
* the type of the input object used inside the template
* @param template
* the template used to render items, not null
* @return an initialized TemplateRenderer
* @see TemplateRenderer#withProperty(String, ValueProvider)
*/
public static TemplateRenderer of(String template) {
Objects.requireNonNull(template);
warnIfClassOrStyleWithoutDollar(template);
TemplateRenderer renderer = new TemplateRenderer<>(template);
return renderer;
}
private static void warnIfClassOrStyleWithoutDollar(String template) {
if (hasClassOrStyleWithoutDollar(template)) {
LoggerFactory.getLogger(TemplateRenderer.class).warn(
"Bindings for 'class' and 'style' need a $ prefix (e.g. 'class$=\"[[item.className]]\"') to use an attribute binding instead of a property binding."
+ " Apparent omission detected in the template '{}'",
template);
}
}
// Not private for testing purposes
static boolean hasClassOrStyleWithoutDollar(String template) {
return BINDING_MISSING_DOLLAR.matcher(template).find();
}
private TemplateRenderer(String template) {
super(template);
}
/**
* Sets a property to be used inside the template. Each property is
* referenced inside the template by using the {@code [[item.property]]}
* syntax.
*
")
* .withProperty("name", Person::getName);
*
* // Property that uses a bean. Note that in this case the entire "Address" object will be sent to the template.
* // Note that even properties of the bean which are not used in the template are sent to the client, so use
* // this feature with caution.
* TemplateRenderer. of("Street: [[item.address.street]]")
* .withProperty("address", Person::getAddress);
*
* // In this case only the street field inside the Address object is sent
* TemplateRenderer. of("Street: [[item.street]]")
* .withProperty("street", person -> person.getAddress().getStreet());
* }
*
*
* Any types supported by the {@link JsonSerializer} are valid types for the
* TemplateRenderer.
*
* @param property
* the name of the property used inside the template, not
* null
* @param provider
* a {@link ValueProvider} that provides the actual value for the
* property, not null
* @return this instance for method chaining
*/
public TemplateRenderer withProperty(String property,
ValueProvider provider) {
setProperty(property, provider);
return this;
}
/**
* Sets an event handler for events from elements inside the template. Each
* event is referenced inside the template by using the {@code on-event}
* syntax.
*
* Examples:
*
*
* {@code
* // Standard event
* TemplateRenderer.of("")
* .withEventHandler("handleClick", object -> doSomething());
*
* // You can handle custom events from webcomponents as well, using the same syntax
* TemplateRenderer.of("")
* .withEventHandler("onCustomEvent", object -> doSomething());
* }
*
*
* The name of the function used on the {@code on-event} attribute should be
* the name used at the handlerName parameter. This name must be a valid
* Javascript function name.
*
* @param handlerName
* the name of the handler used inside the
* {@code on-event="handlerName"}, not null
* @param handler
* the handler executed when the event is triggered, not
* null
* @return this instance for method chaining
* @see https://www.polymer-project.org/2.0/docs/devguide/events
*/
public TemplateRenderer withEventHandler(String handlerName,
SerializableConsumer handler) {
setEventHandler(handlerName, handler);
return this;
}
}