-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.js
68 lines (59 loc) · 1.58 KB
/
Cell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
export class Cell {
constructor(value = null, annotations = [], onClick = () => { }) {
this.value = Array.isArray(value) ? null : value;
this.annotations = Array.isArray(value) ? value : annotations;
this.type = Array.isArray(value) ? 'Annotations' : 'Value';
this.onClick = onClick;
}
setValue(value) {
if (typeof value === 'number') {
this.value = value;
this.type = 'Value';
}
}
removeAnnotation(number) {
const index = this.annotations.indexOf(number);
if (index > -1) {
this.annotations.splice(index, 1);
}
}
addAnnotation(number) {
if (!this.annotations.includes(number)) {
this.annotations.push(number);
}
}
toggleAnnotation(number) {
if (this.annotations.includes(number)) {
this.removeAnnotation(number);
} else {
this.addAnnotation(number);
}
this.type = 'Annotations';
}
renderValue() {
return `<div>${this.value}</div>`;
}
renderAnnotations() {
const rows = [1, 2, 3].map(i => this.renderAnnotationsRow(i)).join("");
return `<table class="annotations">${rows}</table>`;
}
renderAnnotationsRow(row) {
const cells = [1, 2, 3].map(j => this.renderAnnotationsCell(row, j)).join("");
return `<tr>${cells}</tr>`;
}
renderAnnotationsCell(row, cell) {
let value = row * 3 + cell - 3;
if (this.annotations.includes(value)) {
return `<td>${value}</td>`;
} else {
return '<td></td>';
}
}
render() {
if (this.type === 'Value') {
return this.renderValue();
} else {
return this.renderAnnotations();
}
}
}