-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
executable file
·206 lines (170 loc) · 5.84 KB
/
script.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
var links={};
var counter={};
window.addEventListener('DOMContentLoaded', init);
function init() {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", showInfo);
oReq.divId='coproduction';
oReq.open("GET", "data/coproduction.txt");
oReq.send();
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", showInfo);
oReq.divId='novel';
oReq.open("GET", "data/novel.txt");
oReq.send();
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", showInfo);
oReq.divId='tools';
oReq.open("GET", "data/tools.txt");
oReq.send();
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", showInfo);
oReq.divId='visualisations';
oReq.open("GET", "data/visualisations.txt");
oReq.send();
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", showInfo);
oReq.divId='writing';
oReq.open("GET", "data/writing.txt");
oReq.send();
}
function gettingData(i,divId) {
if(links[divId][links[divId].length-(i+1)]){
var link = links[divId][links[divId].length-(i+1)]['FULL_ONS_URL'];
makeCorsRequest(link,divId);
}else{
document.getElementById(divId+"Button").style.display = "none"
}
}
function showInfo(evt) {
var divId=evt.currentTarget.divId
links[divId] = [];
this.responseText.split('\n').forEach(function(d) {
if (d.length > 0) {
links[divId].push({FULL_ONS_URL: d});
}
});
if(links[divId].length>3){
var element = document.getElementById(divId+'-section');
var button = document.createElement('button');
button.className="button"
button.id=divId+"Button"
button.innerHTML="Show more"
button.addEventListener('click',function(){
var j=0;
while(j<3){
gettingData(counter[divId]+j+1,divId);
j++;
if(j==3){counter[divId]=counter[divId]+3};
}
})
element.appendChild(button)
}
// setTimeout(function() {
// for(i=0; i<links[divId].length; i++) {
for(i=0; i<3; i++) {
(function(i) {
setTimeout(function () {
gettingData(i,divId)
}, 400*i);
})(i)
counter[divId]=i
}
}
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Make the actual CORS request.
function makeCorsRequest(link,divId) {
// This is a sample server that supports CORS.
var url = link+'/data';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
return;
}
xhr.addEventListener('load', processResponse);
xhr.divId=divId;
// Response handlers.
xhr.onload = function() {
var text = 'success';
};
xhr.send();
}
// // Function that will process the response from the API
var processResponse = function(evt) {
var data = JSON.parse(this.response);
var divId = evt.target.divId
// place holders
var element = document.getElementById(divId);
var container = document.createElement('div');
container.className = 'container';
//image
var img = document.createElement('img');
var divImage = document.createElement("div");
var imageLink = document.createElement('a');
divImage.className = 'thumbnail';
if (data.imageUri === ''||data.imageUri === undefined) {
// } else if (data.imageUri === undefined) {
// img.src = 'generic.png';
} else {
img.src = 'https://www.ons.gov.uk/resource?uri='+ data.imageUri;
imageLink.appendChild(img);
imageLink.href = 'https://www.ons.gov.uk' + data.uri;
imageLink.target="_blank";
divImage.appendChild(imageLink);
container.appendChild(divImage);
}
element.appendChild(container);
//title + link
var divTitle = document.createElement('div');
divTitle.className = 'title';
var link = document.createElement('a');
var node = document.createTextNode(data.description.title);
var title = document.createElement('p');
title.appendChild(node);
link.appendChild(title);
link.href = 'https://www.ons.gov.uk' + data.uri;
link.target="_blank";
divTitle.appendChild(link);
container.appendChild(divTitle);
element.appendChild(container);
// pub date
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var divDate = document.createElement('div');
divDate.className = 'date';
var dateNode = document.createTextNode(new Date(data.description.releaseDate).toLocaleDateString('en-GB', options));
var date = document.createElement('p');
date.appendChild(dateNode);
divDate.appendChild(date);
container.appendChild(divDate);
element.appendChild(container);
// keywords
// var divKey = document.createElement('div');
// divKey.className = 'keyword';
// if(data.description.keywords && data.description.keywords.length > 1) {
// for(j=0; j<data.description.keywords.length; j++) {
// if(data.description.keywords[j] === "") {
// continue;
// } else {
// var keyword = data.description.keywords[j]+" | ";
// }
// var keyNode = document.createTextNode(keyword);
// divKey.appendChild(keyNode);
// }
// container.appendChild(divKey);
// element.appendChild(container);
// }
}