-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.js
178 lines (155 loc) · 5.13 KB
/
create.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
window.onload = function() {
createContent();
// Find content + links from apis then dynamically create content!
// execution starts here
// main
function createContent() {
var url, request, data;
if (dH.findById('splash'))
dH.findById('splash').addEventListener('click', splashClick);
url = "http://content.guardianapis.com/search?" +
"section=music&show-tags=keyword&api-key=test&" +
"show-fields=all&show-most-viewed=true";
request = new XMLHttpRequest();
request.open("GET", url);
request.onload = function(e) {
if (request.readyState === 4) {
if (request.status === 200) {
data = JSON.parse(dH.getInnerData(["responseText"], request));
initTagCalls(data);
}
}
};
request.send(null);
}
// ask the guardian for tag info for each article
function initTagCalls(data) {
var tagsArray;
var articleObjs = dH.getInnerData(["response", "results"], data);
articleObjs.forEach(function(elem) {
tagsArray = elem.tags.map(function(tagElem) {
return tagElem.webTitle;
});
generateSongURL(tagsArray, elem);
});
}
// use tags to find song on soundcloud, keep data about articles so we can
// push all content to page later
function generateSongURL(tags_array, articleData) {
var songURL;
// create the query
var url_api = createSoundcloudQuery(tags_array);
var request = new XMLHttpRequest();
request.open("GET", url_api);
request.onload = function(e) {
if (request.readyState === 4) {
if (request.status === 200) {
songURL = URLFromResponse(request.responseText);
if (songURL) {
createArticleDiv(songURL, articleData);
} else {
console.log("failed to load song url: " + songURL);
}
}
}
};
request.send(null);
}
// create HTML element, set attributes and insert into page
function createArticleDiv(songURL, articleData) {
var contentDiv, newDiv, contentLink, title, article_content, player;
contentDiv = dH.findByClass('content-main')[0];
newDiv = document.createElement('div');
newDiv.className = "article";
contentLink = document.createElement('a');
contentLink.href = articleData.webUrl;
contentLink.target= "_blank";
title = document.createElement('h3');
title.innerHTML = articleData.webTitle;
title.className = "titles";
article_content = initArticleContent(articleData);
player = initPlayer(songURL);
contentLink.appendChild(title);
newDiv.appendChild(contentLink);
newDiv.appendChild(article_content);
newDiv.appendChild(player);
if (contentDiv) contentDiv.appendChild(newDiv);
}
// create an iframe with songurl pointing to url given as input
function initPlayer(url) {
var player = document.createElement('iframe');
player.className = 'player';
player.setAttribute('height', '166');
player.setAttribute('scrolling', 'no');
player.setAttribute('frameborder', 'no');
player.setAttribute('src', url);
return player;
}
// Create article div with content
function initArticleContent(data) {
var article_content = document.createElement('p');
var content = data.fields.body;
if (content.length > 500) {
article_content.innerHTML = content.substring(0, 500) + "...";
} else {
article_content.innerHTML = content;
}
article_content.className = "flavour";
return article_content;
}
// change the css to make things slide on/off the screen
function splashClick() {
var splashDiv = dH.findById('splash');
var wrapper = dH.findByClass('wrapper')[0];
var header = dH.findByClass('header')[0];
var body = dH.findByTag('body')[0];
splashDiv.style.bottom = '150vh';
wrapper.style["margin-top"] = 0;
wrapper.style["display"] = "block"
header.style["top"] = 0;
body.style["overflow-y"] = "auto";
}
};
var dH = (function() {
var classes = {};
var ids = {};
var tags = {};
var findByClass = function(className, elem) {
if (elem)
return Array.prototype.slice.call(elem.getElementsByClassName(className));
if (!classes[className]) {
classes[className] =
Array.prototype.slice.call(document.getElementsByClassName(className));
}
return classes[className];
};
var findById = function(className, elem) {
if (elem)
elem.getElementById(className);
if (!ids[className]) ids[className] = document.getElementById(className);
return ids[className];
};
var findByTag = function(tagName, elem) {
if (elem)
return Array.prototype.slice.call(elem.getElementsByTagName(tagNane));
if (!tags[tagName]) {
tags[tagName] =
Array.prototype.slice.call(document.getElementsByTagName(tagName));
}
return tags[tagName];
};
var getInnerData = function(accessors, elem) {
var out = (elem) ? elem : document;
accessors.forEach(function(accessor) {
if (out[accessor]) out = out[accessor];
else return ;
});
return out;
};
return {
findById : findById,
findByTag : findByTag,
findByClass : findByClass,
getInnerData: getInnerData
};
}());