Search in sources :

Example 1 with CommandMap

use of org.apache.myfaces.tobago.internal.renderkit.CommandMap in project myfaces-tobago by apache.

the class SheetRenderer method encodeHeaderRows.

private void encodeHeaderRows(final FacesContext facesContext, final AbstractUISheet sheet, final TobagoResponseWriter writer, final List<AbstractUIColumnBase> columns) throws IOException {
    final Selectable selectable = sheet.getSelectable();
    final Grid grid = sheet.getHeaderGrid();
    final boolean autoLayout = sheet.isAutoLayout();
    final boolean multiHeader = grid.getRowCount() > 1;
    int offset = 0;
    for (int i = 0; i < grid.getRowCount(); i++) {
        writer.startElement(HtmlElements.TR);
        final AbstractUIRow row = ComponentUtils.findChild(sheet, AbstractUIRow.class);
        if (row != null) {
            writer.writeClassAttribute(row.getCustomClass());
        }
        for (int j = 0; j < columns.size(); j++) {
            final AbstractUIColumnBase column = columns.get(j);
            if (!column.isRendered() || column instanceof AbstractUIRow) {
                offset++;
            } else {
                final Cell cell = grid.getCell(j - offset, i);
                if (cell instanceof OriginCell) {
                    writer.startElement(HtmlElements.TH);
                    if (cell.getColumnSpan() > 1) {
                        writer.writeAttribute(HtmlAttributes.COLSPAN, cell.getColumnSpan());
                    }
                    if (cell.getRowSpan() > 1) {
                        writer.writeAttribute(HtmlAttributes.ROWSPAN, cell.getRowSpan());
                    }
                    final TextAlign align;
                    final UIComponent cellComponent = cell.getComponent();
                    if (multiHeader && cell.getColumnSpan() > 1) {
                        align = TextAlign.center;
                    } else if (column instanceof AbstractUIColumn) {
                        align = ((AbstractUIColumn) column).getAlign();
                    } else {
                        align = null;
                    }
                    writer.writeClassAttribute(BootstrapClass.textAlign(align), column.getCustomClass());
                    writer.startElement(HtmlElements.SPAN);
                    boolean sortable = false;
                    boolean ascending = false;
                    boolean descending = false;
                    String tip = ComponentUtils.getStringAttribute(column, Attributes.tip);
                    // sorter icons should only displayed when there is only 1 column and not input
                    CommandMap behaviorCommands = null;
                    if (cell.getColumnSpan() == 1 && cellComponent instanceof AbstractUIOut) {
                        sortable = ComponentUtils.getBooleanAttribute(column, Attributes.sortable);
                        if (sortable) {
                            AbstractUILink sortCommand = (AbstractUILink) ComponentUtils.getFacet(column, Facets.sorter);
                            if (sortCommand == null) {
                                // assign id to column
                                column.getClientId(facesContext);
                                final String sorterId = column.getId() + "_" + AbstractUISheet.SORTER_ID;
                                sortCommand = (AbstractUILink) ComponentUtils.createComponent(facesContext, Tags.link.componentType(), RendererTypes.Link, sorterId);
                                sortCommand.setTransient(true);
                                final AjaxBehavior reloadBehavior = createReloadBehavior(sheet);
                                sortCommand.addClientBehavior("click", reloadBehavior);
                                ComponentUtils.setFacet(column, Facets.sorter, sortCommand);
                            }
                            writer.writeIdAttribute(sortCommand.getClientId(facesContext));
                            behaviorCommands = getBehaviorCommands(facesContext, sortCommand);
                            ComponentUtils.removeFacet(column, Facets.sorter);
                            if (tip == null) {
                                tip = "";
                            } else {
                                tip += " - ";
                            }
                            tip += ResourceUtils.getString(facesContext, "sheet.sorting");
                            final SheetState sheetState = sheet.getSheetState(facesContext);
                            if (column.getId().equals(sheetState.getSortedColumnId())) {
                                final String sortTitle;
                                if (sheetState.isAscending()) {
                                    sortTitle = ResourceUtils.getString(facesContext, "sheet.ascending");
                                    ascending = true;
                                } else {
                                    sortTitle = ResourceUtils.getString(facesContext, "sheet.descending");
                                    descending = true;
                                }
                                tip += " - " + sortTitle;
                            }
                        }
                    }
                    writer.writeClassAttribute(sortable ? TobagoClass.SORTABLE : null, ascending ? TobagoClass.ASCENDING : null, descending ? TobagoClass.DESCENDING : null);
                    writer.writeAttribute(HtmlAttributes.TITLE, tip, true);
                    encodeBehavior(writer, behaviorCommands);
                    if (column instanceof AbstractUIColumnSelector && selectable.isMulti()) {
                        writer.startElement(HtmlElements.INPUT);
                        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX);
                        writer.writeClassAttribute(TobagoClass.SELECTED);
                        writer.writeAttribute(HtmlAttributes.TITLE, ResourceUtils.getString(facesContext, "sheet.selectAll"), true);
                        writer.endElement(HtmlElements.INPUT);
                    } else {
                        cellComponent.encodeAll(facesContext);
                    }
                    writer.endElement(HtmlElements.SPAN);
                    if (!autoLayout) {
                        if (column.isResizable()) {
                            encodeResizing(writer, sheet, j - offset + cell.getColumnSpan() - 1);
                        }
                    }
                    writer.endElement(HtmlElements.TH);
                }
            }
        }
        if (!autoLayout) {
            // Add two filler columns. The second one get the size of the scrollBar via JavaScript.
            encodeHeaderFiller(writer, sheet);
            encodeHeaderFiller(writer, sheet);
        }
        writer.endElement(HtmlElements.TR);
    }
}
Also used : AbstractUIRow(org.apache.myfaces.tobago.internal.component.AbstractUIRow) Grid(org.apache.myfaces.tobago.internal.layout.Grid) UIComponent(jakarta.faces.component.UIComponent) AbstractUIColumnSelector(org.apache.myfaces.tobago.internal.component.AbstractUIColumnSelector) AbstractUIColumn(org.apache.myfaces.tobago.internal.component.AbstractUIColumn) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) SheetState(org.apache.myfaces.tobago.model.SheetState) OriginCell(org.apache.myfaces.tobago.internal.layout.OriginCell) Selectable(org.apache.myfaces.tobago.model.Selectable) AbstractUILink(org.apache.myfaces.tobago.internal.component.AbstractUILink) AbstractUIColumnBase(org.apache.myfaces.tobago.internal.component.AbstractUIColumnBase) TextAlign(org.apache.myfaces.tobago.layout.TextAlign) AbstractUIOut(org.apache.myfaces.tobago.internal.component.AbstractUIOut) Cell(org.apache.myfaces.tobago.internal.layout.Cell) OriginCell(org.apache.myfaces.tobago.internal.layout.OriginCell) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 2 with CommandMap

