Constellations/js/Grid.js

53 lines
1 KiB
JavaScript

import CellSet;
import * as Dom from UnitJS.Dom;
import {iter, square} from Grid.Util;
var grid = {
element: document.getElementById('grid'),
colors: null,
missing: null,
size: null
};
return {
cell: cell,
clear: clear,
init: init,
get: get,
};
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.colors = square(grid.size);
grid.missing = CellSet.make(
{type: 'rectangle', row: 0, column: 0, width: 8, height: 8}
);
iter(grid.colors, function(row, column) {
cell(row, column).className = '';
});
}
function get() {
return grid;
}
function cell(row, column) {
return grid.element.children[row].children[column];
}