-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (125 loc) · 4.63 KB
/
index.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
//Global object which contains HTML elements
const elements = {
rowNum: document.querySelector("#rowNum"),
colNum: document.querySelector("#colNum"),
tableWrapper: document.querySelector(".table-wrapper")
};
//Create Head cells in table
const createHeadCells = (colNum, table, accRow) => {
const tableHead = document.createElement("thead");
const headTr = document.createElement("tr");
[...Array(parseInt(colNum))].reduce((accCol, curr, index) => {
let th = document.createElement("th");
let thText = document.createTextNode(`Head ${index}`);
th.appendChild(thText);
headTr.appendChild(th);
tableHead.appendChild(headTr);
table.appendChild(tableHead);
}, accRow);
};
//Create body cells in table
const createBodyCells = (colNum, table, accRow, rowIndex, tr) => {
const tblBody = document.createElement("tbody");
[...Array(parseInt(colNum))].reduce((accCol, curr, index) => {
let td = document.createElement("td");
let tdText = document.createTextNode(`row ${rowIndex}, col ${index}`);
td.appendChild(tdText);
tr.appendChild(td);
tblBody.appendChild(tr);
table.appendChild(tblBody);
}, accRow);
};
//Create new table with row & col numbers' input
const createTable = (rowNum, colNum) => {
//Clean the table elements inside of table Wrapper
elements.tableWrapper.innerHTML = "";
//Create Table HTML elements
const table = document.createElement("table");
//Create Table Rows
[...Array(parseInt(rowNum))].reduce(
(accRow, curr, rowIndex) => {
const tr = document.createElement("tr");
table.appendChild(tr);
elements.tableWrapper.appendChild(table);
console.log("accRow", accRow);
console.log("index", rowIndex);
//Create table Cols : in first row, it creates Head cells. After first row, it creates body cells
rowIndex == 0
? createHeadCells(colNum, table, accRow)
: createBodyCells(colNum, table, accRow, rowIndex, tr);
//Append table to table wrapper
elements.tableWrapper.appendChild(table);
}, table);
return table;
};
/****** Functions for giving styles to tables *******/
const changeTableWidth = newtable => {
let tableWidth = document.querySelector("#tableWidth");
newtable.style.width = `${tableWidth.value}%`;
};
const changeBorderWidthPixel = newtable => {
let borderPixel = document.querySelector("#borderPixel");
newtable.style.borderWidth = `${borderPixel.value}px`;
};
const changeTableBgColor = newtable => {
let tableBgColor = document.querySelector("#tableBgColor");
newtable.style.backgroundColor = tableBgColor.value;
};
const changeHeadBgColor = () => {
let headBgColor = document.querySelector("#headBgColor");
let tableHead = document.querySelector("thead");
tableHead.style.backgroundColor = headBgColor.value;
};
const changeBodyBgColor = () => {
let bodyBgColor = document.querySelector("#bodyBgColor");
let tableBody = document.querySelector("tbody");
tableBody.style.background = bodyBgColor.value;
};
const changeBorderColor = newtable => {
let borderColor = document.querySelector("#borderColor");
let allTableTrs = document.querySelectorAll("tr");
let allTableThs = document.querySelectorAll("th");
let allTableTds = document.querySelectorAll("td");
let borderStyle = `2px solid ${borderColor.value}`;
newtable.style.border = borderStyle;
allTableTrs.forEach(tr => {
tr.style.border = borderStyle;
});
allTableThs.forEach(th => {
th.style.border = borderStyle;
});
allTableTds.forEach(td => {
td.style.border = borderStyle;
});
};
const changeFontColor = newtable => {
let fontColor = document.querySelector("#fontColor");
newtable.style.color = fontColor.value;
};
const changeFontType = newtable => {
let fontType = document.querySelector("#fontType");
newtable.style.fontFamily = fontType.value;
};
const changeFontSize = newtable => {
let sizeForFont = document.querySelector("#fontSize");
newtable.style.fontSize = sizeForFont.value;
};
const changeTextAlign = newtable => {
let AlignForText = document.querySelector("#AlignForText");
newtable.style.textAlign = AlignForText.value;
};
/******** table generate button and event listener ********/
const generateBtn = document.querySelector(".generateBtn");
generateBtn.addEventListener("click", () => {
let newtable = createTable(elements.rowNum.value, elements.colNum.value);
changeTableWidth(newtable);
changeBorderWidthPixel(newtable);
changeTableBgColor(newtable);
changeHeadBgColor();
changeBodyBgColor();
changeBorderColor(newtable);
changeFontColor(newtable);
changeFontType(newtable);
changeFontSize(newtable);
changeTextAlign(newtable);
});