-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
452 lines (414 loc) · 16.5 KB
/
Copy pathapp.js
File metadata and controls
452 lines (414 loc) · 16.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// n8n Preview Mode Test Application
class PreviewModeTestApp {
constructor() {
this.iframe = null;
this.isIframeReady = false;
this.logs = [];
this.currentOrigin = 'http://localhost:5678';
this.initializeElements();
this.bindEvents();
this.setupMessageListener();
this.log('Application initialized');
}
initializeElements() {
this.iframe = document.getElementById('n8nIframe');
this.status = document.getElementById('status');
this.renderButton = document.getElementById('renderWorkflow');
this.clearButton = document.getElementById('clearWorkflow');
this.refreshButton = document.getElementById('refreshIframe');
this.toggleLogsButton = document.getElementById('toggleLogs');
this.originInput = document.getElementById('originInput');
this.workflowTextarea = document.getElementById('workflowTextarea');
this.exampleSelect = document.getElementById('exampleSelect');
this.loadExampleButton = document.getElementById('loadExample');
this.logsContainer = document.getElementById('logs');
this.logEntries = document.getElementById('logEntries');
}
bindEvents() {
this.renderButton.addEventListener('click', () => this.renderWorkflow());
this.clearButton.addEventListener('click', () => this.clearWorkflow());
this.refreshButton.addEventListener('click', () => this.refreshIframe());
this.toggleLogsButton.addEventListener('click', () => this.toggleLogs());
this.originInput.addEventListener('change', () => this.updateOrigin());
this.loadExampleButton.addEventListener('click', () => this.loadExampleWorkflow());
this.iframe.addEventListener('load', () => {
this.log('Iframe loaded, waiting for n8n ready signal...');
this.updateStatus('loading', 'Iframe loaded, waiting for n8n ready signal...');
});
}
setupMessageListener() {
window.addEventListener('message', (event) => {
// Only accept messages from the configured n8n origin
if (event.origin !== this.currentOrigin) {
return;
}
try {
const data = JSON.parse(event.data);
this.log(`Received message: ${event.data}`);
if (data.command === 'n8nReady') {
this.isIframeReady = true;
this.updateStatus('ready', `n8n Demo ready (version: ${data.version || 'unknown'})`);
this.renderButton.disabled = false;
this.clearButton.disabled = false;
this.log('n8n iframe is ready for commands');
} else if (data.command === 'error') {
this.updateStatus('error', `Error: ${data.message}`);
this.log(`Error from iframe: ${data.message}`);
} else if (data.command === 'openNDV') {
this.log('Node Details View opened');
} else if (data.command === 'closeNDV') {
this.log('Node Details View closed');
}
} catch (error) {
this.log(`Failed to parse message: ${error.message}`);
}
});
}
renderWorkflow() {
if (!this.isIframeReady) {
this.log('Cannot render workflow: iframe not ready');
return;
}
const workflowText = this.workflowTextarea.value.trim();
if (!workflowText) {
this.log('No workflow JSON provided');
this.updateStatus('error', 'Please paste a workflow JSON in the textarea');
return;
}
try {
const workflow = JSON.parse(workflowText);
this.sendWorkflowToIframe(workflow);
} catch (error) {
this.log(`Failed to parse workflow JSON: ${error.message}`);
this.updateStatus('error', `Invalid JSON: ${error.message}`);
}
}
updateOrigin() {
const newOrigin = this.originInput.value.trim();
if (!newOrigin) {
this.log('Origin cannot be empty');
return;
}
// Validate URL format
try {
new URL(newOrigin);
this.currentOrigin = newOrigin;
this.log(`Origin updated to: ${newOrigin}`);
// Update iframe src to match new origin
const currentPath = new URL(this.iframe.src).pathname + new URL(this.iframe.src).search;
this.iframe.src = newOrigin + currentPath;
// Reset ready state
this.isIframeReady = false;
this.renderButton.disabled = true;
this.clearButton.disabled = true;
this.updateStatus('loading', 'Updating origin and reloading iframe...');
} catch (error) {
this.log(`Invalid origin URL: ${error.message}`);
this.updateStatus('error', `Invalid origin URL: ${error.message}`);
}
}
loadExampleWorkflow() {
const selectedTemplate = this.exampleSelect.value;
if (!selectedTemplate) {
this.log('No example workflow selected');
return;
}
const workflow = this.getWorkflowTemplate(selectedTemplate);
if (!workflow) {
this.log('Example workflow not found');
return;
}
this.workflowTextarea.value = JSON.stringify(workflow, null, 2);
this.log(`Loaded example workflow: ${workflow.name}`);
}
sendWorkflowToIframe(workflow) {
const message = {
command: 'openWorkflow',
workflow: workflow,
canOpenNDV: true,
hideNodeIssues: false,
projectId: null
};
this.log(`Sending workflow to iframe: ${workflow.name}`);
this.iframe.contentWindow.postMessage(JSON.stringify(message), this.currentOrigin);
}
clearWorkflow() {
if (!this.isIframeReady) {
this.log('Cannot clear workflow: iframe not ready');
return;
}
const emptyWorkflow = {
name: 'Empty Workflow',
nodes: [],
connections: {},
active: false,
settings: {},
tags: []
};
this.sendWorkflowToIframe(emptyWorkflow);
this.log('Sent empty workflow to clear canvas');
}
refreshIframe() {
this.isIframeReady = false;
this.renderButton.disabled = true;
this.clearButton.disabled = true;
this.updateStatus('loading', 'Refreshing iframe...');
this.log('Refreshing iframe...');
this.iframe.src = this.iframe.src;
}
toggleLogs() {
const isVisible = this.logsContainer.style.display !== 'none';
this.logsContainer.style.display = isVisible ? 'none' : 'block';
this.toggleLogsButton.textContent = isVisible ? 'Show Logs' : 'Hide Logs';
}
updateStatus(type, message) {
this.status.className = `status ${type}`;
this.status.textContent = message;
}
log(message) {
const timestamp = new Date().toLocaleTimeString();
this.logs.push({ timestamp, message });
const logEntry = document.createElement('div');
logEntry.className = 'log-entry';
logEntry.innerHTML = `<span class="log-timestamp">[${timestamp}]</span> ${message}`;
this.logEntries.appendChild(logEntry);
this.logEntries.scrollTop = this.logEntries.scrollHeight;
// Keep only last 100 log entries
if (this.logEntries.children.length > 100) {
this.logEntries.removeChild(this.logEntries.firstChild);
}
console.log(`[Preview Test] ${message}`);
}
getWorkflowTemplate(template) {
const templates = {
simple: {
name: 'Simple HTTP Request',
nodes: [
{
id: 'start-node',
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [100, 200]
},
{
id: 'http-node',
name: 'HTTP Request',
type: 'n8n-nodes-base.httpRequest',
typeVersion: 4.2,
position: [300, 200],
parameters: {
url: 'https://jsonplaceholder.typicode.com/posts/1',
options: {}
}
}
],
connections: {
'Start': {
main: [
[
{
node: 'HTTP Request',
type: 'main',
index: 0
}
]
]
}
},
active: false,
settings: {},
tags: []
},
webhook: {
name: 'Webhook + Set Workflow',
nodes: [
{
id: 'webhook-node',
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
typeVersion: 2,
position: [100, 200],
parameters: {
path: 'test-webhook',
responseMode: 'onReceived',
options: {}
}
},
{
id: 'set-node',
name: 'Set',
type: 'n8n-nodes-base.set',
typeVersion: 3.4,
position: [300, 200],
parameters: {
assignments: {
assignments: [
{
id: '1',
name: 'message',
value: 'Hello from n8n preview!',
type: 'string'
},
{
id: '2',
name: 'timestamp',
value: '={{ new Date().toISOString() }}',
type: 'string'
}
]
},
options: {}
}
}
],
connections: {
'Webhook': {
main: [
[
{
node: 'Set',
type: 'main',
index: 0
}
]
]
}
},
active: false,
settings: {},
tags: []
},
schedule: {
name: 'Schedule + Email Workflow',
nodes: [
{
id: 'schedule-node',
name: 'Schedule Trigger',
type: 'n8n-nodes-base.scheduleTrigger',
typeVersion: 1.2,
position: [100, 200],
parameters: {
rule: {
interval: [
{
field: 'hours',
value: 1
}
]
}
}
},
{
id: 'set-node',
name: 'Prepare Data',
type: 'n8n-nodes-base.set',
typeVersion: 3.4,
position: [300, 200],
parameters: {
assignments: {
assignments: [
{
id: '1',
name: 'to',
value: 'test@example.com',
type: 'string'
},
{
id: '2',
name: 'subject',
value: 'Scheduled Report',
type: 'string'
}
]
}
}
},
{
id: 'email-node',
name: 'Send Email',
type: 'n8n-nodes-base.emailSend',
typeVersion: 2.1,
position: [500, 200],
parameters: {
toEmail: '={{ $json.to }}',
subject: '={{ $json.subject }}',
text: 'This is a scheduled email from n8n preview mode test.',
options: {}
}
}
],
connections: {
'Schedule Trigger': {
main: [
[
{
node: 'Prepare Data',
type: 'main',
index: 0
}
]
]
},
'Prepare Data': {
main: [
[
{
node: 'Send Email',
type: 'main',
index: 0
}
]
]
}
},
active: false,
settings: {},
tags: []
},
custom: {
name: 'Custom Test Workflow',
nodes: [
{
id: 'start-node',
name: 'Start',
type: 'n8n-nodes-base.start',
typeVersion: 1,
position: [100, 200]
},
{
id: 'code-node',
name: 'Code',
type: 'n8n-nodes-base.code',
typeVersion: 2,
position: [300, 200],
parameters: {
jsCode: 'return [{\\n json: {\\n message: "Hello from preview mode!",\\n timestamp: new Date().toISOString(),\\n randomNumber: Math.floor(Math.random() * 100)\\n }\\n}];'
}
}
],
connections: {
'Start': {
main: [
[
{
node: 'Code',
type: 'main',
index: 0
}
]
]
}
},
active: false,
settings: {},
tags: []
}
};
return templates[template];
}
}
// Initialize the application when the page loads
document.addEventListener('DOMContentLoaded', () => {
new PreviewModeTestApp();
});