node-red/packages/node_modules/@node-red/nodes/core/function/80-template.html

160 lines
7.1 KiB
HTML
Raw Normal View History

2013-09-05 16:02:48 +02:00
<script type="text/html" data-template-name="template">
2013-09-05 16:02:48 +02:00
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<div style="display: inline-block; width: calc(100% - 105px)"><input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"></div>
2013-09-05 16:02:48 +02:00
</div>
2016-01-04 16:03:33 +01:00
<div class="form-row">
<label for="node-input-field"><i class="fa fa-ellipsis-h"></i> <span data-i18n="template.label.property"></span></label>
2016-01-04 16:03:33 +01:00
<input type="text" id="node-input-field" placeholder="payload" style="width:250px;">
<input type="hidden" id="node-input-fieldType">
</div>
<div class="form-row" style="position: relative; margin-bottom: 0px;">
2015-05-26 22:11:14 +02:00
<label for="node-input-template"><i class="fa fa-file-code-o"></i> <span data-i18n="template.label.template"></span></label>
2013-10-09 23:12:06 +02:00
<input type="hidden" id="node-input-template" autofocus="autofocus">
<div style="position: absolute; right:0;display:inline-block; text-align: right; font-size: 0.8em;">
2016-01-04 16:03:33 +01:00
<span data-i18n="template.label.format"></span>:
<select id="node-input-format" style="width:110px; font-size: 10px !important; height: 24px; padding:0;">
2016-01-04 16:03:33 +01:00
<option value="handlebars">mustache</option>
<option value="html">HTML</option>
<option value="json">JSON</option>
<option value="javascript">JavaScript</option>
<option value="css">CSS</option>
2016-01-04 16:03:33 +01:00
<option value="markdown">Markdown</option>
<option value="python">Python</option>
<option value="sql">SQL</option>
<option value="yaml">YAML</option>
<option value="text" data-i18n="template.label.none"></option>
2016-01-04 16:03:33 +01:00
</select>
<button id="node-template-expand-editor" class="red-ui-button red-ui-button-small"><i class="fa fa-expand"></i></button>
2016-01-04 16:03:33 +01:00
</div>
2014-11-13 18:21:12 +01:00
</div>
<div class="form-row node-text-editor-row">
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-template-editor" ></div>
2013-09-05 16:02:48 +02:00
</div>
<div class="form-row">
<label for="node-input-syntax"><i class="fa fa-code"></i> <span data-i18n="template.label.syntax"></span></label>
<select id="node-input-syntax" style="width:180px;">
<option value="mustache" data-i18n="template.label.mustache"></option>
<option value="plain" data-i18n="template.label.plain"></option>
</select>
</div>
<div class="form-row">
<label for="node-input-output"><i class="fa fa-long-arrow-right"></i> <span data-i18n="template.label.output"></span></label>
<select id="node-input-output" style="width:180px;">
<option value="str" data-i18n="template.label.plain"></option>
<option value="json" data-i18n="template.label.json"></option>
<option value="yaml" data-i18n="template.label.yaml"></option>
</select>
</div>
2013-09-05 16:02:48 +02:00
</script>
<script type="text/javascript">
RED.nodes.registerType('template',{
color:"rgb(243, 181, 103)",
category: 'function',
defaults: {
name: {value:""},
field: {value:"payload",
label:"payload",
validate:RED.validators.typedInput("fieldType", false)},
2016-01-04 16:03:33 +01:00
fieldType: {value:"msg"},
format: {value:"handlebars"},
syntax: {value:"mustache"},
2015-02-13 01:15:07 +01:00
template: {value:"This is the payload: {{payload}} !"},
output: {value:"str"}
2013-09-05 16:02:48 +02:00
},
inputs:1,
outputs:1,
icon: "template.svg",
2013-09-05 16:02:48 +02:00
label: function() {
return this.name||this._("template.template");;
2013-09-05 16:02:48 +02:00
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
2013-09-05 16:02:48 +02:00
oneditprepare: function() {
const that = this;
const stateId = RED.editor.generateViewStateId("node", this, "");
if (!this.field) {
this.field = 'payload';
$("#node-input-field").val("payload");
}
2016-01-04 16:03:33 +01:00
if (!this.fieldType) {
this.fieldType = 'msg';
}
if (!this.syntax) {
this.syntax = 'mustache';
$("#node-input-syntax").val(this.syntax);
}
2016-01-04 16:03:33 +01:00
$("#node-input-field").typedInput({
default: 'msg',
2016-01-04 16:11:24 +01:00
types: ['msg','flow','global'],
2016-01-04 16:03:33 +01:00
typeField: $("#node-input-fieldType")
});
this.editor = RED.editor.createEditor({
id: 'node-input-template-editor',
mode: 'ace/mode/html',
stateId: stateId,
value: $("#node-input-template").val()
});
2015-02-13 01:15:07 +01:00
RED.library.create({
url:"templates", // where to get the data from
type:"template", // the type of object the library is for
2015-02-13 01:15:07 +01:00
editor:that.editor, // the field name the main text body goes to
fields:['name','format','output','syntax'],
ext: "txt"
2015-02-13 01:15:07 +01:00
});
2016-01-04 16:03:33 +01:00
$("#node-input-format").on("change", function() {
var mod = "ace/mode/"+$("#node-input-format").val();
2015-02-13 01:15:07 +01:00
that.editor.getSession().setMode({
2017-04-24 21:36:27 +02:00
path: mod,
v: Date.now()
});
2013-09-05 16:02:48 +02:00
});
RED.popover.tooltip($("#node-template-expand-editor"), RED._("node-red:common.label.expand"));
$("#node-template-expand-editor").on("click", function (e) {
e.preventDefault();
const value = that.editor.getValue();
that.editor.saveView();
RED.editor.editText({
mode: $("#node-input-format").val(),
value: value,
stateId: stateId,
width: "Infinity",
focus: true,
complete: function (v, cursor) {
that.editor.setValue(v, -1);
setTimeout(function () {
that.editor.restoreView();
that.editor.focus();
}, 250);
}
})
})
2013-09-05 16:02:48 +02:00
},
oneditsave: function() {
$("#node-input-template").val(this.editor.getValue());
this.editor.destroy();
2013-09-05 16:02:48 +02:00
delete this.editor;
2016-01-04 17:53:32 +01:00
},
oneditcancel: function() {
this.editor.destroy();
delete this.editor;
},
2016-01-04 17:53:32 +01:00
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-text-editor-row)");
var height = $("#dialog-form").height();
for (var i=0; i<rows.length; i++) {
2016-01-04 17:53:32 +01:00
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-text-editor-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
$(".node-text-editor").css("height",height+"px");
this.editor.resize();
2013-09-05 16:02:48 +02:00
}
});
</script>