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,
|
2022-08-06 20:15:53 +02:00
|
|
|
colors: null,
|
2022-07-28 17:39:51 +02:00
|
|
|
missing: null,
|
|
|
|
size: null
|
2022-07-24 11:20:22 +02:00
|
|
|
};
|
2022-07-28 17:39:51 +02:00
|
|
|
|
2022-07-23 18:33:01 +02:00
|
|
|
return {
|
2022-07-28 17:39:51 +02:00
|
|
|
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
|
|
|
|
2022-07-28 17:39:51 +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
|
|
|
}
|
2022-07-28 17:39:51 +02:00
|
|
|
clear();
|
2022-07-25 15:40:19 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:39:51 +02:00
|
|
|
function clear() {
|
2022-08-06 20:15:53 +02:00
|
|
|
grid.colors = square(grid.size);
|
2022-07-28 17:39:51 +02:00
|
|
|
grid.missing = CellSet.make(
|
2022-08-10 21:12:39 +02:00
|
|
|
{type: 'rectangle', origin: zero(), offset: diagonal(grid.size)}
|
2022-07-28 17:39:51 +02:00
|
|
|
);
|
2022-08-10 21:12:39 +02:00
|
|
|
iter(grid.colors, function(cell) {
|
|
|
|
at(grid.cells, cell).className = '';
|
2022-07-28 17:39:51 +02:00
|
|
|
});
|
2022-07-25 15:40:19 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 17:39:51 +02:00
|
|
|
function get() {
|
|
|
|
return grid;
|
2022-07-26 19:25:56 +02:00
|
|
|
}
|