Constellations/js/Grid.js

44 lines
887 B
JavaScript
Raw Normal View History

2022-08-10 21:12:39 +02:00
import * as CellSet from Geometry.CellSet;
2022-07-23 18:33:01 +02:00
import * as Dom from UnitJS.Dom;
2022-08-10 21:12:39 +02:00
import {at, generate, iter, square} from Grid.Util;
import {diagonal, zero} from Geometry.Vector;
2022-07-26 19:25:56 +02:00
2022-07-24 11:20:22 +02:00
var grid = {
2022-08-10 21:12:39 +02:00
root: document.getElementById('grid'),
cells: null,
colors: null,
missing: null,
size: null
2022-07-24 11:20:22 +02:00
};
2022-07-23 18:33:01 +02:00
return {
clear: clear,
get: get,
2022-08-10 21:12:39 +02:00
init: init,
2022-07-24 11:20:22 +02:00
};
2022-07-23 18:33:01 +02:00
function init(size, eventHandlers) {
grid.size = size;
2022-08-10 21:12:39 +02:00
grid.cells = generate(size, size, function(cell) {
return Dom.make('td', eventHandlers(cell));
});
2022-07-23 18:33:01 +02:00
for(var row = 0; row < size; row++) {
2022-08-10 21:12:39 +02:00
grid.root.appendChild(Dom.make('tr', {}, grid.cells[row]));
2022-07-23 18:33:01 +02:00
}
clear();
2022-07-25 15:40:19 +02:00
}
function clear() {
grid.colors = square(grid.size);
grid.missing = CellSet.make(
2022-08-10 21:12:39 +02:00
{type: 'rectangle', origin: zero(), offset: diagonal(grid.size)}
);
2022-08-10 21:12:39 +02:00
iter(grid.colors, function(cell) {
at(grid.cells, cell).className = '';
});
2022-07-25 15:40:19 +02:00
}
function get() {
return grid;
2022-07-26 19:25:56 +02:00
}