-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxy-sequencer.html
198 lines (174 loc) · 8.68 KB
/
noxy-sequencer.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<script type="text/javascript">
console.log('Script loaded!');
RED.nodes.registerType('noxy-sequencer', {
category: 'Voxta',
color: '#ff6961',
defaults: {
name: { value: '' },
sequence: { value: [] },
outputs: { value: 1 },
outputLabels: { value: [] }
},
inputs: 1,
outputs: 1, // Keep a single output port
icon: 'noxyred.png',
label: function () {
return this.name || 'Noxy Sequencer';
},
oneditprepare: function () {
const node = this;
const $sequenceTable = $('#node-input-sequenceTable');
function createElement(entry, sequenceIndex) {
const $row = $('<div></div>')
.addClass('sequence-element')
.css('display', 'flex')
.css('align-items', 'center')
.css('margin-bottom', '10px');
// Drag handle for rearranging elements
const $dragHandle = $('<div class="drag-handle" style="cursor: grab; margin-right: 10px;">☰</div>');
// Dropdown for type (Output or Delay)
const $typeDropdown = $('<select class="sequence-type" style="width: 70px; margin-right: 10px;"></select>')
.append('<option value="output">Output</option>')
.append('<option value="delay">Delay</option>')
.val(entry.type || 'output')
.on('change', function () {
$outputValueField.toggle($(this).val() === 'output');
$delayFields.toggle($(this).val() === 'delay');
});
// Dropdown for output type
const $outputTypeDropdown = $('<select class="output-type" style="width: 70px; margin-right: 10px;"></select>')
.append('<option value="string">String</option>')
.append('<option value="boolean">Boolean</option>')
.append('<option value="number">Number</option>')
.append('<option value="json">JSON</option>')
.val(entry.outputType || 'string')
.toggle(entry.type === 'output');
// Field for output value
const $outputValueField = $('<input type="text" class="sequence-value" style="flex: 1; margin-right: 10px;" placeholder="Output Value">')
.val(entry.value || '')
.toggle(entry.type === 'output');
// Delay configuration
const $delayFields = $('<div class="delay-fields" style="display: flex; align-items: center; margin-right: 10px;"></div>')
.append('<span>From:</span>')
.append($('<input type="number" class="delay-from" style="width: 100px; margin-left: 5px; margin-right: 5px;">').val(entry.from || ''))
.append('<span>To:</span>')
.append($('<input type="number" class="delay-to" style="width: 100px; margin-left: 5px; margin-right: 5px;">').val(entry.to || ''))
.append(
$('<select class="delay-unit" style="width: 50px;"></select>')
.append('<option value="ms">ms</option>')
.append('<option value="seconds">s</option>')
.append('<option value="minutes">m</option>')
.val(entry.unit || 'ms')
)
.toggle(entry.type === 'delay');
// Remove button
const $removeButton = $('<button class="delete-element" style="margin-left: 10px;">Remove</button>').on('click', function () {
$row.remove();
});
$row.append($dragHandle)
.append($typeDropdown)
.append($outputTypeDropdown)
.append($outputValueField)
.append($delayFields)
.append($removeButton);
return $row;
}
function populateTable() {
$sequenceTable.empty();
(node.sequence || []).forEach((entry, sequenceIndex) => {
$sequenceTable.append(createElement(entry, sequenceIndex));
});
makeTableSortable();
}
function makeTableSortable() {
$sequenceTable.sortable({
handle: '.drag-handle',
update: function () {
saveSequence();
}
});
}
function saveSequence() {
const newSequence = [];
$sequenceTable.find('.sequence-element').each(function () {
const $row = $(this);
const type = $row.find('.sequence-type').val();
if (type === 'output') {
const value = $row.find('.sequence-value').val();
newSequence.push({
type: 'output',
outputType: $row.find('.output-type').val(),
value: value
});
} else if (type === 'delay') {
newSequence.push({
type: 'delay',
from: $row.find('.delay-from').val(),
to: $row.find('.delay-to').val(),
unit: $row.find('.delay-unit').val()
});
}
});
node.sequence = newSequence;
// Mark the node as dirty and trigger save
RED.events.emit('nodes:change', node);
RED.nodes.dirty(true);
RED.editor.validateNode(node);
}
// Add event listeners
$('#add-output-button').on('click', function () {
const newEntry = { type: 'output', value: '', outputType: 'string' };
$sequenceTable.append(createElement(newEntry, $sequenceTable.children().length));
saveSequence();
});
$('#add-delay-button').on('click', function () {
const newEntry = { type: 'delay', from: '', to: '', unit: 'ms' };
$sequenceTable.append(createElement(newEntry, $sequenceTable.children().length));
saveSequence();
});
$('#save-button').on('click', function () {
saveSequence();
});
$('#refresh-button').on('click', function () {
// Make sure to refresh by using the existing data in `node.sequence`
populateTable();
});
// Populate table on load
populateTable();
}
});
</script>
<script type="text/html" data-template-name="noxy-sequencer">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<table id="node-input-sequenceTable" style="width: 100%; border: 1px solid #ddd; border-spacing: 0;"></table>
<div style="margin-top: 10px;">
<button id="add-output-button" style="margin-right: 10px;">Add Output</button>
<button id="add-delay-button" style="margin-right: 10px;">Add Delay</button>
<button id="save-button" style="margin-right: 10px;">Save</button>
<button id="refresh-button">Refresh</button>
</div>
</div>
</script>
<script type="text/html" data-template-name="noxy-sequencer">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<table id="node-input-sequenceTable" style="width: 100%; border: 1px solid #ddd; border-spacing: 0;"></table>
<div style="margin-top: 10px;">
<button id="add-output-button" style="margin-right: 10px;">Add Output</button>
<button id="add-delay-button" style="margin-right: 10px;">Add Delay</button>
<button id="save-button" style="margin-right: 10px;">Save</button>
<button id="refresh-button">Refresh</button>
</div>
</div>
</script>
<script type="text/html" data-help-name="noxy-sequencer">
<p>The Noxy Sequencer node allows you to set a sequence of outputs and delays.</p>
<p>Each sequence item can either be an "Output" with a message or a "Delay" that waits before executing the next item.</p>
</script>