-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dom.js
73 lines (65 loc) · 1.89 KB
/
Dom.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
/* global Game, Square, selectedSquare, whiteToPlay */
/* eslint-disable no-global-assign, no-unused-vars */
const $hint = document.getElementById('hint');
$hint.addEventListener('click', Game.giveHint);
const $form = document.getElementById('notation');
$form.addEventListener('submit', Game.handleNotation);
function handleSquareClick(square) {
return function() {
Dom.clearHighlights();
if (!selectedSquare && square.piece
&& Game.isMyTurn(square.piece) && square.piece.availableSquares().length) {
selectSquare(square);
} else if (selectedSquare && selectedSquare.piece) {
if (square.piece && Game.isMyTurn(square.piece)) {
selectSquare(square);
} else {
Game.move(selectedSquare.piece, square);
}
} else {
selectedSquare = null;
}
};
}
function selectSquare(square) {
square.piece.availableSquares().forEach(sq => sq.highlight());
selectedSquare = square;
square.highlight(2);
}
Square.prototype.render = function() {
const el = this.domElement;
const piece = this.piece;
if (!piece) {
el.textContent = '';
} else {
el.classList.remove('black-piece');
el.classList.remove('white-piece');
el.classList.add(`${piece.colour}-piece`);
el.textContent = this.piece.symbol;
}
};
class Dom {
static message(text) {
document.getElementById('message-board')
.innerHTML = text;
}
static showHint(visible) {
if (visible) {
$hint.classList.remove('hidden');
} else {
$hint.classList.add('hidden');
}
}
static togglePlayer() {
whiteToPlay = !whiteToPlay;
document.querySelectorAll('.player-indicator')
.forEach(el => el.classList.toggle('hidden'));
}
static clearHighlights() {
document.querySelectorAll('.square')
.forEach(square => {
square.classList.remove('highlight1');
square.classList.remove('highlight2');
});
}
}