-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoxta-context.js
104 lines (92 loc) · 3.98 KB
/
voxta-context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module.exports = function (RED) {
function VoxtaContextNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.contexts = config.contexts || [];
node.on('input', function (msg) {
if (typeof msg.payload !== 'string') {
return;
}
let incomingText = msg.payload.trim();
let disableContext = incomingText.endsWith("!");
if (disableContext) {
incomingText = incomingText.slice(0, -1);
}
let matchingContext = node.contexts.find(ctx => ctx.Name === incomingText);
if (matchingContext) {
msg.payload = {
ContextKey: matchingContext.Name,
Contexts: [{
Name: matchingContext.Name,
Text: matchingContext.Text,
Disabled: disableContext
}]
};
if (matchingContext.SetFlags && matchingContext.SetFlags.length > 0) {
msg.payload.SetFlags = matchingContext.SetFlags;
}
node.send(msg);
}
});
node.on('close', function () {
node.contexts = [];
});
node.on('editprepare', function () {
let dropdown = $("#node-input-contextKey");
dropdown.empty();
node.contexts.forEach((context, index) => {
dropdown.append(new Option(context.Name, index));
});
dropdown.on("change", function () {
let selectedIndex = dropdown.prop("selectedIndex");
if (selectedIndex >= 0) {
$("#node-input-contextName").val(node.contexts[selectedIndex].Name);
$("#node-input-contextText").val(node.contexts[selectedIndex].Text);
$("#node-input-enabled").prop("checked", !node.contexts[selectedIndex].Disabled);
$("#node-input-setFlags").val(node.contexts[selectedIndex].SetFlags ? node.contexts[selectedIndex].SetFlags.join(', ') : "");
}
});
$("#node-input-contextName").on("input", function () {
let selectedIndex = dropdown.prop("selectedIndex");
if (selectedIndex >= 0) {
node.contexts[selectedIndex].Name = $(this).val();
}
});
$("#node-input-contextText").on("input", function () {
let selectedIndex = dropdown.prop("selectedIndex");
if (selectedIndex >= 0) {
node.contexts[selectedIndex].Text = $(this).val();
}
});
$("#node-input-enabled").on("change", function () {
let selectedIndex = dropdown.prop("selectedIndex");
if (selectedIndex >= 0) {
node.contexts[selectedIndex].Disabled = !$(this).prop("checked");
}
});
$("#node-input-setFlags").on("input", function () {
let selectedIndex = dropdown.prop("selectedIndex");
if (selectedIndex >= 0) {
node.contexts[selectedIndex].SetFlags = $(this).val().split(',').map(f => f.trim()).filter(f => f);
}
});
});
node.on('editsave', function () {
let updatedContexts = [];
$("#node-input-contextKey option").each(function (index) {
updatedContexts.push(node.contexts[index]);
});
node.contexts = updatedContexts;
node.context().contexts = updatedContexts;
node.dirty = true;
node.changed();
});
}
RED.nodes.registerType("voxta-context", VoxtaContextNode, {
category: "Voxta",
defaults: {
name: { value: "" },
contexts: { value: [] }
}
});
};