-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
310 lines (268 loc) · 9.28 KB
/
script.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// targets colour palette
const form = document.querySelector('form');
// target generate button
const generateBtn = document.querySelector('.generate-btn');
// target add colour button
const addColorBtn = document.querySelector('.color__btn__add');
// target remove colour button
const removeColorBtn = document.querySelector('.color__btn__remove');
// target tile shapes radio button
const rectangleTileOption = document.querySelector(
"input[id='tile-rectangle']"
);
const squareTileOption = document.querySelector("input[id='tile-square']");
// targets tile grid div container
let tileGrid = document.querySelector('.tile-generator__grid');
// targets types of generating grid
let randomBtn = document.querySelector('#randomise');
let notAdjacentBtn = document.querySelector('#not-adjacent');
// targetting template
let template = document.querySelector('template');
let templateContent = template.content;
function cloneFragment() {
return (cloneDocumentFragment = templateContent.cloneNode(true));
}
// creating new tiles
function addTiles(event) {
cloneFragment();
let newTile = cloneDocumentFragment.querySelector('.tile');
event.appendChild(newTile);
}
function addNewRow(event) {
// targets row number
let columnNumber = document.querySelector('.column-input').value;
cloneFragment();
let newRow = cloneDocumentFragment.querySelector('.row-container');
let i = 0;
do {
addTiles(newRow);
i++;
} while (i < columnNumber);
event.appendChild(newRow);
}
// adding additional colour picker
function addColor() {
cloneFragment();
let newColor = cloneDocumentFragment.querySelector('.color-picker');
let colorPickerContainer = document.querySelector('.color-picker-container');
colorPickerContainer.appendChild(newColor);
}
// removing last colour picker
function removeColor() {
let colourPickers = form.querySelectorAll('.color-picker');
let colorPickerContainer = document.querySelector('.color-picker-container');
if (colourPickers.length > 1) {
colorPickerContainer.lastChild.remove();
} else {
// need to add text to be revealed eventually
alert('you need atleast one colour!');
}
}
// color picker value to show above color picker
function showHexColor(event) {
let currentTarget = event.currentTarget;
let colorValue = currentTarget.querySelector('input').value;
currentTarget.querySelector('label').textContent = colorValue;
}
// Tile Shape
function showWidthInput(event) {
let tileWidthInput = document.querySelector("input[id='tile-width']");
if (event.target.id === 'tile-rectangle') {
tileWidthInput.classList.remove('hidden');
} else {
tileWidthInput.classList.add('hidden');
}
}
function tileType() {
let tileHeight = document.querySelector("input[id='tile-height']").value;
let tileWidth = document.querySelector("input[id='tile-width']").value;
let rectangleChecked = document.querySelector("input[id='tile-rectangle']")
.checked;
if (tileHeight === '' || tileHeight === '0') {
tileHeight = 300;
}
if (tileWidth === '' || tileWidth === '0') {
tileWidth = 300;
}
// inserting html in style
let styleTile = document.getElementById('style__tiletype');
const scales = document.querySelectorAll("input[name='tile-scale']");
const scalesArray = [...scales];
const filterScale = scalesArray.filter((scale) => scale.checked);
let fixedScale = Number(filterScale[0].value);
if (fixedScale === 0) {
fixedScale = 20;
}
tileHeight /= fixedScale;
tileWidth /= fixedScale;
if (rectangleChecked) {
styleTile.innerHTML = `.tile {
height: ${tileHeight}mm;
width: ${tileWidth}mm;
}`;
} else {
styleTile.innerHTML = `.tile {
height: ${tileHeight}mm;
width: ${tileHeight}mm;
}`;
}
}
// creating the grid and filling with tiles
function createGrid() {
// targets column number
let rowNumber = document.querySelector('.row-input').value;
// clearing any existing tiles
while (tileGrid.firstChild) {
tileGrid.firstChild.remove();
}
// adding rows to create grid
let i = 0;
do {
addNewRow(tileGrid);
i++;
} while (i < rowNumber);
addColoursAsCSSClass();
if (randomBtn.checked === true) {
assignRandomColors();
}
if (notAdjacentBtn.checked === true) {
assignNotAdjacentColors();
}
}
function randomInteger(num) {
return Math.floor(Math.random() * num);
}
function addColoursAsCSSClass() {
// getting the colours from picker
let colorRange = document.getElementsByClassName('color-input');
let colorValues = [];
// pushing colour values into array
for (let i = 0; i < colorRange.length; i++) {
colorValues.push(colorRange[i].value);
}
// clearing any html in style
let styleColor = document.getElementById('style__colors');
styleColor.innerHTML = '';
// assigning new styles to classes
for (let i = 0; i < colorValues.length; i++) {
styleColor.insertAdjacentHTML(
'beforeend',
`.tile-color${i} {background-color:${colorValues[i]}}`
);
}
}
// obtaining colour values
function assignRandomColors() {
// getting the colours from picker
let colorRange = document.getElementsByClassName('color-input');
let colorRangeClass = [];
// pushing class value names into array
for (let i = 0; i < colorRange.length; i++) {
colorRangeClass.push(`tile-color${i}`);
}
// looping through the row containers and assign colours by class
for (let i = 0; i < tileGrid.childElementCount; i++) {
for (let j = 0; j < tileGrid.children[0].childElementCount; j++) {
let randomColor = colorRangeClass[randomInteger(colorRangeClass.length)];
tileGrid.children[i].children[j].classList.add('class', randomColor);
}
}
}
// This currently needs updating
function assignNotAdjacentColors() {
// getting the colours from picker
let colorRange = document.getElementsByClassName('color-input');
let colorRangeClass = [];
// pushing class value names into array
for (let i = 0; i < colorRange.length; i++) {
colorRangeClass.push(`tile-color${i}`);
}
// removing array element by value
for (let i = 0; i < tileGrid.childElementCount; i++) {
for (let j = 0; j < tileGrid.children[0].childElementCount; j++) {
if (i === 0 && j === 0) {
let randomColor =
colorRangeClass[randomInteger(colorRangeClass.length)];
tileGrid.children[i].children[j].classList.add('class', randomColor);
}
if (i === 0 && j > 0) {
let possibleColors = [...colorRangeClass];
// targets the color of previous tile
for (let k = 0; k < possibleColors.length; k++) {
if (
tileGrid.children[i].children[j - 1].classList.contains(
possibleColors[k]
)
) {
possibleColors.splice(k, 1);
}
}
// random color that does not include previous tile color
let randomColor = possibleColors[randomInteger(possibleColors.length)];
tileGrid.children[i].children[j].classList.add('class', randomColor);
}
if (i > 0 && j === 0) {
let possibleColors = [...colorRangeClass];
for (let k = 0; k < possibleColors.length; k++) {
// target color of tile above
if (
tileGrid.children[i - 1].children[j].classList.contains(
possibleColors[k]
)
) {
possibleColors.splice(k, 1);
}
}
// random color that does not include previous tile color
let randomColor = possibleColors[randomInteger(possibleColors.length)];
tileGrid.children[i].children[j].classList.add('class', randomColor);
}
// to check left and above tiles
if (i > 0 && j > 0) {
let possibleColors = [...colorRangeClass];
for (let k = 0; k < possibleColors.length; k++) {
// targets the color of previous tile
if (
tileGrid.children[i].children[j - 1].classList.contains(
possibleColors[k]
)
) {
possibleColors.splice(k, 1);
}
}
// target color of tile above
for (let m = 0; m < possibleColors.length; m++) {
if (
tileGrid.children[i - 1].children[j].classList.contains(
possibleColors[m]
)
) {
possibleColors.splice(m, 1);
}
}
// random color that does not include previous tile color
let randomColor = possibleColors[randomInteger(possibleColors.length)];
tileGrid.children[i].children[j].classList.add('class', randomColor);
}
}
}
}
// initial tile grid
tileType();
createGrid();
// event listeners
generateBtn.addEventListener('click', (event) => {
event.preventDefault();
tileType();
createGrid();
});
addColorBtn.addEventListener('click', (event) => {
event.preventDefault();
addColor();
});
removeColorBtn.addEventListener('click', (event) => {
event.preventDefault();
removeColor();
});
rectangleTileOption.addEventListener('click', showWidthInput);
squareTileOption.addEventListener('click', showWidthInput);