-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (107 loc) · 3.68 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
// Step 1: HTML Structure Initialization
const app = document.getElementById("app");
app.innerHTML = `
<header>PIXEL PALETTE<header>
`;
// Step 2: State Management
const state = {
backgroundColor: "white",
color: "#ff0000",
column: 10,
row: 10,
};
// Step 3: Utility Functions
const resetState = () => {
state.backgroundColor = "white";
state.column = 10;
state.row = 10;
}
const inputChangeHandler = (key, value) => {
state[key] = value;
createGrid();
}
const createContainerDiv = (containerId) => {
const container = document.createElement("div");
container.id = containerId;
return container;
};
const createLabeledInput = (labelText, inputId, inputType = "text", inputValue) => {
const container = document.createElement("div");
const label = document.createElement("label");
const input = document.createElement("input");
label.htmlFor = inputId;
label.innerHTML = labelText;
input.id = inputId;
input.type = inputType;
input.value = inputValue;
container.classList = 'labeledInputContainer'
container.appendChild(label);
container.appendChild(input);
return { container, input };
};
const createGrid = () => {
pixelContainer.innerHTML = "";
pixelContainer.style.gridTemplateColumns = `repeat(${state.column},30px)`;
pixelContainer.style.gridTemplateRows = `repeat(${state.row},30px)`;
for (let i = 0; i < state.row * state.column; i++) {
const pixel = document.createElement("div");
pixel.classList.add("pixel");
pixel.style.backgroundColor = state.backgroundColor;
pixel.addEventListener("mousedown", (e) => {
const selectedColor = inputColor.value;
e.target.style.backgroundColor = selectedColor;
});
pixelContainer.appendChild(pixel);
}
};
const createLayout = () => {
const appContainer = createContainerDiv("appContainer");
const operationContainer = createContainerDiv("operationContainer");
const btnContainer = createContainerDiv("btnContainer");
const pixelContainer = createContainerDiv("pixelContainer");
app.appendChild(appContainer);
appContainer.appendChild(operationContainer);
appContainer.appendChild(pixelContainer);
const {container: inputColorContainer, input: inputColor} = createLabeledInput(
"Color ",
"inputColor",
"color",
"#ff0000"
);
const { container: inputRowContainer, input: inputRow } = createLabeledInput("Row ", "inputRow", "number", 10);
const { container: inputColumnContainer, input: inputColumn } = createLabeledInput(
"Column ",
"inputColumn",
"number",
10
);
const resetBtn = document.createElement("button");
resetBtn.id = "resetBtn";
resetBtn.innerHTML = "RESET";
const inputSizeContainer = createContainerDiv("inputSizeContainer");
const inputContainer = createContainerDiv("inputContainer");
inputContainer.appendChild(inputColorContainer);
operationContainer.appendChild(inputContainer);
inputSizeContainer.appendChild(inputColumnContainer);
inputSizeContainer.appendChild(inputRowContainer);
inputContainer.appendChild(inputSizeContainer);
btnContainer.appendChild(resetBtn);
operationContainer.appendChild(btnContainer);
createGrid();
return {inputColor, inputRow, inputColumn, resetBtn};
}
const {inputColor, inputRow, inputColumn, resetBtn} = createLayout();
// Step 4: Event Listeners
inputRow.addEventListener("change", (input) => {
inputChangeHandler('row', parseInt(input.target.value))
});
inputColumn.addEventListener("change", (input) => {
inputChangeHandler('column', parseInt(input.target.value))
});
resetBtn.addEventListener("click", () => {
resetState();
inputRow.value = state.row;
inputColumn.value = state.column;
inputColor.value = state.color;
createGrid();
});