Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatalin Mititiuc <webdevcat@proton.me>2024-04-30 16:58:12 -0700
committerCatalin Mititiuc <webdevcat@proton.me>2024-04-30 16:58:12 -0700
commitd393934679290ab4e16aa0b4a73408273eb26596 (patch)
tree3b660a79ee2c9e062675b87f7bda9afa4a06f8ca /src/modules/record_sheet.js
parent9c67c4a4a538c44fe8365536fa3dc4b1cc973b4b (diff)
Refactor index.js
Diffstat (limited to 'src/modules/record_sheet.js')
-rw-r--r--src/modules/record_sheet.js60
1 files changed, 45 insertions, 15 deletions
diff --git a/src/modules/record_sheet.js b/src/modules/record_sheet.js
index 88c8e15..736ffb2 100644
--- a/src/modules/record_sheet.js
+++ b/src/modules/record_sheet.js
@@ -46,6 +46,39 @@ function createRecord({ dataset: { allegiance, number }}) {
return div;
}
+function createRecords(units) {
+ const grouped = Array.from(units).reduce((acc, unit) => {
+ acc[unit.dataset.allegiance]?.push(unit) || (acc[unit.dataset.allegiance] = [unit]);
+ return acc;
+ }, {});
+
+ for (const al in grouped) {
+ grouped[al] = grouped[al].map(createRecord);
+ }
+
+ return grouped;
+}
+
+function clear() {
+ document.querySelectorAll('#attacker-record > div, #defender-record > div').forEach(el => el.remove());
+ document.querySelector('#attacker-record .name').textContent = 'attacker';
+ document.querySelector('#defender-record .name').textContent = 'defender';
+}
+
+function addEventListeners(unSelectCounter, selectCounter) {
+ document.querySelectorAll('.soldier-record').forEach(el =>
+ el.addEventListener('click', () => {
+ if (el.classList.contains('selected')) {
+ el.classList.remove('selected');
+ unSelectCounter();
+ unSelect();
+ } else {
+ selectCounter(el);
+ }
+ })
+ );
+}
+
export function unSelect() {
const selected = getSelected();
@@ -79,21 +112,18 @@ export function endMove() {
unSelect();
}
-export function createRecords(units, { content }) {
- const grouped = Array.from(units).reduce((acc, unit) => {
- acc[unit.dataset.allegiance]?.push(unit) || (acc[unit.dataset.allegiance] = [unit]);
- return acc;
- }, {});
-
- for (const al in grouped) {
- grouped[al] = grouped[al].map(createRecord);
+export function start(startLoc, units, gbUnSelect, gbSelect) {
+ clear();
+ const forces = createRecords(units);
+
+ for (const affiliation in forces) {
+ const container = document.querySelector(`#${affiliation}-record`);
+ const name = startLoc.dataset[`${affiliation}Name`];
+ if (name) {
+ container.querySelector('.name').textContent = name;
+ }
+ forces[affiliation].forEach(r => container.appendChild(r));
}
- return grouped;
-}
-
-export function clear() {
- document.querySelectorAll('#attacker-record > div, #defender-record > div').forEach(el => el.remove());
- document.querySelector('#attacker-record .name').textContent = 'attacker';
- document.querySelector('#defender-record .name').textContent = 'defender';
+ addEventListeners(gbUnSelect, gbSelect);
}