I have the following javascript es6 class (along with some functions) that generates an area where you can draw in a page.
class Painter {
constructor(selector, cols, rows, style) {
this.looper = null;
if (!selector)
throw new TypeError("Provide a valid table selector");
this.cols = cols || 20;
this.rows = rows || 20;
this.selector = selector;
this.style = style;
let markup = "";
for (let height = 0; height < this.rows; height++) {
markup += "<tr>";
for (let width = 0; width < this.cols; width++)
markup += "<td data-col=" + width + " data-row=" + height + "></td>";
markup += "</tr>";
}
$(function() {
$(selector).html(markup);
$(selector).on("click", "td", function() {
$(this).toggleClass("tile-active");
});
});
}
setStyleSheet(stylesheet) {
this.style = stylesheet;
}
changeTileColor(stylesheet, newColor) {
if (!newColor)
return;
var css = ".tile-active{\n\tbackground:" + newColor + ";\n}";
try {
changeCss((stylesheet || this.style), css);
} catch (e) {
console.log("Suppressed error");
}
}
createChessBoard() {
var id = this.selector;
$(function() {
$(id + " tr:nth-child(even) td:nth-child(even)").addClass("black-tile");
$(id + " tr:nth-child(odd) td:nth-child(odd)").addClass("grey-tile");
});
}
toggleBorder() {
var bname = $(this.selector).css("border");
$(this.selector).css("border", bname.indexOf("none") != -1 ? "2px solid" : "none");
}
clearBoard() {
$(this.selector + " .tile-active").removeClass("tile-active");
}
createRandowDraw() {
this.clearBoard();
let mat = $(this.selector + " td");
let max = mat.length;
for (let i = 0; i < max; i++) {
let num = Math.floor(Math.random(2) * 2);
if (num && num <= 1)
$(mat[i]).addClass("tile-active");
}
}
}
As I am new to classes in ECMAScript 6 I was wondering if there is anything I can do to improve the performance/readability of this code.
Here is a link to the following code: Painter