-
Notifications
You must be signed in to change notification settings - Fork 9
/
recipes.js
181 lines (167 loc) · 5.17 KB
/
recipes.js
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
174
175
176
177
178
179
180
181
/* Read JSON-File from Server */
let importedRecipes;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
importedRecipes = JSON.parse(this.responseText);
}
};
xmlhttp.open(
"GET",
"https://rawgit.com/crelder/recipe-shoppinglist-generator/master/recipes.json",
false
);
xmlhttp.send();
// Use the hereby added recipe.selected Property to select/unselect recipes in the GUI
// and to define the shopping list.
importedRecipes.forEach(function(recipe, index) {
recipe.selected = false;
});
function getRecipeCollection() {
recipes = [];
// We want to randomize the recipces,
// so that each time we look at it other recipes catch our attention
// therefore hopefully creating diversity in our nutrition.
importedRecipes.sort(function() {
return 0.5 - Math.random();
});
uniquePriorities = getUniquePriorities();
uniquePriorities.forEach(priority => {
recipes.push(importedRecipes.filter(function(recipe) {
return recipe.priority == priority;
}));
});
// Flatten array
recipes = [].concat(...recipes);
return recipes;
}
function getUniquePriorities() {
priorities = importedRecipes.map(function(recipe) {
return parseInt(recipe.priority);
})
uniquePriorities = [...new Set(priorities)];
return uniquePriorities.sort();
}
// Random order of recipes
const recipeCollection = getRecipeCollection();
/* --------------------------- */
function filterSelectedAndAggregateAmounts(recipes) {
const ingredients = {};
counter = 1;
filteredRecipes = recipes.filter(recipe => recipe.selected == true)
filteredRecipes.forEach(receipe => {
receipe.ingredients.forEach(ing => {
if (!ingredients[ing.name]) {
ingredients[ing.name] = {
unit: ing.unit,
amount: ing.amount,
department: ing.department
};
} else {
ingredients[ing.name].amount = ingredients[ing.name].amount + ing.amount;
}
});
});
return ingredients;
}
/* --------- VUE component------------*/
Vue.component("my-meal", {
props: ["recipe", "recipes", "index"],
methods: {
toggleSelectedRecipe: function() {
this.recipes[this.index].selected = !this.recipes[this.index].selected;
}
},
template:
'<a href="javascript:void(0);" class="list-group-item list-group-item-action" v-bind:class="{active: recipes[index].selected}" v-on:click="toggleSelectedRecipe"> {{ recipe.recipeName }}</a>'
});
/* ---------- VUE instance ------------*/
var vm = new Vue({
el: "#app",
data: {
recipes: recipeCollection
},
methods: {
onCopy: function(e) {
alert("Folgende Liste ist in die Zwischenablage kopiert:\n\n" + e.text);
},
onError: function(e) {
alert("Fehler beim Kopieren in die Zwischenablage.");
}
},
computed: {
shoppingList: function() {
let recipes = this.recipes;
const ingredients = filterSelectedAndAggregateAmounts(recipes);
const lst = Object.keys(ingredients).map(name => ({
name: name,
unit: ingredients[name].unit,
amount: ingredients[name].amount,
department: ingredients[name].department
}));
lst.sort(
(l, r) => (l.name <= r.name ? -1 : 1)
);
const sortedByDepartment = lst.sort(
(l, r) => (l.department <= r.department ? 1 : -1)
);
return sortedByDepartment.map(
ing => `${ing.amount} ${ing.unit} ${ing.name}`
);
},
clipboardShoppingList: function() {
date = new Date();
return (
"Einkaufsliste für den " +
date.toLocaleDateString("de-DE", {
weekday: "short",
year: "numeric",
month: "long",
day: "numeric"
}) +
":\n" +
this.shoppingList.join("\n")
);
},
clipboardMenues: function() {
let selectedRecipesSorted = this.selectedRecipes
.sort( (l, r) => (l.recipeName >= r.recipeName ? 1 : -1) )
.sort( (l, r) => (l.priority >= r.priority ? 1 : -1) )
date = new Date();
let output =
"Menüliste ab dem " +
date.toLocaleDateString("de-DE", {
weekday: "short",
year: "numeric",
month: "long",
day: "numeric"
}) +
":\n" +
selectedRecipesSorted
.map(recipe => recipe.recipeName)
.join(", ")
.toUpperCase() +
"\n\n";
for (var i = 0; i < selectedRecipesSorted.length; i++) {
output +=
selectedRecipesSorted[i].recipeName.toUpperCase() +
"\n" +
"--------------------" +
"\n";
for (var j = 0; j < selectedRecipesSorted[i].ingredients.length; j++) {
output +=
selectedRecipesSorted[i].ingredients[j].amount + " " +
selectedRecipesSorted[i].ingredients[j].unit +
" " +
selectedRecipesSorted[i].ingredients[j].name +
"\n";
}
output += '\n"' + selectedRecipesSorted[i].comment + ' Priorität ' + selectedRecipesSorted[i].priority + '"' + "\n\n\n";
}
return output;
},
selectedRecipes: function() {
return this.recipes.filter(recipe => recipe.selected == true);
}
}
});