Task:
Draw a grid with a given number of rows and columns. Each cell can be any color. The same grid should also be updated at a predetermined time interval. The grid should cover the entire visible portion of the page in the browser. Add a context menu with which you can change the parameters of the lattice.
I would really like to hear feedback on the code: syntax, logic and general any comments and tips.
I'm a young programmer so I want to hear real reviews for further development.
<!DOCTYPE HTML>
<html style="height:100%">
<head>
<meta charset="utf-8">
<title></title>
<link href="css/common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/grid.js"></script>
<script type="text/javascript">
window.onload = function () {
var grid = new Grid ("20", // rows
"6", // columns
"2000"); // update grid interval
}
</script>
</head>
<body style="margin:0; height:100%">
</body>
</html>
JS:
var interval = 0;
var stopUpdateGrid = false;
function Grid(numRows, numColumns, updateInterval) {
this.numRows = numRows;
this.numColumns = numColumns;
this.updateInterval = updateInterval;
var clientWidth = document.body.clientWidth;
var clientHeight = document.body.clientHeight;
(function createGrid(rows, columns) {
var container = document.createElement("div");
document.body.appendChild(container);
container.style.width = clientWidth + "px";
container.style.height = clientHeight + "px";
container.setAttribute("id", "container");
for (var i = 1; i <= rows * columns; i++) {
var div = document.createElement("div");
container.appendChild(div);
div.style.width = (clientWidth - rows - 1) / rows + "px";
div.style.height = (clientHeight - columns - 1) / columns + "px";
div.style.cssFloat = "left";
div.style.margin = "-1px 0 0 -1px";
div.style.border = "1px solid #000";
var n = randomColors();
div.style.backgroundColor = "#" + n;
}
})(this.numRows, this.numColumns);
function randomColors() {
var min = 0;
var max = 15;
var colors = '';
for (var i = 1; i <= 6; i++) {
var rand = min - 0.5 + Math.random() * (max - min + 1);
rand = Math.round(rand);
rand = parseInt(rand, 10).toString(16);
colors = colors + rand;
}
return colors;
}
(function updateColorsGrid(updInterval, rows, columns) {
if (interval) {
clearInterval(interval);
}
interval = setInterval(function() {
if (stopUpdateGrid === false) {
var container = document.getElementById("container");
document.body.removeChild(container);
var grid = new Grid(rows, columns, updInterval);
}
}, updInterval);
})(this.updateInterval, this.numRows, this.numColumns);
document.onkeyup = function(e) {
e = e || window.event;
if (e.keyCode === 13) {
stopUpdateGrid = true;
changeGridOptions();
}
return false;
};
(function createNotification() {
var container = document.getElementById("container");
var notificationContainer = document.createElement("div");
container.appendChild(notificationContainer);
notificationContainer.style.width = "500px";
notificationContainer.style.height = "50px";
notificationContainer.innerHTML = "Press Enter to show menu change!!!";
notificationContainer.style.color = "#fff";
notificationContainer.style.fontSize = "30px";
notificationContainer.style.position = "absolute";
})();
function changeGridOptions() {
var container = document.getElementById("container");
var changeOptionsForm = document.createElement("form");
container.appendChild(changeOptionsForm);
changeOptionsForm.style.width = "200px";
changeOptionsForm.style.height = "200px";
changeOptionsForm.setAttribute("id", "changeOptionsForm");
changeOptionsForm.style.border = "2px solid #fff";
changeOptionsForm.style.backgroundColor = "#808080";
changeOptionsForm.style.position = "absolute";
changeOptionsForm.style.left = clientWidth - 220 + "px";
changeOptionsForm.style.top = "15px";
var changeRowsText = document.createElement("div");
changeOptionsForm.appendChild(changeRowsText);
changeRowsText.style.width = "50px";
changeRowsText.style.height = "20px";
changeRowsText.style.margin = "10px";
changeRowsText.innerHTML = "Rows:";
changeRowsText.style.cssFloat = "left";
var changeRows = document.createElement("input");
changeOptionsForm.appendChild(changeRows);
changeRows.type = "text";
changeRows.style.width = "100px";
changeRows.style.height = "20px";
changeRows.style.border = "2px solid #000";
changeRows.style.margin = "10px";
changeRows.style.cssFloat = "left";
var changeColumnsText = document.createElement("div");
changeOptionsForm.appendChild(changeColumnsText);
changeColumnsText.style.width = "50px";
changeColumnsText.style.height = "20px";
changeColumnsText.style.margin = "10px";
changeColumnsText.innerHTML = "Columns:";
changeColumnsText.style.cssFloat = "left";
var changeColumns = document.createElement("input");
changeOptionsForm.appendChild(changeColumns);
changeColumns.type = "text";
changeColumns.style.width = "100px";
changeColumns.style.height = "20px";
changeColumns.style.border = "2px solid #000";
changeColumns.style.margin = "10px";
changeColumnsText.style.cssFloat = "left";
var changeDelayUpdateGridText = document.createElement("div");
changeOptionsForm.appendChild(changeDelayUpdateGridText);
changeDelayUpdateGridText.style.width = "50px";
changeDelayUpdateGridText.style.height = "20px";
changeDelayUpdateGridText.style.margin = "10px";
changeDelayUpdateGridText.innerHTML = "Delay Update:";
changeDelayUpdateGridText.style.cssFloat = "left";
var changeDelayUpdateGrid = document.createElement("input");
changeOptionsForm.appendChild(changeDelayUpdateGrid);
changeDelayUpdateGrid.type = "text";
changeDelayUpdateGrid.style.width = "100px";
changeDelayUpdateGrid.style.height = "20px";
changeDelayUpdateGrid.style.border = "2px solid #000";
changeDelayUpdateGrid.style.margin = "10px";
changeDelayUpdateGrid.style.cssFloat = "left";
var changeButton = document.createElement("input");
changeOptionsForm.appendChild(changeButton);
changeButton.type = "button";
changeButton.value = "change";
changeButton.style.width = "100px";
changeButton.style.height = "25px";
changeButton.style.margin = "20px 50px";
changeButton.style.cssFloat = "left";
changeButton.onclick = function() {
numRows = changeRows.value || numRows;
numColumns = changeColumns.value || numColumns;
updateInterval = changeDelayUpdateGrid.value || updateInterval;
changeOptionsForm.style.display = "none";
stopUpdateGrid = false;
document.body.removeChild(container);
var grid = new Grid(numRows, numColumns, updateInterval);
};
}
}