-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
178 lines (162 loc) · 4.43 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Selection</title>
<style>
body, html {
height: 100%;
margin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
.grid {
display: grid;
gap: 2px;
}
.cell {
width: 20px;
height: 20px;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
user-select: none;
border: 1px solid #ccc;
}
.cell:hover {
background-color: #e0e0e0;
}
.cell_column_header {
width: 20px;
height: 20px;
font-weight: bold;
background-color: #8a92ff;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
user-select: none;
}
.cell_row_header {
width: 20px;
height: 20px;
font-weight: bold;
background-color: #fdd244;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
user-select: none;
}
.cell.selected {
background-color: lightblue;
}
.inl {
display: inline-block;
margin-right: 5px;
}
.vertalign {
display: flex;
align-items: center;
}
#griddata {
width: 800px;
}
</style>
</head>
<body>
<h1>Grid Selection</h1>
<div id="explanation">
<div class="vertalign"><div class="cell selected inl"></div><span>selected led</span></div>
<div class="vertalign"><div class="cell_column_header inl"></div>controller</div>
<div class="vertalign"><div class="cell_row_header inl"></div>led</div>
</div>
<div id="griddata"></div>
<div id="grid"></div>
<script>
const gridWidth = 9; // X width
const gridHeight = 32; // Y height
let controller_selected = [];
const gridContainer = document.getElementById('grid');
gridContainer.style.gridTemplateColumns = `repeat(${gridWidth+1}, 1fr)`;
gridContainer.className = 'grid';
for (let col = -1; col < gridWidth; col++) {
const cell = document.createElement('div');
if(col == -1) {
cell.className = 'cellnothing';
} else {
cell.className = 'cell_column_header';
cell.innerHTML = col;
}
cell.dataset.row = -1;
cell.dataset.col = col;
gridContainer.appendChild(cell);
}
for (let row = 0; row < gridHeight; row++) {
const rowcell = document.createElement('div');
rowcell.className = 'cell_row_header';
rowcell.dataset.row = -1;
rowcell.dataset.col = row;
rowcell.innerHTML = row;
gridContainer.appendChild(rowcell);
for (let col = 0; col < gridWidth; col++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener('click', function() {
selectCell(row, col);
});
gridContainer.appendChild(cell);
if(col == 0) {
setCellSelected(row, col, true);
}
}
}
updateSelectedCells();
function cellIsSelected(row, col) {
const selectedCell = document.querySelector(`.cell[data-row='${row}'][data-col='${col}']`);
return selectedCell.classList.contains('selected');
}
function updateSelectedCellsText() {
const gridData = document.getElementById('griddata');
gridData.innerHTML = '';
for(let col=0;col<gridWidth;col++) {
gridData.innerHTML += `controller ${col}: ${controller_selected[col].join(', ')}<br>`;
}
}
function updateSelectedCells() {
controller_selected = [];
for(let col=0;col<gridWidth;col++) {
controller_selected[col] = [];
for(let row=0;row<gridHeight;row++) {
if(cellIsSelected(row, col)) {
controller_selected[col].push(row);
}
}
}
updateSelectedCellsText();
}
function setCellSelected(row, col, selected) {
const cell = document.querySelector(`.cell[data-row='${row}'][data-col='${col}']`);
if (selected) {
cell.classList.add('selected');
} else {
cell.classList.remove('selected');
}
}
function selectCell(row, col) {
const allCellsInRow = document.querySelectorAll(`.cell[data-row='${row}']`);
allCellsInRow.forEach(cell => cell.classList.remove('selected'));
setCellSelected(row, col, true);
updateSelectedCells();
}
</script>
</body>
</html>