Skip to content

Commit

Permalink
#498 likely done
Browse files Browse the repository at this point in the history
  • Loading branch information
vaadin-miki committed Dec 11, 2023
1 parent bde6a55 commit f8d715e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
32 changes: 0 additions & 32 deletions demo-v24/frontend/generated/index.ts

This file was deleted.

5 changes: 0 additions & 5 deletions demo-v24/frontend/generated/vaadin.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static final class LazyLoadElement extends Div {}

private final SerializableSupplier<C> componentProvider;

private final ComponentObserver observer = new ComponentObserver();
private final ComponentObserver observer;

private final boolean onlyLoadedOnce;

Expand Down Expand Up @@ -77,9 +77,38 @@ public LazyLoad(C contents, boolean removeOnHide) {
* @param removeOnHide Whether to remove the component when this object gets hidden.
*/
public LazyLoad(SerializableSupplier<C> supplier, boolean removeOnHide) {
this(supplier, ComponentObserver::new, removeOnHide);
}

/**
* Creates lazy load wrapper for given contents. It will be displayed the first time this component becomes shown on screen.
* @param contents Contents to wrap for lazy loading.
* @param observerSupplier a way to create a customised {@link ComponentObserver} that will be used by this component.
*/
public LazyLoad(C contents, SerializableSupplier<ComponentObserver> observerSupplier) {
this(() -> contents, observerSupplier, false);
}

/**
* Creates lazy load wrapper for given component supplier. It will be called exactly once, the first time this component becomes shown on screen.
* @param contentSupplier {@link Supplier} that will be called when the component gets into view.
* @param observerSupplier a way to create a customised {@link ComponentObserver} that will be used by this component.
*/
public LazyLoad(SerializableSupplier<C> contentSupplier, SerializableSupplier<ComponentObserver> observerSupplier) {
this(contentSupplier, observerSupplier, false);
}

/**
* Creates lazy load wrapper for given component supplier.
* @param contentSupplier {@link Supplier} that will be called when the component gets into view.
* @param observerSupplier a way to create a customised {@link ComponentObserver} that will be used by this component.
* @param removeOnHide Whether to remove the component when this object gets hidden.
*/
public LazyLoad(SerializableSupplier<C> contentSupplier, SerializableSupplier<ComponentObserver> observerSupplier, boolean removeOnHide) {
super();
this.componentProvider = supplier;
this.componentProvider = contentSupplier;
this.onlyLoadedOnce = !removeOnHide;
this.observer = observerSupplier.get();
this.getContent().addClassNames(EMPTY_CLASS_NAME);
this.getContent().add(this.observer);
this.observer.addComponentObservationListener(this::onComponentObserved);
Expand All @@ -96,6 +125,9 @@ else if(event.isNotVisible())
this.onNowHidden();
}

/**
* Called when the content becomes hidden.
*/
protected void onNowHidden() {
if(this.lazyLoadedContent != null) {
this.getContent().removeClassName(LOADED_CLASS_NAME);
Expand All @@ -105,6 +137,9 @@ protected void onNowHidden() {
}
}

/**
* Called when the content becomes visible.
*/
protected void onNowVisible() {
if(this.lazyLoadedContent == null) {
this.getContent().removeClassName(EMPTY_CLASS_NAME);
Expand Down Expand Up @@ -134,7 +169,7 @@ public Optional<C> getLoadedContent() {

/**
* Checks if the content has been already loaded ({@link #isOnlyLoadedOnce()} ()} is {@code true}) or is currently loaded.
* @return Whether or not the content has been loaded.
* @return Whether the content has been loaded.
* @see #getLoadedContent()
* @see #isOnlyLoadedOnce()
*/
Expand Down

0 comments on commit f8d715e

Please sign in to comment.