forked from vlee19/therecipebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
147 lines (124 loc) · 4.1 KB
/
Copy pathcreate.js
File metadata and controls
147 lines (124 loc) · 4.1 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
const categories = {};
function addIngredient() {
const categoryInput = document.getElementById("ingredient-category");
const ingredientInput = document.getElementById("ingredient-input");
const category = categoryInput.value.trim();
const ingredient = ingredientInput.value.trim();
if (!category || !ingredient) {
alert("Please provide both a category and an ingredient.");
return;
}
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(ingredient);
renderIngredients();
// Reset inputs
ingredientInput.value = "";
categoryInput.value = "";
ingredientInput.focus();
}
function renderIngredients() {
const container = document.getElementById("categorized-ingredients");
container.innerHTML = "";
for (const [category, ingredients] of Object.entries(categories)) {
const section = document.createElement("div");
section.classList.add("ingredient-category");
const title = document.createElement("h4");
title.textContent = category;
section.appendChild(title);
const list = document.createElement("ul");
ingredients.forEach((ingredient, index) => {
const li = document.createElement("li");
li.innerHTML = `
<span>${ingredient}</span>
<button class="edit-btn" onclick="editIngredient('${category}', ${index})">Edit</button>
<button class="delete-btn" onclick="deleteIngredient('${category}', ${index})">Delete</button>
`;
list.appendChild(li);
});
section.appendChild(list);
container.appendChild(section);
}
}
function deleteIngredient(category, index) {
categories[category].splice(index, 1);
if (categories[category].length === 0) {
delete categories[category];
}
renderIngredients();
}
function editIngredient(category, index) {
const current = categories[category][index];
const updated = prompt("Edit ingredient:", current);
if (updated && updated.trim() !== "") {
categories[category][index] = updated.trim();
renderIngredients();
}
}
function addDirection() {
const input = document.getElementById("direction-input");
const direction = input.value.trim();
if (direction === "") {
alert("Please enter a direction.");
return;
}
const list = document.getElementById("directions-list");
const li = document.createElement("li");
li.innerHTML = `
<span>${direction}</span>
<button class="edit-btn" onclick="editItem(this)">Edit</button>
<button class="delete-btn" onclick="deleteItem(this)">Delete</button>
`;
list.appendChild(li);
input.value = "";
input.focus();
}
function deleteItem(button) {
const li = button.parentElement;
li.remove();
}
function editItem(button) {
const li = button.parentElement;
const span = li.querySelector("span");
const currentText = span.textContent;
const newText = prompt("Edit text:", currentText);
if (newText !== null && newText.trim() !== "") {
span.textContent = newText.trim();
}
}
const backBtn = document.querySelector('[onclick="backToProfile()"]');
function backToProfile() {
window.location.href = 'profile.html';
}
function previewPhoto(event) {
const preview = document.getElementById("photo-preview");
preview.innerHTML = ""; // Clear previous image
const file = event.target.files[0];
if (file) {
const img = document.createElement("img");
img.src = URL.createObjectURL(file);
img.style.maxWidth = "100%";
img.style.maxHeight = "300px";
img.style.borderRadius = "10px";
img.style.marginTop = "10px";
preview.appendChild(img);
}
}
document.addEventListener('DOMContentLoaded', function () {
const cultureSelect = document.getElementById('recipe-culture');
const choices = new Choices(cultureSelect, {
removeItemButton: true,
placeholderValue: 'Select one or more cultures',
searchPlaceholderValue: 'Search cultures'
});
const tagInput = new Choices('#recipe-tags', {
removeItemButton: true,
placeholderValue: 'Add tags (e.g., meat, vegan, spicy)',
duplicateItemsAllowed: false,
addItems: true,
paste: true,
duplicateItems: false,
addItemFilter: value => !!value.trim(),
});
});