Skip to content

Commit 992ab5a

Browse files
committed
finished weapons
1 parent 64c11fc commit 992ab5a

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

app/views/spaceships/add.html.erb

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55
<title>Adicionar Nave</title>
66

77
<script>
8-
const weapons = [];
8+
let weapons = [];
99
const MAX_WEAPONS = 20;
10-
10+
let hasWeaponsCheckBox = null;
11+
let weaponsAerea = null;
12+
1113
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+
1423
await addMap();
1524
}
1625

@@ -54,6 +63,46 @@
5463

5564
}
5665

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+
57106
function addWeapon(){
58107
const weaponName = document.getElementById("weapon-name");
59108
if(!weaponName) return;
@@ -101,11 +150,8 @@
101150
}
102151

103152
function updateWeaponsDelete(event){
104-
if(!event) return;
105-
event.preventDefault();
106-
107-
if(!event.target) return;
108-
153+
if(!event || !event.target) return;
154+
109155
deleteWeapon(event);
110156
renderWeapons();
111157
}
@@ -164,22 +210,11 @@
164210
</gmp-map>
165211

166212
<%= 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 %>
175214

176-
<ul id="weapons-list"></ul>
177215

178-
</div>
216+
<div id="weapons-area"></div>
179217

180218
<% end %>
181219
</body>
182220
</html>
183-
184-
185-

0 commit comments

Comments
 (0)