-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddRecipe.html
440 lines (379 loc) · 17 KB
/
AddRecipe.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript"
src="Scripts/p5.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
max-width: 600px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.text-input,
textarea {
/* width: 2fr; */
padding: 5px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
resize: vertical;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.time-container {
display: inline-block;
margin-right: 10px;
}
.time-container label,
.time-container input {
display: inline;
}
.ingredient-container {
margin-bottom: 10px;
}
.ingredient-container input,
.ingredient-container select {
margin-right: 5px;
}
/* Mobile Styles */
@media only screen and (max-width: 1060px) {
/* Adjust styles for smaller screens */
.time-container {
display: block;
}
.time-container input {
margin-bottom: 10px;
}
.ingredient-subcontainer {
flex-direction: column;
align-items: flex-start;
}
.ingredient-subcontainer input,
.ingredient-subcontainer select {
width: 100%;
margin-bottom: 10px;
}
}
/* Tablet Styles */
@media only screen and (min-width: 601px) and (max-width: 1124px) {
/* Adjust styles for tablet-sized screens */
.time-container {
display: flex;
}
.time-container input {
margin-bottom: 0;
}
}
/* Desktop Styles */
@media only screen and (min-width: 1125px) {
/* Adjust styles for larger screens */
.time-container {
display: flex;
}
.time-container input {
margin-bottom: 0;
}
}
</style>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add Recipe</title>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GSJV2RPDMC"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-GSJV2RPDMC');
</script>
<body>
<h1>Add Recipe</h1>
<h2>How to Add a Recipe:</h2>
<p>Welcome! To add a recipe, please follow these steps:</p>
<ol>
<li>Fill in the required information</li>
<li>Add Ingredients:</li>
<ul>
<li>Click on the "Add Ingredient" button to add an ingredient.</li>
<li>For each ingredient, enter the quantity, unit, and ingredient name.</li>
<li>Click on the "Add Ingredient Subheading" button to add a subheading for a section of ingredients (e.g., sauce).</li>
</ul>
<li>Add Instructions:</li>
<ul>
<li>Enter the recipe instructions, with each line representing a new instruction.</li>
<li>If you want to create a subheading within the instructions, write the name of the subheading followed by a ":" at the end.</li>
<li>For any optional food items, write the heading "Optional:". </li>
</ul>
<li>Optional: Provide Credit</li>
<ul>
<li>If you would like to be credited for the recipe, enter your name or username in the Credit field.</li>
</ul>
<li>Preview and Save:</li>
<ul>
<li>Click the "Preview" button to review the filled-out recipe.</li>
<li>Make sure all fields are correctly filled.</li>
<li>Click the "Save" button to generate a text file of the recipe.</li>
<li>Send the text file to the intended recipient.</li>
</ul>
</ol>
<!-- <h1>Add Recipe</h1>
<h2>How to add a recipe:</h2>
<p>To add a recipe, fill in the boxes below. </p>
<p>Click on the "Add Ingredient" button to add additional ingredients.</p>
<p>Click on the "Add Ingredient Subheading" to add a subheading to the ingredients. This can be used to have a list of ingredients for the sauce of a dish for example.</p>
<p> For the instructions, each line is a new instruction. If you want a subheading for the instructions, write the name of the subheading and leave a ":" at the end to show it is a subheading.</p>
<p> If you would like credit, fill out the box. Otherwise leave it blank.</p>
<p> Click Preview to make sure things have been filled out correctly.</p>
<p> Then hit save and send the text file to me</p> -->
<label for="recipeTitle">Recipe Title:</label>
<input class = "text-input" type="text" id="recipeTitle" name="recipeTitle" ><br>
<label for="recipeCategory">Category:</label>
<input class = "text-input" type="text" id="recipeCategory" name="recipeCategory" placeholder="E.g Chicken"><br>
<div class="time-container">
<label for="prepHours">Prep Time:</label>
<input class = "text-input" type="number" id="prepHours" name="prepHours" style="width: 40px; text-align: center;"placeholder="HH" min = "0">
<span style="font-weight: bold;">:</span>
<input class = "text-input"type="number" id="prepMinutes" name="prepMinutes" style="width: 40px; text-align: center;"placeholder="MM" min="0" max="60">
</div>
<div class="time-container">
<label for="cookHours">Cook Time:</label>
<input class = "text-input" type="number" id="cookHours" name="cookHours" style="width: 40px; text-align: center;"placeholder="HH" min="0">
<span style="font-weight: bold;">:</span>
<input class = "text-input" type="number" id="cookMinutes" name="cookMinutes" style="width: 40px; text-align: center;"placeholder="MM" min="0" max="60">
</div>
<label for="recipeServes">Serves:</label>
<input class = "text-input" type="number" id="recipeServes" name="recipeCategory" min="1"placeholder="E.g. 4"><br>
<div>
<label for="ingredients">Ingredients:</label>
<div id="ingredient-list">
<div class="ingredient-container">
<div class = "ingredient-subcontainer">
<input type="number" class="ingredient-quantity" placeholder="Quantity">
<select class="ingredient-unit">
<option value="Unit">Unit</option>
<option value="N/A">N/A</option>
<option value="g">g</option>
<option value="kg">kg</option>
<option value="oz">oz</option>
<option value="lbs">lbs</option>
<option value="ml">ml</option>
<option value="L">L</option>
<option value="tsp">tsp</option>
<option value="tbsp">tbsp</option>
</select>
<input type="text" class="ingredient-name" placeholder="Ingredient">
</div>
</div>
</div>
<button id="add-ingredient-btn" onclick = "add_ingredient()">Add Ingredient</button>
<button id="add-subheading-btn" onclick = "add_ingredient_heading()">Add Ingredient Subheading</button>
</div>
<label for="recipeInstructions">Instructions:</label>
<textarea id="recipeInstructions" name="recipeInstructions" rows="4" cols="50"></textarea><br>
<label for="recipeCredit">Credit:</label>
<input class = "text-input" type="text" id="recipeCredit" name="recipeCredit"><br>
<input type="submit" value="Save" onclick = "save_data()">
<input type="submit" value="Preview" onclick="preview()">
<div id="previewContainer"></div>
<script>
function add_ingredient(){
//var ingredientList = document.getElementById("ingredient-list");
var ingredientLists = document.getElementsByClassName("ingredient-container")
var ingredientList = ingredientLists[ingredientLists.length-1]
var ingredientContainer = document.createElement("div");
ingredientContainer.className = "ingredient-subcontainer";
var quantityInput = document.createElement("input");
quantityInput.type = "number";
quantityInput.className = "ingredient-quantity";
quantityInput.placeholder = "Quantity";
var unitSelect = document.createElement("select");
unitSelect.className = "ingredient-unit";
var unitOptions = ["Unit","N/a","g", "kg","oz", "lbs", "ml", "L", "tsp", "tbsp"];
for (var i = 0; i < unitOptions.length; i++) {
var option = document.createElement("option");
option.value = unitOptions[i];
option.text = unitOptions[i];
unitSelect.appendChild(option);
}
var nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.className = "ingredient-name";
nameInput.placeholder = "Ingredient";
ingredientContainer.appendChild(quantityInput);
ingredientContainer.appendChild(unitSelect);
ingredientContainer.appendChild(nameInput);
ingredientList.appendChild(ingredientContainer);
}
function add_ingredient_heading(){
var ingredientList = document.getElementById("ingredient-list");
var ingredientContainer = document.createElement("div");
ingredientContainer.className = "ingredient-container";
var subheadingInput = document.createElement("input");
subheadingInput.type = "text";
subheadingInput.className = "ingredient-subheading";
subheadingInput.placeholder = "Subheading";
ingredientContainer.appendChild(subheadingInput);
ingredientList.appendChild(ingredientContainer);
}
function preview(){
if (validateForm()){
var previewText = take_form_data('html');
var previewContainer = document.getElementById("previewContainer");
var previewElement = document.createElement("div");
previewElement.innerHTML = previewText;
previewContainer.appendChild(previewElement);
}
}
function save_data(){
if (validateForm()){
var text = take_form_data('text')
var blob = new Blob([text[0]], { type: "text/plain" });
console.log(text)
//Create a temporary <a> element and set its attributes
var downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = text[1];
// Programmatically trigger a click event on the download link
downloadLink.click();
// Clean up the URL object
URL.revokeObjectURL(downloadLink.href);
}
}
function take_form_data(format){
var string_data = ""
var text_file = ""
var recipe_title = document.getElementById("recipeTitle").value
string_data += "<h1>" + recipe_title + "</h1>"
text_file += recipe_title + "\n"
var recipeCategory = document.getElementById("recipeCategory").value
string_data += "<h2>Category: " + recipeCategory+ "</h2>"
text_file += "Category: "+recipeCategory + "\n"
var recipePrepTime = document.getElementById("prepHours").value + ":" + document.getElementById("prepMinutes").value
string_data += "<h2>Prep Time: " +convert_to_time(recipePrepTime)+ "</h2>"
text_file += "Prep Time: " +convert_to_time_text_file(recipePrepTime) + "\n"
var recipeCookTime = document.getElementById("cookHours").value + ":" + document.getElementById("cookMinutes").value
string_data += "<h2>Cook Time: " +convert_to_time(recipeCookTime)+ "</h2>"
text_file += "Cook Time: " +convert_to_time_text_file(recipeCookTime) + "\n"
//Serving
var recipeServes = document.getElementById("recipeServes").value
string_data += "<h2>Serves: " +recipeServes+ "</h2>"
text_file += "Serves: " + recipeServes+ "\n"
//Ingredient Section
string_data += "<h2>Ingredients: </h2><ul>"
text_file += "Ingredients:\n"
var ingredient_sections = document.getElementsByClassName("ingredient-container")
for (let j = 0; j < ingredient_sections.length; j++){
var ingredients = ingredient_sections[j].getElementsByClassName("ingredient-subcontainer")
var sub_heading = ingredient_sections[j].getElementsByClassName("ingredient-subheading")[0]
if (sub_heading != undefined){
console.log("subheading:" +sub_heading.value)
text_file += sub_heading.value
string_data += "</ul>" + "<h3>" + sub_heading.value + "</h3>"+ "<ul>"
}
for (let i = 0; i < ingredients.length; i++){
let ing_quantity = ingredients[i].getElementsByClassName("ingredient-quantity")[0].value
let ing_unit_html = ingredients[i].getElementsByClassName("ingredient-unit")[0]
let ing_unit = ing_unit_html.options[ing_unit_html.selectedIndex].value
if (ing_unit == "Unit" || ing_unit == "N/A"){
ing_unit = ""
}
let ing_name = ingredients[i].getElementsByClassName("ingredient-name")[0].value
text_file += ing_quantity + ing_unit + " " + ing_name + "\n"
string_data += "<li data-value='null'>" + ing_quantity + ing_unit + " " + ing_name + "</li>"
}
string_data += "</ul>"
}
//Instruction section
text_file += "Instructions:"+"\n"
string_data += "<h2>Instructions:</h2><ol>"
var instruction_text = document.getElementById("recipeInstructions").value
instruction_text = instruction_text.split("\n")
for (let i=0; i<instruction_text.length;i++){
if (instruction_text[i].charAt(instruction_text[i].length-1) == ":"){
string_data += "<h3>" + instruction_text[i] + "</h3>"
}else{
string_data += "<li>" + instruction_text[i] + "</li>"
}
text_file += instruction_text[i]+"\n"
}
string_data += "</ol>"
//Credit Section
var credit = document.getElementById("recipeCredit").value
if (credit != ""){
text_file += "Credit: " + credit
string_data += "<p> Credit: " + credit + "</p>"
}
if (format == "html"){
return string_data
}else{
return [text_file, recipe_title+".txt"]
}
}
function convert_to_time(time){
var text = ""
let time_split= time.split(":")
if (time_split[0] =="1"){
text += time_split[0] + " hour "
}else if (time_split[0] != "0"){
text += time_split[0] + " hours "
}
if (time_split[1] != "00"){
if (time_split[1][0] == "0"){
text += time_split[1][1] + " minutes"
}else{
text += time_split[1] + " minutes"
}
}
return text
}
function convert_to_time_text_file(time){
var text = ""
let time_split= time.split(":")
text += time_split[0] + ":" + time_split[1]
return text
}
function validateForm() {
var recipeTitle = document.getElementById("recipeTitle").value;
var recipeCategory = document.getElementById("recipeCategory").value;
var prepHours = document.getElementById("prepHours").value;
var prepMinutes = document.getElementById("prepMinutes").value;
// Add more variables for other fields that require validation
console.log(recipeTitle)
// Perform validation checks
if (recipeTitle == "") {
alert("Please enter a recipe title.");
return false;
}
if (recipeCategory == "") {
alert("Please enter a recipe category.");
return false;
}
if (prepHours == "" || prepMinutes == "" || isNaN(prepHours) || isNaN(prepMinutes)) {
alert("Please enter valid prep time.");
return false;
}
// Add more validation checks for other fields
// If all validations pass, return true to allow form submission
return true;
}
</script>
</body>
</html>