-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetsHandling.js
133 lines (116 loc) · 3.99 KB
/
sheetsHandling.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
let addSheetBtn = document.querySelector(".sheet-add-icon");
let sheetsFolderCont = document.querySelector(".sheets-folder-cont");
addSheetBtn.addEventListener("click", (e) => {
let sheet = document.createElement("div");
sheet.setAttribute("class", "sheet-folder");
let allSheetFolders = document.querySelectorAll(".sheet-folder");
sheet.setAttribute("id", allSheetFolders.length);
sheet.innerHTML = `
<div class="sheet-content">Sheet${allSheetFolders.length + 1}</div>
`;
sheetsFolderCont.appendChild(sheet);
sheet.scrollIntoView();
// DB storage
createSheetDB();
createGraphComponentMatrix();
handleSheetActiveness(sheet);
handleSheetRemoval(sheet);
sheet.click();
})
function handleSheetRemoval(sheet){
sheet.addEventListener("mousedown", (e) => {
//right click
if(e.button !== 2) return;
let allSheetFolders = document.querySelectorAll(".sheet-folder");
if(allSheetFolders.length === 1){
alert("You need to have atleast one sheet!");
return;
}
let response = confirm("Your sheet will be removed permanently. Are you sure to delete?");
if(response === false) return;
let sheetIdx = Number(sheet.getAttribute("id"));
collectedSheetDB.splice(sheetIdx, 1);
collectedGraphComponent.splice(sheetIdx, 1);
handleSheetUIRemoval(sheet);
//by default make sheet 1 active
sheetDB = collectedSheetDB[0];
graphComponentMatrix = collectedGraphComponent[0];
handleSheetProperties();
})
}
function handleSheetUIRemoval(sheet){
sheet.remove();
let allSheetFolders = document.querySelectorAll(".sheet-folder");
for(let i = 0; i < allSheetFolders.length; i++){
allSheetFolders[i].setAttribute("id", i);
let sheetCont = allSheetFolders[i].querySelector(".sheet-content");
sheetCont.innerText = `Sheet${i + 1}`;
allSheetFolders[i].style.backgroundColor = "#ecf0f1";
}
allSheetFolders[0].style.backgroundColor = "#ced6e0";
}
function handleSheetDB(sheetIdx){
sheetDB = collectedSheetDB[sheetIdx];
graphComponentMatrix = collectedGraphComponent[sheetIdx];
}
function handleSheetProperties(){
for(let r = 0; r < rows; r++){
for(let c = 0; c < cols; c++){
let cell = document.querySelector(`.cell[rid="${r}"][cid="${c}"]`);
cell.click();
}
}
// by default click on first cell via DOM
let firstCell = document.querySelector(".cell");
firstCell.click();
}
function handleSheetUI(sheet){
let allSheetFolders = document.querySelectorAll(".sheet-folder");
for(let i = 0; i < allSheetFolders.length; i++){
allSheetFolders[i].style.backgroundColor = "#ecf0f1";
}
sheet.style.backgroundColor = "#ced6e0";
}
function handleSheetActiveness(sheet){
sheet.addEventListener("click", (e) => {
let sheetIdx = Number(sheet.getAttribute("id"));
handleSheetDB(sheetIdx);
handleSheetProperties();
handleSheetUI(sheet);
})
}
function createSheetDB(){
let sheetDB = [];
for(let r = 0; r < rows; r++){
let sheetRow = [];
for(let c = 0; c < cols; c++){
let cellProp = {
bold: false,
italic: false,
underline: false,
alignment: "left",
fontFamily: "monospace",
fontSize: "14",
fontColor: "#000000",
BGcolor: "#ecf0f1",
value: "",
formula: "",
children: []
};
sheetRow.push(cellProp);
}
sheetDB.push(sheetRow);
}
collectedSheetDB.push(sheetDB);
}
function createGraphComponentMatrix(){
let graphComponentMatrix = [];
for(let r = 0; r < rows; r++){
let row = [];
for(let c = 0; c < cols; c++){
row.push([]);
}
graphComponentMatrix.push(row);
}
collectedGraphComponent.push(graphComponentMatrix);
}