-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportionsTable.ts
More file actions
157 lines (129 loc) · 3.88 KB
/
portionsTable.ts
File metadata and controls
157 lines (129 loc) · 3.88 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
interface Portion<T> {
protein : T,
proteinFat : T,
fat : T,
carbs : T,
dairy : T,
fruit : T,
vegetables : T
}
type PortionType = 'protein' | 'proteinFat' | 'fat' | 'carbs' | 'dairy' | 'fruit' | 'vegetables';
interface PortionLabels extends Portion<string>{}
interface PortionAmounts extends Portion<Number>{}
class Meal{
name : string;
portions : PortionAmounts;
constructor(name : string,portions? : PortionAmounts){
this.name = name;
this.portions = portions ||
{
protein : 0,
proteinFat : 0,
fat : 0,
carbs : 0,
dairy : 0,
fruit : 0,
vegetables : 0
};
}
public getMealLabel() : string{
return this.name;
}
public substractPortion(portion : PortionType) : Number{
if(this.portions[portion]>0)
this.portions[portion] -= 1;
return this.portions[portion];
}
public addPortion(portion : PortionType) : Number{
this.portions[portion] += 1;
return this.portions[portion];
}
}
class PortionsTable{
meals : Meal[];
portionLabels : PortionLabels;
targetHTML : string;
storageKey : string;
static HOLD_TIME = 500;
constructor(meals : Meal[], tableID : string, portionLabels? : PortionLabels){
this.meals = meals;
this.targetHTML = tableID;
this.storageKey = tableID;
this.portionLabels = portionLabels ||
{
protein : 'Protein',
proteinFat : 'ProteinFat',
fat : 'Fat',
carbs : 'Carbs',
dairy : 'Dairy',
fruit : 'Fruit',
vegetables : 'Vegetables'
};
}
public createTable() : void{
let table = document.getElementById(this.targetHTML);
table.appendChild(this.createHeaderRow());
this.meals.map(meal => table.appendChild(this.createMealRow(meal)));
}
private createHeaderRow() : HTMLElement {
let headerRow : HTMLElement = document.createElement('tr');
headerRow.id = 'headerRow';
let firstHeaderCol = document.createElement('th');
firstHeaderCol.id = 'cornerCell';
firstHeaderCol.innerHTML = "";
headerRow.appendChild(firstHeaderCol);
for (let portionType in this.portionLabels){
let headerCol = document.createElement('th');
headerCol.id = portionType;
headerCol.innerHTML = this.portionLabels[portionType];
headerCol.classList.add(portionType);
headerRow.appendChild(headerCol);
}
return headerRow;
}
private createMealRow(meal : Meal) : HTMLElement{
let row : HTMLElement = document.createElement('tr');
row.id = meal.name;
this.addColumns(row,meal);
return row;
}
private addColumns(row : HTMLElement, meal : Meal) : void{
let rowHeader = this.createCell(meal.name, meal.getMealLabel());
rowHeader.classList.add("rowHeader");
row.appendChild(rowHeader);
for (let portionType in this.portionLabels){
let cell = this.createCell(meal.name+'-'+portionType, meal.portions[portionType] || '');
cell.classList.add(portionType);
this.handleCellEvents(cell, meal, <PortionType>portionType);
row.appendChild(cell);
}
}
private handleCellEvents(cell : HTMLElement, meal : Meal, portionType : PortionType) : void{
let startMousedown;
let timer;
cell.onmousedown = () =>{
startMousedown = (new Date()).getTime();
timer = setTimeout(() =>{
cell.innerHTML = '' + meal.addPortion(portionType);
this.storeData();
}, PortionsTable.HOLD_TIME);
};
cell.onmouseup = () =>{
if(new Date().getTime() - startMousedown < PortionsTable.HOLD_TIME){
clearTimeout(timer);
cell.innerHTML = (meal.substractPortion(portionType)||'')+'';
this.storeData();
}
};
}
private createCell(id : string, value : string) : HTMLElement{
let cell = document.createElement('td');
cell.id = id;
cell.innerHTML = value;
return cell;
}
private storeData() : void{
localStorage.setItem(this.storageKey, JSON.stringify(this.meals));
console.log(localStorage.getItem(this.storageKey));
}
}