-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
140 lines (113 loc) · 4.82 KB
/
index.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
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
const url = window.location.pathname;
const filename = url.substring(url.lastIndexOf('/') + 1);
//load just if index.html
if (filename === 'index.html') {
const mainCards = document.querySelectorAll('.main-card');
// console.log(document.querySelector("./dinner.html #rec5"));
mainCards.forEach((card, index) => {
//EVENT mouse in
card.addEventListener('mouseenter', () => {
const listItems = document.querySelectorAll(`#card${index + 1} .recipe-list li`);
//picture slides up
document.querySelectorAll('.main-card .card-img-top')[index]
.classList.add('card-img-move');
document.querySelectorAll('.main-card ul')[index]
.classList.remove('ul-hide');
//text slides down
document.querySelectorAll('.card-body')[index].style.transform = 'translateY(100%)';
//animate li's
listItems.forEach((item, ind) => {
item.style.animation = `recipeListFade 0.5s ease forwards ${ind / 7 +0.2}s`;
});
});
});
mainCards.forEach((card,index) => {
//EVENT mouse out
card.addEventListener('mouseleave', () => {
document.querySelectorAll('.main-card .card-img-top')[index]
.classList.remove('card-img-move');
document.querySelectorAll('.main-card ul')[index]
.classList.add('ul-hide');
document.querySelectorAll('.card-body')[index].style.transform = 'translateY(0)';
});
})
} else {
///////servings buttons
//read in document elements for all +/- buttons
const btnsLess = document.querySelectorAll('.btn-less-servings');
const btnsMore = document.querySelectorAll('.btn-more-servings');
const getNumOfServings = (tabNum) => {
return document.querySelectorAll(".num-servings")[tabNum - 1].innerHTML;
}
//detect tab
const getNumOfTab = () => {
const currentTab = document.getElementsByClassName('nav-link active');
const href = currentTab[0].href;
const num = href.slice(href.length - 1);
return num;
}
//get NodeArray and return amounts of units
const getAmounts = (tabNum) => {
const amountNodes = document.querySelectorAll(`#rec${tabNum} .table .data-amount`);
const amounts = Array.from(amountNodes, obj => obj.innerHTML);
return amounts;
}
//calculate amounts
const calcAmounts = (oldServings, newServings, actualAmounts) => {
const newAmounts = actualAmounts.map(amount => {
return Number.parseFloat((amount / oldServings) * newServings).toFixed(2);
});
return newAmounts;
}
//set new Amounts to dom elements
const setNewAmounts = (newAmounts, tableNum) => {
for (let i = 0; i < newAmounts.length; i++) {
document.querySelectorAll(`#rec${tableNum} .table .data-amount`)[i].innerHTML = newAmounts[i];
}
}
//events on btn click
const less = () => {
//detect tab and get servings and Amount-array
const tabNum = getNumOfTab();
const numOfServingsOld = getNumOfServings(tabNum);
let actualNumOfServings = numOfServingsOld;
if (actualNumOfServings > 1) {
actualNumOfServings--;
document.querySelectorAll(".num-servings")[tabNum - 1].innerHTML = actualNumOfServings;
}
else
alert('You need to chose at least one serving!');
const newAmounts = calcAmounts(numOfServingsOld, actualNumOfServings, getAmounts(tabNum));
setNewAmounts(newAmounts, tabNum);
}
const more = () => {
//detect tab and get servings and Amount-array
const tabNum = getNumOfTab();
const numOfServingsOld = getNumOfServings(tabNum);
let actualNumOfServings = numOfServingsOld;
console.log("More");
actualNumOfServings++;
document.querySelectorAll(".num-servings")[tabNum - 1].innerHTML = actualNumOfServings;
const newAmounts = calcAmounts(numOfServingsOld, actualNumOfServings, getAmounts(tabNum));
setNewAmounts(newAmounts, tabNum);
}
//add eventlistener for every less-btn in the html
btnsLess.forEach(btn => {
btn.addEventListener('click', less);
});
//add eventlistener for every more-btn in the html
btnsMore.forEach(btn => {
btn.addEventListener('click', more);
});
///////share buttons
// Select all buttons
const shareButtons = document.querySelectorAll('.share-btn');
// Add eventlistener to all buttons
shareButtons.forEach((button) => {
button.addEventListener('click', () => {
const tabNum = getNumOfTab();
const recipe = document.querySelector(`#rec${tabNum} h3`).innerText;
alert(`Thank you for Sharing the ${recipe}!`);
});
});
}