1
0
mirror of https://github.com/danog/2048.git synced 2024-12-14 02:07:39 +01:00
2048/js/html_actuator.js
2014-03-08 13:32:37 +01:00

37 lines
865 B
JavaScript

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);
};