Skip to content

Commit

Permalink
adapting LazyLoadingComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Jun 25, 2024
1 parent 4ba2d05 commit bb21634
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
* Enrich a {@link LazyLoadingModel} with a type and a request id to be handled
* in a asynchronous thread.
* in an asynchronous thread.
*/
public interface LazyLoadingThreadModel<T> extends LazyLoadingModel {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@
*/
package de.cuioss.jsf.bootstrap.lazyloading;

import java.util.Optional;

import jakarta.el.MethodExpression;
import jakarta.faces.application.ResourceDependency;
import jakarta.faces.component.FacesComponent;
import jakarta.faces.component.NamingContainer;
import jakarta.faces.component.StateHelper;
import jakarta.faces.component.UICommand;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.ComponentSystemEvent;
import jakarta.faces.event.ListenerFor;
import jakarta.faces.event.PostAddToViewEvent;
import jakarta.faces.event.PreRenderViewEvent;

import de.cuioss.jsf.api.components.css.ContextState;
import de.cuioss.jsf.api.components.model.lazyloading.LazyLoadingModel;
import de.cuioss.jsf.api.components.partial.ComponentBridge;
Expand All @@ -44,8 +29,18 @@
import de.cuioss.jsf.bootstrap.waitingindicator.WaitingIndicatorComponent;
import de.cuioss.tools.string.MoreStrings;
import de.cuioss.uimodel.nameprovider.IDisplayNameProvider;
import jakarta.el.MethodExpression;
import jakarta.faces.application.ResourceDependency;
import jakarta.faces.component.*;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.ComponentSystemEvent;
import jakarta.faces.event.ListenerFor;
import jakarta.faces.event.PostAddToViewEvent;
import jakarta.faces.event.PreRenderViewEvent;
import lombok.experimental.Delegate;

import java.util.Optional;

/**
* Displays a content that should be loaded lazy after initial page rendering.
* <p>
Expand Down Expand Up @@ -78,7 +73,7 @@ public class LazyLoadingComponent extends UICommand implements ComponentBridge,

private static final String WAITING_INDICATOR_STYLE_CLASS_KEY = "waitingIndicatorStyleClass";

static final String ID_SUFFIX_ISLOADED = "isloaded";
static final String ID_SUFFIX_IS_LOADED = "is_loaded";

static final String DATA_RESULT_NOTIFICATION_BOX = "data-resultNotificationBox";

Expand Down Expand Up @@ -115,7 +110,7 @@ public void processEvent(final ComponentSystemEvent event) {
if (!oneTimeCheck.readAndSetChecked()) {
var startInitialize = getStartInitialize();
if (null != startInitialize) {
startInitialize.invoke(getFacesContext().getELContext(), new Object[] {});
startInitialize.invoke(getFacesContext().getELContext(), new Object[]{});
}
if (!evaluateInitialized() && null != getViewModel()) {
super.addActionListener(getViewModel());
Expand Down Expand Up @@ -153,7 +148,7 @@ public UIComponent facet(final String facetName) {
* method!
*
* @return the startInitialize, a function to be called one time at
* PreRenderViewEvent
* PreRenderViewEvent
*/
public MethodExpression getStartInitialize() {
return (MethodExpression) getStateHelper().eval(START_INITIALIZE_KEY);
Expand Down Expand Up @@ -287,7 +282,7 @@ public void setRenderContent(final boolean renderContent) {

/**
* @return boolean indicating whether the children are rendered or should be
* rendered
* rendered
*/
public boolean getChildrenLoaded() {
return state.getBoolean(CHILDREN_LOADED_KEY);
Expand Down Expand Up @@ -345,12 +340,12 @@ public void processUpdates(FacesContext context) {
*
* @param context the FacesContext
* @return true if the current request was triggered by the ajax request to
* reload the lazy loading content
* reload the lazy loading content
*/
public boolean isContentLoadRequest(FacesContext context) {
var componentWrapper = new ComponentWrapper<>(this);
return context.getExternalContext().getRequestParameterMap()
.containsKey(componentWrapper.getSuffixedClientId(ID_SUFFIX_ISLOADED));
.containsKey(componentWrapper.getSuffixedClientId(ID_SUFFIX_IS_LOADED));
}

/**
Expand Down Expand Up @@ -384,7 +379,7 @@ NotificationBoxComponent createNotificationBox() {
if (result.isEmpty()) {
var notificationBoxComponent = new NotificationBoxComponent();
notificationBoxComponent.getPassThroughAttributes().put(DATA_RESULT_NOTIFICATION_BOX,
DATA_RESULT_NOTIFICATION_BOX);
DATA_RESULT_NOTIFICATION_BOX);
getChildren().add(0, notificationBoxComponent);
return notificationBoxComponent;
}
Expand All @@ -398,7 +393,7 @@ public Optional<UIComponent> retrieveWaitingIndicator() {

public Optional<UIComponent> retrieveNotificationBox() {
return getChildren().stream()
.filter(child -> child.getPassThroughAttributes().containsKey(DATA_RESULT_NOTIFICATION_BOX))
.findFirst();
.filter(child -> child.getPassThroughAttributes().containsKey(DATA_RESULT_NOTIFICATION_BOX))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@
*/
package de.cuioss.jsf.bootstrap.lazyloading;

import static de.cuioss.jsf.bootstrap.lazyloading.LazyLoadingComponent.DATA_RESULT_NOTIFICATION_BOX;
import static de.cuioss.jsf.bootstrap.lazyloading.LazyLoadingComponent.ID_SUFFIX_ISLOADED;
import static de.cuioss.jsf.bootstrap.lazyloading.LazyLoadingComponent.WAITING_INDICATOR_ID;

import java.io.IOException;

import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.ActionEvent;
import jakarta.faces.render.FacesRenderer;

import de.cuioss.jsf.api.components.html.AttributeName;
import de.cuioss.jsf.api.components.html.Node;
import de.cuioss.jsf.api.components.renderer.BaseDecoratorRenderer;
Expand All @@ -34,31 +23,39 @@
import de.cuioss.jsf.bootstrap.BootstrapFamily;
import de.cuioss.jsf.bootstrap.CssCuiBootstrap;
import de.cuioss.jsf.bootstrap.notification.NotificationBoxComponent;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.ActionEvent;
import jakarta.faces.render.FacesRenderer;

import java.io.IOException;

import static de.cuioss.jsf.bootstrap.lazyloading.LazyLoadingComponent.*;

/**
* Renderer for {@linkplain LazyLoadingComponent}.
*/
@FacesRenderer(rendererType = BootstrapFamily.LAZYLOADING_RENDERER, componentFamily = BootstrapFamily.COMPONENT_FAMILY)
public class LazyLoadingRenderer extends BaseDecoratorRenderer<LazyLoadingComponent> {

private static final String DATA_LAZY_LOADING_CONTENT = "data-lazyloading-content";
static final String DATA_LAZY_LOADING_CONTENT = "data-lazy_loading-content";

private static final String LAZY_LOADING_CONTENT_ID = "content";

private static final String DATA_IGNORE_AUTO_UPDATE = "data-ignore-auto-update";

private static final String DATA_ASYNC = "data-async";

private static final String DATA_WAITING_INDICATOR_ID = "data-lazyloading-waiting-indicator-id";
static final String DATA_WAITING_INDICATOR_ID = "data-lazy_loading-waiting-indicator-id";

public LazyLoadingRenderer() {
super(true);
}

@Override
protected void doEncodeBegin(final FacesContext context,
final DecoratingResponseWriter<LazyLoadingComponent> writer, final LazyLoadingComponent component)
throws IOException {
final DecoratingResponseWriter<LazyLoadingComponent> writer, final LazyLoadingComponent component)
throws IOException {
writer.withStartElement(Node.DIV);
writer.withClientId();
if (!component.shouldRenderWaitingIndicator(context)) {
Expand All @@ -71,31 +68,31 @@ protected void doEncodeBegin(final FacesContext context,
throw new IllegalStateException("Waiting indicator not found!");
}
writer.writeAttribute(DATA_WAITING_INDICATOR_ID, waitingIndicatorComponentResult.get().getClientId(),
DATA_WAITING_INDICATOR_ID);
DATA_WAITING_INDICATOR_ID);
writer.withStyleClass(CssCuiBootstrap.CUI_LAZY_LOADING.getStyleClassBuilder().append(component.getStyleClass())
.append(CssCuiBootstrap.UI_HIDDEN_CONTAINER));
.append(CssCuiBootstrap.UI_HIDDEN_CONTAINER));
writer.withAttributeStyle(component.getStyle());
writer.withPassThroughAttributes();
}

@Override
protected void doEncodeEnd(final FacesContext context, final DecoratingResponseWriter<LazyLoadingComponent> writer,
final LazyLoadingComponent component) throws IOException {
final LazyLoadingComponent component) throws IOException {
writer.withEndElement(Node.DIV);
}

@Override
protected void doEncodeChildren(final FacesContext context,
final DecoratingResponseWriter<LazyLoadingComponent> writer, final LazyLoadingComponent component)
throws IOException {
final DecoratingResponseWriter<LazyLoadingComponent> writer, final LazyLoadingComponent component)
throws IOException {

var waitingIndicatorComponentResult = component.retrieveWaitingIndicator();
if (waitingIndicatorComponentResult.isEmpty()) {
throw new IllegalStateException("Waiting indicator not found!");
}
var waitingIndicatorComponent = waitingIndicatorComponentResult.get();
waitingIndicatorComponent.getAttributes().put("style",
"display: " + (component.shouldRenderWaitingIndicator(context) ? "block;" : "none;"));
"display: " + (component.shouldRenderWaitingIndicator(context) ? "block;" : "none;"));

if (!component.shouldRenderWaitingIndicator(context)) { // render all children including
// waiting indicator
Expand All @@ -104,7 +101,7 @@ protected void doEncodeChildren(final FacesContext context,
writer.withClientId(LAZY_LOADING_CONTENT_ID);
writer.writeAttribute(DATA_LAZY_LOADING_CONTENT, DATA_LAZY_LOADING_CONTENT, DATA_LAZY_LOADING_CONTENT);
var resultNotificationBoxComponent = (NotificationBoxComponent) component.retrieveNotificationBox()
.orElseThrow(IllegalStateException::new);
.orElseThrow(IllegalStateException::new);
if (null != component.evaluateNotificationBoxValue()) {
resultNotificationBoxComponent.setState(component.evaluateNotificationBoxState().name());
resultNotificationBoxComponent.setContentValue(component.evaluateNotificationBoxValue());
Expand All @@ -117,8 +114,8 @@ protected void doEncodeChildren(final FacesContext context,
if (component.evaluateRenderContent()) {
for (final UIComponent child : component.getChildren()) {
if (child.isRendered()
&& !child.getPassThroughAttributes().containsKey(DATA_RESULT_NOTIFICATION_BOX)
&& !WAITING_INDICATOR_ID.equals(child.getId())) {
&& !child.getPassThroughAttributes().containsKey(DATA_RESULT_NOTIFICATION_BOX)
&& !WAITING_INDICATOR_ID.equals(child.getId())) {
child.encodeAll(context);
}
}
Expand All @@ -136,7 +133,7 @@ protected void doDecode(final FacesContext context, final ComponentWrapper<LazyL
final var component = componentWrapper.getWrapped();
final var map = context.getExternalContext().getRequestParameterMap();

final var isLoadedValue = map.get(componentWrapper.getSuffixedClientId(ID_SUFFIX_ISLOADED));
final var isLoadedValue = map.get(componentWrapper.getSuffixedClientId(ID_SUFFIX_IS_LOADED));
if (isLoadedValue != null) {
var loadedValue = Boolean.parseBoolean(isLoadedValue);
if (loadedValue && !component.getChildrenLoaded()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
function intitializeLazyLoading() {
jQuery(".cui-lazy-loading").each(function() {
let lazyloading = jQuery(this);
let lazyloadingId = lazyloading.attr("id");
if (!lazyloading.data("content-loaded")) {
lazyloading.data("content-loaded", "true");
function initializeLazyLoading() {
jQuery(".cui-lazy-loading").each(function () {
let lazyLoading = jQuery(this);
let lazyLoadingId = lazyLoading.attr("id");
if (!lazyLoading.data("content-loaded")) {
lazyLoading.data("content-loaded", "true");
PrimeFaces.ab({
source: lazyloadingId, process: lazyloadingId,
update: lazyloadingId, ignoreAutoUpdate: lazyloading.data("ignore-auto-update"),
async: lazyloading.data("async"), params: [{ name: lazyloadingId + "_isloaded", value: true }]
source: lazyLoadingId, process: lazyLoadingId,
update: lazyLoadingId, ignoreAutoUpdate: lazyLoading.data("ignore-auto-update"),
async: lazyLoading.data("async"), params: [{name: lazyLoadingId + "_is_loaded", value: true}]
});
}
});
}

jQuery(document).ready(function() {
Cui.Core.registerComponentEnabler(intitializeLazyLoading);
jQuery(document).ready(function () {
Cui.Core.registerComponentEnabler(initializeLazyLoading);

$(document).on("pfAjaxSend", function(event, xhr, options) {
$(document).on("pfAjaxSend", function (event, xhr, options) {
handleEvent(options, "begin");
});
$(document).on("pfAjaxComplete", function(event, xhr, options) {
$(document).on("pfAjaxComplete", function (event, xhr, options) {
handleEvent(options, "complete");
});

Expand Down Expand Up @@ -59,17 +59,17 @@ jQuery(document).ready(function() {
}

function handleLazyLoadingContainer(id, status, container, dataMap, options, includedInUpdate) {
let element = jQuery("[data-lazyloading-content]", container);
let lazyloadingId = container.attr("id");
let element = jQuery("[data-lazy_loading-content]", container);
let lazyLoadingId = container.attr("id");
if (includedInUpdate) {
options.data = options.data + "&" + encodeURIComponent(lazyloadingId + "_isloaded") + "=true";
options.data = options.data + "&" + encodeURIComponent(lazyLoadingId + "_is_loaded") + "=true";
}
if (isUiTreeTable(id) && dataMap.hasOwnProperty(encodeURIComponent(jQuery(id).attr("id") + "_expand"))) {
return;
}

if (container && container.length === 1 && container.data("lazyloading-waiting-indicator-id")) {
let waitingIndicator = jQuery(Cui.Utilities.escapeClientId(container.data("lazyloading-waiting-indicator-id")));
if (container && container.length === 1 && container.data("lazy_loading-waiting-indicator-id")) {
let waitingIndicator = jQuery(Cui.Utilities.escapeClientId(container.data("lazy_loading-waiting-indicator-id")));
if (waitingIndicator) {
if (status === "begin") {
waitingIndicator.show();
Expand All @@ -81,15 +81,15 @@ jQuery(document).ready(function() {
// special treatment of primefaces datatable: when changing the filter, only the body should be exchanged
// with the waiting indicator
if (isUiDataTable(id) && dataMap["jakarta.faces.behavior.event"] === "filter") {
element = jQuery(".ui-datatable-data.ui-widget-content", jQuery(id)).add("[data-lazyloading-content] .alert", container);
element = jQuery(".ui-datatable-data.ui-widget-content", jQuery(id)).add("[data-lazy_loading-content] .alert", container);
} else if (isUiTreeTable(id)) {
element = jQuery(".ui-treetable-data.ui-widget-content", jQuery(id)).add("[data-lazyloading-content] .alert", container);
element = jQuery(".ui-treetable-data.ui-widget-content", jQuery(id)).add("[data-lazy_loading-content] .alert", container);
}
if (status === "begin") {
element.hide();
} else {
element.show();
PrimeFaces.invokeDeferredRenders(lazyloadingId);
PrimeFaces.invokeDeferredRenders(lazyLoadingId);
}
}

Expand All @@ -103,14 +103,14 @@ jQuery(document).ready(function() {
handleLazyLoadingContainer(id, status, element, dataMap, options, true);
} else {
if (!element.data("lazyloading-content")) {
element = element.parents("[data-lazyloading-content]").first();
element = element.parents("[data-lazy_loading-content]").first();
}
if (!element || element.length === 0) {
element = jQuery(".cui-lazy-loading", jQuery(id));
if (!element || element.length === 0) {
return;
}
element.each(function() {
element.each(function () {
handleLazyLoadingContainer(id, status, jQuery(this), dataMap, options, true);
});
} else {
Expand Down
Loading

0 comments on commit bb21634

Please sign in to comment.