make actuator properly create html elements
[2048.git] / js / html_actuator.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
function HTMLActuator() {
  this.tileContainer = document.getElementsByClassName("tile-container")[0];
}
 
HTMLActuator.prototype.actuate = function (grid) {
  var self = this;
 
  this.clearContainer();
 
  grid.cells.forEach(function (column) {
    column.forEach(function (cell) {
      if (cell) {
        self.addTile(cell);
      }
    });
  });
};
 
HTMLActuator.prototype.clearContainer = function () {
  while (this.tileContainer.firstChild) {
    this.tileContainer.removeChild(this.tileContainer.firstChild);
  }
};
 
HTMLActuator.prototype.addTile = function (tile) {
  var element = document.createElement("div");
 
  var x = tile.x + 1;
  var y = tile.y + 1;
  var position = "tile-position-" + x + "-" + y;
 
  element.classList.add("tile", "tile-" + tile.value, position);
  element.textContent = tile.value;
 
  this.tileContainer.appendChild(element);
};
 
comments