-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformula.js
153 lines (134 loc) · 6.18 KB
/
formula.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
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
cell.addEventListener("blur", (e) => {
let address = addressBar.value;
let [Cell, cellProp] = activeCell(address)
let enteredData = Cell.innerText;
if (enteredData == cellProp.value) return;
cellProp.value = enteredData;
// if cells are modifies directly(without formula bar) -
// remove parent-child relationship (remove all parents of cell but keep its children)
removeChildFromParent(cellProp.formula);
// empty formula since it is no longer depended on it (including the parents in it)
cellProp.formula = "";
// update all children of cell
updateChildrenCells(address);
})
}
}
// let formulaBar = document.querySelector(".formula-bar");
formulaBar.addEventListener("keydown", async (e) => {
let inputFormula = formulaBar.value;
if (inputFormula === "") {
let [cell, cellProp] = activeCell(addressBar.value);
cellProp.formula = "";
}
if (e.key === "Enter" && inputFormula) {
// if formula changed -> break old parent-child relation
let [cell, cellProp] = activeCell(addressBar.value);
if (inputFormula !== cellProp.formula) removeChildFromParent(cellProp.formula);
// append child in parent in graphComponentMatrix arr
addChildToGraphComponent(inputFormula, addressBar.value);
// check if formula is cyclic or not, only evaluate if not cyclic.
let cycleResponse = isGraphCyclic(graphComponentMatrix);
if (cycleResponse) {
// ("Your formula is cyclic");
let response = confirm("Your formula is cyclic. Do you want to trace your path ?");
while (response) {
// keep on tracking color until user is satisfied
await isGraphCyclicTracePath(graphComponentMatrix, cycleResponse); // we want to complete full iteration of color tracking, so we will attach wait here also
response = confirm("Your formula is cyclic. Do you want to trace your path ?");
}
removeChildFromGraphComponent(inputFormula);
return;
}
// evaluate new formula
let evaluatedValue = evaluateFormula(inputFormula);
// update UI and DB
setCellUIAndCellProp(evaluatedValue, inputFormula, addressBar.value);
// add new parent-child relation
addChildToParent(inputFormula);
// update all children cells
updateChildrenCells(addressBar.value);
}
})
function removeChildFromParent(formula) {
let childAddress = addressBar.value;
let encodedFormula = formula.split(" ");
for (let i = 0; i < encodedFormula.length; i++) {
let asciiValue = encodedFormula[i].charCodeAt(0);
if (asciiValue >= 65 && asciiValue <= 90) {
let [parentCell, parentCellProp] = activeCell(encodedFormula[i]);
let idx = parentCellProp.children.indexOf(childAddress);
parentCellProp.children.splice(idx, 1);
}
}
}
function addChildToParent(formula) {
let childAddress = addressBar.value;
let encodedFormula = formula.split(" ");
for (let i = 0; i < encodedFormula.length; i++) {
let asciiValue = encodedFormula[i].charCodeAt(0);
if (asciiValue >= 65 && asciiValue <= 90) {
let [parentCell, parentCellProp] = activeCell(encodedFormula[i]);
parentCellProp.children.push(childAddress);
}
}
}
function updateChildrenCells(parentAddress) {
let [parentCell, parentCellProp] = activeCell(parentAddress);
let children = parentCellProp.children;
for (let i = 0; i < children.length; i++) {
let childAddress = children[i];
let [childCell, childCellProp] = activeCell(childAddress);
let childFormula = childCellProp.formula;
let evaluatedVal = evaluateFormula(childFormula);
setCellUIAndCellProp(evaluatedVal, childFormula, childAddress);
updateChildrenCells(childAddress);
}
}
function evaluateFormula(formula) {
let encodedFormula = formula.split(" ");
for (let i = 0; i < encodedFormula.length; i++) {
let asciiValue = encodedFormula[i].charCodeAt(0);
if (asciiValue >= 65 && asciiValue <= 90) {
let [cell, cellProp] = activeCell(encodedFormula[i]);
encodedFormula[i] = cellProp.value;
// cell.focus();
}
}
let decodedFormula = encodedFormula.join(" ");
return eval(decodedFormula);
}
function setCellUIAndCellProp(evaluatedValue, formula, address) {
let [cell, cellProp] = activeCell(address);
cell.innerText = evaluatedValue; // UI update
cellProp.value = evaluatedValue; //DB update
cellProp.formula = formula; // DB update
// setTimeout bec without setTimeout, the evaluatedValue is set AFTER cell has been focused causing the evaluatedValue to shift to a new line creating y-overflow in the cell. setTimeout ensures that cell.focus runs only after the value has been changed.
setTimeout(() => {
cell.focus();
}, 1);
}
function addChildToGraphComponent(formula, childAddress) {
let [crid, ccid] = decodeRIDCIDFromAddress(childAddress);
let encodedFormula = formula.split(" ");
for (let i = 0; i < encodedFormula.length; i++) {
let asciiValue = encodedFormula[i].charCodeAt(0);
if (asciiValue >= 65 && asciiValue <= 90) {
let [prid, pcid] = decodeRIDCIDFromAddress(encodedFormula[i]);
graphComponentMatrix[prid][pcid].push([crid, ccid]);
}
}
}
function removeChildFromGraphComponent(formula) {
let encodedFormula = formula.split(" ");
for (let i = 0; i < encodedFormula.length; i++) {
let asciiValue = encodedFormula[i].charCodeAt(0);
if (asciiValue >= 65 && asciiValue <= 90) {
let [prid, pcid] = decodeRIDCIDFromAddress(encodedFormula[i]);
graphComponentMatrix[prid][pcid].pop();
}
}
}