org.primefaces.component.button.ButtonRenderer Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.button;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.primefaces.renderkit.OutcomeTargetRenderer;
import org.primefaces.util.EscapeUtils;
import org.primefaces.util.HTML;
import org.primefaces.util.SharedStringBuilder;
import org.primefaces.util.WidgetBuilder;
public class ButtonRenderer extends OutcomeTargetRenderer {
private static final String SB_BUILD_ONCLICK = ButtonRenderer.class.getName() + "#buildOnclick";
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
Button button = (Button) component;
encodeMarkup(context, button);
encodeScript(context, button);
}
public void encodeMarkup(FacesContext context, Button button) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = button.getClientId(context);
Object value = button.getValue();
String icon = button.getIcon();
String title = button.getTitle();
writer.startElement("button", button);
writer.writeAttribute("id", clientId, "id");
writer.writeAttribute("name", clientId, "name");
writer.writeAttribute("type", "button", null);
writer.writeAttribute("class", button.resolveStyleClass(), "styleClass");
renderPassThruAttributes(context, button, HTML.BUTTON_WITHOUT_CLICK_ATTRS);
if (button.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
writer.writeAttribute("onclick", buildOnclick(context, button), null);
//icon
if (!isValueBlank(icon)) {
String defaultIconClass = button.getIconPos().equals("left") ? HTML.BUTTON_LEFT_ICON_CLASS : HTML.BUTTON_RIGHT_ICON_CLASS;
String iconClass = defaultIconClass + " " + icon;
writer.startElement("span", null);
writer.writeAttribute("class", iconClass, null);
writer.endElement("span");
}
//text
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_TEXT_CLASS, null);
renderButtonValue(writer, button.isEscape(), value, title, button.getAriaLabel());
writer.endElement("span");
writer.endElement("button");
}
public void encodeScript(FacesContext context, Button button) throws IOException {
WidgetBuilder wb = getWidgetBuilder(context);
wb.init("Button", button);
wb.finish();
}
protected String buildOnclick(FacesContext context, Button button) {
String userOnclick = button.getOnclick();
StringBuilder onclick = SharedStringBuilder.get(context, SB_BUILD_ONCLICK);
String targetURL = getTargetURL(context, button);
if (userOnclick != null) {
onclick.append(userOnclick).append(";");
}
String onclickBehaviors = getEventBehaviors(context, button, "click", null);
if (onclickBehaviors != null) {
onclick.append(onclickBehaviors).append(";");
}
if (targetURL != null) {
onclick.append("window.open('").append(EscapeUtils.forJavaScript(targetURL)).append("','");
onclick.append(EscapeUtils.forJavaScript(button.getTarget())).append("')");
}
return onclick.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy