-
Notifications
You must be signed in to change notification settings - Fork 0
/
Moody.js
77 lines (65 loc) · 2.23 KB
/
Moody.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
jQuery(document).ready(GetCurrentMood());
function GetCurrentMood() {
// get moods json
var moods = new XMLHttpRequest();
moods.open('GET', '/moods/moods.json');
moods.responseType = 'json';
moods.send();
// get current mood json
moods.onload = function() {
var currentMood = moods.response;
document.getElementById("current").innerHTML = "current mood: " + currentMood.current;
var currentMoodJSON = new XMLHttpRequest();
currentMoodJSON.open('GET', '/moods/' + currentMood.current + '.json');
currentMoodJSON.responseType = 'json';
currentMoodJSON.send();
// insert content from json
currentMoodJSON.onload = function() {
var json = currentMoodJSON.response;
ParseAndInsertFromMood(json);
var myLazyLoad = new LazyLoad({threshold: 480});
}
}
}
function ParseAndInsertFromMood(json) {
const container = $('#container');
json.forEach(row => {
container.append(`<div class="grid"></div>`)
const el = $('div.grid:last')
row.forEach(cell => {
el.append(`
<div class="grid-cell" height="960px">
<div class="img_"><a href="${cell.img_link}" height="960px">
<img class="lazyload" data-src="${cell.img_src}" height="960px"/></a>
</div>
</div> `) }) })
}
// todo: implement
function GetRandomMood() {
// get random json file
var moods = new XMLHttpRequest();
moods.open('GET', '/moods/moods.json');
moods.responseType = 'json';
moods.send();
// get contents of active json file
moods.onload = function() {
var moodsResponse = moods.response;
var moodList = moodsResponse.moods.split(',');
var rMood = moodList[Math.floor(Math.random() * moodList.length)];
var current_Mood = document.getElementById("current").innerHTML;
if (current_Mood == "current mood: " + rMood)
{ var rMood = moodList[Math.floor(Math.random() * moodList.length)]; }
document.getElementById("current").innerHTML = "current mood: " + rMood;
var randomMood = new XMLHttpRequest();
randomMood.open('GET', '/moods/' + rMood + '.json');
randomMood.responseType = 'json';
randomMood.send();
// insert json content
randomMood.onload = function() {
var json = randomMood.response;
document.getElementById("container").innerHTML = "";
ParseAndInsertFromMood(json);
var myLazyLoad = new LazyLoad({threshold: 480});
}
}
}