Constellations/js/Grid.js

66 lines
1.3 KiB
JavaScript

import CellSet;
import * as Dom from UnitJS.Dom;
var grid = {
element: document.getElementById('grid'),
data: null,
missing: null,
size: null
};
var iter = generate; // alias for more intuitive use (discarding the result)
return {
cell: cell,
clear: clear,
init: init,
get: get,
iter: iter,
};
function init(size, eventHandlers) {
grid.size = size;
for(var row = 0; row < size; row++) {
grid.element.appendChild(
makeRow({tag: 'td', attributes: eventHandlers, row: row})
);
}
clear();
}
function makeRow(config) {
var cells = [];
for(var column = 0; column < grid.size; column++) {
cells.push(Dom.make(config.tag, config.attributes(config.row, column)));
}
return Dom.make('tr', {}, cells);
}
function clear() {
grid.data = generate(function() {return; });
grid.missing = CellSet.make(
{type: 'rectangle', row: 0, column: 0, width: 8, height: 8}
);
iter(function(row, column) {
cell(row, column).className = '';
});
}
function generate(f) {
var result = [];
for(var row = 0; row < grid.size; row++) {
result[row] = [];;
for(var column = 0; column < grid.size; column++) {
result[row].push(f(row, column));
}
}
return result;
}
function get() {
return grid;
}
function cell(row, column) {
return grid.element.children[row].children[column];
}