All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.primefaces.mobile.renderkit.BaseMenuRenderer Maven / Gradle / Ivy

There is a newer version: 14.0.0-RC3
Show newest version
/*
 * Copyright 2009-2014 PrimeTek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.mobile.renderkit;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.primefaces.component.api.AjaxSource;
import org.primefaces.component.api.UIOutcomeTarget;
import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.mobile.util.MobileUtils;
import org.primefaces.model.menu.MenuItem;
import org.primefaces.util.ComponentTraversalUtils;
import org.primefaces.util.ComponentUtils;

public abstract class BaseMenuRenderer extends org.primefaces.component.menu.BaseMenuRenderer {
     
    @Override
    protected void encodeMenuItem(FacesContext context, AbstractMenu menu, MenuItem menuitem) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String title = menuitem.getTitle();
		boolean disabled = menuitem.isDisabled();
        String styleClass = getLinkStyleClass(menuitem);
        if(disabled) {
            styleClass = styleClass + " ui-state-disabled";
        }
            
        writer.startElement("a", null);
        writer.writeAttribute("tabindex", "-1", null);
        if(shouldRenderId(menuitem)) {
            writer.writeAttribute("id", menuitem.getClientId(), null);
        }
        if(title != null) {
            writer.writeAttribute("title", title, null);
        }

        writer.writeAttribute("class", styleClass, null);

        if(menuitem.getStyle() != null) {
            writer.writeAttribute("style", menuitem.getStyle(), null);
        }

        if(disabled) {
            writer.writeAttribute("href", "#", null);
            writer.writeAttribute("onclick", "return false;", null);
        }
        else {
            String url = menuitem.getUrl();
            String outcome = menuitem.getOutcome();
            String onclick = menuitem.getOnclick();

            //GET
            if(url != null || outcome != null) {
                if(outcome != null && outcome.startsWith("pm:")) {
                    String command = MobileUtils.buildNavigation(outcome) + "return false;";
                    onclick = (onclick == null) ? command : onclick + ";" + command;
                    writer.writeAttribute("href", "#", null);
                }
                else {
                    String targetURL = getTargetURL(context, (UIOutcomeTarget) menuitem);
                    writer.writeAttribute("href", targetURL, null);

                    if(menuitem.getTarget() != null) {
                        writer.writeAttribute("target", menuitem.getTarget(), null);
                    }
                }
            }
            //POST
            else {
                writer.writeAttribute("href", "#", null);

                UIComponent form = ComponentTraversalUtils.closestForm(context, menu);
                if(form == null) {
                    throw new FacesException("MenuItem must be inside a form element.");
                }

                String command;
                if(menuitem.isDynamic()) {
                    String menuClientId = menu.getClientId(context);
                    Map> params = menuitem.getParams();
                    if(params == null) {
                        params = new LinkedHashMap>();
                    }
                    List idParams = new ArrayList();
                    idParams.add(menuitem.getId());
                    params.put(menuClientId + "_menuid", idParams);

                    command = menuitem.isAjax() ? buildAjaxRequest(context, menu, (AjaxSource) menuitem, form, params) : buildNonAjaxRequest(context, menu, form, menuClientId, params, true);
                } 
                else {
                    command = menuitem.isAjax() ? buildAjaxRequest(context, (AjaxSource) menuitem, form) : buildNonAjaxRequest(context, ((UIComponent) menuitem), form, ((UIComponent) menuitem).getClientId(context), true);
                }

                onclick = (onclick == null) ? command : onclick + ";" + command;
            }

            if(onclick != null) {
                writer.writeAttribute("onclick", onclick, null);
            }
        }

        Object value = menuitem.getValue();
        if(value != null) {
            writer.writeText(value, null);
        }

        writer.endElement("a");  
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy