-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.js
132 lines (115 loc) · 3.71 KB
/
widget.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
var link=document.querySelector('input').value;
var response=null;
var audio=document.getElementById('audio');
var progress=document.querySelector("progress");
var dashboard=document.querySelector('.widget');
var results=document.querySelector("#results");
var loadAudio = function (link){
var xhr =new XMLHttpRequest();
xhr.open('GET', "https://api.spotify.com/v1/tracks/"+link);
xhr.setRequestHeader('Accept', 'application/json'); //why accept
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
response = JSON.parse(this.response);
audio.setAttribute('src', response.preview_url);
document.querySelector(".metadata .title").textContent=response.name;
document.querySelector(".metadata .author").textContent=response.artists[0].name;
audio.play();
document.querySelector('.btn-play').classList.add("playing");
}
}
};
xhr.send();
};
loadAudio(link);
var loadSearch=function(link){
var xhr =new XMLHttpRequest();
xhr.open('GET', "https://api.spotify.com/v1/search/?q="+link);
xhr.setRequestHeader('Accept', 'application/json');
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
response = JSON.parse(this.response);
console.log(response);
putResults(response.tracks.items);
}
}
};
xhr.send();
};
var parseSearch=function(query){
var newValue="";
for(var i=0;i<query.length;i++){
if(query[i]==" ")
newValue+="%20";
else
newValue+=query[i];
}
return newValue;
};
var setProgress=function(audio,progress){
var ratio=30/audio.duration;
var progressValue=Math.floor(ratio*audio.currentTime);
progress.value=progressValue;
}
document.addEventListener('submit', function (evt){
if(evt.target.id==='track'){
evt.preventDefault();
link= document.querySelector('input').value;
loadAudio(link);
}
else if(evt.target.id==='search'){
evt.preventDefault();
var words= evt.target.searchItem.value;
words+="&type=track";
link=parseSearch(words);
loadSearch(link);
}
});
var putResults=function(object){
var ul=document.querySelector("#results");
//First we have to remove previous searches
while(ul.hasChildNodes()){
ul.removeChild(ul.firstChild);
}
for(var key in object){
var artist=document.createElement("span");
var song=document.createElement("span");
var li=document.createElement("li");
var a=document.createElement("a");
artist.className="artist";
song.className="song";
artist.textContent=object[key].artists[0].name;
song.textContent=object[key].name;
a.id=object[key].id;
a.appendChild(artist);
a.appendChild(song);
li.appendChild(a);
ul.appendChild(li);
}
}
dashboard.addEventListener('click', function (evt){
if(evt.target.className==="btn-play disabled"){
audio.play();
evt.target.classList.add("playing");
}else if(evt.target.className==="btn-play disabled playing"){
audio.pause();
evt.target.classList.remove("playing");
}else if(evt.target.nodeName==="PROGRESS"){
audio.currentTime=evt.layerX/evt.target.clientWidth*audio.duration;
}
});
audio.addEventListener('timeupdate',function(evt){
setProgress(audio,progress);
if(audio.duration===audio.currentTime)
document.querySelector(".btn-play").classList.remove("playing")
})
results.addEventListener('click',function(evt){
evt.preventDefault();
var selected=evt.target;
//If the user click on the song name or author name we have to get the url from <a></a>
if(evt.target.nodeName!=="A")
selected=evt.target.parentNode;
loadAudio(selected.id);
});