-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (84 loc) · 3.03 KB
/
app.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
'use strict';
let channels = [
"ESL_SC2",
"OgamingSC2",
"cretetion",
"freecodecamp",
"storbeck",
"habathcx",
"RobotCaleb",
"noobs2ninjas"
];
let clientId = 'c8a3wkkb56yqjhlcui7tcfyjvs65dy6';
function getChannelInfo() {
channels.forEach(function(channel) {
function makeURL(type, name) {
return 'https://api.twitch.tv/kraken/' + type + '/' + name + '?client_id=' + clientId + '?callback=?';
}
/* global fetch */
fetch(makeURL("streams", channel))
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
var game,
status;
if (data.stream === null) {
game = 'Offline';
status = 'offline';
} else if (data.stream === undefined) {
game = 'Account Closed';
status = 'offline';
} else {
game = data.stream.game;
status = 'online';
}
fetch(makeURL('channels', channel))
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
let logo = data.logo != null ? data.logo : "https://dummyimage.com/50x50/ecf0e7/5c5457.jpg&text=0x3F",
name = data.display_name != null ? data.display_name : channel,
description = status === "online" ? ': ' + data.status : "";
let html = '<div class="row ' +
status + '"><div class="col-xs-2 col-sm-1" id="icon"><img src="' +
logo + '" class="logo"></div><div class="col-xs-10 col-sm-3" id="name"><a href="' +
data.url + '" target="_blank">' +
name + '</a></div><div class="col-xs-10 col-sm-8" id="streaming">'+
game + '<span class="hidden-xs">' +
description + '</span></div></div>';
var el = document.getElementById('display');
var elChild = document.createElement('div');
elChild.innerHTML = html;
status === 'online' ? el.insertBefore(elChild, el.firstChild) : el.appendChild(elChild);
});
});
});
}
window.onload = function() {
getChannelInfo();
var selector, elems, makeActive;
selector = '.selector';
elems = document.querySelectorAll(selector);
makeActive = function () {
for (var i = 0; i < elems.length; i++) elems[i].classList.remove('active');
this.classList.add('active');
var status = this.id;
if (status === 'all') {
remove('.online, .offline');
} else if (status === 'online') {
remove('.online');
add('.offline');
} else {
remove('.offline');
add('.online');
}
};
for (var i = 0; i < elems.length; i++)
elems[i].addEventListener('mousedown', makeActive);
function add(select) {
let elements = document.querySelectorAll(select);
for (let i=0; i<elements.length; i++) elements[i].classList.add('hidden');
}
function remove(select) {
let elements = document.querySelectorAll(select);
for (let i=0; i<elements.length; i++) elements[i].classList.remove('hidden');
}
};