-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell-properties.js
206 lines (183 loc) · 6.58 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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: "transparent", //just for indication purpose
};
sheetRow.push(cellProp);
}
sheetDB.push(sheetRow);
}
// Selectors for cell properties
const bold = document.querySelector(".bold");
const italic = document.querySelector(".italic");
const underline = document.querySelector(".underline");
const fontSize = document.querySelector(".font-size-prop");
const fontFamily = document.querySelector(".font-family-prop");
const fontColor = document.querySelector(".font-color-prop");
const BGcolor = document.querySelector(".bgcolor-prop");
const alignment = document.querySelectorAll(".alignment");
const leftAlign = alignment[0];
const centerAlign = alignment[1];
const rightAlign = alignment[2];
const addressBar = document.querySelector(".address-bar");
let activeColorProp = "#d1d8e0";
let inactiveColorProp = "#ecf0f1";
//Application of two way binding
//Attach property listeners
bold.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
//modificatoin
cellProp.bold = !cellProp.bold;
cell.style.fontWeight = cellProp.bold ? "bold" : "normal";
bold.style.backgroundColor = cellProp.bold
? activeColorProp
: inactiveColorProp;
});
italic.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
//modificatoin
cellProp.italic = !cellProp.italic;
cell.style.fontStyle = cellProp.italic ? "italic" : "normal";
italic.style.backgroundColor = cellProp.italic
? activeColorProp
: inactiveColorProp;
});
underline.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
//modificatoin
cellProp.underline = !cellProp.underline; //datachange
cell.style.textDecoration = cellProp.underline ? "underline" : "none"; //ui change 1
underline.style.backgroundColor = cellProp.underline
? activeColorProp
: inactiveColorProp; //ui change 2
});
fontSize.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
cellProp.fontSize = fontSize.value;
cell.style.fontSize = cellProp.fontSize + "px";
fontSize.value = cellProp.fontSize;
});
fontFamily.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
cellProp.fontFamily = fontFamily.value;
cell.style.fontFamily = cellProp.fontFamily;
fontFamily.value = cellProp.fontFamily;
});
fontColor.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
cellProp.fontColor = fontColor.value;
cell.style.color = cellProp.fontColor;
fontColor.value = cellProp.fontColor;
});
BGcolor.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
cellProp.BGcolor = BGcolor.value;
console.log(BGcolor.value);
cell.style.backgroundColor = cellProp.BGcolor;
BGcolor.value = cellProp.BGcolor;
});
alignment.forEach((alignElem) => {
alignElem.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = activeCell(address);
alignVal = e.target.classList[0];
cellProp.alignment = alignVal;
cell.style.textAlign = alignVal;
switch (alignVal) {
case "left":
leftAlign.style.backgroundColor = activeColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "center":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "right":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = activeColorProp;
break;
}
});
});
function activeCell(address) {
let [rid, cid] = decodeRidCidfromAddress(address);
console.log(rid, cid, "rowid ", "colid");
// Access cell and storage object
let cell = document.querySelector(`.cell[rid="${rid}"][cid="${cid}"]`);
console.log("cell ", cell);
let cellProp = sheetDB[rid][cid];
return [cell, cellProp];
}
function decodeRidCidfromAddress(address) {
//address ->A1
let rid = Number(address.slice(1) - 1);
let cid = Number(address.charCodeAt(0)) - 65;
return [rid, cid];
}
let cells = document.querySelectorAll(".cell");
cells.forEach((cell) => {
addListenerToAttachCellProperties(cell);
});
function addListenerToAttachCellProperties(cell) {
cell.addEventListener("click", (e) => {
let rid = cell.getAttribute("rid");
let cid = cell.getAttribute("cid");
let cellProp = sheetDB[rid][cid];
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;
switch (cellProp.alignment) {
case "left":
leftAlign.style.backgroundColor = activeColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "center":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "right":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = activeColorProp;
break;
}
underline.style.backgroundColor = cellProp.underline
? activeColorProp
: inactiveColorProp;
bold.style.backgroundColor = cellProp.bold
? activeColorProp
: inactiveColorProp;
italic.style.backgroundColor = cellProp.italic
? activeColorProp
: inactiveColorProp;
BGcolor.value = cellProp.BGcolor;
fontColor.value = cellProp.fontColor;
fontSize.value = cellProp.fontSize;
fontFamily.value = cellProp.fontFamily;
});
}