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
/
Copy pathsearchrecipe.js
172 lines (157 loc) · 6.28 KB
/
searchrecipe.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
//file that renderers events and sends them to main
const { ipcRenderer } = require('electron');
const { remote } = require('electron'),
currWindow = remote.getCurrentWindow();
window.$ = window.jQuery = require('jquery');
//sends message to main for debugging
function log(message){
ipcRenderer.send('log', message)
}
var returnarr;
$( document ).ready(function() {
ipcRenderer.send('redirect');
});
//when main sends back a response message it will be received here
ipcRenderer.on('recipe', (event, arg ) => {
last_table = arg;
//getting a handle on the table and clearing it
let table = document.getElementById('tableId');
table.innerHTML = "";
//if no elements in array, display no elements
if(arg.length === 0){
var tr = document.createElement('tr');
var cell1 = document.createElement('td');
cell1.innerHTML = "No recipes found";
tr.appendChild(cell1);
table.appendChild(tr);
returnarr = arg;
}
else{
returnarr = arg;
//making headings
var tr = document.createElement('tr');
var cell0 = document.createElement('th');
var cell1 = document.createElement('th');
var cell2 = document.createElement('th');
//var cell3 = document.createElement('th');
var cell4 = document.createElement('th');
var cell5 = document.createElement('th');
var cell6 = document.createElement('th');
cell0.innerHTML = "";
cell1.innerHTML = "Recipe Name";
cell2.innerHTML = "Ingredients";
//cell3.innerHTML = "Directions";
cell4.innerHTML = "Origin";
cell5.innerHTML = "Prep-time";
cell6.innerHTML = "Course";
tr.appendChild(cell0);
tr.appendChild(cell1);
tr.appendChild(cell2);
//tr.appendChild(cell3);
tr.appendChild(cell4);
tr.appendChild(cell5);
tr.appendChild(cell6);
tr.id = "title";
table.appendChild(tr);
//for every recipe returned make a new table row
for(var i = 0; i < arg.length; i++){
let ingString = '';
let dirString = '';
var tr = document.createElement('tr');
tr.id = i;
var image_cell = document.createElement('td');
var recipe_cell = document.createElement('td');
var ingredient_cell = document.createElement('td');
//var direction_cell = document.createElement('td');
var origin_cell = document.createElement('td');
var preptime_cell = document.createElement('td');
var course_cell = document.createElement('td');
//parse out ingredients string
for(let j = 0; j < arg[i].ingredients.length; j++){
if(j == arg[i].ingredients.length - 1){
ingString += arg[i].ingredients[j];
}
else{
ingString += arg[i].ingredients[j] + ", ";
}
}
//parse out directions string
for(let j = 0; j < arg[i].directions.length; j++){
if(j == arg[i].directions.length - 1){
dirString += arg[i].directions[j];
}
else{
dirString += arg[i].directions[j] + ", ";
}
}
//fill cells with appropriate data
recipe_cell.innerHTML = arg[i].recipename;
ingredient_cell.innerHTML = ingString;
//direction_cell.innerHTML = dirString;
origin_cell.innerHTML = arg[i].origin;
preptime_cell.innerHTML = arg[i].prep;
course_cell.innerHTML = arg[i].course;
//blake breaking images here uncomment below line to fix
if (arg[i].img != "") image_cell.innerHTML = "<img src = 'images/" + arg[i].img + "' width = 128px height = 128px> </img>";
else image_cell.innerHTML = "";
//image_cell.innerHTML = "<img src =" + arg[i].img + "' width = 128px height = 128px> </img>";
//add rows to the table
tr.appendChild(image_cell);
tr.appendChild(recipe_cell);
tr.appendChild(ingredient_cell);
//tr.appendChild(direction_cell);
tr.appendChild(origin_cell);
tr.appendChild(preptime_cell);
tr.appendChild(course_cell);
table.appendChild(tr);
}
}
})
//gets text in the search bar upon click and sends it to main
document.getElementById('search-request').addEventListener('click', function(){
let search = document.getElementById('search').value;
let ingredients_checkbox = document.getElementById('ingredient_checkbox').checked;
let recipename_checkbox = document.getElementById('recipename_checkbox').checked;
let preptime_checkbox = document.getElementById('preptime_checkbox').checked;
let origin_checkbox = document.getElementById('origin_checkbox').checked;
let course_checkbox = document.getElementById('course_checkbox').checked;
//creating a checkbox code for to append to the search string so that the search funciton
//knows which values to search for
//checkbox_code = "ingredient checkbox value, recipe_name checkbox value"
var checkbox_code = "";
if(ingredients_checkbox === true)
checkbox_code = checkbox_code + "1";
else{
checkbox_code = checkbox_code + "0";
}
if(recipename_checkbox === true)
checkbox_code = checkbox_code + "1";
else{
checkbox_code = checkbox_code + "0";
}
if(preptime_checkbox === true)
checkbox_code = checkbox_code + "1";
else{
checkbox_code = checkbox_code + "0";
}
if(origin_checkbox === true)
checkbox_code = checkbox_code + "1";
else{
checkbox_code = checkbox_code + "0";
}
if(course_checkbox === true)
checkbox_code = checkbox_code + "1";
else{
checkbox_code = checkbox_code + "0";
}
//if no checkbox is selected search by everything
if(checkbox_code === "00000") checkbox_code = "11111";
ipcRenderer.send('search-request', checkbox_code + search);
})
$("#tableId").on('click', 'tr', function() {
var rowid = this.id;
log(returnarr[rowid]);
if(rowid !== "title"){
ipcRenderer.send('display_recipe', returnarr[rowid], returnarr, "search");
}
});