From ed07f2f5a9402f7975e712b7e501a11ec785748a Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 24 Mar 2026 11:09:42 +0000 Subject: [PATCH] Add badges to func node tabs with code in --- .../nodes/core/function/10-function.html | 69 ++++++++++++++----- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/10-function.html b/packages/node_modules/@node-red/nodes/core/function/10-function.html index 4b63373a1..58d77c88d 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-function.html +++ b/packages/node_modules/@node-red/nodes/core/function/10-function.html @@ -351,6 +351,27 @@ return _libs; } + function getEditorValue(editor, defaultValue) { + var val = editor.getValue(); + if (defaultValue && val.trim() === defaultValue.trim()) { + return ""; + } else { + return val; + } + } + + function getEditorErrorCount(editor) { + const annot = editor.getSession().getAnnotations(); + let count = 0 + for (var k=0; k < annot.length; k++) { + if (annot[k].type === "error") { + count += annot.length; + break; + } + } + return count + } + RED.nodes.registerType('function',{ color:"#fdd0a2", category: 'function', @@ -427,6 +448,7 @@ } else if (that.finalizeEditor.getDomNode() == editor[0]) { that.finalizeEditor.focus(); } + updateTabBadges(); } } }); @@ -451,6 +473,28 @@ tabs.activateTab("func-tab-body"); + + const updateTabBadge = (tabId, editorName, defaultValue) => { + const editor = this[editorName] + const hasContent = getEditorValue(editor, defaultValue) !== '' + // This doesn't work as the annotations appear to be cleared when the editor isn't active + // TODO: add a badge for tabs with errors + // const hasErrors = getEditorErrorCount(editor) > 0 + const badgeSpan = $(`#red-ui-tab-${tabId} .red-ui-tabs-badges`) + badgeSpan.find('i').remove() + $(`#red-ui-tab-${tabId} .red-ui-tabs-badges i`).remove() + if (hasContent) { + $('').appendTo(badgeSpan) + } + + } + + const updateTabBadges = function () { + updateTabBadge("func-tab-body", "editor", "") + updateTabBadge("func-tab-init", "initEditor", RED._("node-red:function.text.initialize")) + updateTabBadge("func-tab-finalize", "finalizeEditor", RED._("node-red:function.text.finalize")) + } + $( "#node-input-outputs" ).spinner({ min: 0, max: 500, @@ -593,27 +637,18 @@ if (RED.settings.functionExternalModules !== false) { prepareLibraryConfig(that); } + + updateTabBadges(); }, oneditsave: function() { - var node = this; - var noerr = 0; + const node = this; + let noerr = 0; $("#node-input-noerr").val(0); - var disposeEditor = function(editorName,targetName,defaultValue) { - var editor = node[editorName]; - var annot = editor.getSession().getAnnotations(); - for (var k=0; k < annot.length; k++) { - if (annot[k].type === "error") { - noerr += annot.length; - break; - } - } - var val = editor.getValue(); - if (defaultValue) { - if (val.trim() == defaultValue.trim()) { - val = ""; - } - } + const disposeEditor = function(editorName,targetName,defaultValue) { + const editor = node[editorName]; + noerr += getEditorErrorCount(editor) + const val = getEditorValue(editor, defaultValue) editor.destroy(); delete node[editorName]; $("#"+targetName).val(val);