-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
432 lines (411 loc) · 15.1 KB
/
script.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
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
let addbtnContainer = document.querySelector(".add-sheet_container");
let sheetList = document.querySelector(".sheets-list");
let firstSheet = document.querySelector(".sheet");
let Allcells = document.querySelectorAll(".grid .col");
let addressBar = document.querySelector(".address-box");
let leftBtn = document.querySelector(".left");
let rightBtn = document.querySelector(".right");
let centerBtn = document.querySelector(".center");
let fontBtn = document.querySelector(".font-size");
let fontFamily = document.querySelector(".font-family");
let boldElem = document.querySelector(".bold");
let italicElem = document.querySelector(".italic");
let underlineElem = document.querySelector(".underline");
let allAlignBtns = document.querySelectorAll(".alignment-container>input");
let formulaInput = document.querySelector(".formula-box");
let gridContainer = document.querySelector(".grid_container");
let topLeftBlock = document.querySelector(".top-left-block");
let sheetDB = workSheetDB[0];
firstSheet.addEventListener("click", handleActiveSheet);
// create sheets and add functionlities
addbtnContainer.addEventListener("click", function () {
let sheetsArr = document.querySelectorAll(".sheet");
let lastSheetElem = sheetsArr[sheetsArr.length - 1];
let idx = lastSheetElem.getAttribute("sheetIdx");
idx = Number(idx);
let NewSheet = document.createElement("div");
NewSheet.setAttribute("class", "sheet");
NewSheet.setAttribute("sheetIdx", idx + 1);
NewSheet.innerText = `Sheet ${idx + 1}`;
// page add
sheetList.appendChild(NewSheet);
// db
// active set
sheetsArr.forEach(function (sheet) {
sheet.classList.remove("active-sheet");
})
sheetsArr = document.querySelectorAll(".sheet");
sheetsArr[sheetsArr.length - 1].classList.add("active-sheet");
// 2 d array
initCurrentSheetDb();
// /current change
sheetDB = workSheetDB[idx];
// cell empty
// new page element value empty
initUI();
// change sheet
NewSheet.addEventListener("click", handleActiveSheet);
})
function handleActiveSheet(e) {
let MySheet = e.currentTarget;
let sheetsArr = document.querySelectorAll(".sheet");
sheetsArr.forEach(function (sheet) {
sheet.classList.remove("active-sheet");
})
if (!MySheet.classList[1]) {
MySheet.classList.add("active-sheet");
}
// index
let sheetIdx = MySheet.getAttribute("sheetIdx")
;
sheetDB = workSheetDB[sheetIdx - 1];
// get data from that and set ui
setUI(sheetDB);
}
// *****************************************************
// address set on click of a cell
for (let i = 0; i < Allcells.length; i++) {
Allcells[i].addEventListener("click", function handleCell() {
let rid = Number(Allcells[i].getAttribute("rid"));
let cid = Number(Allcells[i].getAttribute("cid"));
let rowAdd = rid + 1;
let colAdd = String.fromCharCode(cid + 65);
let address = colAdd + rowAdd;
addressBar.value = address;
let cellObject = sheetDB[rid][cid];
// styling-> set
// object styling set
// UI
// cell
// boldness
if (cellObject.formula != "") {
formulaInput.value = cellObject.formula;
} else {
formulaInput.value = "";
}
if (cellObject.bold == true) {
boldElem.classList.add("active-btn")
} else {
boldElem.classList.remove("active-btn");
}
// alignment
for (let i = 0; i < allAlignBtns.length; i++) {
allAlignBtns[i].classList.remove("active-btn");
}
console.log(cellObject.halign);
if (cellObject.halign == "left") {
// left active
leftBtn.classList.add("active-btn")
} else if (cellObject.halign == "right") {
rightBtn.classList.add("active-btn")
// right active
} else if (cellObject.halign == "center") {
centerBtn.classList.add("active-btn")
}
});
Allcells[i].addEventListener("keydown", function (e) {
let obj = Allcells[i].getBoundingClientRect();
let height = obj.height;
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
let leftCol = document.querySelectorAll(".left-col .left-col_box")[rid];
leftCol.style.height = height + "px";
});
}
gridContainer.addEventListener("scroll", function () {
// console.log(e);
let top = gridContainer.scrollTop;
let left = gridContainer.scrollLeft;
console.log(left);
topLeftBlock.style.top = top + "px";
topRow.style.top = top + "px";
leftCol.style.left = left + "px";
topLeftBlock.style.left = left + "px";
})
// initial cell click emulate
Allcells[0].click();
// ************Formatting****************
leftBtn.addEventListener("click", function () {
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
// console.log(rid, cid);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
cell.style.textAlign = "left";
for (let i = 0; i < allAlignBtns.length; i++) {
allAlignBtns[i].classList.remove("active-btn");
}
leftBtn.classList.add("active-btn");
// db update
let cellObject = sheetDB[rid][cid];
cellObject.halign = "left";
})
rightBtn.addEventListener("click", function () {
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
// console.log(rid, cid);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
cell.style.textAlign = "right";
for (let i = 0; i < allAlignBtns.length; i++) {
allAlignBtns[i].classList.remove("active-btn");
}
rightBtn.classList.add("active-btn");
// db update
let cellObject = sheetDB[rid][cid];
cellObject.halign = "right";
})
centerBtn.addEventListener("click", function () {
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
console.log(rid, cid);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
cell.style.textAlign = "center";
for (let i = 0; i < allAlignBtns.length; i++) {
allAlignBtns[i].classList.remove("active-btn");
}
centerBtn.classList.add("active-btn");
let cellObject = sheetDB[rid][cid];
cellObject.halign = "center";
})
fontBtn.addEventListener("change", function () {
let fontSize = fontBtn.value;
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
console.log(rid, cid);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
console.log(fontSize);
cell.style.fontSize = fontSize + "px";
})
fontFamily.addEventListener("change", function () {
// alert(fontFamily.value);
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
let cFont = fontFamily.value
cell.style.fontFamily = cFont;
})
boldElem.addEventListener("click", function () {
let isActive = boldElem.classList.contains("active-btn");
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
let cellObject = sheetDB[rid][cid];
if (isActive == false) {
// cell text bold
cell.style.fontWeight = "bold";
boldElem.classList.add("active-btn");
cellObject.bold = true;
} else {
// cell text normal
cell.style.fontWeight = "normal";
boldElem.classList.remove("active-btn");
cellObject.bold = false
}
// console.log(sheetDB)
})
italicElem.addEventListener("click", function () {
let isActive = italicElem.classList.contains("active-btn");
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
if (isActive == false) {
// cell text bold
cell.style.fontStyle = "italic";
italicElem.classList.add("active-btn");
} else {
// cell text normal
cell.style.fontStyle = "normal";
italicElem.classList.remove("active-btn");
}
})
underlineElem.addEventListener("click", function () {
let isActive = underlineElem.classList.contains("active-btn");
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
if (isActive == false) {
// cell text bold
cell.style.textDecoration = "underline";
underlineElem.classList.add("active-btn");
} else {
// cell text normal
cell.style.textDecoration = "none";
underlineElem.classList.remove("active-btn");
}
})
// ****************************************************************
// Helper function
function initUI() {
for (let i = 0; i < Allcells.length; i++) {
// boldness
Allcells[i].style.fontWeight = "normal";
Allcells[i].style.fontStyle = "normal";
Allcells[i].style.textDecoration = "none";
Allcells[i].style.fontFamily = "Arial";
Allcells[i].style.fontSize = "16px";
Allcells[i].style.textAlign = "left";
Allcells[i].innerText = "";
}
}
function setUI(sheetDB) {
for (let i = 0; i < sheetDB.length; i++) {
for (let j = 0; j < sheetDB[i].length; j++) {
let cell = document.querySelector(`.col[rid="${i}"][cid="${j}"]`);
let { bold, italic, underline, fontFamily, fontSize, halign, value } = sheetDB[i][j];
cell.style.fontWeight = bold == true ? "bold" : "normal";
cell.innerText = value;
}
}
}
// ********Formula code*******************
// cell blur
// "value"-> value
// formula value-> manually value set
for (let i = 0; i < Allcells.length; i++) {
Allcells[i].addEventListener("blur", function handleCell() {
let address = addressBar.value;
let { rid, cid } = getRIdCIdfromAddress(address);
// 2d array
let cellObject = sheetDB[rid][cid];
// grid
let cell = document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`);
// formula -> 40, 40
if (cellObject.value == cell.innerText) {
return;
}
if (cellObject.formula) {
removeFormula(cellObject, address);
}
// db entry
cellObject.value = cell.innerText;
// depend update
changeChildrens(cellObject);
});
}
// formula bar enter// value -> formula set
// old formula -> new formula
formulaInput.addEventListener("keydown", function (e) {
if (e.key == "Enter" && formulaInput.value != "") {
// 2*A1
let Newformula = formulaInput.value;
// cellObject formula
let address = addressBar.value;
// getCurrentCell
let { rid, cid } = getRIdCIdfromAddress(address);
// 2d
let cellObject = sheetDB[rid][cid];
let prevFormula = cellObject.formula;
if (prevFormula == Newformula) {
return;
}
// previoulsy formula is set so remove it
if (prevFormula != "" && prevFormula != Newformula) {
removeFormula(cellObject, address);
}
// evaluate new formula and get value
let evaluatedValue = evaluateFormula(Newformula);
// alert(value);
// UI change
setUIByFormula(evaluatedValue, rid, cid);
// db -> works
// set formula -> parent set yourself as children
setFormula(evaluatedValue, Newformula, rid, cid, address);
// if you are parent of someone then change them recursivley
changeChildrens(cellObject);
}
})
// parsing
function evaluateFormula(formula) {
// (A100+A20)
//
let formulaTokens = formula.split(" ");
// split
// [(, A1, +, A2,)]
for (let i = 0; i < formulaTokens.length; i++) {
let firstCharOfToken = formulaTokens[i].charCodeAt(0);
if (firstCharOfToken >= 65 && firstCharOfToken <= 90) {
// console.log(formulaTokens[i]);
// A1
let { rid, cid } = getRIdCIdfromAddress(formulaTokens[i]);
let cellObject = sheetDB[rid][cid];
// getting value from db
let {value} = cellObject;
formula = formula.replace(formulaTokens[i], value);
}
}
// (10 +20 )
// infix evaluation
// stack infix evaluation
let ans = eval(formula);
return ans;
// eval
// ( 10 + 20 )
}
function setUIByFormula(value, rid, cid) {
document.querySelector(`.col[rid="${rid}"][cid="${cid}"]`).innerText = value;
// parent add yourself as a
}
// formula update db, value update , parent children array update
function setFormula(value, formula, rid, cid, address) {
let cellObject = sheetDB[rid][cid];
cellObject.value = value;
cellObject.formula = formula;
let formulaTokens = formula.split(" ");
// (A1 + A2)
for (let i = 0; i < formulaTokens.length; i++) {
let firstCharOfToken = formulaTokens[i].charCodeAt(0);
if (firstCharOfToken >= 65 && firstCharOfToken <= 90) {
// console.log(formulaTokens[i]);
let parentRIdCid = getRIdCIdfromAddress(formulaTokens[i]);
let cellObject = sheetDB[parentRIdCid.rid][parentRIdCid.cid];
// getting value from db
cellObject.children.push(address)
}
}
}
function changeChildrens(cellObject) {
// children get
// formula reevaluate
// recursively call
let childrens = cellObject.children;
for (let i = 0; i < childrens.length; i++) {
let chAddress = childrens[i];
let chRICIObj = getRIdCIdfromAddress(chAddress);
let chObj = sheetDB[chRICIObj.rid][chRICIObj.cid];
let formula = chObj.formula;
let evaluatedValue = evaluateFormula(formula);
setUIByFormula(evaluatedValue, chRICIObj.rid, chRICIObj.cid);
chObj.value = evaluatedValue;
// your children have children
changeChildrens(chObj);
}
}
// remove yourself from parents children array
function removeFormula(cellObject, address) {
// (_A1_+_A2_)_
let formula = cellObject.formula;
let formulaTokens = formula.split(" ");
// [ "(","A1","+","A2",")"]
for (let i = 0; i < formulaTokens.length; i++) {
let firstCharOfToken = formulaTokens[i].charCodeAt(0);
if (firstCharOfToken >= 65 && firstCharOfToken <= 90) {
// console.log(formulaTokens[i]);
let parentRIdCid = getRIdCIdfromAddress(formulaTokens[i]);
let parentCellObject = sheetDB[parentRIdCid.rid][parentRIdCid.cid];
// getting value from db
let childrens = parentCellObject.children;
let idx = childrens.indexOf(address);
childrens.splice(idx, 1);
}
}
cellObject.formula = "";
}
// ***********helper fn**********************
function getRIdCIdfromAddress(adress) {
// B3
let cellColAdr = adress.charCodeAt(0);
// console.log(cellColAdr);
let cellrowAdr = adress.slice(1);
let cid = cellColAdr - 65;
let rid = Number(cellrowAdr) - 1;
return { cid, rid };
}