Skip to content

Commit

Permalink
Simplify LazyLoadingComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Jul 12, 2024
1 parent d3e3314 commit bb7690c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package de.cuioss.jsf.api.components.partial;

import jakarta.faces.component.StateHelper;

import de.cuioss.jsf.api.components.util.CuiState;
import jakarta.faces.component.StateHelper;
import lombok.NonNull;

/**
* Sets the ignoreAutoUpdate flag to ajax calls. When set to true,
* {@code <p:autoUpdate/>} elements will not be updated.
* Defaults to {@code false}
*/
public class IgnoreAutoUpdateProvider {

/** The key for the {@link StateHelper} */
/**
* The key for the {@link StateHelper}
*/
private static final String KEY = "ignoreAutoUpdate";

private final CuiState state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package de.cuioss.jsf.api.components.support;

import static java.util.Objects.requireNonNull;

import de.cuioss.jsf.api.components.partial.ComponentBridge;
import de.cuioss.jsf.api.components.util.KeyMappingUtility;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import static java.util.Objects.requireNonNull;

/**
* Some attribute like disabled and the styleclass needs to be changed once.
* PostAddToView does not fit, its a called on ajax requests also. This class
* helps keeping track of said changes.
* Some attribute like disabled and the style class needs to be changed once.
* PostAddToView does not fit, it's called on ajax requests also.
* This class helps keep track of said changes.
*
* @author Oliver Wolff
*/
Expand All @@ -48,7 +48,7 @@ public OneTimeCheck(final ComponentBridge componentBridge) {
}

/**
* Constructor. Needed only for cases, if a component uses more than one
* Constructor. Needed only for cases if a component uses more than one
* {@link OneTimeCheck}
*
* @param componentBridge must not be null
Expand All @@ -61,7 +61,7 @@ public OneTimeCheck(final ComponentBridge componentBridge, final String extensio

/**
* @return the actual flag whether the check was done. Defaults to
* <code>false</code>
* <code>false</code>
*/
public boolean isChecked() {
return (Boolean) componentBridge.stateHelper().eval(compoundKey, Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import jakarta.faces.event.ComponentSystemEvent;
import jakarta.faces.event.ListenerFor;
import jakarta.faces.event.PostAddToViewEvent;
import jakarta.faces.event.PreRenderViewEvent;
import jakarta.faces.event.PreRenderComponentEvent;
import lombok.experimental.Delegate;

import java.util.Optional;
Expand All @@ -52,7 +52,7 @@
*/
@ResourceDependency(library = "javascript.enabler", name = "enabler.lazyLoading.js", target = "head")
@FacesComponent(BootstrapFamily.LAZYLOADING_COMPONENT)
@ListenerFor(systemEventClass = PreRenderViewEvent.class)
@ListenerFor(systemEventClass = PreRenderComponentEvent.class)
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class LazyLoadingComponent extends UICommand implements ComponentBridge, NamingContainer {

Expand Down Expand Up @@ -108,17 +108,14 @@ public LazyLoadingComponent() {

@Override
public void processEvent(final ComponentSystemEvent event) {
if (event instanceof PreRenderViewEvent) {
if (event instanceof PreRenderComponentEvent) {
LOGGER.debug("Handling PreRenderComponentEvent %s before OneTimeCheck", event);
if (!oneTimeCheck.readAndSetChecked()) {
LOGGER.debug("Handling PreRenderViewEvent %s after OneTimeCheck", event);
LOGGER.debug("Handling PreRenderComponentEvent %s after OneTimeCheck", event);
var startInitialize = getStartInitialize();
if (null != startInitialize) {
if (startInitialize.isPresent()) {
LOGGER.debug("Invoking startInitialize");
startInitialize.invoke(getFacesContext().getELContext(), new Object[]{});
}
if (evaluateNotInitialized() && null != getViewModel()) {
LOGGER.debug("Adding actionListener");
super.addActionListener(getViewModel());
startInitialize.get().invoke(getFacesContext().getELContext(), new Object[]{});
}
}
} else if (event instanceof PostAddToViewEvent) {
Expand Down Expand Up @@ -156,8 +153,8 @@ public UIComponent facet(final String facetName) {
* @return the startInitialize, a function to be called one time at
* PreRenderViewEvent
*/
public MethodExpression getStartInitialize() {
return (MethodExpression) getStateHelper().eval(START_INITIALIZE_KEY);
private Optional<MethodExpression> getStartInitialize() {
return Optional.ofNullable((MethodExpression) getStateHelper().eval(START_INITIALIZE_KEY));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4953,7 +4953,7 @@
</attribute>
</tag>
<tag>
<description><![CDATA[<h2>Displays a content that should be loaded lazy after initial page rendering.</h2>
<description><![CDATA[<h2>Displays content that should be loaded lazy after initial page rendering.</h2>
<p>The initial page will display the waiting indicator and trigger an ajax update of the content.
This update will first call an ActionListener (if defined) during Invoke Application phase,
Expand Down Expand Up @@ -4997,7 +4997,7 @@
<pre>public void methodName(ActionEvent event)</pre>]]>
</description>
<name>actionListener</name>
<required>false</required>
<required>true</required>
<type>jakarta.faces.event.ActionListener</type>
</attribute>
<attribute>
Expand Down Expand Up @@ -5038,7 +5038,7 @@
<type>de.cuioss.jsf.api.components.model.lazyloading.LazyLoadingModel</type>
</attribute>
<attribute>
<description><![CDATA[Start the initialization. Will be called at the first PostAddToView.]]>
<description><![CDATA[Start the initialization. Will be called at the first PostAddToView, if present]]>
</description>
<name>startInitialize</name>
<required>false</required>
Expand All @@ -5054,15 +5054,15 @@
<type>java.lang.String</type>
</attribute>
<attribute>
<description><![CDATA[When set to true, <p:autoUpdate/> elements will not be updated.]]>
<description><![CDATA[When set to true, <p:autoUpdate/> elements will not be updated. Defaults to false.]]>
</description>
<name>ignoreAutoUpdate</name>
<required>false</required>
<type>boolean</type>
</attribute>
<attribute>
<description>
<![CDATA[When set to true, ajax requests will be made asynchronous. Attention: There is a maximum in the number of connections a browser allows.]]>
<![CDATA[When set to true, ajax requests will be made asynchronous. Attention: There is a maximum in the number of connections a browser allows. Defaults to false.]]>
</description>
<name>async</name>
<required>false</required>
Expand Down

0 comments on commit bb7690c

Please sign in to comment.