Constellations/js/Grid.js

39 lines
786 B
JavaScript
Raw Normal View History

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