|
5 | 5 | <title>Adicionar Nave</title> |
6 | 6 |
|
7 | 7 | <script> |
8 | | - const weapons = []; |
| 8 | + let weapons = []; |
9 | 9 | const MAX_WEAPONS = 20; |
10 | | - |
| 10 | + let hasWeaponsCheckBox = null; |
| 11 | + let weaponsAerea = null; |
| 12 | + |
11 | 13 | async function init() { |
12 | | - const addWeaponButton = document.getElementById("add-weapon"); |
13 | | - addWeaponButton.addEventListener('click', updateWeaponsAdd); |
| 14 | + hasWeaponsCheckBox = document.getElementById("has-weapons"); |
| 15 | + weaponsArea = document.getElementById('weapons-area'); |
| 16 | + |
| 17 | + hasWeaponsCheckBox.checked = false; |
| 18 | + removeWeaponsArea(); |
| 19 | + |
| 20 | + hasWeaponsCheckBox.addEventListener('change', updateWeaponsArea); |
| 21 | + |
| 22 | + |
14 | 23 | await addMap(); |
15 | 24 | } |
16 | 25 |
|
|
54 | 63 |
|
55 | 64 | } |
56 | 65 |
|
| 66 | + function updateWeaponsArea(event){ |
| 67 | + if(!event) return; |
| 68 | + event.preventDefault(); |
| 69 | + if(!event.target) return; |
| 70 | + |
| 71 | + const checked = event.target.checked; |
| 72 | + if(checked) addWeaponsArea(); |
| 73 | + else removeWeaponsArea(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + function addWeaponsArea(){ |
| 78 | + const label = document.createElement("label"); |
| 79 | + label.for = "weapon"; |
| 80 | + label.innerText = "Nome do Armamento"; |
| 81 | + |
| 82 | + const input = document.createElement("input"); |
| 83 | + input.type = 'text'; |
| 84 | + input.id = 'weapon-name'; |
| 85 | + |
| 86 | + const button = document.createElement('button'); |
| 87 | + button.type = 'button'; |
| 88 | + button.id = 'add-weapon'; |
| 89 | + button.innerText = 'Adicionar'; |
| 90 | + button.addEventListener('click', updateWeaponsAdd); |
| 91 | + |
| 92 | + const ul = document.createElement('ul'); |
| 93 | + ul.id = 'weapons-list'; |
| 94 | + |
| 95 | + weaponsArea.appendChild(label); |
| 96 | + weaponsArea.appendChild(input); |
| 97 | + weaponsArea.appendChild(button); |
| 98 | + weaponsArea.appendChild(ul); |
| 99 | + } |
| 100 | + |
| 101 | + function removeWeaponsArea(){ |
| 102 | + weaponsArea.innerHTML = ''; |
| 103 | + weapons = []; |
| 104 | + } |
| 105 | + |
57 | 106 | function addWeapon(){ |
58 | 107 | const weaponName = document.getElementById("weapon-name"); |
59 | 108 | if(!weaponName) return; |
|
101 | 150 | } |
102 | 151 |
|
103 | 152 | function updateWeaponsDelete(event){ |
104 | | - if(!event) return; |
105 | | - event.preventDefault(); |
106 | | - |
107 | | - if(!event.target) return; |
108 | | - |
| 153 | + if(!event || !event.target) return; |
| 154 | + |
109 | 155 | deleteWeapon(event); |
110 | 156 | renderWeapons(); |
111 | 157 | } |
|
164 | 210 | </gmp-map> |
165 | 211 |
|
166 | 212 | <%= form.label :has_weapons, "Possui Armamento" %> |
167 | | - <%= form.check_box :has_weapons, id: "has_weapons" %> |
168 | | - |
169 | | - |
170 | | - <div style="display: block;"> |
171 | | - <%= form.label :weapon, "Nome do Armamento" %> |
172 | | - <%= form.text_field :weapon, id: "weapon-name" %> |
173 | | - <button type="button" id="add-weapon">Adicionar</button> |
174 | | - |
| 213 | + <%= form.check_box :has_weapons, id: "has-weapons", checked: false %> |
175 | 214 |
|
176 | | - <ul id="weapons-list"></ul> |
177 | 215 |
|
178 | | - </div> |
| 216 | + <div id="weapons-area"></div> |
179 | 217 |
|
180 | 218 | <% end %> |
181 | 219 | </body> |
182 | 220 | </html> |
183 | | - |
184 | | - |
185 | | - |
|
0 commit comments