Skip to content

Commit

Permalink
feat: Reimplement paginator for sheet (#5824)
Browse files Browse the repository at this point in the history
progress...

issue: TOBAGO-2243
  • Loading branch information
lofwyr14 authored Dec 17, 2024
1 parent e2c143a commit b0a51ad
Show file tree
Hide file tree
Showing 28 changed files with 564 additions and 397 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,32 @@

package org.apache.myfaces.tobago.event;

import org.apache.myfaces.tobago.internal.util.JsonUtils;

import jakarta.faces.event.ActionEvent;
import jakarta.faces.component.UIComponent;

public class PageActionEvent extends ActionEvent {

private static final long serialVersionUID = 3364193750247386220L;

private SheetAction action;
private final SheetAction action;
private int value;

public PageActionEvent(final UIComponent component, final SheetAction action) {
super(component);
this.action = action;
}

public PageActionEvent(final UIComponent component, final JsonUtils.SheetActionRecord sheetActionRecord) {
super(component);
this.action = sheetActionRecord.action();
final Integer target = sheetActionRecord.target();
if (target != null) {
this.value = target;
}
}

/**
* Returns the action type ({@link SheetAction}).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@

package org.apache.myfaces.tobago.internal.component;

import org.apache.myfaces.tobago.component.SupportFieldId;
import org.apache.myfaces.tobago.component.Visual;
import org.apache.myfaces.tobago.util.ComponentUtils;

import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UIOutput;
import jakarta.faces.context.FacesContext;

public abstract class AbstractUIPaginator extends UIOutput implements Visual {
public abstract class AbstractUIPaginator extends UIOutput implements Visual, SupportFieldId {

public abstract String getFor();

public abstract boolean isAlwaysVisible();

// todo: might be an interface instead of AbstractUISheet
// todo: might be an interface instead of AbstractUISheet
public AbstractUISheet getPageable() {
final String forId = getFor();
if (forId != null) {
Expand All @@ -42,4 +44,9 @@ public AbstractUISheet getPageable() {
}
return ComponentUtils.findAncestor(this, AbstractUISheet.class);
}

@Override
public String getFieldId(final FacesContext facesContext) {
return getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "field";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.myfaces.tobago.internal.component;

import org.apache.myfaces.tobago.component.Attributes;
import org.apache.myfaces.tobago.component.Pageable;
import org.apache.myfaces.tobago.component.Visual;
import org.apache.myfaces.tobago.event.PageActionEvent;
import org.apache.myfaces.tobago.event.SheetAction;
Expand Down Expand Up @@ -81,8 +80,7 @@
*/
@ListenerFor(systemEventClass = PreRenderComponentEvent.class)
public abstract class AbstractUISheet extends AbstractUIData
implements SheetStateChangeSource, SortActionSource, ClientBehaviorHolder, Visual, Pageable,
ComponentSystemEventListener {
implements SheetStateChangeSource, SortActionSource, ClientBehaviorHolder, Visual, ComponentSystemEventListener {

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Expand Down Expand Up @@ -698,12 +696,13 @@ public boolean isLazyUpdate(FacesContext facesContext) {
final String clientId = getClientId(facesContext);

final String sheetClientIdWithAction =
clientId + UINamingContainer.getSeparatorChar(facesContext) + Pageable.SUFFIX_PAGE_ACTION + SheetAction.lazy;
clientId + UINamingContainer.getSeparatorChar(facesContext) + "pageAction" + SheetAction.lazy;
// TODO: please use something like this:
// final String sheetClientIdWithAction = clientId + ComponentUtils.SUB_SEPARATOR + SheetAction.lazy;

return sheetClientIdWithAction.equals(sourceId);
}


public void init(final FacesContext facesContext) {
sort(facesContext, null);
layoutHeader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.apache.myfaces.tobago.layout.Arrows;
import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
import org.apache.myfaces.tobago.renderkit.css.Icons;
import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
import org.apache.myfaces.tobago.util.ComponentUtils;
import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
import org.slf4j.Logger;
Expand All @@ -41,10 +43,6 @@ public class PaginatorListRenderer<T extends AbstractUIPaginatorList> extends Pa

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@Override
public void decodeInternal(final FacesContext facesContext, final T paginator) {
}

@Override
public void encodeBeginInternal(final FacesContext facesContext, final T paginator) throws IOException {

Expand All @@ -58,6 +56,13 @@ public void encodeBeginInternal(final FacesContext facesContext, final T paginat
writer.writeClassAttribute(
visible ? null : BootstrapClass.D_NONE,
paginator.getCustomClass());

writer.startElement(HtmlElements.INPUT);
writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
writer.writeIdAttribute(paginator.getFieldId(facesContext));
writer.writeNameAttribute(clientId);
writer.endElement(HtmlElements.INPUT);

if (sheet != null) {
writer.startElement(HtmlElements.UL);
writer.writeClassAttribute(BootstrapClass.PAGINATION);
Expand Down Expand Up @@ -104,44 +109,42 @@ public void encodeBeginInternal(final FacesContext facesContext, final T paginat
final Arrows arrows = paginator.getArrows();
if (arrows == Arrows.show || arrows == Arrows.auto) {
final boolean disabled = sheet.isAtBeginning();
encodeLink(
facesContext, sheet, disabled, SheetAction.first, null, Icons.SKIP_START, null);
encodeLink(facesContext, sheet, disabled, SheetAction.prev, null, Icons.CARET_LEFT, null);
encodeLink(facesContext, disabled, SheetAction.first, null, Icons.SKIP_START, null);
encodeLink(facesContext, disabled, SheetAction.prev, null, Icons.CARET_LEFT, null);
}

int skip = prevs.size() > 0 ? prevs.get(0) : 1;
int skip = !prevs.isEmpty() ? prevs.get(0) : 1;
if (!(arrows == Arrows.show || arrows == Arrows.auto) && skip > 1) {
skip -= linkCount - (linkCount / 2);
skip--;
if (skip < 1) {
skip = 1;
}
encodeLink(facesContext, sheet, false, SheetAction.toPage, skip, Icons.THREE_DOTS, null);
encodeLink(facesContext, false, SheetAction.toPage, skip, Icons.THREE_DOTS, null);
}
for (final Integer prev : prevs) {
encodeLink(facesContext, sheet, false, SheetAction.toPage, prev, null, null);
encodeLink(facesContext, false, SheetAction.toPage, prev, null, null);
}
encodeLink(facesContext, sheet, false, SheetAction.toPage,
sheet.getCurrentPage() + 1, null, BootstrapClass.ACTIVE);
encodeLink(facesContext, false, SheetAction.toPage, sheet.getCurrentPage() + 1, null, BootstrapClass.ACTIVE);

for (final Integer next : nexts) {
encodeLink(facesContext, sheet, false, SheetAction.toPage, next, null, null);
encodeLink(facesContext, false, SheetAction.toPage, next, null, null);
}

skip = nexts.size() > 0 ? nexts.get(nexts.size() - 1) : pages;
skip = !nexts.isEmpty() ? nexts.get(nexts.size() - 1) : pages;
if (!(arrows == Arrows.show || arrows == Arrows.auto) && skip < pages) {
skip += linkCount / 2;
skip++;
if (skip > pages) {
skip = pages;
}
encodeLink(facesContext, sheet, false, SheetAction.toPage, skip, Icons.THREE_DOTS, null);
encodeLink(facesContext, false, SheetAction.toPage, skip, Icons.THREE_DOTS, null);
}
if (arrows == Arrows.show || arrows == Arrows.auto) {
final boolean disabled = sheet.isAtEnd();
encodeLink(facesContext, sheet, disabled, SheetAction.next, null, Icons.CARET_RIGHT, null);
encodeLink(facesContext, sheet, disabled || !sheet.hasRowCount(), SheetAction.last, null,
Icons.SKIP_END, null);
final boolean disabledEnd = disabled || !sheet.hasRowCount();
encodeLink(facesContext, disabled, SheetAction.next, null, Icons.CARET_RIGHT, null);
encodeLink(facesContext, disabledEnd, SheetAction.last, null, Icons.SKIP_END, null);
}
writer.endElement(HtmlElements.UL);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.apache.myfaces.tobago.component.Facets;
import org.apache.myfaces.tobago.event.SheetAction;
import org.apache.myfaces.tobago.internal.component.AbstractUILink;
import org.apache.myfaces.tobago.internal.component.AbstractUIPaginatorPage;
import org.apache.myfaces.tobago.internal.component.AbstractUISheet;
import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
Expand All @@ -33,23 +32,14 @@
import org.apache.myfaces.tobago.util.ComponentUtils;
import org.apache.myfaces.tobago.util.ResourceUtils;
import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.faces.context.FacesContext;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.text.MessageFormat;
import java.util.Locale;

public class PaginatorPageRenderer<T extends AbstractUIPaginatorPage> extends PaginatorRenderer<T> {

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@Override
public void decodeInternal(final FacesContext facesContext, final T component) {
}

@Override
public void encodeBeginInternal(final FacesContext facesContext, final T paginator) throws IOException {

Expand All @@ -58,21 +48,20 @@ public void encodeBeginInternal(final FacesContext facesContext, final T paginat
final boolean visible = paginator.isAlwaysVisible() || sheet.needMoreThanOnePage();

writer.startElement(HtmlElements.TOBAGO_PAGINATOR_PAGE);
writer.writeIdAttribute(paginator.getClientId(facesContext));
final String clientId = paginator.getClientId(facesContext);
writer.writeIdAttribute(clientId);
writer.writeClassAttribute(
visible ? null : BootstrapClass.D_NONE,
BootstrapClass.PAGINATION,
paginator.getCustomClass());

final AbstractUILink command
= sheet.ensurePagingCommand(facesContext, sheet, Facets.pagerPage.name(), SheetAction.toPage.name(), false);
final String pagerCommandId = command.getClientId(facesContext);
writer.startElement(HtmlElements.UL);
writer.writeClassAttribute(BootstrapClass.PAGINATION);

if (sheet.isShowPageRangeArrows()) {
final boolean disabled = sheet.isAtBeginning();
encodeLink(
facesContext, sheet, disabled, SheetAction.first, null, Icons.SKIP_START, null);
encodeLink(facesContext, sheet, disabled, SheetAction.prev, null, Icons.CARET_LEFT, null);
facesContext, disabled, SheetAction.first, null, Icons.SKIP_START, null);
encodeLink(facesContext, disabled, SheetAction.prev, null, Icons.CARET_LEFT, null);
}
writer.startElement(HtmlElements.LI);
writer.writeClassAttribute(BootstrapClass.PAGE_ITEM);
Expand Down Expand Up @@ -105,8 +94,8 @@ public void encodeBeginInternal(final FacesContext facesContext, final T paginat
writer.writeText(Integer.toString(first));
writer.endElement(HtmlElements.SPAN);
writer.startElement(HtmlElements.INPUT);
writer.writeIdAttribute(pagerCommandId);
writer.writeNameAttribute(pagerCommandId);
writer.writeIdAttribute(paginator.getFieldId(facesContext));
writer.writeNameAttribute(clientId);
writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.TEXT);
writer.writeAttribute(HtmlAttributes.VALUE, first);
if (!unknown) {
Expand All @@ -123,10 +112,11 @@ public void encodeBeginInternal(final FacesContext facesContext, final T paginat
writer.endElement(HtmlElements.LI);
if (sheet.isShowPageRangeArrows()) {
final boolean disabled = sheet.isAtEnd();
encodeLink(facesContext, sheet, disabled, SheetAction.next, null, Icons.CARET_RIGHT, null);
encodeLink(facesContext, sheet, disabled || !sheet.hasRowCount(), SheetAction.last, null,
Icons.SKIP_END, null);
encodeLink(facesContext, disabled, SheetAction.next, null, Icons.CARET_RIGHT, null);
encodeLink(facesContext, disabled || !sheet.hasRowCount(), SheetAction.last, null, Icons.SKIP_END, null);
}

writer.endElement(HtmlElements.UL);
}

@Override
Expand Down
Loading

0 comments on commit b0a51ad

Please sign in to comment.