-
Notifications
You must be signed in to change notification settings - Fork 2
/
cap_table.js
76 lines (54 loc) · 2.01 KB
/
cap_table.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
// Constructor
function NanoCapTable(name) {
this.name = name;
this.table = [];
}
// Computed property for totalShares
Object.defineProperty(NanoCapTable.prototype, 'totalShares', {
get: function() {
return this.table.reduce((sum, entry) => sum + entry.shares, 0);
}
});
// Add Entry
NanoCapTable.prototype.add = function(name, username, shares) {
if (shares <= 0) {
alert("Please Enter a valid share number");
return false;
}
if (this.table.some(entry => entry.username === username) || username === "") {
console.debug(DEBUG_LEVEL <= 0 & this.table)
alert("Username Already Exists");
return false;
}
let timestamp = new Date();
// Could check to see if the table is empty, and if so, add created_date.
// Push new entry
this.table.push({"timestamp": timestamp, "name": name, "username": username, "shares": shares});
//this.renderTable();
return true;
}
// Render Table on Website
/**
NanoCapTable.prototype.renderTable = function() {
const tableElement = document.getElementById("table").getElementsByTagName('tbody')[0]; // fragile
tableElement.innerHTML = "";
const totalShares = this.totalShares;
this.table.forEach(entry => {
const row = document.createElement("tr");
// Calculate ownership for current entry
const ownership = (entry.shares / totalShares * 100).toFixed(2) + '%';
// Include timestamp, name, username, shares, and calculated ownership
let data = [new Date(Date.parse(entry.timestamp)).toLocaleString(), entry.name, entry.username, entry.shares, ownership];
console.debug(typeof data[0]);
data.forEach(value => {
const cell = document.createElement("td");
cell.textContent = value;
row.appendChild(cell);
});
tableElement.appendChild(row);
});
// Update JSON display
document.getElementById("json-data").textContent = JSON.stringify(this, null, 2);
}
*/
fetchFromGitHub()