use of org.apache.myfaces.tobago.internal.renderkit.CommandMap in project myfaces-tobago by apache.

the class TreeSelectRenderer method encodeBeginInternal.

@Override
public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
    final AbstractUITree tree = ComponentUtils.findAncestor(component, AbstractUITree.class);
    final AbstractUITreeNodeBase node = ComponentUtils.findAncestor(component, AbstractUITreeNodeBase.class);
    final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
    final TobagoResponseWriter writer = getResponseWriter(facesContext);
    if (data instanceof AbstractUITreeListbox) {
        writer.write(StringUtils.defaultString(component.getLabel()));
        return;
    }
    final String id = component.getClientId(facesContext);
    final String currentValue = getCurrentValue(facesContext, component);
    final boolean checked;
    if (component.isValueStoredInState()) {
        checked = data.getSelectedState().isSelected(node.getPath());
    } else {
        checked = "true".equals(currentValue);
    }
    final boolean folder = data.isFolder();
    final Selectable selectable = data.getSelectable();
    final boolean showFormCheck = component.isShowCheckbox() && selectable != Selectable.none && (!selectable.isLeafOnly() || !folder);
    writer.startElement(HtmlElements.TOBAGO_TREE_SELECT);
    writer.writeClassAttribute(component.getCustomClass(), showFormCheck ? BootstrapClass.FORM_CHECK_INLINE : null, showFormCheck && selectable.isMulti() ? BootstrapClass.FORM_CHECK : null, showFormCheck && selectable.isSingle() ? BootstrapClass.FORM_CHECK : null);
    HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
    if (showFormCheck) {
        writer.startElement(HtmlElements.INPUT);
        writer.writeClassAttribute(BootstrapClass.FORM_CHECK_INPUT);
        if (selectable.isSingle()) {
            writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO);
            writer.writeNameAttribute(getClientIdWithoutRowIndex(data, id));
        } else {
            writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX);
            writer.writeNameAttribute(id);
        }
        writer.writeAttribute(HtmlAttributes.VALUE, id, false);
        // TODO: ID must be at the outer tag
        writer.writeIdAttribute(id);
        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
        writer.endElement(HtmlElements.INPUT);
    }
    final String label = component.getLabel();
    writer.startElement(HtmlElements.LABEL);
    writer.writeClassAttribute(showFormCheck ? BootstrapClass.FORM_CHECK_LABEL : null);
    final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
    if (title != null) {
        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
    }
    writer.writeAttribute(HtmlAttributes.FOR, id, false);
    writer.writeText(label);
    writer.endElement(HtmlElements.LABEL);
    if (showFormCheck) {
        final CommandMap behaviorCommands = getBehaviorCommands(facesContext, component);
        if (behaviorCommands != null) {
            Map<ClientBehaviors, Command> other = behaviorCommands.getOther();
            if (other != null) {
                Command change = other.get(ClientBehaviors.change);
                change.setExecute(change.getExecute() + " " + tree.getBaseClientId(facesContext));
                change.setRender(change.getRender() + " " + tree.getBaseClientId(facesContext));
            }
        }
        encodeBehavior(writer, behaviorCommands);
    }
    writer.endElement(HtmlElements.TOBAGO_TREE_SELECT);
}
Also used : AbstractUIData(org.apache.myfaces.tobago.internal.component.AbstractUIData) ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) TobagoResponseWriter(org.apache.myfaces.tobago.webapp.TobagoResponseWriter) Selectable(org.apache.myfaces.tobago.model.Selectable) Command(org.apache.myfaces.tobago.internal.renderkit.Command) AbstractUITreeNodeBase(org.apache.myfaces.tobago.internal.component.AbstractUITreeNodeBase) AbstractUITreeListbox(org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox) AbstractUITree(org.apache.myfaces.tobago.internal.component.AbstractUITree) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 3 with CommandMap

