Skip to content

Commit

Permalink
Fix #11447: Dashboard var attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Feb 17, 2024
1 parent e3fee47 commit 0551835
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 10 deletions.
32 changes: 31 additions & 1 deletion docs/14_0_0/components/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Dashboard provides a portal like layout with drag&drop based reorder capabilitie
| responsive | false | Boolean | In responsive mode, allows use of PrimeFlex CSS to control widget sizes and responsiveness.
| style | null | String | Inline style of the dashboard container
| styleClass | null | String | Style class of the dashboard container
| var | null | String | Name of collection iterator which will be the value of DashboardWidget.getValue().

## Getting started with Dashboard
Dashboard is backed by a DashboardModel and consists of panel components.
Expand Down Expand Up @@ -84,7 +85,36 @@ public class Bean {
public Bean() {
responsiveModel = new DefaultDashboardModel();
responsiveModel.addWidget(new DefaultDashboardWidget("bar", "col-12 lg:col-6 xl:col-4"));
responsiveModel.addColumn(new DefaultDashboardWidget("stacked", "col-12 lg:col-6 xl:col-8"));
responsiveModel.addWidget(new DefaultDashboardWidget("stacked", "col-12 lg:col-6 xl:col-8"));
}
}
```

## Dynamic
Dashboard can be dynamic using `var="widget"` mode to allow dynamic panels. The `value` must be set in your
DashboardWidget model.

```xhtml
<p:dashboard model="#{bean.model}" var="dash">
<p:panel id="bar" rendered="#{dash.visible}">
#{dash.name}
</p:panel>
<p:panel id="stacked" rendered="#{dash.visible}">
#{dash.name}
</p:panel>
//more panels like lifestyle, weather, politics...
</p:dashboard>
```
Dashboard model in this mode wants `one` widget per column and should be given a PrimeFlex responsive CSS options.

```java
public class Bean {
private DashboardModel model;

public Bean() {
responsiveModel = new DefaultDashboardModel();
responsiveModel.addWidget(new DefaultDashboardWidget("bar", "col-12 lg:col-6 xl:col-4", yourObject1));
responsiveModel.addWidget(new DefaultDashboardWidget("stacked", "col-12 lg:col-6 xl:col-8", yourObject1));
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions docs/14_0_0/gettingstarted/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Look into [migration guide](https://primefaces.github.io/primefaces/14_0_0/#/../
* Added `filterPlaceholder` for `Column` and `Columns`
* Added `rowData` to `CellEditEvent` which contains the entire row from the cell being edited.

* Dashboard
* Added `var` to allow dynamic panels in `DashboardWidget.setValue(obj)` per panel

* Messages
* Added `clearMessages` widget method to clear all current messages.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public enum PropertyKeys {
reordering,
style,
styleClass,
responsive
responsive,
var
}

public DashboardBase() {
Expand Down Expand Up @@ -110,4 +111,12 @@ public boolean isResponsive() {
public void setResponsive(boolean responsive) {
getStateHelper().put(PropertyKeys.responsive, responsive);
}

public String getVar() {
return (String) getStateHelper().eval(PropertyKeys.var, null);
}

public void setVar(String var) {
getStateHelper().put(PropertyKeys.var, var);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.util.List;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
Expand All @@ -35,6 +36,7 @@
import org.primefaces.model.dashboard.DashboardModel;
import org.primefaces.model.dashboard.DashboardWidget;
import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.GridLayoutUtils;
import org.primefaces.util.WidgetBuilder;

Expand All @@ -57,6 +59,7 @@ protected void encodeMarkup(FacesContext context, Dashboard dashboard) throws IO
ResponseWriter writer = context.getResponseWriter();
String clientId = dashboard.getClientId(context);
boolean responsive = dashboard.isResponsive();
String var = dashboard.getVar();

writer.startElement("div", dashboard);
writer.writeAttribute("id", clientId, "id");
Expand Down Expand Up @@ -92,7 +95,14 @@ protected void encodeMarkup(FacesContext context, Dashboard dashboard) throws IO
for (String widgetId : column.getWidgets()) {
Panel widget = (Panel) SearchExpressionUtils.contextlessResolveComponent(context, dashboard, widgetId);
if (widget != null) {
renderChild(context, widget);
ComponentUtils.executeInRequestScope(context, var, column.getValue(), () -> {
try {
return renderChild(context, widget);
}
catch (IOException e) {
throw new FacesException(e);
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public interface DashboardWidget {
void setStyle(String style);

void setStyleClass(String styleClass);

Object getValue();

void setValue(Object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,35 @@ public class DefaultDashboardWidget implements DashboardWidget, Serializable {
private List<String> widgets;
private String style;
private String styleClass;
private Object value;

public DefaultDashboardWidget() {
widgets = new LinkedList<>();
}

public DefaultDashboardWidget(String widgetId, String styleClass) {
public DefaultDashboardWidget(String style, String styleClass, Object value, Collection<String> widgets) {
this();
this.widgets.addAll(widgets);
this.style = style;
this.styleClass = styleClass;
this.value = value;
}

public DefaultDashboardWidget(String style, String styleClass, Collection<String> widgets) {
this(style, styleClass, null, widgets);
}

public DefaultDashboardWidget(String widgetId, String styleClass, Object value) {
this();
getWidgets().addAll(Arrays.asList(widgetId));
setStyleClass(styleClass);
setValue(value);
}

public DefaultDashboardWidget(String style, String styleClass, Collection<String> widgets) {
public DefaultDashboardWidget(String widgetId, String styleClass) {
this();
this.widgets.addAll(widgets);
this.style = style;
this.styleClass = styleClass;
getWidgets().addAll(Arrays.asList(widgetId));
setStyleClass(styleClass);
}

public DefaultDashboardWidget(Collection<String> widgets) {
Expand Down Expand Up @@ -118,4 +131,14 @@ public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(Object value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected void renderChildren(FacesContext context, UIComponent component) throw
}
}

protected void renderChild(FacesContext context, UIComponent child) throws IOException {
protected UIComponent renderChild(FacesContext context, UIComponent child) throws IOException {
if (!child.isRendered()) {
return;
return child;
}

child.encodeBegin(context);
Expand All @@ -84,6 +84,7 @@ protected void renderChild(FacesContext context, UIComponent child) throws IOExc
renderChildren(context, child);
}
child.encodeEnd(context);
return child;
}

protected String getResourceURL(FacesContext context, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,11 @@ public static String encodeComponent(UIComponent component, FacesContext context
}

public static <T> T executeInRequestScope(FacesContext context, String var, Object value, Supplier<T> callback) {
// if no var passed just execute the callback
if (LangUtils.isBlank(var)) {
return callback.get();
}

Map<String, Object> requestMap = context.getExternalContext().getRequestMap();

Object oldValue = requestMap.remove(var);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7696,6 +7696,14 @@
<required>false</required>
<type>java.lang.Boolean</type>
</attribute>
<attribute>
<description>
<![CDATA[Name of collection iterator which will be the value of DashboardWidget.getValue().]]>
</description>
<name>var</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
</tag>
<tag>
<description>
Expand Down

0 comments on commit 0551835

Please sign in to comment.