-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
135 lines (122 loc) · 6.81 KB
/
functions.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
function getRandomMember() {
$.ajax({
url: 'https://cors-anywhere.herokuapp.com/https://api.parliament.uk/Live/fixed-query/person_photo_index?format=application%2Fjson',
dataType: 'JSON',
type: 'GET',
success: function (data) {
var m = Math.floor(Math.random() * (data.results.bindings.length));
var imageId = data.results.bindings[m].image.value;
var imageId = imageId.replace('https://id.parliament.uk/','');
var imageURL = 'https://api.parliament.uk/Live/photo/'+imageId+'.jpeg?crop=CU_1:1&width=186&quality=80';
document.getElementById("image").src = imageURL;
var img = document.getElementById("image");
console.log('You\'ve requested the image for: '+data.results.bindings[m].displayAs.value);
if (img.complete) {
} else {
img.addEventListener('load', imageLoaded(imageId))
img.addEventListener('error', function() {})
}
correctAnswer = Math.floor(Math.random() * 4);
document.getElementById("name"+correctAnswer).innerHTML = data.results.bindings[m].displayAs.value;
document.getElementById("correctAnswer").value = correctAnswer;
for(i=0; i<4; i++){
if(i !== correctAnswer){
document.getElementById("name"+i).innerHTML = data.results.bindings[Math.floor(Math.random() * (data.results.bindings.length))].displayAs.value;
}
}
$('#names').removeClass('hidden');
var mid = data.results.bindings[m].person.value;
var mid = mid.replace('https://id.parliament.uk/','');
correctConstituency = Math.floor(Math.random() * 4);
document.getElementById("correctConstituency").value = correctConstituency;
$.ajax({
url: 'https://cors-anywhere.herokuapp.com/https://api.parliament.uk/Live/fixed-query/person_by_id?person_id='+mid+'&format=application%2Fjson',
dataType: 'JSON',
type: 'GET',
success: function (mdata) {
for(const prop in mdata){
if(typeof mdata[prop]["https://id.parliament.uk/schema/constituencyGroupName"] != "undefined"){
var constituency = mdata[prop]["https://id.parliament.uk/schema/constituencyGroupName"][0].value;
}
}
document.getElementById("const"+correctConstituency).innerHTML = constituency;
}
});
// Get some random constituency names
$.ajax({
url: 'https://cors-anywhere.herokuapp.com/https://api.parliament.uk/Live/fixed-query/constituency_current?format=application%2Fjson',
dataType: 'JSON',
type: 'GET',
success: function (cdata) {
var constlist = Array();
for(const prop in cdata){
if(typeof cdata[prop]["https://id.parliament.uk/schema/constituencyGroupName"] != "undefined"){
var currentconst = cdata[prop]["https://id.parliament.uk/schema/constituencyGroupName"][0].value;
constlist.push(currentconst);
}
}
for(i=0; i<4; i++){
if(i !== correctConstituency){
document.getElementById("const"+i).innerHTML = constlist[Math.floor(Math.random() * constlist.length)];
}
}
}
});
}
});
}
function imageLoaded(imageId){
var img2 = document.getElementById("secondImage");
var imageURL = 'https://api.parliament.uk/Live/photo/'+imageId+'.jpeg?crop=CU_1:1&width=500&quality=80';
document.getElementById("secondImage").src = imageURL;
if (img2.complete) {
} else {
img2.addEventListener('load', function() {
document.getElementById("image").src = imageURL;
console.log('Loaded high quality image');
})
img2.addEventListener('error', function() {})
}
}
function checkname(answer) {
var correctAnswer = document.getElementById("correctAnswer").value;
if(answer == correctAnswer){
var correctElement = document.getElementById("name"+correctAnswer);
correctElement.classList.add("btn-success");
correctElement.classList.remove("btn-secondary");
setTimeout(function(){
// getRandomMember();
correctElement.classList.add("btn-secondary");
correctElement.classList.remove("btn-success");
$('.btn-danger').addClass('btn-secondary');
$('.btn-danger').removeClass('btn-danger');
$('#names').addClass('hidden');
$('#constituencies').removeClass('hidden');
}, 2000);
} else {
var incorrectElement = document.getElementById("name"+answer);
incorrectElement.classList.add("btn-danger");
incorrectElement.classList.remove("btn-secondary");
}
}
function checkconst(answer) {
var correctConstituency = document.getElementById("correctConstituency").value;
if(answer == correctConstituency){
var correctElement = document.getElementById("const"+correctConstituency);
correctElement.classList.add("btn-success");
correctElement.classList.remove("btn-secondary");
setTimeout(function(){
getRandomMember();
correctElement.classList.add("btn-secondary");
correctElement.classList.remove("btn-success");
$('.btn-danger').addClass('btn-secondary');
$('.btn-danger').removeClass('btn-danger');
$('#constituencies').addClass('hidden');
document.getElementById("image").src = 'images/blankface.jpg';
}, 2000);
} else {
var incorrectElement = document.getElementById("const"+answer);
incorrectElement.classList.add("btn-danger");
incorrectElement.classList.remove("btn-secondary");
}
}