-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnutritionWidget.html
157 lines (133 loc) · 5.77 KB
/
nutritionWidget.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nutrition Visualizer by spoonacular</title>
<meta name="author" content="David Urbansky">
<meta name="description" content="A spoonacular nutrition visualizer demo for recipes.">
<meta name="keywords" content="spoonacular, API, nutrition, nutrients, 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 Nutrition Visualizer</h1>
<p>Create an informative and attractive visualization of nutritional information of a recipe.</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" /><br><br>
<div class="button" @click="previewNutritionWidget">Preview Nutrition 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,
ingredients: '1 apple\n2 cups of coffee\n1.4 liters almond milk\n2 1/2 salmon fillets\nkale',
};
},
methods: {
previewNutritionWidget() {
var postContent = this.ingredients;
let self = this;
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
self.previewWidgetCallback(xmlHttp.responseText);
}
}
xmlHttp.open("POST", 'https://api.spoonacular.com/recipes/visualizeNutrition?apiKey=' + this.spoonacularApiKey, true);
xmlHttp.send('defaultCss=true&servings=' + this.servings + '&ingredientList=' + postContent);
},
previewWidgetCallback(response) {
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);
el = document.createElement("script");
el.setAttribute("type", "text/javascript");
el.setAttribute("src", "https://spoonacular.com/application/frontend/js/jquery.canvasjs.min");
document.getElementById('previewWidget').contentDocument.head.appendChild(el);
// wait until jquery is loaded
setTimeout(function () {
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://spoonacular.com/application/frontend/js/nutritionWidget.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;
}
iframe {
width: 100%;
height: 800px;
border: none;
margin-top: 20px;
}
h1 {
margin-top: 0;
}
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>