Constellations/js/Grid.js

39 lines
786 B
JavaScript

import * as Dom from UnitJS.Dom;
import currentColor from Toolbox;
var grid = {
element: null,
data: {}
};
return {
init: init
};
function init(size, elementId) {
grid.element = document.getElementById(elementId || 'grid');
for(var row = 0; row < size; row++) {
grid.element.appendChild(
makeRow({tag: 'td', attributes: bodyAttributes, row: row, size: size})
);
grid.data[row] = {};
}
}
function makeRow(config) {
var cells = [];
for(var column = 0; column < config.size; column++) {
cells.push(Dom.make(config.tag, config.attributes(config.row, column)));
}
return Dom.make('tr', {}, cells);
}
function bodyAttributes(i, j) {
return {
onClick: function(e) {
grid.data[i][j] = currentColor();
e.target.className = 'color' + grid.data[i][j];
}
};
}