Ok,
I've done it here. In case someone need it. I may have done an overkill to solve this, but it was the faster way I find without having to recompile the whole Primefaces.
1 - Copied both classes ConfirmBehavior and ConfirmBehaviorHandler from Primefaces to my project renaming both to CustomConfirmBehavior and CustomConfirmBehaviorHandler
In the CustomConfirmBehavior class the changes were:
Code: Select all
//changed the id
public final static String BEHAVIOR_ID = "org.primefaces.behavior.MyConfirmBehavior";
public enum PropertyKeys {
//... ,
escape(Boolean.class); // Added this attribute
// ...
}
//in the method:
@Override
public String getScript(ClientBehaviorContext behaviorContext) {
//...
String messageText = JSONObject.quote(this.getMessage());
String escape = JSONObject.quote(String.valueOf(this.isDisabled()));
if(component instanceof Confirmable) {
String sourceProperty = (source == null) ? "source:this" : "source:\"" + source + "\"";
String script = "PrimeFaces.confirm({" + sourceProperty + ",escape:" + escape + ",header:" + headerText + ",message:" + messageText + ",icon:\"" + getIcon() + "\"});return false;";
((Confirmable) component).setConfirmationScript(script);
return null;
}
else {
throw new FacesException("Component " + source + " is not a Confirmable. ConfirmBehavior can only be attached to components that implement org.primefaces.component.api.Confirmable interface");
}
//....
// Added get and set to the new attribute
public boolean isEscape() {
return eval(PropertyKeys.escape, Boolean.FALSE);
}
public void setEscape(boolean escape) {
setLiteral(PropertyKeys.escape, escape);
}
}
In the CustomConfirmBehaviorHandler class the changes were:
Code: Select all
public class CustomConfirmBehaviorHandler extends AbstractBehaviorHandler<CustomConfirmBehavior> {
//...
private final TagAttribute escape;
//added in the constructor:
this.escape = this.getAttribute(CustomConfirmBehavior.PropertyKeys.escape.name());
//added in the createBehavior method:
setBehaviorAttribute(ctx, behavior, this.escape, CustomConfirmBehavior.PropertyKeys.escape.expectedType);
Then I created a custom tag lib adding it in my web.xml
Code: Select all
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/my.taglib.xml</param-value> <!-- if you have others, separate it with ; -->
</context-param>
And the contents of my.taglib.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://primefaces.org/my</namespace>
<tag>
<tag-name>myconfirm</tag-name>
<behavior>
<behavior-id>org.primefaces.behavior.MyConfirmBehavior</behavior-id>
<handler-class>com.myproject.customcomponents.CustomConfirmBehaviorHandler</handler-class>
</behavior>
<attribute>
<description>Header text of the confirmation dialog.</description>
<name>header</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>Detail text of the confirmation dialog.</description>
<name>message</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>Icon to display inside the confirm dialog.</description>
<name>icon</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>Disables confirm behavior.</description>
<name>disabled</name>
<required>false</required>
<type>java.lang.Boolean</type>
</attribute>
<attribute>
<description>Escape the message attribute.</description>
<name>escape</name>
<required>false</required>
<type>java.lang.Boolean</type>
</attribute>
</tag>
</facelet-taglib>
And in faces-config.xml file I registered it:
Code: Select all
<!-- .... -->
</application>
<behavior>
<behavior-id>org.primefaces.behavior.MyConfirmBehavior</behavior-id>
<behavior-class>com.myproject.customcomponents.CustomConfirmBehavior</behavior-class>
</behavior>
And in order to use it in a xthml file I added my namespace:
Code: Select all
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:my="http://primefaces.org/my"
template="../templates/masterTemplate.xhtml">
<!-- .... -->
<p:commandButton action="#{bean.someAction}">
<my:myconfirm escape="false" message="#{messageBundle['some.key']}" header="some header"/>
</p:commandButton>
</ui:composition>
The last step is to override the primefaces confirmDialog javascript, In my master template I added in the
h:header section
Code: Select all
<h:outputScript name="js/my-components-override.js"/>
And the code is:
Code: Select all
/**
* PrimeFaces ConfirmDialog Widget Override to CMConfirm
* that understand the escape parameter.
*/
PrimeFaces.widget.ConfirmDialog = PrimeFaces.widget.Dialog.extend({
init: function(cfg) {
cfg.draggable = false;
cfg.resizable = false;
cfg.modal = true;
if (!cfg.appendTo && cfg.global) {
cfg.appendTo = '@(body)';
}
this._super(cfg);
this.title = this.titlebar.children('.ui-dialog-title');
this.message = this.content.children('.ui-confirm-dialog-message');
this.icon = this.content.children('.ui-confirm-dialog-severity');
if(this.cfg.global) {
PrimeFaces.confirmDialog = this;
this.jq.find('.ui-confirmdialog-yes').on('click.ui-confirmdialog', function(e) {
if(PrimeFaces.confirmSource) {
var fn = new Function('event',PrimeFaces.confirmSource.data('pfconfirmcommand'));
fn.call(PrimeFaces.confirmSource.get(0),e);
PrimeFaces.confirmDialog.hide();
PrimeFaces.confirmSource = null;
}
e.preventDefault();
});
this.jq.find('.ui-confirmdialog-no').on('click.ui-confirmdialog', function(e) {
PrimeFaces.confirmDialog.hide();
PrimeFaces.confirmSource = null;
e.preventDefault();
});
}
},
applyFocus: function() {
this.jq.find(':button,:submit').filter(':visible:enabled').eq(0).focus();
},
showMessage: function(msg) {
var icon = (msg.icon === 'null') ? 'ui-icon-alert' : msg.icon;
this.icon.removeClass().addClass('ui-icon ui-confirm-dialog-severity ' + icon);
if(msg.header)
this.title.text(msg.header);
// THE CHANGE WAS MADE HERE
if(msg.message){
if (msg.escape == "false"){
this.message.html(msg.message);
}else{
this.message.text(msg.message);
}
}
this.show();
}
});
And that's it