-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (153 loc) · 4.96 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const tableInfo = {}
const colors = [
"#1e0492",
"#550fcb",
"#e07df3",
"#6a6582",
"#c4b6cc",
"#ADFF02",
"#34343a",
"#01BEFE",
"#FFDD00",
"#FF7D00",
"#FF006D",
"#FF006D",
"#ADFF02",
"#8F00FF"
]
let cacheElements = {}
/**
* Update table infos and erase cache elements
*/
const updateTableInfo = () => {
tableInfo.tableSize = Number(document.querySelector('#tableSize').value)
tableInfo.totalSize = Math.pow(tableInfo.tableSize, 2)
tableInfo.lastColor = null
cacheElements = {}
}
/**
* Returns a randon color
* @returns { String }
*/
const getRandonColor = () => {
let color = colors[Math.floor(Math.random() * colors.length - 1)]
while (tableInfo.lastColor === color) {
color = colors[Math.floor(Math.random() * colors.length - 1)]
}
tableInfo.lastColor = color
return tableInfo.lastColor
}
/**
* Return a td with the passed index
* @param { Number } actualIndex
* @returns { HTMLElement }
*/
function getElement(actualIndex) {
if (!cacheElements[actualIndex]) {
cacheElements[actualIndex] = document.querySelector(`[data-position="${actualIndex}"]`)
}
return cacheElements[actualIndex]
}
/**
* Check if exists a left td element and return it
* @param { Number } actualIndex
* @returns { HTMLElement }
*/
function getLeftElement(actualIndex) {
const leftIndex = actualIndex - 1;
if (leftIndex % tableInfo.tableSize === tableInfo.tableSize - 1) return null
return getElement(leftIndex)
}
/**
* Check if exists a rigth td element and return it
* @param { Number } actualIndex
* @returns { HTMLElement }
*/
function getRigthElement(actualIndex) {
const rigthIndex = actualIndex + 1;
if (rigthIndex % tableInfo.tableSize === 0) return null
return getElement(rigthIndex)
}
/**
* Check if exists a top td element and return it
* @param { Number } actualIndex
* @returns { HTMLElement }
*/
function getTopElement(actualIndex) {
const topIndex = actualIndex - tableInfo.tableSize;
if (topIndex < 0) return null
return getElement(topIndex)
}
/**
* Check if exists a down td element and return it
* @param { Number } actualIndex
* @returns { HTMLElement }
*/
function getDownElement(actualIndex) {
const downIndex = actualIndex + tableInfo.tableSize;
if (downIndex > tableInfo.totalSize) return null
return getElement(downIndex)
}
/**
* Use the passed element as the source of wave and call the propagation function
* @param { HTMLElement } element
* @returns { Void }
*/
function createWave(element) {
/**
* For each passed element, call's recursivly four times, one for each direction
* The recursion stops when the element don't exists or if the element already was propagated
* @param { HTMLElement } element
* @param { Number } actualPower
* @param { Set } history
* @param { String } color
* @returns { Void }
*/
function wavePropagation(element, actualPower, history, color) {
//Recursion stop
if (!element) return;
if (history.has(element.dataset.position)) return;
const actualIndex = Number(element.dataset.position);
const delayPropagation = document.querySelector('#propagationDelay').value;
element.innerHTML = document.querySelector('#showNumbers').checked ? actualPower : null
element.bgColor = color
history.add(element.dataset.position)
setTimeout(() => {
if (document.querySelector('#propagateUp').checked) {
wavePropagation(getTopElement(actualIndex), actualPower - 1, history, color)
}
if (document.querySelector('#propagateLeft').checked) {
wavePropagation(getLeftElement(actualIndex), actualPower - 1, history, color)
}
if (document.querySelector('#propagateDown').checked) {
wavePropagation(getDownElement(actualIndex), actualPower - 1, history, color)
}
if (document.querySelector('#propagateRigth').checked) {
wavePropagation(getRigthElement(actualIndex), actualPower - 1, history, color)
}
if (document.querySelector('#justEdges').checked) {
element.bgColor = '#DCECC9'
}
}, delayPropagation);
}
const history = new Set()
wavePropagation(element, tableInfo.tableSize, history, getRandonColor())
}
/**
* Create intire table, with the positions to can be possible find each element
* @returns { Void }
*/
function inicializeTable() {
const table = document.querySelector('table');
updateTableInfo()
let tableContent = '<table>';
for (let i = 0, count = 0; i < tableInfo.tableSize; i++) {
tableContent += '<tr>'
for (let j = 0; j < tableInfo.tableSize; j++ , count++) {
tableContent += `<td data-position=${count} onclick="createWave(this)"></td>`
}
tableContent += '</tr>'
}
table.innerHTML = tableContent
}
inicializeTable();