Constellations/js/Grid.js

39 lines
794 B
JavaScript
Raw Normal View History

import size from Config;
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,
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(eventHandlers) {
grid.cells = square(size, function(cell) {
2022-08-10 21:12:39 +02:00
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(size);
grid.missing = CellSet.rectangle(zero(), diagonal(size));
iter(grid.cells, function(td) {td.className = '';});
2022-07-25 15:40:19 +02:00
}
function get() {
return grid;
2022-07-26 19:25:56 +02:00
}