-
Notifications
You must be signed in to change notification settings - Fork 6
/
tetra-listing.js
76 lines (70 loc) · 2.12 KB
/
tetra-listing.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
69
70
71
72
73
74
75
76
import { models } from './tetra-challenge-models.js';
let selectedRow;
const table = document.getElementById( "partsTable" );
const tbody = table.createTBody();
for (const tetra of models) {
const tr = tbody.insertRow();
fillRow(tr, tetra);
tr.addEventListener("click", () => selectTetra( tetra, tr ) );
}
selectTetra( models[ 0 ], tbody .rows[ 0 ] );
function selectTetra( tetra, tr ) {
const { url, id } = tetra;
if(url) {
switchModel(url);
} else {
alert("Tetrahedron #" + id + " is not available.\n\nPlease help us collect the full set.");
}
if ( selectedRow ) selectedRow.className = "";
selectedRow = tr;
selectedRow.className = "selected";
const index = document.getElementById( "index" );
index.textContent = id;
}
function fillRow(tr, tetra) {
const { id, url } = tetra;
const chiral = (tetra.chiral === "true");
if(!tr.id) {
tr.id = "tetra-" + id;
}
tr.setAttribute("data-id", id);
// Id column
let td = tr.insertCell();
td.className = url ? "ident done" : "ident todo";
if(chiral) {
td.className += " chiral";
}
td.innerHTML = id;
// Balls column
td = tr.insertCell();
const ballsTd = td; // populate later
ballsTd.className = "balls";
// strut columns
let nStruts = listStrutsByColor(tr, tetra.blu, "blu");
nStruts += listStrutsByColor(tr, tetra.yel, "yel");
nStruts += listStrutsByColor(tr, tetra.red, "red");
// The number of balls is not in the json data.
// It is calculated from the number of struts
// and also serves as a simplistic checksum
// using Euler's formula as applied to tetrahedra:
// F + V = E + 2
// Where F = 4, E = nStruts and V = the number of balls shown.
ballsTd.innerHTML = nStruts - 2;
}
function listStrutsByColor(tr, strutCounts, color) {
let nStruts = 0;
for(let i = 0; i < 3; i++) {
const td = tr.insertCell();
td.className = color;
const num = strutCounts[i];
if(num != 0) {
td.innerHTML = num;
nStruts += num;
}
}
return nStruts;
}
function switchModel( url ) {
const viewer = document.getElementById( "viewer" );
viewer.src = url;
}