-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
227 lines (189 loc) · 6.54 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
/*
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
*/
var checked_in, checked_out;
var submit_in = document.getElementById("btn_ch");
if (submit_in){
submit_in.addEventListener("click", function() {
checked_in= getCheckBoxValues(".in");
localStorage.setItem("checked_in",checked_in);
console.log(checked_in);
});
}
var submit_out = document.getElementById("btn_out");
if (submit_in){
submit_out.addEventListener("click", function() {
checked_out = getCheckBoxValues(".out");
console.log(checked_out);
});
}
function export2txt(originalData) {
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([JSON.stringify(originalData, null, 2)], {
type: "text/plain"
}));
a.setAttribute("download", "data.json");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function makeVisible()
{
checker('year_ch','label-startyear');
checker('year_ch','label-endyear');
checker('year_ch','startyear');
checker('year_ch','endyear');
checker('dir_ch','dir');
checker('nation_ch','nation');
checker('address_ch','address');
}
function checker(check,input){
if(document.getElementById(check).checked){
document.getElementById(input).style.visibility = 'visible';
}
else
{
document.getElementById(input).style.visibility = 'hidden';
}
}
function getCheckBoxValues(target){
console.log("inside getCheckBoxValues");
var checkboxes = document.querySelectorAll(target);
var checked = [];
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
if (checkbox.checked) checked.push(checkbox.value);
}
return checked;
}
function waitForCondition() {
return new Promise(resolve => {
function checkFlag() {
if (appD!=null) {
console.log('met');
resolve();
} else {
window.setTimeout(checkFlag, 30000);
}
}
checkFlag();
});
}
async function run() {
console.log('before');
await waitForCondition()
console.log('after');
}
function testGS(){
document.getElementById("announce").textContent= "Data is loading ^_^";
const url="https://script.google.com/macros/s/AKfycbyVJ1atQPDr7J_SmlrveoZsI0nqXtxVnThOjrx165pUh-rnif9BablI/exec";
fetch(url).then(d => d.json())
.then(d => {
var allData=d[0].data;
console.log(checked_in);
//var year=(document.getElementById("year")).value;
var startyear=parseInt((document.getElementById("startyear")).value);
var endyear=parseInt((document.getElementById("endyear")).value);
var years=[];
if (startyear==endyear){
years.push(startyear);
}
else{
for (var i=startyear; i<=endyear; i++)
years.push(i);
}
console.log(years);
nations = [];
for (var option of document.getElementById('nation').options) {
if (option.selected) {
nations.push(option.value);
console.log(option.value);
}
}
var address=(document.getElementById("address")).value;
var result_obj={};
var count =0;
var correctSets=[];
if (checked_in.includes("ADDRESS")){
var addressData=allData.filter(function (person) { return person.street_clean == address});
result_obj[count]=addressData;
count+=1;
}
if (checked_in.includes("YR")){
var k;
for (k=0;k<endyear-startyear+1; k++){
var yearData=allData.filter(function (person) { return person.YR == years[0]});
result_obj[count]=yearData;
count+=1;
if (checked_in.includes("NATION")){
var nationSets = [];
for (let i = 0; i < nations.length; i++) {
nationData = allData.filter(function (person) { return person.NATIONALITY == nations[i] });
nationSets.push(nationData);
result_obj[count]=nationData;
result_obj,
correctData = Object
.values(result_obj)
.reduce((a, b) => b.filter(Set.prototype.has, new Set(a)));
if (correctData.length!=0 && !correctSets.includes(correctData)){
correctSets.push(correctData);
}
}
console.log("nation");
} else{
result_obj,
correctData = Object
.values(result_obj)
.reduce((a, b) => b.filter(Set.prototype.has, new Set(a)));
if (correctData.length!=0 && !correctSets.includes(correctData)){
correctSets.push(correctData);
}
console.log("no nation");
}
}
}
console.log(correctSets);
var jsonOut = JSON.stringify(correctSets);
//export2txt(correctData);
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(jsonOut);
var dlAnchorElem = document.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href", dataStr );
dlAnchorElem.setAttribute("download", "out.json");
dlAnchorElem.click();
document.getElementById("app").textContent= jsonOut;
document.getElementById("announce").textContent= "Data is ready ^_^";
document.getElementById("results_btn").style.visibility = 'visible';
//send elements to the results.js page
localStorage.setItem("output",jsonOut);
var jsonCheckedin=JSON.stringify(checked_in);
var jsonCheckedout=JSON.stringify(checked_out);
localStorage.setItem("check-in",jsonCheckedin);
localStorage.setItem("check-out",jsonCheckedout);
});
}
function addGS(){
const url="https://script.google.com/macros/s/AKfycbyVJ1atQPDr7J_SmlrveoZsI0nqXtxVnThOjrx165pUh-rnif9BablI/exec";
fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'no-cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'omit', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow', // manual, *follow, error
body: JSON.stringify({name:"Olivia"}) // body data type must match "Content-Type" header
});
}
function clear(){
document.getElementById("app").textContent= "";
}
document.getElementById("btn2").addEventListener("click",addGS);
document.getElementById("btn").addEventListener("click",testGS);
document.getElementById("btn3").addEventListener("click",clear);