-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell-properties.js
187 lines (168 loc) · 6.52 KB
/
cell-properties.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
let sheetDB = [];
for (let i = 0; i < row; i++) {
let sheetRow = [];
for (let j = 0; j < col; j++) {
let cellProp = {
bold: false,
italic: false,
underline: false,
alignment: "left",
fontFamily: "Monospace",
fontSize: "14",
fontColor: "#000000",
bgColor: "#000000",
value: "",
formula: "",
};
sheetRow.push(cellProp);
}
sheetDB.push(sheetRow);
}
let bold = document.querySelector(".bold");
let italic = document.querySelector(".italic");
let underline = document.querySelector(".underline");
let alignment = document.querySelectorAll(".alignment");
let leftAlign = alignment[0];
let centreAlign = alignment[1];
let rightAlign = alignment[2];
let fontColor = document.querySelector(".fontColor");
let bgColor = document.querySelector(".bgColor");
let fontFamily = document.querySelector(".font-family-cell");
let fontSize = document.querySelector(".font-size-cell");
let activeColor = "#d1d8e0";
let nonActiveColor = "#ecf0f1";
bold.addEventListener("click", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.bold = !cellProp.bold; //toggle bold
cell.style.fontWeight = cellProp.bold ? "bold" : "normal"; //UI change
bold.style.backgroundColor = cellProp.bold ? activeColor : nonActiveColor;
});
italic.addEventListener("click", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.italic = !cellProp.italic; //toggle bold
cell.style.fontStyle = cellProp.italic ? "italic" : "normal"; //UI change
italic.style.backgroundColor = cellProp.italic ? activeColor : nonActiveColor;
});
underline.addEventListener("click", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.underline = !cellProp.underline; //toggle bold
cell.style.textDecoration = cellProp.underline ? "underline" : "none"; //UI change
underline.style.backgroundColor = cellProp.underline
? activeColor
: nonActiveColor;
});
fontSize.addEventListener("change", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.fontSize = fontSize.value; //set value in db
cell.style.fontSize = cellProp.fontSize + "px"; //UI change 1 in cells
fontSize.value = cellProp.fontSize; //UI change 2 in props
});
fontFamily.addEventListener("change", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.fontFamily = fontFamily.value; //set value in db
cell.style.fontFamily = cellProp.fontFamily; //UI change 1 in cells
fontFamily.value = cellProp.fontFamily; //UI change 2 in props
});
fontColor.addEventListener("change", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.fontColor = fontColor.value; //set value in db
cell.style.color = cellProp.fontColor; //UI change 1 in cells
fontColor.value = cellProp.fontColor; //UI change 2 in props
});
bgColor.addEventListener("change", () => {
let [cell, cellProp] = getActiveCell(addressBar);
cellProp.bgColor = bgColor.value; //set value in db
cell.style.backgroundColor = cellProp.bgColor; //UI change 1 in cells
bgColor.value = cellProp.bgColor; //UI change 2 in props
});
alignment.forEach((alignElem) => {
alignElem.addEventListener("click", (e) => {
let [cell, cellProp] = getActiveCell(addressBar);
let alignValue = e.target.classList[0];
cellProp.alignment = alignValue; //db
cell.style.textAlign = cellProp.alignment; //ui
switch (alignValue) {
case "left":
leftAlign.style.backgroundColor = activeColor;
centreAlign.style.backgroundColor = nonActiveColor;
rightAlign.style.backgroundColor = nonActiveColor;
break;
case "center":
leftAlign.style.backgroundColor = nonActiveColor;
centreAlign.style.backgroundColor = activeColor;
rightAlign.style.backgroundColor = nonActiveColor;
break;
case "right":
leftAlign.style.backgroundColor = nonActiveColor;
centreAlign.style.backgroundColor = nonActiveColor;
rightAlign.style.backgroundColor = activeColor;
break;
}
});
});
let allCells = document.querySelectorAll(".cell-box");
for (let i = 0; i < allCells.length; i++) {
addListenerToAttachCellProperties(allCells[i]);
}
function addListenerToAttachCellProperties(cell) {
cell.addEventListener("click", (e) => {
let address = addressBar.value;
let [rId, cId] = decodeIdsfromAddress(address);
let cellProp = sheetDB[rId][cId];
// Apply cell Properties
cell.style.fontWeight = cellProp.bold ? "bold" : "normal";
cell.style.fontStyle = cellProp.italic ? "italic" : "normal";
cell.style.textDecoration = cellProp.underline ? "underline" : "none";
cell.style.fontSize = cellProp.fontSize + "px";
cell.style.fontFamily = cellProp.fontFamily;
cell.style.color = cellProp.fontColor;
cell.style.backgroundColor =
cellProp.bgColor === "#000000" ? "transparent" : cellProp.bgColor;
cell.style.textAlign = cellProp.alignment;
// console.log(cellProp.fontFamily);
// Apply properties UI Props container
bold.style.backgroundColor = cellProp.bold ? activeColor : nonActiveColor;
italic.style.backgroundColor = cellProp.italic
? activeColor
: nonActiveColor;
underline.style.backgroundColor = cellProp.underline
? activeColor
: nonActiveColor;
fontColor.value = cellProp.fontColor;
bgColor.value = cellProp.bgColor;
fontSize.value = cellProp.fontSize;
fontFamily.value = cellProp.fontFamily; //UI change 2 in props
switch (
cellProp.alignment // UI change (2)
) {
case "left":
leftAlign.style.backgroundColor = activeColor;
centreAlign.style.backgroundColor = nonActiveColor;
rightAlign.style.backgroundColor = nonActiveColor;
break;
case "center":
leftAlign.style.backgroundColor = nonActiveColor;
centreAlign.style.backgroundColor = activeColor;
rightAlign.style.backgroundColor = nonActiveColor;
break;
case "right":
leftAlign.style.backgroundColor = nonActiveColor;
centreAlign.style.backgroundColor = nonActiveColor;
rightAlign.style.backgroundColor = activeColor;
break;
}
});
}
const getActiveCell = (addressBar) => {
let [rId, cId] = decodeIdsfromAddress(addressBar);
//acess particular cell
let cell = document.querySelector(`.cell-box[rid="${rId}"][cid="${cId}"]`);
let cellProp = sheetDB[rId][cId];
return [cell, cellProp];
};
const decodeIdsfromAddress = () => {
let cellValue = addressBar.value; //K4
let rId = Number(cellValue.slice(1)) - 1; //3
let cId = cellValue.charCodeAt(0) - 65; // 10
return [rId, cId];
};