Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: 1a7a6c7f63c638d217b4df9ae7ec5373f70ebbb4 (plain)
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
import * as panzoom from './modules/pan-zoom.js';
import * as gameboard from './modules/gameboard.js';
import * as recordSheet from './modules/record_sheet.js';
import * as mapSelectDialog from './modules/map_select_dialog.js';

globalThis.svgns = 'http://www.w3.org/2000/svg';

const mapPlaceholder = document.querySelector('.map-placeholder'),
  distanceOutput = document.getElementById('status'),
  proneToggle = document.getElementById('toggle-prone-counter'),
  contentVisToggleEl = document.querySelector('#content input[type="checkbox"].visible'),
  // fileName = localStorage.getItem('map') || (env === 'test' ? 'test_map' : 'map1'),
  fileName = localStorage.getItem('map') || 'map1',
  map = `assets/images/${fileName}.svg`,
  fileInputEl = document.querySelector('input[type="file"]'),

  toggleContentVis = (function () {
    document.querySelectorAll('#content div').forEach(div => {
      if (this.checked) {
        div.style.display = div.id == 'record-sheet' ? 'flex' : 'block';
      } else {
        div.style.display = 'none';
      }
    });

    localStorage.setItem('content-visibility', this.checked);
  }).bind(contentVisToggleEl),

  mapResourceObserver = new MutationObserver(function () {
    const current = document.querySelector('object');
    const resource = current.getAttribute('data');
    const next = document.createElement('object');
    next.setAttribute('type', 'image/svg+xml');
    next.style.opacity = 0;
    next.addEventListener('load', load);
    mapPlaceholder.after(next);
    mapPlaceholder.style.opacity = 1;
    next.data = resource;
    this.disconnect();
    current.remove();
    this.observe(next, { attributeFilter: ['data'] });
  });

  let mapResourceEl = document.querySelector('object');

function updateTurnCounter() {
  const turnCounter = document.getElementById('turn-count');

  if (turnCounter.dataset.update === '1') {
    turnCounter.children.namedItem('count').textContent++;
    turnCounter.dataset.update = '0';
  } else {
    turnCounter.dataset.update = '1';
  }
}

function enableEndTurnButton(allegiance) {
  document
    .querySelector(`button.end-turn:not([data-allegiance="${allegiance}"])`)
    .removeAttribute('disabled');
}

function clearMoveEndedIndicators(records) {
  records.forEach(el => el.classList.remove('movement-ended'));
}

function load() {
  URL.revokeObjectURL(this.data);
  const svg = this.contentDocument.querySelector('svg'),
    startLocs = svg.querySelector('.start-locations');

  this.style.opacity = 1;
  mapPlaceholder.style.opacity = 0;

  panzoom.start(svg);
  gameboard.start(svg);
  recordSheet.start(startLocs, gameboard.getUnits(), gameboard.unSelect, gameboard.select);
}

document.querySelectorAll('.end-turn').forEach(el =>
  el.addEventListener('click', ({ target: { dataset: { allegiance: opponent }}}) => {
    const dataSelector = `[data-allegiance="${opponent}"]`,
      opponentRecords = Array.from(document.querySelectorAll(`.soldier-record${dataSelector}`)),
      firstOpponentRecord = opponentRecords.sort((el1, el2) => el1.dataset.number > el2.dataset.number).at(0);

    el.setAttribute('disabled', '');
    updateTurnCounter();
    enableEndTurnButton(opponent);
    clearMoveEndedIndicators(opponentRecords);

    gameboard.clearFiringArcs(opponent);
    gameboard.select(firstOpponentRecord);
  })
);

document.querySelectorAll('.set-firing-arc').forEach(el =>
  el.addEventListener('click', gameboard.setFiringArc)
);

document.querySelector('.set-grenade').addEventListener('click', gameboard.setGrenade);

document.querySelectorAll('#toggle-firing-arc-vis input').forEach(el =>
  el.addEventListener('input', gameboard.toggleFiringArcVisibility)
);

document.getElementById('toggle-prone-counter').addEventListener('input', function () {
  const selected = recordSheet.getSelected();
  selected && gameboard.toggleProne();
});

contentVisToggleEl.addEventListener('input', toggleContentVis);

gameboard.setDistanceCallback((count = '-') => {
  distanceOutput.querySelector('#hex-count').textContent = count;
  distanceOutput.style.display = count === '-' ? 'none' : 'block';
});

gameboard.setProneFlagCallback(checked => proneToggle.checked = checked);
gameboard.setSelectCallback(data => recordSheet.select(data));

document.querySelectorAll('.end-move').forEach(el => el.addEventListener('click', () => {
  recordSheet.endMove();
  gameboard.endMove();
}));

document.querySelector('#fullscreen').addEventListener('click', e => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else if (document.exitFullscreen) {
    document.exitFullscreen();
  }
});

contentVisToggleEl.checked = (localStorage.getItem('content-visibility') !== 'false');
toggleContentVis();

mapSelectDialog
  .init()
  .selectCurrentOptionOnPageLoad()
  .showOnClick()
  .updateValueOnSelection()
  .changeMapOnConfirm();

mapResourceEl.addEventListener('load', load);
mapResourceEl.data = map;
mapResourceObserver.observe(mapResourceEl, { attributeFilter: ['data'] });
mapResourceEl = null;

document.querySelector('#download-save').addEventListener('click', e => {
  const data = document.querySelector('object').contentDocument.documentElement.outerHTML;
  const element = document.createElement('a');

  element.setAttribute('download', 'save.svg');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data));
  element.style.display = 'none';

  // document.body.appendChild(element);
  element.click();
  // document.body.removeChild(element);
});

document.querySelector('#upload-save').addEventListener('click', () => {
  fileInputEl.click();
});

document.querySelector('input[type="file"]').addEventListener('change', e => {
  const [file] =  fileInputEl.files;
  document.querySelector('object').data = URL.createObjectURL(file);
});