imamnurby commited on
Commit
e10f5bd
1 Parent(s): 130b79c

Create frontend.js

Browse files
Files changed (1) hide show
  1. static/frontend.js +223 -0
static/frontend.js ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //display the pop up with more information
2
+ function onClickMore(string){
3
+ document.getElementById(string).style.display ='block';
4
+ }
5
+
6
+ //hide the pop up with more information
7
+ function onOut(string){
8
+ document.getElementById(string).style.display ='none';
9
+ }
10
+
11
+ async function getResponse(url, user_query){
12
+ try {
13
+ let predictions = await fetch(url,{
14
+ method: 'POST',
15
+ body: JSON.stringify({'user_query': user_query}),
16
+ headers: {
17
+ 'Content-type': 'application/json; charset=UTF-8',
18
+ 'Access-Control-Allow-Origin': '*',
19
+ 'Access-Control-Allow-Methods': 'POST, GET',
20
+ }
21
+ });
22
+ return predictions.json()
23
+ } catch (error) {
24
+ console.log(error)
25
+ }
26
+ }
27
+
28
+ //when retrieve is clicked
29
+ async function onSubmit(){
30
+
31
+
32
+
33
+ let user_query = document.getElementById("exampleFormControlInput1").value
34
+ // let url_query = "http://10.27.32.183:8111/predict"
35
+
36
+
37
+ if (user_query != ''){
38
+ document.getElementById("spinner").style.display = 'block';
39
+
40
+ let url_query = "/predict"
41
+ let predictions = await getResponse(url_query, user_query)
42
+
43
+ document.getElementById("spinner").style.display = 'none';
44
+ document.getElementById("grey-container").className = 'album py-5 bg-light';
45
+
46
+ let response = predictions['predictions']
47
+
48
+
49
+ //loop through number of results
50
+ html = ''
51
+ var w = 0
52
+ for(let i=0; i < response.length; i++){
53
+ console.log(i)
54
+ html += '<div class="row g-4 m-2" id="predictions">';
55
+ html += '<div class="col-6">';
56
+ html += '<div class="card border-primary mb-3">';
57
+ // library name and description
58
+ html += '<div class="card-body">';
59
+ html += '<h5 class="card-title text-primary">'+ response[i]["library_name"] +'</h5>';
60
+ html += '<p class="card-text">'+ response[i]["Description"] +'</p>';
61
+ html += '</div>';
62
+ // end library name and description
63
+ // group button bottom (View GitHub and See Usage Patterns and Configs)
64
+ html += '<div class="card-footer">';
65
+ html += '<div class=" d-flex justify-content-between align-items-center">';
66
+ html += '<div class="btn-group">'
67
+ html += '<a type="button" class="btn btn-sm btn-outline-secondary" href='+ response[i]["Github URL"] +'>View Github</a>';
68
+ html += '</div>'
69
+ html += '<a href="javascript:void(0)" id="myTooltip" data-bs-toggle="tooltip" data-bs-placement="right" onclick="onClickMore('+ response[i]["id"] +')">See Usage Patterns and Interface Configs</a>';
70
+ html += '</div>';
71
+ html += '</div>';
72
+ html += '</div>';
73
+ html += '</div>';
74
+ // end card group button
75
+ html += '<div class="col-6"><div id="'+ response[i]["id"] +'" style="display: none;" class="card border-primary mb-3">';
76
+ html += '<div class="card-body">';
77
+ //begin accordion outer
78
+ html += '<div class="accordion" id="accordionOuter'+ i + '">';
79
+ // first item
80
+ html += '<div class="accordion-item">';
81
+ html += '<h2 class="accordion-header" id="headingOneOuter"'+ i + '>';
82
+ html += '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne'+ i +'" aria-expanded="true" aria-controls="collapseOne'+ i +'">'
83
+ html += 'Interface Configuration';
84
+ html += '</button>';
85
+ html += '</h2>';
86
+ html += '<div id="collapseOne'+ i +'" class="accordion-collapse collapse show" aria-labelledby="headingOneOuter"'+ i +'data-bs-parent="#accordionOuter'+ i + '">'
87
+ html += '<div class="accordion-body">'
88
+ // html += 'Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven\'t heard of them accusamus labore sustainable VHS.'
89
+
90
+ // interface configuration begin
91
+ html += '<h6>Protocol: '+ response[i]["hw_config"]["protocol"]+ '</h6>';
92
+ html += '<table class="table table-hover"><thead><tr><th>Arduino Type</th><th>I/O hardware --> Arduino</th></tr></thead><tbody>'
93
+ //loop through hardware config
94
+ for (const property in response[i]["hw_config"]["pin_connection_from_hw_to_arduino"]){
95
+ let firstLetter = property.charAt(0).toUpperCase();
96
+ let remLetter = property.substring(1).split("_");
97
+ html += '<tr><td>' + firstLetter + remLetter[0] + '_' + remLetter[1].charAt(0).toUpperCase() + remLetter[1].substring(1) + '</td><td>';
98
+ //loop through the arduino type
99
+ for (let y=0; y < response[i]["hw_config"]["pin_connection_from_hw_to_arduino"][property].length; y++){
100
+ // html += '(' + (response[i]["hw_config"]["pin_connection_from_hw_to_arduino"][property][y][0] + ',' + response[i]["hw_config"]["pin_connection_from_hw_to_arduino"][property][y][1]) + ') ';
101
+ html += '(' + response[i]["hw_config"]["pin_connection_from_hw_to_arduino"][property][y] +') ';
102
+ }
103
+ html += ' </td></tr>';
104
+ }
105
+ html += '</tbody></table> ';
106
+ // end interface configuration
107
+
108
+ html += '</div>'
109
+ html += '</div>'
110
+ html += '</div>'
111
+ // end first item
112
+
113
+ //second item
114
+ html += '<div class="accordion-item">';
115
+ html += '<h2 class="accordion-header" id="headingTwoOuter'+ i +'">';
116
+ html += '<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo'+ i +'" aria-expanded="false" aria-controls="collapseTwo'+ i +'">'
117
+ html += 'Usage Patterns';
118
+ html += '</button>';
119
+ html += '</h2>';
120
+ html += '<div id="collapseTwo'+ i +'" class="accordion-collapse collapse" aria-labelledby="headingTwoOuter'+ i +'" data-bs-parent="#accordionOuter'+ i + '">'
121
+ html += '<div class="accordion-body">'
122
+ // html += 'Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven\'t heard of them accusamus labore sustainable VHS.'
123
+ // begin usage pattern
124
+ let w = 0
125
+ Object.keys(response[i]["usage_patterns"]).forEach(function(key) {
126
+ let usages_arr = response[i]["usage_patterns"][key]
127
+ // begin accordion inner
128
+ html += '<div class="accordion" id="accordionInner">'
129
+ // begin item
130
+ html += ' <div class="accordion-item">'
131
+ html += ' <h2 class="accordion-header" id="headingOneInner'+ key + i +'">'
132
+ if (w == 0){
133
+ html += ' <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOneInner'+ key + i +'" aria-expanded="true" aria-controls="collapseOneInner'+ key + i +'">'
134
+ }
135
+ else {
136
+ html += ' <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOneInner'+ key + i +'" aria-expanded="false" aria-controls="collapseOneInner'+ key + i +'">'
137
+ }
138
+ html += ' Sensor Object: '+ key
139
+ html += ' </button>'
140
+ html += ' </h2>'
141
+ if (w == 0){
142
+ html += ' <div id="collapseOneInner'+ key + i +'" class="accordion-collapse collapse show" aria-labelledby="headingOneInner'+ key + i +'">'
143
+ }
144
+ else {
145
+ html += ' <div id="collapseOneInner'+ key + i +'" class="accordion-collapse collapse" aria-labelledby="headingOneInner'+ key + i +'">'
146
+ }
147
+ html += ' <div class="accordion-body">'
148
+
149
+ // being accordion inner
150
+ html += '<div style="margin-bottom: 15px" class="accordion" id="accordionPanelsStayOpenExample"' + response[i]["id"] + key + '>'; //1
151
+
152
+ for (let x=0; x < usages_arr.length; x++){
153
+ let pattern = usages_arr[x].split(" ")
154
+ html += '<div data-bs-toggle="collapse" data-bs-target="#patternCollapse' + response[i]["id"] + key + x + '">'; //2
155
+ html += '<div class="accordion-item">'; //3
156
+ html += '<h2 class="accordion-header" id="panelsStayOpen-heading' + response[i]["id"] + key +'">';
157
+
158
+ if (x == 0){
159
+ html += '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse' + response[i]["id"] + key + x + '" aria-expanded="true" aria-controls="panelsStayOpen-collapse' + response[i]["id"] + key + x + '">';
160
+ }
161
+ else {
162
+ html += '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse' + response[i]["id"] + key + x + '" aria-expanded="false" aria-controls="panelsStayOpen-collapse' + response[i]["id"] + key + x + '">';
163
+ }
164
+
165
+ html += 'Pattern ' + (x+1);
166
+ html += '</button>';
167
+ html += '</h2>';
168
+
169
+ //4
170
+ if (x== 0){
171
+ html += '<div id="panelsStayOpen-collapse' + response[i]["id"] + key + x + '" class="accordion-collapse collapse show" aria-labelledby="panelsStayOpen-heading' + response[i]["id"] + key + x + '">';
172
+ } else {
173
+ html += '<div id="panelsStayOpen-collapse' + response[i]["id"] + key + x + '" class="accordion-collapse collapse" aria-labelledby="panelsStayOpen-heading' + response[i]["id"] + key + x + '">';
174
+ }
175
+ html += '<div class="accordion-body">' //5
176
+ html += '<ul>';
177
+ for (let y=0; y<pattern.length; y++){
178
+ html += '<li>' + pattern[y] + '</li>';
179
+ }
180
+ html += '</ul>';
181
+ html += '</div>' //5
182
+ html += '</div>'; //4
183
+ html += '</div>'; //3
184
+ html += '</div>'; //2
185
+ }
186
+ html += '</div>' //1
187
+
188
+ html += ' </div>'
189
+ html += ' </div>'
190
+ html += ' </div>'
191
+ //end item
192
+ html += '</div>'
193
+ // end accordion inner
194
+ w += 1
195
+ });
196
+ // end usage pattern
197
+
198
+ html += '</div>'
199
+ html += '</div>'
200
+ html += '</div>'
201
+ //end second item
202
+
203
+ html += '</div>';
204
+ // end accordion outer
205
+
206
+ //button close
207
+ html += '<div class="position-absolute top-0 end-0">'
208
+ html += '<button type="button" class="btn-close" aria-label="Close" onclick="onOut('+ response[i]["id"] +')"></button>';
209
+ html += '</div>';
210
+ // end button close
211
+
212
+ html += '</div></div>';
213
+ html += '</div></div>';
214
+ }
215
+
216
+ //append the html to the container
217
+ document.getElementById("main-parent").innerHTML = (html);
218
+
219
+ html2 = '<div class="container"><p class="float-right"><a href="#">Back to top</a></p>';
220
+ //append footer to allow going back to the top
221
+ document.getElementById("footer").innerHTML = (html2);
222
+ }
223
+ }