-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.js
153 lines (127 loc) · 5.41 KB
/
extractor.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
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
{
var flow;
var registerNodes;
var extractedBranch = new Map();
/**
* Called on load, creates an event listener on the file input element to process the uploaded file
*/
function init() {
document.getElementById('fileInput').addEventListener('change', handleFileSelect, false);
}
/**
* Read the uploaded file as text
* @param {*} event
*/
function handleFileSelect(event) {
const reader = new FileReader()
reader.onload = handleFileLoad;
reader.readAsText(event.target.files[0])
}
/**
* Called after uploading and reading the file. Update the UI to show uploaded file information and processing options
* @param {*} event
*/
function handleFileLoad(event) {
console.log(event);
document.getElementById('fileContent').textContent = event.target.result;
//parse the json text into an object
flow = JSON.parse(event.target.result);
document.getElementById('btnOriginal').innerHTML = "Original JSON Input: " + Object.keys(flow).length + " nodes";
//loop through to find all register nodes
registerNodes = new Map();
for (var key of Object.keys(flow)) {
if (flow[key].action == "sfdcRegister") {
registerNodes.set(key, flow[key]);
}
}
//display the results
document.getElementById('results').textContent = "Found " + registerNodes.size + " register nodes. Click the nodes to extract.";
let nodePicker = document.getElementById('nodePicker');
//add buttons for each register node, so the user can select which to extract
registerNodes.forEach((node, name) => {
let button = document.createElement("input");
button.setAttribute("value", name);
button.setAttribute("class", "btn btn-outline-primary m-1");
button.setAttribute("type", "button");
button.setAttribute("id", name);
button.setAttribute("onclick", "extractBranch('" + name + "')");
nodePicker.appendChild(button);
})
}
/**
* Extract the desired branch, based on the button clicked
* @param {String} registerNodeName Name of the dataset the user selected
*/
function extractBranch(registerNodeName) {
//set button style so user knows it was clicked
document.getElementById(registerNodeName).setAttribute("class", "btn btn-primary m-1");
let registerNode = extractAndAdd(registerNodeName);
//Recursively build the branch
extractBranchRecursive(registerNode.parameters.source);
//enable the download button and show json preview
document.getElementById('downloadButton').setAttribute("class", "btn btn-success visible");
document.getElementById('resultJSON').textContent = JSON.stringify(Object.fromEntries(extractedBranch), null, 2);
document.getElementById('resultsButton').innerHTML = "Results: " + extractedBranch.size + " nodes";
}
/**
* Recursively travels down a branch, adding dependent nodes to extractedBranch
* @param {String} nodeName The starting node
*/
function extractBranchRecursive(nodeName) {
let working = true;
let node = extractAndAdd(nodeName);
while (working) {
if (node.action == "sfdcDigest" || node.action == "edgemart" ||
node.action == "digest") {
working = false;
} else {
if (node.parameters.source != null) {
node = extractAndAdd(node.parameters.source);
} else if (node.parameters.left != null) {
//call the recursive function for the right side of the augment as well
extractBranchRecursive(node.parameters.right);
//keep going down the left side of the branch
node = extractAndAdd(node.parameters.left);
} else if(node.parameters.sources != null && node.parameters.sources.length > 0) {
//Append nodes has an array of sources
//stop this loop and start a new loop for each source
working = false;
node.parameters.sources.forEach((source) => {
extractBranchRecursive(source);
})
}
}
}
}
/**
* Add the specified node to extractedBranch, and return its object representation
* @param {String} nodeName Name of the node to extract
* @returns
*/
function extractAndAdd(nodeName) {
let nextNode = getNode(nodeName);
extractedBranch.set(nodeName, nextNode);
return nextNode;
}
/**
* Get the node with the specified name
* @param {String} name
* @returns
*/
function getNode(name) {
return flow[name];
}
/**
* Down the results in a JSON file
*/
function download() {
let resultsString = JSON.stringify(Object.fromEntries(extractedBranch));
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(resultsString));
element.setAttribute('download', 'extracted-results.json');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
}