-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetsHandling.js
145 lines (124 loc) · 4.41 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
140
141
142
143
144
145
let sheetsFolderContainer = document.querySelector('.sheets-folder-cont');
let addSheetBtn = document.querySelector('.sheet-add-icon');
let activeSheetColor = '#c8d6e5';
addSheetBtn.addEventListener('click', (e) => {
let sheet = document.createElement('div'); // created sheet
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>`;
sheetsFolderContainer.appendChild(sheet);
sheet.scrollIntoView(); // Page scrolls to right when sheet count increases beyond initial limit on viewport
// DB: Every sheet has different storage
createSheetDB();
createGraphComponentMatrix();
handleSheetActiveness(sheet);
handleSheetRemoval(sheet);
sheet.click();
});
function handleSheetRemoval(sheet) {
sheet.addEventListener('mousedown', e => {
// Returns a number: 0 - Left click, 1 - scroll button; 2 - right click button
if (e.button !== 2) return;
// For min 1 sheet folder
let allSheetFolders = document.querySelectorAll('.sheet-folder');
if (allSheetFolders.length === 1) {
alert("You need to have at least one sheet!");
return;
}
let response = confirm('Heads up!!! Are you sure you want to delete this sheet?');
if (response === false) return;
let sheetIdx = Number(sheet.getAttribute('id'));
// Sheet Removal from DB
collectedSheetDB.splice(sheetIdx, 1); // on the given index, removes 1 sheet from the DB
collectedGraphComponent.splice(sheetIdx, 1);
// UI
handleSheetUIRemoval(sheet);
// By default assign DB to sheet 1 (active)
sheetDB = collectedSheetDB[0];
createGraphComponentMatrix = collectedGraphComponent[0];
handleSheetProperties();
})
}
function handleSheetUIRemoval(sheet) {
// Sheet Removal from UI
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];
}
// Handling cell properties on every cell
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 on every sheet toggle
let firstCell = document.querySelector('.cell');
firstCell.click();
}
// Takes the active sheet and applies BG-color to it
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;
}
// when a sheet is clicked/toggled some task performed on it should be shown
function handleSheetActiveness(sheet) {
sheet.addEventListener('click', e => {
let sheetIdx = Number(sheet.getAttribute('id'));
handleSheetDB(sheetIdx);
handleSheetProperties();
handleSheetUI(sheet);
})
}
function createSheetDB() {
// Storage
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', // default color: identification purpose
value: '',
formula: '',
children: [], // keeps address of child cells
};
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 (dependencies)
row.push([]);
}
graphComponentMatrix.push(row);
}
collectedGraphComponent.push(graphComponentMatrix);
}