-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Expand file tree
/
Copy pathrecipeView.js
More file actions
149 lines (133 loc) · 4.33 KB
/
recipeView.js
File metadata and controls
149 lines (133 loc) · 4.33 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
import View from './View.js';
// import icons from '../img/icons.svg'; // Parcel 1
import icons from 'url:../../img/icons.svg'; // Parcel 2
// old (not working)
// import { Fraction } from 'fractional';
// new
import Fraction from 'fraction.js';
class RecipeView extends View {
_parentElement = document.querySelector('.recipe');
_errorMessage = 'We could not find that recipe. Please try another one!';
_message = '';
addHandlerRender(handler) {
['hashchange', 'load'].forEach(ev => window.addEventListener(ev, handler));
}
addHandlerUpdateServings(handler) {
this._parentElement.addEventListener('click', function (e) {
const btn = e.target.closest('.btn--update-servings');
if (!btn) return;
const { updateTo } = btn.dataset;
if (+updateTo > 0) handler(+updateTo);
});
}
addHandlerAddBookmark(handler) {
this._parentElement.addEventListener('click', function (e) {
const btn = e.target.closest('.btn--bookmark');
if (!btn) return;
handler();
});
}
_generateMarkup() {
return `
<figure class="recipe__fig">
<img src="${this._data.image}" alt="${
this._data.title
}" class="recipe__img" />
<h1 class="recipe__title">
<span>${this._data.title}</span>
</h1>
</figure>
<div class="recipe__details">
<div class="recipe__info">
<svg class="recipe__info-icon">
<use href="${icons}#icon-clock"></use>
</svg>
<span class="recipe__info-data recipe__info-data--minutes">${
this._data.cookingTime
}</span>
<span class="recipe__info-text">minutes</span>
</div>
<div class="recipe__info">
<svg class="recipe__info-icon">
<use href="${icons}#icon-users"></use>
</svg>
<span class="recipe__info-data recipe__info-data--people">${
this._data.servings
}</span>
<span class="recipe__info-text">servings</span>
<div class="recipe__info-buttons">
<button class="btn--tiny btn--update-servings" data-update-to="${
this._data.servings - 1
}">
<svg>
<use href="${icons}#icon-minus-circle"></use>
</svg>
</button>
<button class="btn--tiny btn--update-servings" data-update-to="${
this._data.servings + 1
}">
<svg>
<use href="${icons}#icon-plus-circle"></use>
</svg>
</button>
</div>
</div>
<div class="recipe__user-generated ${this._data.key ? '' : 'hidden'}">
<svg>
<use href="${icons}#icon-user"></use>
</svg>
</div>
<button class="btn--round btn--bookmark">
<svg class="">
<use href="${icons}#icon-bookmark${
this._data.bookmarked ? '-fill' : ''
}"></use>
</svg>
</button>
</div>
<div class="recipe__ingredients">
<h2 class="heading--2">Recipe ingredients</h2>
<ul class="recipe__ingredient-list">
${this._data.ingredients.map(this._generateMarkupIngredient).join('')}
</div>
<div class="recipe__directions">
<h2 class="heading--2">How to cook it</h2>
<p class="recipe__directions-text">
This recipe was carefully designed and tested by
<span class="recipe__publisher">${
this._data.publisher
}</span>. Please check out
directions at their website.
</p>
<a
class="btn--small recipe__btn"
href="${this._data.sourceUrl}"
target="_blank"
>
<span>Directions</span>
<svg class="search__icon">
<use href="${icons}#icon-arrow-right"></use>
</svg>
</a>
</div>
`;
}
_generateMarkupIngredient(ing) {
return `
<li class="recipe__ingredient">
<svg class="recipe__icon">
<use href="${icons}#icon-check"></use>
</svg>
<div class="recipe__quantity">
${
ing.quantity ? new Fraction(ing.quantity).toFraction(true) : ''
}</div>
<div class="recipe__description">
<span class="recipe__unit">${ing.unit}</span>
${ing.description}
</div>
</li>
`;
}
}
export default new RecipeView();