-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathingredientWidget.html
173 lines (149 loc) · 6.63 KB
/
ingredientWidget.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ingredient Visualizer by spoonacular</title>
<meta name="author" content="David Urbansky">
<meta name="description" content="A spoonacular ingredient visualizer demo for recipes.">
<meta name="keywords" content="spoonacular, API, ingredient, visualization, demo, app">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Indie+Flower&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="https://spoonacular.com/favicon.ico" type="image/x-icon">
<link rel="icon" href="https://spoonacular.com/favicon.ico" type="image/x-icon">
</head>
<body>
<div id="app">
<h1>spoonacular Ingredient Visualizer</h1>
<p>Create an informative and attractive ingredient list for your recipe. This tool allows your readers to
quickly see the ingredients with amounts and images. You can pick between the grid and list view, the number
of servings, and which measurements to use.</p>
<h3>Ingredients</h3>
<textarea v-model="ingredients" name="ingredients" style="height:150px;width:calc(100% - 20px);padding:10px"
placeholder="one ingredient per line, such as "200 grams of cucumber""></textarea>
<h3>Number of Servings</h3>
<input name="servings" type="number" style="width:45px;padding:10px;" v-model="servings" />
<h3>View Style</h3>
<div>
<input id="spoonacular-grid" name="view" type="radio" checked="" value="grid" v-model="viewStyle">
<label for="spoonacular-grid" onclick="">grid</label>
<input id="spoonacular-list" name="view" type="radio" value="list" v-model="viewStyle">
<label for="spoonacular-list" onclick="">list</label>
<span class="slide-button"></span>
</div>
<h3>Measure</h3>
<div>
<input id="spoonacular-us" name="measure" type="radio" checked="" value="us" v-model="measure">
<label for="spoonacular-us" onclick="">US</label>
<input id="spoonacular-metric" name="measure" type="radio" value="metric" v-model="measure">
<label for="spoonacular-metric" onclick="">metric</label>
<span class="slide-button"></span>
</div>
<div class="button" @click="previewIngredientWidget">Preview Ingredient Visualizer</div>
<iframe id="previewWidget"></iframe>
<!-- shameless plug -->
<div id="spoonacular">
powered by<br>
<a href="https://spoonacular.com/food-api">
<img src="https://spoonacular.com/application/frontend/images/logo-simple-framed-green-gradient.svg"
alt="spoonacular logo"><br>
spoonacular API
</a>
</div>
</template>
</div>
<script>
var app = new Vue({
el: '#app',
mounted() {
},
data: function () {
return {
spoonacularApiKey: '871cc9ddc1ea4733830dd2c30e3d691a', // REPLACE THIS DEMO KEY, get a free key here: https://spoonacular.com/food-api
servings: 2,
measure: 'metric',
viewStyle: 'grid',
ingredients: '1 apple\n2 cups of coffee\n1.4 liters almond milk\n2 1/2 salmon fillets\nkale',
};
},
methods: {
previewIngredientWidget() {
var postContent = this.ingredients;
let self = this;
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
self.previewIngredientWidgetCallback(xmlHttp.responseText);
}
}
xmlHttp.open("POST", 'https://api.spoonacular.com/recipes/visualizeIngredients?apiKey=' + this.spoonacularApiKey, true);
xmlHttp.send('defaultCss=true&servings=' + this.servings + '&view=' + this.viewStyle + '&measure=' + this.measure + '&ingredientList=' + postContent);
},
previewIngredientWidgetCallback(response) {
var iframeDocument = document.getElementById('previewWidget').contentDocument;
iframeDocument.open();
iframeDocument.write(response);
iframeDocument.close();
var el = document.createElement("script");
el.setAttribute("type", "text/javascript");
el.setAttribute("src", "https://code.jquery.com/jquery-1.9.1.min.js");
document.getElementById('previewWidget').contentDocument.head.appendChild(el);
// wait until jquery is loaded
setTimeout(function () {
var el = document.createElement("script");
el.setAttribute("type", "text/javascript");
el.setAttribute("src", "https://spoonacular.com/application/frontend/js/ingredientWidget.min.js?c=1");
document.getElementById('previewWidget').contentDocument.body.appendChild(el);
}, 1000);
},
},
});
</script>
<style>
body {
background-color: rgb(241, 255, 241);
}
#app {
font-family: 'Indie Flower', cursive;
background-color: #fff;
max-width: 800px;
margin-left: auto;
margin-right: auto;
padding: 20px;
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.2), 0 2px 2px rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12);
border-radius: 22px;
}
h1 {
margin-top: 0;
}
iframe {
width: 100%;
height: 350px;
border: none;
margin-top: 20px;
}
p {
font-size: 22px;
display: inline;
}
.button:hover {
background-color: rgb(241, 255, 241);
}
.button {
padding: 5px 10px;
border: 1px solid #333;
border-radius: 5px;
display: inline-block;
font-weight: bold;
cursor: pointer;
margin-top: 20px;
}
#spoonacular {
margin-top: 50px;
margin-left: auto;
margin-right: auto;
width: 200px;
text-align: center;
}
</style>
</body>
</html>