This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
295 lines (260 loc) · 8.84 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const {app, BrowserWindow } = require('electron');
let win;
var RecipeMap = new Map();
var fs = require('fs');
fs.open('recipeDatabase.txt', 'r+', function(err, file) {
if (err) throw err;
});
class Recipe{
constructor(name, ing, dir, orig, prep, course, img, servings){
this.recipename = name;
this.ingredients = ing;
this.directions = dir;
this.origin = orig;
this.prep = prep;
this.course = course;
this.img = img;
this.servings = servings;
}
get name(){
return this.recipename;
}
set name(x){
this.recipename = x;
}
get ings(){
return this.ingredients;
}
set ing(x){
this.ingredients = x;
}
get dirs(){
return this.directions;
}
set dirs(x){
this.directions = x;
}
};
function readTextFile(file) {
try {
var data = fs.readFileSync(file, 'utf8').toString().split('\n');
//console.log(data);
//console.log(data.length);
} catch(e) {
console.log('error:', e.stack);
}
if (data[0] != '') { // if its an empty file there's nothing to read
for (var i = 0; i < data.length; i++) {
if (data[i] == '') break; // there's always an extra line at the end of the file
let recipe_name = data[i];
i++;
//if (i >= data.length) break; // error checking? in case the file is formatted incorrectly?
let origin = data[i];
i++;
//if (i >= data.length) break;
let prep = data[i];
i++;
//if (i >= data.length) break;
let course = data[i];
i++;
//if (i >= data.length) break;
let img = data[i];
i++;
//if (i >= data.length) break;
let ingredients = data[i].split(',');
i++;
//if (i >= data.length) break;
let directions = data[i].split(',');
i++;
//if (i >= data.length) break;
//i++; // this last one is for the blank line(s?) that separates recipes
let newrecipe = new Recipe(recipe_name, ingredients, directions, origin, prep, course, img);
RecipeMap.set(recipe_name, newrecipe);
//console.log(newrecipe);
}
}
/*fs.readTextFile(file, 'utf8', function(err, contents) {
if (err) throw err;
console.log(contents);
console.log(file.length);
});*/
}
function writeMap(map) {
fs.writeFile('recipeDatabase.txt', '', function(err) { // clear the file because we're about to rewrite it
if (err) throw err;
});
map.forEach(writeRecipe);
}
function writeRecipe(value, key, map) {
fs.appendFileSync('recipeDatabase.txt', key + '\n' + value.origin + '\n' + value.prep + '\n' + value.course + '\n' + value.img + '\n' + value.ingredients + '\n' + value.directions + '\n\n', function(err) {
if (err) throw err;
});
}
app.on('ready', () => {
// read in the file and sets up the map with previously saved recipes
readTextFile('./recipeDatabase.txt');
// This creates a new BrowserWindow and sets win to be a reference to that new window.
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // This ie extremely important later, but is not used in this example.
devTools: true,
}
});
win.maximize();
// This is a method of a BrowserWindow that lets you load an html file to view.
win.loadFile('basic.html');
win.on('close', () => {
console.log(RecipeMap);
writeMap(RecipeMap);
win = null;
});
});
//event listener
const { ipcMain } = require('electron')
//listens for event "search request"
//gets passed arg which is the text in the search request
ipcMain.on('search-request', (event, arg) => {
var itemsret = [];
//separting arguement to get the check box code and value typed into search field
//checkbox = ingredients, recipename, preptime, origin, course
var checkbox_code = arg.slice(0,5);
var search = arg.slice(5);
RecipeMap.forEach(function (item, index){
//ingredients
if(checkbox_code[0] === "1"){
for(var i = 0; i < item.ingredients.length; i++){
var string1 = item.ingredients[i].toUpperCase();
var string2 = search.toUpperCase();
if(string1.includes(string2)){
itemsret.push(item);
}
}
}
//recipe name
if(checkbox_code[1] === "1"){
var string1 = item.recipename.toUpperCase();
var string2 = search.toUpperCase();
if(string1.includes(string2)){
itemsret.push(item);
}
}
//preptime
if(checkbox_code[2] === "1"){
if(item.prep <= search){
itemsret.push(item);
}
}
//origin
if(checkbox_code[3] === "1"){
var string1 = item.origin.toUpperCase();
var string2 = search.toUpperCase();
if(string1.includes(string2)){
itemsret.push(item);
}
}
//course
if(checkbox_code[4] === "1"){
var string1 = item.course.toUpperCase();
var string2 = search.toUpperCase();
if(string1.includes(string2)){
itemsret.push(item);
}
}
});
//removes duplicate items
//inserts each element in hashtable after it checks for its existence
//in the hash table
var seen = {};
var items_no_duplicates = [];
items_no_duplicates = itemsret.filter(function(item) {
return seen.hasOwnProperty(item.recipename)
? false : (seen[item.recipename] = true);
});
//sends array of matching items to searchrecipe
event.sender.send('recipe', items_no_duplicates);
})
//listens for a browse request
ipcMain.on('browse-request', (event, arg) => {
if(RecipeMap.has(arg) == true){
var displayrecipe = RecipeMap.get(arg)
event.sender.send('recipe', displayrecipe);
}
})
//listens for new recipe to add
ipcMain.on('recipe', (event, recipe_name, ingredients, directions, origin, prep, course, img, servings) => {
// if there is no recipe name, then make the user enter one
if (recipe_name == "") {
event.returnValue = "Please enter recipe name";
}
//if recipe name does not exist add it to the map
else if(RecipeMap.has(recipe_name) == false){
let newrecipe = new Recipe(recipe_name, ingredients, directions, origin, prep, course, img, servings);
RecipeMap.set(recipe_name, newrecipe);
event.returnValue = "Recipe added successfully";
}
//if it does exist send back an error
else{
event.returnValue = "Recipe already exists";
}
})
//event for logging message on the console for debugging
ipcMain.on('log' , (event, arg) => {
console.log(arg)
})
//function that gets a random key from the recipe map
function getRandomKey(collection) {
let keys = Array.from(collection.keys());
return keys[Math.floor(Math.random() * keys.length)];
}
//event for handling random recipe request
ipcMain.on('random_recipe', (event, arg) => {
//if the map is empty send back a message saying so
if (RecipeMap.size == 0){
event.sender.send('random_recipe_return', 'empty');
console.log(RecipeMap.size);
}
//if the map is not empty call random key function
else{
//call random recipe function and get random recipe key
var recipe_key = getRandomKey(RecipeMap)
//send random recipe to random.js to display
event.sender.send('random_recipe_return', RecipeMap.get(recipe_key));
}
})
var map_iterator = RecipeMap.entries();
ipcMain.on('first_recipe', (event, arg) => {
var imsorry = [];
RecipeMap.forEach(function (item, index){
imsorry.push(item);
})
event.sender.send('first_recipe', imsorry);
})
var came_from = 0;
var display_recipe;
var searcharr = 0;
ipcMain.on('Planner', (event, arg) => {
var recipenames = [];
RecipeMap.forEach(function (item, index){
recipenames.push(index);
})
event.sender.send('addEvent', recipenames);
})
ipcMain.on('display_recipe', (event, arg, returnarr, link) => {
win.loadFile('display_recipe.html');
display_recipe = arg;
searcharr = returnarr;
came_from = link
})
ipcMain.on('display_recipe_ready', (event, arg) => {
event.sender.send('random_recipe_return', display_recipe);
})
ipcMain.on('redirect', (event) => {
if(searcharr !== 0){
event.sender.send('recipe', searcharr);
}
})
ipcMain.on('redirect_return', (event) => {
if(came_from === 'search') win.loadFile('search.html');
else if(came_from === 'browse') win.loadFile('browse.html');
});