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