-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
204 lines (127 loc) · 4.81 KB
/
main.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
// 1. GRAB THE SEARCH DATA (we did this already)
var UI = {};
UI.handleEnterPress = function() {
document.querySelector(".js-search").addEventListener('keypress', function( e ) {
if ( e.which === 13 ) {
var inputValue = e.target.value;
// onValueRead( inputValue );
//console.log(inputValue);
SoundCloudAPI.getTrack(inputValue);
}
});
}
UI.handleSubmitClick = function() {
document.querySelector(".js-submit").addEventListener('click', function( e ) {
var inputValue = document.querySelector(".js-search").value;
//onValueRead( inputValue );
//console.log(inputValue);
SoundCloudAPI.getTrack(inputValue);
});
}
// // set up the search
UI.handleEnterPress();
UI.handleSubmitClick();
// 2 SEARCH THE SOUNDCLOUD API WITH HARDCODED SEARCH
var SoundCloudAPI = {};
SoundCloudAPI.init = function() {
SC.initialize({
client_id: 'cd9be64eeb32d1741c17cb39e41d254d'
});
}
// // set up soundcloud API
SoundCloudAPI.init();
//Search - https://developers.soundcloud.com/docs/api/guide#search
SoundCloudAPI.getTrack = function(inputValue) {
return SC.get('/tracks/', {
// q: "sun kill moon"
q: inputValue
}).then(function(tracks) {
console.log(tracks);
//MOVING this here (away from renderTrack) only makes sense once you put it there to build
//and then find that the innerHTML text only should get removed once
var searchResult = document.querySelector('.js-search-results');
searchResult.innerHTML = "";
SoundCloudAPI.renderTrack(tracks, searchResult);
// UI.renderTrack(tracks);
});
}
SoundCloudAPI.renderTrack = function( tracks, searchResult ) {
tracks.forEach(function(track) {
// console.log(track.title);
var card = document.createElement('div');
card.classList.add('card');
// image
var imageDiv = document.createElement('div');
imageDiv.classList.add('image');
var image_img = document.createElement('img');
image_img.classList.add('image_img');
image_img.src = track.artwork_url || 'http://lorempixel.com/100/100/abstract/';
imageDiv.appendChild( image_img );
// content
var content = document.createElement('div');
content.classList.add('content');
var header = document.createElement('div');
header.classList.add('header');
header.innerHTML = '<a href="' + track.permalink_url + '" target="_blank">' + track.title + '</a>';
content.appendChild( header );
searchResult.appendChild(content);
// button
var button = document.createElement('div');
button.setAttribute('data-id', track.id) /////THIS COMES DURING PLAY() !!
button.classList.add('ui', 'bottom', 'attached', 'button', 'js-button');
var icon = document.createElement('i');
icon.classList.add('add', 'icon');
var buttonText = document.createElement('span');
buttonText.innerHTML = 'Add to playlist';
button.appendChild( icon );
button.appendChild( buttonText );
button.addEventListener('click', function() {
SoundCloudAPI.getEmbed(track.uri);
//SoundCloudAPI.getWidget(track.uri);
});
// card
card.appendChild( imageDiv );
card.appendChild( content );
card.appendChild( button );
searchResult.appendChild( card );
});
}
//embedding
//https://developers.soundcloud.com/docs/api/sdks#embedding
SoundCloudAPI.getEmbed = function( trackPermalink ) {
// return SC.oEmbed( trackPermalink, {
// maxheight: 200,
// show_comments: false
// }).then(function(oEmbed){
// console.log(oEmbed)
// });
SC.oEmbed(trackPermalink, { auto_play: true }).then(function(oEmbed) {
console.log('oEmbed response: ', oEmbed);
var sideBar = document.querySelector(".col-left");
// var playListArray = [];
// playListArray.push(oEmbed.html)
var box = document.createElement("div");
box.innerHTML = oEmbed.html;
sideBar.insertBefore(box, sideBar.firstChild);
//set local storage so we can call it after refresh
// grab the widget object
var SCWdiget = SoundCloudAPI.getWidget( embed.childNodes[ 0 ] );
// bind the finish event to init
SCWdiget.bind('finish', function() {
alert("FINISHED");
// Playlist.next();
// var nextEmbed = sidebar.childNodes[ Playlist.currentTrack ];
// var nextWidget = SoundCloudAPI.getWidget( nextEmbed.childNodes[ 0 ] );
// nextWidget.play();
});
SCWdiget.bind('play', function() {
var widgetIndex = Array.from( sidebar.childNodes ).indexOf( embed );
// OLDer JAVASCRIPT: [].slice.call( sidebar.childNodes ).indexOf( embed ).
Playlist.currentTrack = widgetIndex;
});
});
}
SoundCloudAPI.getWidget = function( embedElement ) {
return SC.Widget( embedElement );
}
//POPULATE THE TRACK LIST FROM LOCAL STORAGE