Allow RED.notify.update to modify any notification setting

Fixes #3162
This commit is contained in:
Nick O'Leary 2021-10-04 10:31:46 +01:00
parent 061afb3a94
commit 7544241316
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
1 changed files with 60 additions and 17 deletions

View File

@ -45,6 +45,22 @@ RED.notifications = (function() {
var persistentNotifications = {};
var shade = (function() {
var shadeUsers = 0;
return {
show: function() {
shadeUsers++;
$("#red-ui-full-shade").show();
},
hide: function() {
shadeUsers--;
if (shadeUsers === 0) {
$("#red-ui-full-shade").hide();
}
}
}
})();
var currentNotifications = [];
var c = 0;
function notify(msg,type,fixed,timeout) {
@ -54,6 +70,10 @@ RED.notifications = (function() {
fixed = options.fixed;
timeout = options.timeout;
type = options.type;
} else {
options.type = type;
options.fixed = fixed;
options.timeout = options.timeout;
}
if (options.id && persistentNotifications.hasOwnProperty(options.id)) {
@ -62,7 +82,7 @@ RED.notifications = (function() {
}
if (options.modal) {
$("#red-ui-full-shade").show();
shade.show();
}
if (currentNotifications.length > 4) {
@ -79,6 +99,8 @@ RED.notifications = (function() {
var n = document.createElement("div");
n.id="red-ui-notification-"+c;
n.className = "red-ui-notification";
n.options = options;
n.fixed = fixed;
if (type) {
n.className = "red-ui-notification red-ui-notification-"+type;
@ -115,7 +137,6 @@ RED.notifications = (function() {
})
}
$("#red-ui-notifications").append(n);
if (!RED.notifications.hide) {
$(n).slideDown(300);
@ -141,8 +162,8 @@ RED.notifications = (function() {
} else {
nn.parentNode.removeChild(nn);
}
if (options.modal) {
$("#red-ui-full-shade").hide();
if (nn.options.modal) {
shade.hide();
}
};
})();
@ -173,7 +194,7 @@ RED.notifications = (function() {
n.update = (function() {
var nn = n;
return function(msg,options) {
return function(msg,newOptions) {
if (typeof msg === "string") {
if (!/<p>/i.test(msg)) {
msg = "<p>"+msg+"</p>";
@ -182,16 +203,31 @@ RED.notifications = (function() {
} else {
$(nn).empty().append(msg);
}
var timeout;
if (typeof options === 'number') {
timeout = options;
} else if (options !== undefined) {
if (!options.fixed) {
timeout = options.timeout || 5000;
var newTimeout;
if (typeof newOptions === 'number') {
newTimeout = newOptions;
nn.options.timeout = newTimeout;
} else if (newOptions !== undefined) {
if (!options.modal && newOptions.modal) {
nn.options.modal = true;
shade.show();
} else if (options.modal && newOptions.modal === false) {
nn.options.modal = false;
shade.hide();
}
if (options.buttons) {
var newType = newOptions.hasOwnProperty('type')?newOptions.type:type;
if (newType) {
n.className = "red-ui-notification red-ui-notification-"+newType;
}
if (!newOptions.fixed) {
newTimeout = (newOptions.hasOwnProperty('timeout')?newOptions.timeout:timeout)||5000;
}
if (newOptions.buttons) {
var buttonSet = $('<div style="margin-top: 20px;" class="ui-dialog-buttonset"></div>').appendTo(nn)
options.buttons.forEach(function(buttonDef) {
newOptions.buttons.forEach(function(buttonDef) {
var b = $('<button>').text(buttonDef.text).on("click", buttonDef.click).appendTo(buttonSet);
if (buttonDef.id) {
b.attr('id',buttonDef.id);
@ -202,15 +238,22 @@ RED.notifications = (function() {
})
}
}
if (timeout !== undefined && timeout > 0) {
$(nn).off("click.red-ui-notification-close");
if (newTimeout !== undefined && newTimeout > 0) {
window.clearTimeout(nn.timeoutid);
nn.timeoutid = window.setTimeout(nn.close,timeout);
nn.timeoutid = window.setTimeout(nn.close,newTimeout);
setTimeout(function() {
$(nn).on("click.red-ui-notification-close", function() {
nn.close();
window.clearTimeout(nn.timeoutid);
});
},50);
} else {
window.clearTimeout(nn.timeoutid);
}
if (nn.hidden) {
nn.showNotification();
} else if (!options || !options.silent){
} else if (!newOptions || !newOptions.silent){
$(nn).addClass("red-ui-notification-shake-horizontal");
setTimeout(function() {
$(nn).removeClass("red-ui-notification-shake-horizontal");
@ -221,7 +264,7 @@ RED.notifications = (function() {
})();
if (!fixed) {
$(n).on("click", (function() {
$(n).on("click.red-ui-notification-close", (function() {
var nn = n;
return function() {
nn.close();