2020-04-10 14:14:34 +00:00
|
|
|
|
2020-04-20 15:54:58 +00:00
|
|
|
var rl
|
2020-04-10 14:14:34 +00:00
|
|
|
|
2020-05-13 09:38:13 +00:00
|
|
|
|
|
|
|
|
function checkforURLParam(){
|
|
|
|
|
try {
|
|
|
|
|
let url = window.location.href
|
|
|
|
|
params = url.split("?")[1]
|
|
|
|
|
if (params !== undefined){
|
2020-05-13 10:02:55 +00:00
|
|
|
// url param to string for search bar
|
2020-05-13 09:38:13 +00:00
|
|
|
params = "?" + params
|
|
|
|
|
loadRecipes(params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function loadData() {
|
|
|
|
|
// make string of get params for request
|
|
|
|
|
getParams = makeGetParamString()
|
|
|
|
|
window.location = window.location.href.split("?")[0] + getParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadRecipes(getParams){
|
2020-04-18 14:35:21 +00:00
|
|
|
rl = document.getElementById("recipe-list")
|
2020-04-22 13:44:50 +00:00
|
|
|
rl.innerHTML = '<div class="loader"></div>'
|
|
|
|
|
document.getElementById("main").className += " data-loaded"
|
2020-04-18 14:35:21 +00:00
|
|
|
|
2020-04-20 15:54:58 +00:00
|
|
|
getJSON("/api/v1/recipe/" + getParams,
|
2020-05-13 09:38:13 +00:00
|
|
|
function (error, data) {
|
|
|
|
|
console.log(data)
|
2020-05-15 20:29:43 +00:00
|
|
|
ignored = data["data"]["ignored"]
|
|
|
|
|
data = data["data"]["ingred"] // remove wrapper
|
|
|
|
|
|
2020-05-13 09:38:13 +00:00
|
|
|
renderRecipeList(data)
|
2020-05-15 20:29:43 +00:00
|
|
|
renderIgnored(ignored)
|
2020-05-13 09:38:13 +00:00
|
|
|
},
|
|
|
|
|
function (error, data) {
|
|
|
|
|
console.log(error)
|
|
|
|
|
rl.innerHTML = "<p>Es gab einen Fehler, bitte suchen Sie erneut.</p>"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-04-20 15:54:58 +00:00
|
|
|
}
|
2020-05-15 20:29:43 +00:00
|
|
|
function renderIgnored(ignored){
|
|
|
|
|
document.getElementById("search-form").innerHTML += "<br>"
|
|
|
|
|
ignored.forEach(
|
|
|
|
|
function (item, index) {
|
|
|
|
|
document.getElementById("search-form").innerHTML += `<span class="badge badge-danger badge-pill">${item}</span>`
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
let url = window.location.href
|
|
|
|
|
params = url.split("?")[1]
|
|
|
|
|
if (params !== undefined){
|
|
|
|
|
// url param to string for search bar
|
|
|
|
|
paramString = params.split("&").join("").split("ingred=").join(", ").replace(", ", "")
|
|
|
|
|
|
|
|
|
|
document.getElementById("search-field").value = paramString
|
|
|
|
|
params = "?" + params
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-04-20 15:54:58 +00:00
|
|
|
|
|
|
|
|
function makeGetParamString(){
|
2020-04-18 12:56:22 +00:00
|
|
|
searchString = document.getElementById("search-field").value
|
|
|
|
|
searchArray = searchString.split(',');
|
|
|
|
|
getParams = ""
|
|
|
|
|
searchArray.forEach(
|
2020-04-18 14:35:21 +00:00
|
|
|
function (item, index) {
|
2020-04-20 15:54:58 +00:00
|
|
|
if(item.trim() !== ""){
|
|
|
|
|
if (index > 0) {
|
|
|
|
|
getParams += "&ingred=" + item.trim()
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
getParams += "?ingred=" + item.trim()
|
|
|
|
|
}
|
2020-04-18 12:56:22 +00:00
|
|
|
}
|
2020-04-20 15:54:58 +00:00
|
|
|
|
2020-04-18 14:35:21 +00:00
|
|
|
});
|
2020-04-20 15:54:58 +00:00
|
|
|
return getParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderRecipeList(data){
|
|
|
|
|
let keys = Object.keys(data).reverse(); // iterate in reverse order, highest score up top
|
|
|
|
|
rl.innerHTML = ""
|
|
|
|
|
keys.forEach(
|
|
|
|
|
function (key) {
|
|
|
|
|
data1 = data[key]
|
|
|
|
|
ingredString = ""
|
2020-05-13 09:04:21 +00:00
|
|
|
missingString = ""
|
2020-04-20 15:54:58 +00:00
|
|
|
data1[3].forEach(
|
|
|
|
|
function(ingred){
|
2020-05-15 20:29:43 +00:00
|
|
|
if (ingred.charAt(ingred.length-2) === ":"){
|
|
|
|
|
ingred = ingred.substr(0, ingred.length-2)
|
|
|
|
|
}
|
2020-04-20 15:54:58 +00:00
|
|
|
ingredString += `${ingred}<br>`
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-05-13 09:04:21 +00:00
|
|
|
data1[4].forEach(
|
|
|
|
|
function(ingred){
|
|
|
|
|
missingString += `${ingred}<br>`
|
|
|
|
|
}
|
2020-05-15 20:29:43 +00:00
|
|
|
)
|
|
|
|
|
if(data1[4].length === 0){
|
|
|
|
|
missing = ""
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
missing = "<br>Fehlt:<br>"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 15:54:58 +00:00
|
|
|
recString = `
|
2020-04-22 13:44:50 +00:00
|
|
|
<div class="card text-white bg-primary mb-3" style="max-width: 100%">
|
2020-04-20 15:54:58 +00:00
|
|
|
<div class="card-body recipe-container">
|
|
|
|
|
<div class="row">
|
2020-04-22 13:44:50 +00:00
|
|
|
<div class="col-lg-5 col-sm-5 col">
|
2020-05-13 09:04:21 +00:00
|
|
|
<a href="${data1[2]}" target="_blank">
|
|
|
|
|
<img class="recipe-img" src="/api/v1/images/${data1[0]}">
|
|
|
|
|
</a>
|
2020-04-20 15:54:58 +00:00
|
|
|
</div>
|
2020-04-22 13:44:50 +00:00
|
|
|
<div class="col-lg col-sm col">
|
2020-04-18 14:35:21 +00:00
|
|
|
<div class="row">
|
2020-04-20 15:54:58 +00:00
|
|
|
<div class="col">
|
2020-05-13 09:04:21 +00:00
|
|
|
<a href="${data1[2]}" target="_blank">
|
|
|
|
|
<span><h4 class="recipe-name">${data1[1]}</h4></span>
|
|
|
|
|
</a>
|
2020-04-18 14:35:21 +00:00
|
|
|
</div>
|
2020-04-20 15:54:58 +00:00
|
|
|
</div>
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="col">
|
|
|
|
|
<div class="recipe-ingredients">${ingredString}</div>
|
2020-04-18 14:35:21 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-05-13 09:04:21 +00:00
|
|
|
<div class="row">
|
|
|
|
|
<div class="col">
|
2020-05-15 20:29:43 +00:00
|
|
|
${missing}
|
2020-05-13 09:04:21 +00:00
|
|
|
<div class="recipe-ingredients missing">
|
|
|
|
|
${missingString}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-04-18 14:35:21 +00:00
|
|
|
</div>
|
2020-04-20 15:54:58 +00:00
|
|
|
|
2020-04-22 13:44:50 +00:00
|
|
|
<div class="col-lg-1 col-sm-2 col-2">
|
|
|
|
|
<span class="recipe-score badge badge-info badge-pill">${(key*100).toFixed(0) + "%"}</span>
|
2020-04-20 15:54:58 +00:00
|
|
|
</div>
|
2020-04-18 14:35:21 +00:00
|
|
|
</div>
|
2020-04-20 15:54:58 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-05-13 09:04:21 +00:00
|
|
|
|
2020-04-20 15:54:58 +00:00
|
|
|
|
|
|
|
|
`
|
|
|
|
|
rl.innerHTML += recString
|
|
|
|
|
})
|
|
|
|
|
}
|