Constellations/js/Grid.js

54 lines
1 KiB
JavaScript
Raw Normal View History

import CellSet;
2022-07-23 18:33:01 +02:00
import * as Dom from UnitJS.Dom;
import {iter, square} from Grid.Util;
2022-07-26 19:25:56 +02:00
2022-07-24 11:20:22 +02:00
var grid = {
element: document.getElementById('grid'),
colors: null,
missing: null,
size: null
2022-07-24 11:20:22 +02:00
};
2022-07-23 18:33:01 +02:00
return {
cell: cell,
clear: clear,
init: init,
get: get,
2022-07-24 11:20:22 +02:00
};
2022-07-23 18:33:01 +02:00
function init(size, eventHandlers) {
grid.size = size;
2022-07-23 18:33:01 +02:00
for(var row = 0; row < size; row++) {
2022-07-24 11:20:22 +02:00
grid.element.appendChild(
makeRow({tag: 'td', attributes: eventHandlers, row: row})
2022-07-24 11:20:22 +02:00
);
2022-07-23 18:33:01 +02:00
}
clear();
2022-07-25 15:40:19 +02:00
}
function makeRow(config) {
var cells = [];
for(var column = 0; column < grid.size; column++) {
2022-07-24 11:20:22 +02:00
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(
2022-07-31 16:32:24 +02:00
{type: 'rectangle', row: 0, column: 0, width: 8, height: 8}
);
iter(grid.colors, function(row, column) {
cell(row, column).className = '';
});
2022-07-25 15:40:19 +02:00
}
function get() {
return grid;
2022-07-26 19:25:56 +02:00
}
function cell(row, column) {
return grid.element.children[row].children[column];
2022-07-26 19:25:56 +02:00
}