1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #3163 from node-red/notification-fixes

Allow RED.notify.update to modify any notification setting
This commit is contained in:
Nick O'Leary 2021-10-04 10:56:26 +01:00 committed by GitHub
commit 1419729458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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