-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtasteWidget.html
141 lines (122 loc) · 4.79 KB
/
tasteWidget.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Taste Visualizer by spoonacular</title>
<meta name="author" content="David Urbansky">
<meta name="description" content="A spoonacular taste visualizer demo for recipes.">
<meta name="keywords" content="spoonacular, API, taste, flavor, 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 Taste Visualizer</h1>
<p>Create an informative and attractive taste visualization 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 sugar""></textarea>
<div class="button" @click="previewTasteWidget">Preview Taste 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
ingredients: '100g powdered suger\n1 lemon\n1 cup flour\n1 cup coffee',
};
},
methods: {
previewTasteWidget() {
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/visualizeTaste?apiKey=' + this.spoonacularApiKey, true);
xmlHttp.send('ingredientList=' + postContent);
},
previewWidgetCallback(response) {
el = document.createElement("script");
el.setAttribute("type", "text/javascript");
el.setAttribute("src", "https://www.chartjs.org/dist/2.9.4/Chart.min.js");
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();
}, 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: 500px;
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>