-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path401-DataExtractor.html
574 lines (519 loc) · 17.9 KB
/
401-DataExtractor.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<script type="text/html" data-template-name="data-extractor">
<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" style="width: 70%;"/>
</div>
<div class="form-row">
<label for="node-input-response_type"><i class="fa fa-file-o"></i> Type</label>
<select type="text" id="node-input-response_type" style="width:70%!important;">
<option value="json">JSON</option><option value="xml">XML</option>
</select>
</div>
<div class="form-row">
<label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
<input type="text" id="node-input-url" placeholder="URL" style="width: 60%" />
<button type="button" class="red-ui-button" id="node-input-read">Read</button>
</div>
<div class="form-row">
<label for="node-input-path"><i class="fa fa-bookmark"></i> Path</label>
<textarea id="node-input-path" disabled style="height: 80px; width: 100%;"></textarea>
</div>
<div class="form-row node-input-api-tree" style="height: calc(80% - 40px);">
<label for="node-input-api-tree"><i class="fa fa-key"></i></i> Key</label>
</div>
</script>
<script type="text/html" data-help-name="data-extractor">
<p>A node that extracts specific data you want from REST API response.
The criteria for extraction is specific paths checked by user.
</p>
<p>CORS issues may affect data display.
</p>
<h3>Properties</h3>
<dl class="message-properties">
<dt>name</dt>
<dd>You can tag or naming this data extraction node.</dd>
<dt>Type<span class="property-type">require</span></dt>
<dd>You can choose the type of input data format between JSON and XML.</dd>
<dt>URL<span class="property-type">require</span></dt>
<dd>This URL makes the data structure tree of API response keys.</dd>
<dt>Path<span class="property-type">optional</span></dt>
<dd>This Paths is a list of path that is checked in key tree.</dd>
<dt>Key<span class="property-type">optional</span></dt>
<dd>This Key Tree is made by URL response.
And it provides data path checkboxes for extracting specific data.
</dd>
</dl>
</script>
<script type="text/html" data-template-name="data-filter">
<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" style="width: 70%;" />
</div>
<div class="form-row">
<label for="node-input-response_type"><i class="fa fa-file-o"></i> Type</label>
<select type="text" id="node-input-response_type" style="width:70%!important;">
<option value="json">JSON</option><option value="xml">XML</option>
</select>
</div>
<div class="form-row">
<label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
<input type="text" id="node-input-url" placeholder="URL" style="width: 60%" />
<button type="button" class="red-ui-button" id="node-input-read">Read</button>
</div>
<div class="form-row node-input_stdpath" style="height: 180px;">
<label for="node-input_stdpath" style="width: 70%;">
<i class="fa fa-key"></i>
Standard Key
</label>
</div>
<br />
<br />
<div class="form-row">
<label for="node-input-value"><i class="fa fa-ellipsis-h"></i> Compare</label>
<div style="display:flex;">
<input type="text" id="node-input-label_value" style="width:35%; margin: auto 0;" readonly />
<p style="width:10px; margin: auto">=</p>
<input type="text" id="node-input-value" style="width:55%; margin: auto;" />
<input type="hidden" id="node-input-value_type" />
</div>
</div>
<br />
<div class="form-row node-input-output" style="height: calc(50% - 40px);">
<label for="node-input-output"><i class="fa fa-list"></i> Output</label>
</div>
</script>
<script type="text/html" data-help-name="data-filter">
<p>
A node that filter out specific data you want from REST API response. The criteria for filtering
starts from data that satisfy a specific path and value.
</p>
<p>CORS issues may affect data display.
</p>
<h3>Properties</h3>
<dl class="message-properties">
<dt>name</dt>
<dd>You can tag or naming this data filter node.</dd>
<dt>Type<span class="property-type">require</span></dt>
<dd>You can choose the type of input data format between JSON and XML.</dd>
<dt>URL<span class="property-type">require</span></dt>
<dd>This URL makes the data structure tree of API response keys.</dd>
<dt>Standard Key<span class="property-type">require</span></dt>
<dd>
This path is the path to check to extract specific target data. It is made by REST API
response of URL.
</dd>
<dt>Compare<span class="property-type">require</span></dt>
<dd>
It is a reference value required to extract specific data through comparison of data
corresponding to the above path.
</dd>
<dt>Output<span class="property-type">require</span></dt>
<dd>
It is data paths that are sibling to reference data. You can check about multiple selection
</dd>
</dl>
</script>
<script type="text/javascript">
(function () {
let treeList;
let tree = [];
let = [];
RED.nodes.registerType("data-extractor", {
category: "Samsung AutomationStudio",
paletteLabel: "Data Extractor",
color: "#00bffe",
defaults: {
name: { value: "Data Extractor", },
url: { value: "", },
tree: { value: [], },
paths: { value: [], },
response_type: { value: "", }
},
inputs: 1,
outputs: 1,
icon: "font-awesome/fa-crosshairs",
label: function () {
return "Data Extractor";
},
oneditsave: function () {
this.paths = paths;
let copiedTree = [];
copyTree(tree, copiedTree);
this.tree = copiedTree;
},
oneditprepare: function () {
const node = this;
treeList = $("<div>")
.css({ width: "100%", height: "100%" })
.appendTo(".node-input-api-tree")
.treeList({});
if (this.tree !== undefined) {
tree = [];
copyTree(this.tree, tree);
treeList.treeList("data", tree);
}
if (this.paths !== undefined) {
paths = this.paths;
showPath();
}
$("#node-input-read").on("click", function () {
let type = document.getElementById('node-input-response_type').value;
const url = $("#node-input-url").val();
fetch(url)
.then(function (response) {
if (url.trim() === "" || !response.ok) {
throw new Error();
}
if (type === "json") {
return response.json();
} else if (type === "xml") {
return response.text();
}
})
.then(function (json) {
if (type === "xml") {
let xmlNode = new DOMParser().parseFromString(json, "text/xml");
json = xmlToJson(xmlNode);
}
reset();
makeKeyTree(json, tree);
treeList.treeList("data", tree);
})
.catch((error) => {
console.log(error);
reset();
});
});
treeList.on("treelistselect", function (event, item) {
if (item === undefined || Object.keys(item).length === 0) {
return;
}
if (item.selected) {
if (item.parent !== undefined) {
item.parent.treeList.select(true);
}
} else if (!item.selected) {
if (item.children !== undefined) {
for (let n of item.children) {
if (n.selected) {
n.treeList.select(false);
}
}
}
}
paths = [];
for (let item of tree) {
generatePath(item, "");
}
showPath();
});
const hasMatch = function (arr, value) {
return arr.some(function (ht) {
return ht.value === value
});
}
function reset() {
paths = [];
tree = [];
$("#node-input-path").val("");
treeList.treeList("empty");
}
function showPath() {
let res = "";
if (typeof paths === "string") {
paths = JSON.parse(paths);
}
for (let path of paths) {
res += path + "\n";
}
$("#node-input-path").val(res);
}
function generatePath(item, pre) {
if (!item.selected) {
return;
}
let cur = item.depth === 0 ? item.label : pre + "." + item.label;
if (item.children === undefined) {
paths.push(cur);
return;
}
let childChecked = false;
for (let n of item.children) {
if (n.selected) {
childChecked = true;
generatePath(n, cur);
}
}
if (!childChecked) {
paths.push(cur);
}
}
function makeKeyTree(json, subtree) {
if (Array.isArray(json) && json.length > 0) {
makeKeyTree(json[0], subtree);
return;
}
let keys = Object.keys(json);
for (let key of keys) {
subtree.push(makeTreeItem(key));
if (
typeof json[key] === "object" &&
json[key] !== null &&
Object.keys(json[key]).length > 0
) {
subtree[subtree.length - 1]["children"] = [];
makeKeyTree(json[key], subtree[subtree.length - 1]["children"]);
}
}
}
function makeTreeItem(label) {
return {
label: label,
checkbox: true,
selected: false,
expanded: true,
};
}
},
});
let stdTreeList;
let stdTree = [];
let stdPath = "";
let outputTreeList;
let outputTree = [];
let outputPaths = [];
RED.nodes.registerType("data-filter", {
category: "Samsung AutomationStudio",
color: "#407EC1",
defaults: {
name: { value: "Data Filter", },
response_type: { value: "", },
url: { value: "", },
value: { value: "", required: true },
value_type: { value: "", },
label_value: { value: "", },
stdPath: { value: "", },
stdTree: { value: [], },
outputPaths: { value: [], required: true },
outputTree: { value: [], required: true },
},
inputs: 1,
outputs: 1,
icon: "font-awesome/fa fa-filter",
paletteLabel: "Data Filter",
label: function () {
return this.name;
},
oneditsave: function () {
this.stdPath = stdPath;
this.outputPaths = outputPaths;
let copiedStdTree = [];
copyTree(stdTree, copiedStdTree);
let copiedOutputTree = [];
copyTree(outputTree, copiedOutputTree);
this.stdTree = copiedStdTree;
this.outputTree = copiedOutputTree;
},
oneditprepare: function () {
stdTreeList = $("<div>")
.css({ width: "100%", height: "100%" })
.appendTo(".node-input_stdpath")
.treeList({});
outputTreeList = $("<div>")
.css({ width: "100%", height: "100%" })
.appendTo(".node-input-output")
.treeList({});
stdPath = this.stdPath;
outputPaths = this.outputPaths;
if (this.stdTree !== undefined) {
stdTree = [];
copyTree(this.stdTree, stdTree);
stdTreeList.treeList("data", stdTree);
}
if (this.outputTree !== undefined) {
outputTree = [];
copyTree(this.outputTree, outputTree);
outputTreeList.treeList("data", outputTree);
}
$("#node-input-value").typedInput({
type: "str",
types: ["str", "num", "bool"],
typeField: "#node-input-value_type",
});
$("#node-input-read").on("click", function () {
let type = document.getElementById('node-input-response_type').value;
const url = $("#node-input-url").val();
fetch(url)
.then(function (response) {
if (url.trim() === "" || !response.ok) {
throw new Error();
}
if (type === "json") {
return response.json();
} else if (type === "xml") {
return response.text();
}
})
.then(function (json) {
if (type === "xml") {
let xmlNode = new DOMParser().parseFromString(json, "text/xml");
json = xmlToJson(xmlNode);
}
reset();
makeStdTree(json, stdTree);
stdTreeList.treeList("data", stdTree);
})
.catch((error) => {
console.log(error);
reset();
});
});
stdTreeList.on("treelistselect", function (event, item) {
if (item === undefined || Object.keys(item).length === 0) {
return;
}
if (item.selected) {
$("#node-input-label_value").val(item.label);
stdPath = "";
generateStdPath(item);
outputTree = [];
let children = item.parent ? item.parent.children : stdTree;
makeOutputTree(outputTree, children);
outputTreeList.treeList("data", outputTree);
}
});
outputTreeList.on("treelistselect", function (event, item) {
if (item === undefined || Object.keys(item).length === 0) {
return;
}
outputPaths = [];
generateOutputPaths(getPrePath(), outputTree);
});
function reset() {
stdTree = [];
outputTree = [];
$("#node-input-label_value").val("");
stdTreeList.treeList("empty");
outputTreeList.treeList("empty");
}
function getPrePath() {
let pathKeywords = stdPath.split(".");
let prePath = "";
if (pathKeywords.length > 1) {
for (let i = 0; i < pathKeywords.length - 1; i++) {
prePath += pathKeywords[i] + ".";
}
}
return prePath;
}
function generateOutputPaths(prePath, subtree) {
subtree.forEach((item) => {
if (item.children) {
generateOutputPaths(prePath + item.label + ".", item.children);
return;
}
if (item.selected) {
outputPaths.push(prePath + item.label);
}
});
}
function makeOutputTree(outputTree, stdTree) {
stdTree.forEach((child) => {
if (child.children === undefined) {
outputTree.push(makeStdTreeItem(child.label, "outputPath", true));
return;
}
outputTree.push(makeStdTreeItem(child.label, "outputPath", false));
outputTree[outputTree.length - 1].children = [];
makeOutputTree(outputTree[outputTree.length - 1].children, child.children);
});
}
function generateStdPath(item) {
if (item.parent !== undefined) {
generateStdPath(item.parent);
}
stdPath += item.children ? item.label + "." : item.label;
}
function makeStdTree(json, subtree) {
if (Array.isArray(json) && json.length > 0) {
makeStdTree(json[0], subtree);
return;
}
let keys = Object.keys(json);
for (let key of keys) {
if (
typeof json[key] === "object" &&
json[key] !== null &&
Object.keys(json[key]).length > 0
) {
subtree.push(makeStdTreeItem(key, "stdPath", false));
subtree[subtree.length - 1]["children"] = [];
makeStdTree(json[key], subtree[subtree.length - 1]["children"]);
continue;
}
subtree.push(makeStdTreeItem(key, "stdPath", true));
}
}
function makeStdTreeItem(label, type, display) {
if (type === "stdPath") {
return {
label: label,
radio: display,
selected: false,
expanded: true,
};
} else {
return {
label: label,
checkbox: display,
selected: false,
expanded: true,
};
}
}
},
});
function copyTree(origin, copy) {
for (let item of origin) {
let obj = {};
for (let key in item) {
if (key === "treeList" || key === "depth" || key === "parent") {
continue;
}
if (key === "children") {
obj[key] = [];
copyTree(item[key], obj[key]);
continue;
}
obj[key] = item[key];
}
copy.push(obj);
}
}
function xmlToJson(xml) {
if (xml.nodeType === Node.TEXT_NODE) {
return xml.nodeValue;
}
const obj = {};
if (xml.hasChildNodes()) {
for (let i = 0; i < xml.childNodes.length; i++) {
const item = xml.childNodes[i];
const nodeName = item.nodeName;
if (item.childNodes.length === 1 && item.firstChild.nodeType === Node.TEXT_NODE) {
obj[nodeName] = item.firstChild.nodeValue;
} else if (item.nodeType === Node.ELEMENT_NODE) {
if (!obj[nodeName]) {
obj[nodeName] = xmlToJson(item);
} else {
if (!Array.isArray(obj[nodeName])) {
obj[nodeName] = [obj[nodeName]];
}
obj[nodeName].push(xmlToJson(item));
}
}
}
}
return obj;
}
})();
</script>