I was looking for the same functionality (to set the growl to sticky from a specific message severity). PrimeFaces (6.1) doesn't offer this functionality, but it is quite easy to hack the growl JavaScript. More specifically, in the bindEvents
function they check if sticky
was configured and based on that:
//hide the message after given time if not sticky
if(!sticky) {
this.setRemovalTimeout(message);
}
So, you could prototype (override) the bindEvents
function and set sticky
based on the message severity.
PrimeFaces.widget.Growl.prototype.bindEvents = function(message) {
var _self = this,
sticky = this.cfg.sticky;
// Start hack
if (!sticky) {
// Your rule
}
...
You could also prototype renderMessage
and add severity as a parameter to bindEvents
. I chose to used a quick hack and read it from the className
.
I've added these utility functions:
var SEVERITIES = [ "info", "warn", "error", "fatal" ];
function getSeverity(domNode) {
// HACK Severity can be found in the className after the last - character.
var severity = domNode.className;
return severity.substring(severity.lastIndexOf("-") + 1);
}
function getSeverityIndex(severityString) {
return SEVERITIES.indexOf(severityString);
}
Now you can use the following check:
if (!sticky) {
sticky = getSeverityIndex(getSeverity(message[0])) >= getSeverityIndex("error");
}
I might create a pull request at GitHub where you can set the minimum severity to stick massages using the stickSeverity
attribute on the p:growl
component.
Here is the full JavaScript hack (PrimeFaces 6.1):
var SEVERITIES = [ "info", "warn", "error", "fatal" ];
function getSeverity(domNode) {
// HACK Severity can be found in the className after the last - character.
var severity = domNode.className;
return severity.substring(severity.lastIndexOf("-") + 1);
}
function getSeverityIndex(severityString) {
return SEVERITIES.indexOf(severityString);
}
PrimeFaces.widget.Growl.prototype.bindEvents = function(message) {
var _self = this,
sticky = this.cfg.sticky;
// Start customization
if (!sticky) {
sticky = getSeverityIndex(getSeverity(message[0])) >= getSeverityIndex("error");
}
// End customization
message.mouseover(function() {
var msg = $(this);
//visuals
if(!msg.is(':animated')) {
msg.find('div.ui-growl-icon-close:first').show();
}
})
.mouseout(function() {
//visuals
$(this).find('div.ui-growl-icon-close:first').hide();
});
//remove message on click of close icon
message.find('div.ui-growl-icon-close').click(function() {
_self.removeMessage(message);
//clear timeout if removed manually
if(!sticky) {
clearTimeout(message.data('timeout'));
}
});
//hide the message after given time if not sticky
if(!sticky) {
this.setRemovalTimeout(message);
}
}