-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathstudy_sorter.js
493 lines (446 loc) · 22.1 KB
/
study_sorter.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// TODO: need to break this into modules rather than one long file
// TODO: do we need to hide the links to the page?
// private key stuff - check into this
// write up a blog post about this procedure - reference other websites:
// https://coderwall.com/p/duapqq/use-a-google-spreadsheet-as-your-json-backend
// https://stackoverflow.com/questions/24531351/retrieve-google-spreadsheet-worksheet-json
const studyLink = 'https://spreadsheets.google.com/feeds/list/1PMWsPEfHFC6myMwhdidyr2hhRK5ZA-JrDQzwRoDTFaw/oua1i2d/public/values?alt=json-in-script&callback=text';
const buildLink = 'https://spreadsheets.google.com/feeds/list/1PMWsPEfHFC6myMwhdidyr2hhRK5ZA-JrDQzwRoDTFaw/oua1i2d/public/values?alt=json-in-script&callback=text';
// Getting the side nav buttons.
const html_css_button = document.getElementById("HTML&CSS_Tab");
const js_button = document.getElementById("Javascript_Tab");
const react_button = document.getElementById("React_Tab");
// Getting the side nav mobile icon buttons
const course_icons = document.querySelectorAll(".mobile-icons");
const full_time = document.getElementById("full-time-wrapper");
const part_time = document.getElementById("part-time-wrapper");
// Creating new Div elements.
const html_full_new_el = document.createElement("DIV");
const html_part_new_el = document.createElement("DIV");
const js_full_new_el = document.createElement("DIV");
const js_part_new_el = document.createElement("DIV");
const react_full_new_el = document.createElement("DIV");
const react_part_new_el = document.createElement("DIV");
// Giving the new div elements ids.
html_full_new_el.setAttribute("ID", "html_full_time_Contents");
html_part_new_el.setAttribute("ID", "html_part_time_Contents");
js_full_new_el.setAttribute("ID", "js_full_time_Contents");
js_part_new_el.setAttribute("ID", "js_part_time_Contents");
react_full_new_el.setAttribute("ID", "react_full_time_Contents");
react_part_new_el.setAttribute("ID", "react_part_time_Contents");
// Attaching those ids to the newly created div elements.
full_time.appendChild(html_full_new_el);
part_time.appendChild(html_part_new_el);
full_time.appendChild(js_full_new_el);
part_time.appendChild(js_part_new_el);
full_time.appendChild(react_full_new_el);
part_time.appendChild(react_part_new_el);
// These variables are what separates the course boxes.
const studySelectorHTML_Full = document.getElementById('html_full_time_Contents');
const studySelectorHTML_Part = document.getElementById('html_part_time_Contents');
const studySelectorJS_Full = document.getElementById('js_full_time_Contents');
const studySelectorJS_Part = document.getElementById('js_part_time_Contents');
const studySelectorReact_Full = document.getElementById('react_full_time_Contents');
const studySelectorReact_Part = document.getElementById('react_part_time_Contents');
const buildSelector = document.querySelector('#build-groups');
const htmlFull12am = ['HTML & CSS - Full Time - 12AM'];
const htmlFull6am = ['HTML & CSS - Full Time - 6AM'];
const htmlFull12pm = ['HTML & CSS - Full Time - 12PM'];
const htmlFull6pm = ['HTML & CSS - Full Time - 6PM'];
const htmlPart12am = ['HTML & CSS - Part Time - 12AM'];
const htmlPart6am = ['HTML & CSS - Part Time - 6AM'];
const htmlPart12pm = ['HTML & CSS - Part Time - 12PM'];
const htmlPart6pm = ['HTML & CSS - Part Time - 6PM'];
const jsFull12am = ['JavaScript - Full Time - 12AM'];
const jsFull6am = ['JavaScript - Full Time - 6AM'];
const jsFull12pm = ['JavaScript - Full Time - 12PM'];
const jsFull6pm = ['JavaScript - Full Time - 6PM'];
const jsPart12am = ['JavaScript - Part Time - 12AM'];
const jsPart6am = ['JavaScript - Part Time - 6AM'];
const jsPart12pm = ['JavaScript - Part Time - 12PM'];
const jsPart6pm = ['JavaScript - Part Time - 6PM'];
const reactFull12am = ['React - Full Time - 12AM'];
const reactFull6am = ['React - Full Time - 6AM'];
const reactFull12pm = ['React - Full Time - 12PM'];
const reactFull6pm = ['React - Full Time - 6PM'];
const reactPart12am = ['React - Part Time - 12AM'];
const reactPart6am = ['React - Part Time - 6AM'];
const reactPart12pm = ['React - Part Time - 12PM'];
const reactPart6pm = ['React - Part Time - 6PM'];
/* The functions for showing the corresponding course boxes on button clicks */
const showHtmlBoxes = () => {
full_time.style.display = "block";
part_time.style.display = "block";
studySelectorHTML_Full.style.display = "block";
studySelectorHTML_Part.style.display = "block";
studySelectorJS_Full.style.display = "none";
studySelectorJS_Part.style.display = "none";
studySelectorReact_Full.style.display = "none";
studySelectorReact_Part.style.display = "none";
}
html_css_button.addEventListener("click", showHtmlBoxes, false);
const showJsBoxes = () => {
full_time.style.display = "block";
part_time.style.display = "block";
studySelectorHTML_Full.style.display = "none";
studySelectorHTML_Part.style.display = "none";
studySelectorJS_Full.style.display = "block";
studySelectorJS_Part.style.display = "block";
studySelectorReact_Full.style.display = "none";
studySelectorReact_Part.style.display = "none";
}
js_button.addEventListener("click", showJsBoxes, false);
const showReactBoxes = () => {
full_time.style.display = "block";
part_time.style.display = "block";
studySelectorHTML_Full.style.display = "none";
studySelectorHTML_Part.style.display = "none";
studySelectorJS_Full.style.display = "none";
studySelectorJS_Part.style.display = "none";
studySelectorReact_Full.style.display = "block";
studySelectorReact_Part.style.display = "block";
}
react_button.addEventListener("click", showReactBoxes, false);
// Course icons event listeners
course_icons[0].addEventListener("click", showHtmlBoxes, false);
course_icons[1].addEventListener("click", showJsBoxes, false);
course_icons[2].addEventListener("click", showReactBoxes, false);
appendGroups();
async function appendGroups() {
try {
const groupsList = await Promise.all([grabGroup(studyLink), grabGroup(buildLink)]);
const study = groupsList[0];
const build = groupsList[1];
//This Shows the people's names and sorts them.
sortPeople(study);
//This sorts the times in 2 lists
createUL(htmlFull12am, studySelectorHTML_Full);
createUL(htmlFull6am, studySelectorHTML_Full);
createUL(htmlFull12pm, studySelectorHTML_Full);
createUL(htmlFull6pm, studySelectorHTML_Full);
createUL(htmlPart12am, studySelectorHTML_Part);
createUL(htmlPart6am, studySelectorHTML_Part);
createUL(htmlPart12pm, studySelectorHTML_Part);
createUL(htmlPart6pm, studySelectorHTML_Part);
createUL(jsFull12am, studySelectorJS_Full);
createUL(jsFull6am, studySelectorJS_Full);
createUL(jsFull12pm, studySelectorJS_Full);
createUL(jsFull6pm, studySelectorJS_Full);
createUL(jsPart12am, studySelectorJS_Part);
createUL(jsPart6am, studySelectorJS_Part);
createUL(jsPart12pm, studySelectorJS_Part);
createUL(jsPart6pm, studySelectorJS_Part);
createUL(reactFull12am, studySelectorReact_Full);
createUL(reactFull6am, studySelectorReact_Full);
createUL(reactFull12pm, studySelectorReact_Full);
createUL(reactFull6pm, studySelectorReact_Full);
createUL(reactPart12am, studySelectorReact_Part);
createUL(reactPart6am, studySelectorReact_Part);
createUL(reactPart12pm, studySelectorReact_Part);
createUL(reactPart6pm, studySelectorReact_Part);
// function to pass study group arrays to build strings of html that
// are unorderedlists of group participants
function createUL(arr, selector) {
const label = arr[0];
const len = arr.length;
let htmlString = '';
let liString = '';
let ulArr = [];
for (let i = 1; i < len; i++) {
if (i % 6 === 0) {
// if on the 6th name finish concatentation THEN
// push string of list items to ul array THEN
// reset liString to ''
liString += `<li> ${arr[i]} </li>`;
ulArr.push(liString);
liString = '';
// }else if (i % 6 !== 0 && (len - i) < 6) {
// console.log('here');
// liString +=
// `
// <li>
// ${arr[i]}
// </li>
// `;
// for (let j = 0; j < - len; j++) {
// liString +=
// `<li>
// open
// </li>
// `;
// }
}else{
// add another list item to string
liString += `<li> ${arr[i]} </li>`;
}
}
ulArr.push(liString)
htmlString = makeHTMLString(label, ulArr);
selector.insertAdjacentHTML('beforeend', htmlString);
}
} catch(err) {
console.log('error', err);
}
}
function makeHTMLString(title, ulArr) {
let color = '';
let htmlString = '';
let rowString = '';
const len = ulArr.length;
if (title.includes('HTML')) {color = 'bg-info'}
else if (title.includes('JavaScript')) {color = 'bg-success'}
else if (title.includes('React')) {color = 'bg-danger'}
for (let i = 0; i < len; i++) {
if ((i + 1) % 4 === 0) {
rowString +=
`
<div class="col-sm-12 col-md-4">
<div class="card ${color} mb-2">
<div class="card-body text-center">
<h5 class="card-title">${title}</h5>
<ul>
${ulArr[i]}
</ul>
</div>
</div>
</div>
`;
htmlString += `<div class="row"> ${rowString} </div>`;
rowString = '';
} else{
rowString +=
`
<div class="col-sm-12 col-md-4">
<div class="card ${color} mb-2">
<div class="card-body text-center">
<h5 class="card-title">${title}</h5>
<ul>
${ulArr[i]}
</ul>
</div>
</div>
</div>
`;
}
}
htmlString += `<div class="row"> ${rowString}</div>`;
return htmlString;
}
async function grabGroup(link) {
try {
const res = await fetch(link,
{ headers: {'Content-Type': 'text/plain; charset=utf-8'} });
const text = await res.text();
const sheetText = await text.replace('// API callback\ntext(', '')
.replace(');', '');
const groupObj = await JSON.parse(sheetText);
const sortedArr = await newParse(groupObj);
return sortedArr;
} catch(err) {
console.log('error', err);
}
}
async function newParse(json) {
try {
let newArr = [];
let returnArr = [];
for (let key in json.feed.entry) {
let newObj = {};
newObj.technology = json.feed.entry[key].gsx$technology.$t;
newObj.commitment = json.feed.entry[key].gsx$commitment.$t;
newObj.beginning = json.feed.entry[key].gsx$beginning.$t;
newObj.name = json.feed.entry[key].gsx$username.$t;
newArr.push(newObj);
}
returnArr = newArr.sort(sortObjMultProp('technology', 'commitment', 'beginning'));
return returnArr;
} catch(err) {
console.log('error', err);
}
}
function sortObjMultProp() {
const props = arguments;
return function (obj1, obj2) {
let i = 0
let result = 0
let numberOfProperties = props.length;
// try getting a different result from 0 (equal) as long as we have // extra properties to compare
while(result === 0 && i < numberOfProperties) {
result = dynamicSort(props[i])(obj1, obj2);
i++;
}
return result;
}
}
function dynamicSort(property) {
let sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
let result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
function sortPeople(people) {
// this giant conditional sort feels really really hacky
/* TODO: find a better way to sort/filter the users -- 6/9/2019 This is Chris L,
I worked on this TODO. I changed the 'if, else if' statements into one switch statement. */
people.forEach((person) => {
switch (true) {
/****************** FOR HTML AND CSS TECHNOLOGY CHOSEN ***************/
//Full Time Hours
case person.technology === 'HTML & CSS'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 am':
htmlFull12am.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 am':
htmlFull6am.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 pm':
htmlFull12pm.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 pm':
htmlFull6pm.push(person.name);
break;
//Part Time Hours
case person.technology === 'HTML & CSS'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 am':
htmlPart12am.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 am':
htmlPart6am.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 pm':
htmlPart12pm.push(person.name);
break;
case person.technology === 'HTML & CSS'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 pm':
htmlFull6pm.push(person.name);
break;
/********************* FOR JAVASCRIPT TECHNOLOGY CHOSEN *****************/
//Full Time Hours
case person.technology === 'JavaScript'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 am':
jsFull12am.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 am':
jsFull6am.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 pm':
jsFull12pm.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 pm':
jsFull6pm.push(person.name);
break;
//Part Time Hours
case person.technology === 'JavaScript'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 am':
jsPart12am.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 am':
jsPart6am.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 pm':
jsPart12pm.push(person.name);
break;
case person.technology === 'JavaScript'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 pm':
jsFull6pm.push(person.name);
break;
/********************* FOR REACT TECHNOLOGY CHOSEN *****************/
//Full Time Hours
case person.technology === 'React'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 am':
reactFull12am.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 am':
reactFull6am.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '12 pm':
reactFull12pm.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'full time - 6 hours a day'
&& person.beginning === '6 pm':
reactFull6pm.push(person.name);
break;
//Part Time Hours
case person.technology === 'React'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 am':
reactPart12am.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 am':
reactPart6am.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '12 pm':
reactPart12pm.push(person.name);
break;
case person.technology === 'React'
&& person.commitment === 'part time - 3 hours a day'
&& person.beginning === '6 pm':
reactFull6pm.push(person.name);
break;
}
});
}
/** 6/12/2019 Chris L: Not sure, didn't quite work the way I wanted it.
* - Creating a ajax call for when html & css button is clicked */
/*const xhrHTMLFull = new XMLHttpRequest();
xhrHTMLFull.onreadystatechange = () => {
if(xhrHTMLFull.readyState === 4) {
document.getElementById("full-time-wrapper").innerHTML = xhrHTMLFull.responseText;
}
};
xhrHTMLFull.open("GET", "./ajax_calls_studyGroup/htmlAjaxFull.html");
//Ajax Call for the Part time
const xhrHTMLPart = new XMLHttpRequest();
xhrHTMLPart.onreadystatechange = () => {
if(xhrHTMLPart.readyState === 4) {
document.getElementById("part-time-wrapper").innerHTML = xhrHTMLPart.responseText;
}
};
xhrHTMLPart.open("GET", "./ajax_calls_studyGroup/htmlAjaxPart.html");
let sendAjaxHTML = () => {
console.log("button was clicked");
xhrHTMLFull.send();
xhrHTMLPart.send();
}
/******************* END OF AJAX CALLS *******************************************/