use of org.apache.myfaces.tobago.internal.renderkit.CommandMap in project myfaces-tobago by apache.

the class RendererBase method encodeBehavior.

/**
 * Renders the tobago-behavior tag.
 *
 * @since 5.0
 */
protected void encodeBehavior(final TobagoResponseWriter writer, final FacesContext facesContext, final ClientBehaviorHolder holder) throws IOException {
    if (holder != null) {
        final CommandMap behaviorCommands = getBehaviorCommands(facesContext, holder);
        encodeBehavior(writer, behaviorCommands);
    }
}
Also used : CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 4 with CommandMap

use of org.apache.myfaces.tobago.internal.renderkit.CommandMap in project myfaces-tobago by apache.

the class RendererBase method encodeBehavior.

/**
 * Renders the tobago-behavior tag.
 *
 * @since 5.0
 */
protected void encodeBehavior(final TobagoResponseWriter writer, final CommandMap behaviorCommands) throws IOException {
    if (behaviorCommands != null) {
        final Command click = behaviorCommands.getClick();
        if (click != null) {
            encodeBehavior(writer, ClientBehaviors.click, click);
        }
        final Map<ClientBehaviors, Command> other = behaviorCommands.getOther();
        if (other != null) {
            for (Map.Entry<ClientBehaviors, Command> entry : other.entrySet()) {
                encodeBehavior(writer, entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map)

Example 5 with CommandMap

use of org.apache.myfaces.tobago.internal.renderkit.CommandMap in project myfaces-tobago by apache.

the class JsonUtilsUnitTest method testCommandMap.

@Test
public void testCommandMap() {
    CommandMap map = new CommandMap();
    map.addCommand(ClientBehaviors.blur, new Command("doit", null, false, "field", "execute", "render", "Do \"you\" want?", 100, new Collapse(Collapse.Operation.hide, "box"), false));
    final String expected = ("{'blur':" + "{'clientId':'doit'," + "'transition':false," + "'target':'field'," + "'execute':'execute'," + "'render':'render'," + "'collapse':{'transition':'hide','forId':'box'}," + "'confirmation':'Do \\'you\\' want?'," + "'delay':100}}").replaceAll("'", "\"");
    Assertions.assertEquals(expected, JsonUtils.encode(map), "command map");
}
Also used : Collapse(org.apache.myfaces.tobago.internal.renderkit.Collapse) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Test(org.junit.jupiter.api.Test)

Aggregations

CommandMap (org.apache.myfaces.tobago.internal.renderkit.CommandMap)15 Command (org.apache.myfaces.tobago.internal.renderkit.Command)12 AbstractUICommand (org.apache.myfaces.tobago.internal.component.AbstractUICommand)10 Test (org.junit.jupiter.api.Test)7 Map (java.util.Map)4 ClientBehaviors (org.apache.myfaces.tobago.component.ClientBehaviors)4 EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)3 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)3 Collapse (org.apache.myfaces.tobago.internal.renderkit.Collapse)3 UIComponent (jakarta.faces.component.UIComponent)2 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)2 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 List (java.util.List)2 Selectable (org.apache.myfaces.tobago.model.Selectable)2 FacesContext (jakarta.faces.context.FacesContext)1 SupportFieldId (org.apache.myfaces.tobago.component.SupportFieldId)1 AbstractUIColumn (org.apache.myfaces.tobago.internal.component.AbstractUIColumn)1 AbstractUIColumnBase (org.apache.myfaces.tobago.internal.component.AbstractUIColumnBase)1 AbstractUIColumnSelector (org.apache.myfaces.tobago.internal.component.AbstractUIColumnSelector)1