Skip to content

Commit d5b1412

Browse files
nusanamdavidchai717
andcommitted
set up helper function to build d3 hierarchy data object
Co-authored-by: Ruth <[email protected]> Co-authored-by: David Chai <[email protected]>
1 parent b59b22f commit d5b1412

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

src/extension/background.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,60 @@ function createTabObj(title) {
2222
},
2323
};
2424
}
25-
// once state is modified (when user does something with app), a step appears in actionContainer.jsx column. That current state snapshot is added to our hierarchy object. That is what the buildHierarchy function is for. It takes in the entire tabObj, which has a hierarcy object as a property within it. Then we build this hierarchy object so that d3 can render graphs in our extension
26-
function buildHierarchy(obj, newNode) {
27-
// whenever we receive a snapshot from contentScript.js via message, we execute this function
28-
//* if empty on extension UI is clicked hierarchy needs to be reset to an object
29-
let num = 1;
30-
class d3Node {
31-
constructor(num, obj) {
32-
this.name = num++;
25+
26+
const makeNewNode = () => {
27+
let num = 0;
28+
29+
return class Node {
30+
constructor(obj) {
31+
this.index = num += 1;
3332
this.stateSnapshot = obj;
3433
this.children = [];
3534
}
35+
};
36+
}
37+
const Node = makeNewNode();
38+
39+
function buildHierachy(tabObj, newNode) {
40+
if (!tabObj.currLocation) {
41+
tabObj.currLocation = newNode;
42+
tabObj.hierachy = newNode;
43+
} else {
44+
tabObj.currLocation.children.push(newNode);
45+
tabObj.currLocation = newNode;
3646
}
37-
38-
obj.hierarchy
39-
/* properties inside this object absolutely requires:
40-
name: string (the first state snapshot has to be a root)
41-
stateSnapshot: object
42-
currentStateSnapshot: boolean
43-
*/
47+
}
48+
49+
function changeCurrLocation(tabObj, currNode, index) {
50+
// check if current node has the index wanted
51+
if (currNode.index === index) {
52+
tabObj.currLocation = currNode;
53+
return;
54+
}
55+
// base case if no children
56+
if (!currNode.children.length) {
57+
return;
58+
} else {
59+
// if not, recurse on each one of the children
60+
currNode.children.forEach(child => {
61+
changeCurrLocation(tabObj, child, index);
62+
});
63+
}
64+
}
65+
66+
//! once state is modified (when user does something with app), a step appears in actionContainer.jsx column. That current state snapshot is added to our hierarchy object. That is what the buildHierarchy function is for. It takes in the entire tabObj, which has a hierarcy object as a property within it. Then we build this hierarchy object so that d3 can render graphs in our extension
67+
// whenever we receive a snapshot from contentScript.js via message, we execute this function
68+
//* if empty on extension UI is clicked hierarchy needs to be reset to an object
4469

4570
// each time a statesnapshot is added, this gets incremented otherwise it will be decremented
4671
// we need to find a way to traverse through the object to know which node the user is on so we can add a new state snapshot in the right location
4772
// could we potentially have a variable in timejump function (timeJump.js in the package) that our function can work with --> contentScript.js has access to it --> we can access that variable message;
48-
stateCount = 1;
49-
50-
class stateNode {
51-
52-
constructor() {
53-
this.name = `state${stateCount}`;
54-
this.stateSnapshot = {};
55-
this.children = [];
56-
}
57-
}
58-
5973

6074
// create a helper function that groups all the snapshots underneath each other
6175
// current state snapshot
6276
// needs to be supplied by the UI
6377
// also need to figure out how we would traverse through the big ass object to find the current state
6478
// Create a new object with name,
65-
}
66-
6779

6880
// establishing connection with devtools
6981
chrome.runtime.onConnect.addListener(port => {
@@ -171,9 +183,10 @@ chrome.runtime.onMessage.addListener((request, sender) => {
171183
reloaded[tabId] = false;
172184

173185
tabsObj[tabId].snapshots.push(request.payload);
174-
//! INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN!!!!
175-
let currentStateObject = tabsObj[tabId]
176-
buildHierarchy(tabsObj[tabId], request.payloadTurnedIntoNODE );
186+
// invoking function to place a new d3 tree node in the right location
187+
const newNode = new Node(request.payload)
188+
buildHierarchy(tabsObj[tabId], newNode);
189+
177190
console.log(tabsObj[tabId].snapshots);
178191
if (portsArr.length > 0) {
179192
portsArr.forEach(bg => bg.postMessage({
@@ -189,8 +202,9 @@ chrome.runtime.onMessage.addListener((request, sender) => {
189202
reloaded[tabId] = false;
190203
} else {
191204
tabsObj[tabId].snapshots.push(request.payload);
192-
//! INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN!!!!
193-
buildHierarchy();
205+
// invoking function to place a new d3 tree node in the right location
206+
const newNode = new Node(request.payload)
207+
buildHierarchy(tabsObj[tabId], newNode);
194208
}
195209
// send message to devtools
196210
if (portsArr.length > 0) {

0 commit comments

Comments
 (0)