-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealplanner.html
243 lines (216 loc) · 7.22 KB
/
mealplanner.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Meal Planner</title>
</head>
<body>
<h1>Meal Planner</h1>
<div class="wrapper container">
<div class="row">
<div class="col-md-4">
<h2>Meal Planner</h2>
<div id="mealtable">
</div>
</div>
<div class="col-md-auto">
<span class="align-middle">
<button type="button" class="btn btn-primary" id="populate-button">← Populate</button>
</span>
</div>
<div class="col-md-auto">
<h2>Meal Options</h2>
<div id="optiontable">
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
var choice_id = "mealtable";
//var mealoptions = [ "Jacket Potatoes", "Burgers", "Sausage and mash", "Jar curry", "Flat breads", "Pasta and sauce", "Beans on toast" ];
var mealoptions = [];
// I can't believe Javascript is this primitive :(
var weekdays = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
function retrieve_meal_options() {
// get from local storage
mealoptions = JSON.parse(localStorage.getItem('mealoptions'))
if(mealoptions === null) {
mealoptions = []
}
}
function get_random(min, max) {
// get between min and max (inclusive?)
return Math.floor(Math.random() * (+max+1 - +min)) + +min;
}
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function randomize_choices() {
choices = []
// https://www.samanthaming.com/tidbits/35-es6-way-to-clone-an-array
checked_options = get_options()
alloptions = shuffle([...checked_options])
for (var i = 0; i < weekdays.length; i++) {
choice = alloptions.pop()
choices.push(choice )
}
return choices;
}
function pack_choices(choices) {
packed_choices = [["Day", "Meal", ""]]
for (var i = 0; i < weekdays.length; i++) {
day = weekdays[i]
choice = choices[i]
packed_choices.push([ day, choice ])
}
return packed_choices;
}
function set_choice_table(container, data) {
container.empty()
var table = $("<table/>").addClass("table table-striped table-bordered table-hover");
$.each(data, function(rowIndex, r) {
if(rowIndex == 0) {
outerrow = $("<thead/>").addClass("thead-light")
innerrow = $("<tr/>")
outerrow.append(innerrow)
} else if(rowIndex == 1) {
outerrow = $("<tbody/>")
innerrow = $("<tr/>")
outerrow.append(innerrow)
} else {
outerrow = $("<tr/>")
innerrow = outerrow
}
$.each(r, function(colIndex, c) {
innerrow.append($("<t"+(rowIndex == 0 ? "h" : "d")+"/>").text(c));
});
if(rowIndex > 0) {
innerrow.append($('<td width="100px"><button type="button" class="btn-primary up">▲</button><button type="button" class="btn-primary down">▼</button></td>'))
}
table.append(outerrow);
});
appended = container.append(table);
$(".up,.down").click(function() {
var row = $(this).parents("tr:first");
index = $(row).index()
if ($(this).is(".up")) {
if(index>0) {
swap = choices[index]
choices[index] = choices[index-1]
choices[index-1] = swap
}
} else {
if(index<choices.length-1){
swap = choices[index]
choices[index] = choices[index+1]
choices[index+1] = swap
}
}
packed_choices = pack_choices(choices)
set_choice_table($("#mealtable"), packed_choices)
});
return appended
}
function _get_add_option_row() {
var append_row = `<div class="input-group mb-3">
<input type="text" id="add-meal-textbox" class="form-control" placeholder="Meal" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" id="add-meal-button">Add meal choice</button>
</div>
</div>`
return append_row
}
function get_options() {
all_options = []
$.each($("input[name='options']:checked"), function() { all_options.push($(this).text()) } )
return all_options
}
function list_to_object(inlist) {
my_object = {}
$.each(inlist, function(rowIndex, r) {
my_object[r] = 1
})
return my_object
}
function get_checked_options_lookup() {
checked_options_list = get_options()
return list_to_object(checked_options_list)
}
function render_option_table(container, optionlist, checked_options) {
container.empty()
form = $("<form/>")
$.each(optionlist, function(rowIndex, r) {
inputdiv = $("<div/>").addClass("form-check")
inputbox = $("<input/>").attr("type", "checkbox").attr("name", "options").addClass("form-check-input").text(r)
if(r in checked_options) { inputbox.attr("checked", ""); }
inputlabel = $("<label/>").text(r).addClass("form-check-label")
inputdiv.append(inputbox)
inputdiv.append(inputlabel)
form.append(inputdiv);
})
container.append(form);
option_row = container.append(_get_add_option_row())
$("#add-meal-button").bind("click", function(event) {
meal_text = $("input", event.target.parentElement.parentElement).val()
if (meal_text != "") {
add_option(meal_text);
}
});
$('#add-meal-textbox').keypress(function(e) {
if(e.keyCode == 13) {
meal_text = $(e.target).val()
if (meal_text != "") {
add_option(meal_text);
}
}
})
return option_row
}
function populate_random() {
if(mealoptions.length < 7) {
alert("not enough options!");
return;
}
choices = randomize_choices()
packed_choices = pack_choices(choices)
set_choice_table($("#mealtable"), packed_choices)
}
function add_option(new_option) {
mealoptions.push(new_option)
check_options_lookup = get_checked_options_lookup()
check_options_lookup[new_option] = 1
localStorage.setItem('mealoptions', JSON.stringify(mealoptions))
render_option_table($("#optiontable"), mealoptions, check_options_lookup)
}
$( document ).ready(function() {
retrieve_meal_options()
render_option_table($("#optiontable"), mealoptions, list_to_object(mealoptions))
$("#populate-button").bind("click", populate_random)
});
</script>
</body>
</html>