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

Reduce duplicated code in Function node html

This commit is contained in:
Nick O'Leary 2020-05-22 14:57:28 +01:00
parent a764a4a44b
commit 9512450d7c
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 85 additions and 144 deletions

View File

@ -87,6 +87,9 @@
expressionEditor.gotoLine(options.cursor.row+1,options.cursor.column,false); expressionEditor.gotoLine(options.cursor.row+1,options.cursor.column,false);
} }
dialogForm.i18n(); dialogForm.i18n();
setTimeout(function() {
expressionEditor.focus();
},300);
}, },
close: function() { close: function() {
expressionEditor.destroy(); expressionEditor.destroy();

View File

@ -109,53 +109,34 @@
} }
}); });
var globals = { var buildEditor = function(id, value, defaultValue) {
msg:true, var editor = RED.editor.createEditor({
context:true, id: id,
RED: true, mode: 'ace/mode/nrjavascript',
util: true, value: value || defaultValue || "",
flow: true, globals: {
global: true, msg:true,
console: true, context:true,
Buffer: true, RED: true,
setTimeout: true, util: true,
clearTimeout: true, flow: true,
setInterval: true, global: true,
clearInterval: true console: true,
}; Buffer: true,
this.editor = RED.editor.createEditor({ setTimeout: true,
id: 'node-input-func-editor', clearTimeout: true,
mode: 'ace/mode/nrjavascript', setInterval: true,
value: $("#node-input-func").val(), clearInterval: true
globals: globals }
}); });
var initCode = $("#node-input-initialize").val(); if (defaultValue && value === "") {
var initText = RED._("node-red:function.text.initialize"); editor.moveCursorTo(defaultValue.split("\n").length - 1, 0);
var isEmptyInitCode = (initCode === ""); }
this.initEditor = RED.editor.createEditor({ return editor;
id: 'node-input-init-editor',
mode: 'ace/mode/nrjavascript',
value: (isEmptyInitCode ? initText : initCode),
globals: globals
});
if (isEmptyInitCode) {
var count = initText.split("\n").length;
this.initEditor.moveCursorTo(count -1, 0);
}
var finalCode = $("#node-input-finalize").val();
var finalText = RED._("node-red:function.text.finalize");
var isEmptyFinalCode = (finalCode === "");
this.finalizeEditor = RED.editor.createEditor({
id: 'node-input-finalize-editor',
mode: 'ace/mode/nrjavascript',
value: (isEmptyFinalCode ? finalText : finalCode),
globals: globals
});
if (isEmptyFinalCode) {
var count = finalText.split("\n").length;
this.finalizeEditor.moveCursorTo(count -1, 0);
} }
this.initEditor = buildEditor('node-input-init-editor',$("#node-input-initialize").val(),RED._("node-red:function.text.initialize"))
this.editor = buildEditor('node-input-func-editor',$("#node-input-func").val())
this.finalizeEditor = buildEditor('node-input-finalize-editor',$("#node-input-finalize").val(),RED._("node-red:function.text.finalize"))
RED.library.create({ RED.library.create({
url:"functions", // where to get the data from url:"functions", // where to get the data from
@ -170,7 +151,7 @@
return that.initEditor.getValue(); return that.initEditor.getValue();
}, },
set: function(v) { set: function(v) {
that.initEditor.setValue(v, -1); that.initEditor.setValue(v||RED._("node-red:function.text.initialize"), -1);
} }
}, },
{ {
@ -179,7 +160,7 @@
return that.finalizeEditor.getValue(); return that.finalizeEditor.getValue();
}, },
set: function(v) { set: function(v) {
that.finalizeEditor.setValue(v, -1); that.finalizeEditor.setValue(v||RED._("node-red:function.text.finalize"), -1);
} }
} }
], ],
@ -187,113 +168,65 @@
}); });
this.editor.focus(); this.editor.focus();
var expandButtonClickHandler = function(editor) {
return function(e) {
e.preventDefault();
var value = editor.getValue();
RED.editor.editJavaScript({
value: value,
width: "Infinity",
cursor: editor.getCursorPosition(),
mode: "ace/mode/nrjavascript",
complete: function(v,cursor) {
editor.setValue(v, -1);
editor.gotoLine(cursor.row+1,cursor.column,false);
setTimeout(function() {
editor.focus();
},300);
}
})
}
}
$("#node-init-expand-js").on("click", expandButtonClickHandler(this.initEditor));
$("#node-function-expand-js").on("click", expandButtonClickHandler(this.editor));
$("#node-finalize-expand-js").on("click", expandButtonClickHandler(this.finalizeEditor));
RED.popover.tooltip($("#node-init-expand-js"), RED._("node-red:common.label.expand"));
RED.popover.tooltip($("#node-function-expand-js"), RED._("node-red:common.label.expand")); RED.popover.tooltip($("#node-function-expand-js"), RED._("node-red:common.label.expand"));
RED.popover.tooltip($("#node-finalize-expand-js"), RED._("node-red:common.label.expand"));
$("#node-function-expand-js").on("click", function(e) {
e.preventDefault();
var value = that.editor.getValue();
RED.editor.editJavaScript({
value: value,
width: "Infinity",
cursor: that.editor.getCursorPosition(),
mode: "ace/mode/nrjavascript",
complete: function(v,cursor) {
that.editor.setValue(v, -1);
that.editor.gotoLine(cursor.row+1,cursor.column,false);
setTimeout(function() {
that.editor.focus();
},300);
}
})
});
$("#node-init-expand-js").on("click", function(e) {
e.preventDefault();
var editor = that.initEditor;
var value = editor.getValue();
RED.editor.editJavaScript({
value: value,
width: "Infinity",
cursor: editor.getCursorPosition(),
mode: "ace/mode/nrjavascript",
complete: function(v,cursor) {
editor.setValue(v, -1);
editor.gotoLine(cursor.row+1,cursor.column,false);
setTimeout(function() {
editor.focus();
},300);
}
})
});
$("#node-finalize-expand-js").on("click", function(e) {
e.preventDefault();
var editor = that.finalizeEditor;
var value = editor.getValue();
RED.editor.editJavaScript({
value: value,
width: "Infinity",
cursor: editor.getCursorPosition(),
mode: "ace/mode/nrjavascript",
complete: function(v,cursor) {
editor.setValue(v, -1);
editor.gotoLine(cursor.row+1,cursor.column,false);
setTimeout(function() {
editor.focus();
},300);
}
})
});
}, },
oneditsave: function() { oneditsave: function() {
var node = this; var node = this;
var noerr = 0; var noerr = 0;
var annot = this.editor.getSession().getAnnotations();
$("#node-input-noerr").val(0); $("#node-input-noerr").val(0);
for (var k=0; k < annot.length; k++) {
//console.log(annot[k].type,":",annot[k].text, "on line", annot[k].row); var disposeEditor = function(editorName,targetName,defaultValue) {
if (annot[k].type === "error") { var editor = node[editorName];
noerr += annot.length; var annot = editor.getSession().getAnnotations();
break; for (var k=0; k < annot.length; k++) {
if (annot[k].type === "error") {
noerr += annot.length;
break;
}
} }
} var val = editor.getValue();
var annotIni = this.initEditor.getSession().getAnnotations(); if (defaultValue && val == defaultValue) {
for (var k=0; k < annotIni.length; k++) { val = "";
if (annotIni[k].type === "error") {
noerr += annotIni.length;
break;
}
}
var annotFin = this.finalizeEditor.getSession().getAnnotations();
for (var k=0; k < annotFin.length; k++) {
if (annotFin[k].type === "error") {
noerr += annotFin.length;
break;
} }
editor.destroy();
delete node[editorName];
$("#"+targetName).val(val);
} }
disposeEditor("editor","node-input-func");
disposeEditor("initEditor","node-input-initialize", RED._("node-red:function.text.initialize"));
disposeEditor("finalizeEditor","node-input-finalize", RED._("node-red:function.text.finalize"));
$("#node-input-noerr").val(noerr); $("#node-input-noerr").val(noerr);
this.noerr = noerr; this.noerr = noerr;
$("#node-input-func").val(node.editor.getValue());
node.editor.destroy();
delete node.editor;
var initCode = node.initEditor.getValue();
if (initCode === RED._("node-red:function.text.initialize")) {
initCode = "";
}
$("#node-input-initialize").val(initCode);
node.initEditor.destroy();
delete node.initEditor;
var finalCode = node.finalizeEditor.getValue();
if (finalCode === RED._("node-red:function.text.finalize")) {
finalCode = "";
}
$("#node-input-finalize").val(finalCode);
node.finalizeEditor.destroy();
delete node.finalizeEditor;
}, },
oneditcancel: function() { oneditcancel: function() {
var node = this; var node = this;
@ -320,8 +253,13 @@
var height = size.height; var height = size.height;
$("#node-input-init-editor").css("height", (height -105)+"px"); $("#node-input-init-editor").css("height", (height -105)+"px");
$("#node-input-func-editor").css("height", (height -140)+"px"); $("#node-input-func-editor").css("height", (height -145)+"px");
$("#node-input-finalize-editor").css("height", (height -105)+"px"); $("#node-input-finalize-editor").css("height", (height -105)+"px");
this.initEditor.resize();
this.editor.resize();
this.finalizeEditor.resize();
} }
}); });
</script> </script>

View File

@ -212,8 +212,8 @@
"outputs": "Outputs" "outputs": "Outputs"
}, },
"text": { "text": {
"initialize": "// Code added here will be run once whenever the node is deployed.\n", "initialize": "// Code added here will be run once\n// whenever the node is deployed.\n",
"finalize": "// Code added here will be run when the node is being stopped or re-deployed.\n" "finalize": "// Code added here will be run when the\n// node is being stopped or re-deployed.\n"
}, },
"error": { "error": {
"inputListener":"Cannot add listener to 'input' event within Function", "inputListener":"Cannot add listener to 'input' event within Function",