-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetsHandling.js
139 lines (121 loc) · 4.22 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
134
135
136
137
138
139
let activeSheetColor ="#ced6e0";
let sheetsFolderCont = document.querySelector(".sheets-folder-cont");
let addSheetBtn = document.querySelector(".sheet-add-icon");
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
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?");
if (response === false) return;
let sheetIdx = Number(sheet.getAttribute("id"));
// DB
collectedSheetDB.splice(sheetIdx, 1);
collectedGraphComponent.splice(sheetIdx, 1);
// UI
handleSheetUIRemoval(sheet)
// By default DB to 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 sheetContent = allSheetFolders[i].querySelector(".sheet-content");
sheetContent.innerText = `Sheet ${i+1}`;
allSheetFolders[i].style.backgroundColor = "transparent";
}
allSheetFolders[0].style.backgroundColor = activeSheetColor;
}
function handleSheetDB(sheetIdx) {
sheetDB = collectedSheetDB[sheetIdx];
graphComponentMatrix = collectedGraphComponent[sheetIdx];
}
function handleSheetProperties() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
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 = "transparent";
}
sheet.style.backgroundColor = activeSheetColor;
}
function handleSheetActiveness(sheet) {
sheet.addEventListener("click", (e) => {
let sheetIdx = Number(sheet.getAttribute("id"));
handleSheetDB(sheetIdx);
handleSheetProperties();
handleSheetUI(sheet);
console.log(sheetDB);
})
}
function createSheetDB() {
let sheetDB = [];
for (let i = 0; i < rows; i++) {
let sheetRow = [];
for (let j = 0; j < cols; j++) {
let cellProp = {
bold: false,
italic: false,
underline: false,
alignment: "left",
fontFamily: "monospace",
fontSize: "14",
fontColor: "#000000",
BGcolor: "#000000", // Just for indication purpose,
value: "",
formula: "",
children: [],
}
sheetRow.push(cellProp);
}
sheetDB.push(sheetRow);
}
collectedSheetDB.push(sheetDB);
}
function createGraphComponentMatrix() {
let graphComponentMatrix = [];
for (let i = 0; i < rows; i++) {
let row = [];
for (let j = 0; j < cols; j++) {
// Why array -> More than 1 child relation(dependency)
row.push([]);
}
graphComponentMatrix.push(row);
}
collectedGraphComponent.push(graphComponentMatrix);
}