-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio5.js
235 lines (170 loc) · 9.03 KB
/
audio5.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(function($) {
//counters:
var verticalIndex = 0;
//plugin wrapper:
var songsApp = {
// 1. initialize function:
init: function(settings) {
songsApp.config = {
serachCta: $("#search-cta"), // search CTA element
searchResults: $("#search-results"), // search results section
songsTemplate: $('#songs-template').html(), // handlebar template for track display
serchString: $("#search-string"), // serach string input element
selectedTrack: $("#current-track"), // selected track box
defaultImg: 'default.jpg', // empty images defult src
tracksPlayer: $("#track-player"), // iframe player for tracks
defaultPlayerBaseUrl: 'https://w.soundcloud.com/player/?auto_play=true"&url=',
tracksListContainer: $("#ul-wrapper"), // containing div of tracks list UL
itemsPerScrenn: 6, //number of tracks to display on each screen
};
// Allow overriding the default config
$.extend(songsApp.config, settings);
//login and initiate the SoundCloud service:
SC.initialize({
client_id: 'd652006c469530a4a7d6184b18e16c81'
});
// initiate the first service that populate the data in the page
songsApp.populatePage();
//add delegation to <li> items in search results
songsApp.config.searchResults.delegate('li', 'click', function() {
var selectedTrack = $(this);
console.log('temp selectedTrack is: ' + selectedTrack.attr('id'));
//get the current track image:
var selectedTrackImage = selectedTrack.attr('data-trackImage');
console.log('temp selectedTrackImage is: ' + selectedTrackImage);
//get the current track uri so we can play it:
var selectedTrackUri = selectedTrack.attr('data-trackUri');
console.log('temp selectedTrackUri is: ' + selectedTrackUri);
//call the service that will handle displaying selected tracks
songsApp.displaySelectedTrack(selectedTrack, selectedTrackImage, selectedTrackUri);
});
},
// 2.populate all items method :
populatePage: function() {
// set a dely for when the user press a key to prevent the keyup event form triggering more then once
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
// perform track search when user enter strings to the search input
songsApp.config.serchString.on("keyup", function() {
//call the delay function with the keyup functionality inside of it
delay(function() {
// reset search results box and exit if the search box is empty:
if (songsApp.config.serchString.val() == "") {
console.log('empty string');
//clear search results if search box is empty
songsApp.config.searchResults.empty();
//clear img src if search box is empty
songsApp.config.selectedTrack.children('img').attr('src', '');
return;
} else {
songsApp.config.searchResults.empty();
};
//reset the tracks array:
//var audioArry = [];
//console.log('audioArry length: ', audioArry.length);
//save the search box text
var serchStringResult = songsApp.config.serchString.val();
console.log('serchStringResult: ', serchStringResult);
//call the SoundCloud service
SC.get('/tracks', {
q: serchStringResult,
}, function(tracks) {
console.log('number of trackes forn the colud: ', tracks.length);
//audioArry = tracks;
//console.log('audioArry now is: ' ,audioArry);
$.each(tracks, function(i, track) {
//console.log('total tracks:',songsApp.config.searchResults.children('li').length);
songsApp.config.searchResults.append(Mustache.render(songsApp.config.songsTemplate, track));
});
//calculate and set UL element height:
var setUlDesignData = songsApp.tracksNavigation();
songsApp.config.tracksListContainer.animate({
height: (setUlDesignData[3]),
}, 800, function() {
// Animation complete.
});
//songsApp.config.tracksListContainer.height(168);
console.log('Ul height: ', songsApp.config.tracksListContainer.height());
});
}, 200);
});
},
// 3.display track image and play it :
displaySelectedTrack: function(trackToDisplay, trackImg, trackUri) {
var slelctedTrackImage = songsApp.config.selectedTrack.children('img');
slelctedTrackImage.attr('src', trackImg);
if (slelctedTrackImage.attr('src') == '') {
slelctedTrackImage.attr('src', songsApp.config.defaultImg);
};
var currentPlayer = songsApp.config.tracksPlayer;
currentPlayer.attr('src', songsApp.config.defaultPlayerBaseUrl + trackUri);
//display the hidden player:
songsApp.config.tracksPlayer.show();
},
//4. handle navigation service - this service wil calulate all scrolling data for naviagtion and return an array with all nedded data for scrolling up \ down
tracksNavigation: function() {
//total number of tracks returned from the server:
var totalTracks = songsApp.config.searchResults.children('li').length;
console.log('totalTracks : ', totalTracks);
// calculate the total number of screens to scroll
var topToScroll = Math.round(totalTracks / songsApp.config.itemsPerScrenn);
console.log('topToScroll : ', topToScroll);
//calaulate the height of an LI element inside our list
var liHeight = songsApp.config.searchResults.children('li').outerHeight(true);
console.log('liHeight : ', liHeight);
//calculate the total height of all LI items
var totalLiHeight = liHeight * songsApp.config.itemsPerScrenn;
console.log('totalLiHeight : ', totalLiHeight);
//reset the current top position of the UL element
var currentTop = songsApp.config.searchResults.css('top');
//calculate the total scroll nedded to be done for the contaner div of the UL elemnt:
var totalScroll = parseInt(currentTop) + totalLiHeight; //**set the ampunt of scroll
console.log('totalScroll : ', totalScroll);
//create an array that will be used by the next and previous button in order to scroll the results up\down:
var navigationData = [verticalIndex, topToScroll, totalScroll, totalLiHeight]
return navigationData;
},
}
$(document).ready(function() {
//call the initializing service:
songsApp.init();
//reset the UL top position (list containing all tracks LI):
songsApp.config.searchResults.css({
top: 0,
});
//handle "next" click navigation:
$('#next-track').on("click", function() {
//call the service that calculate all navigation data
var topNavigationData = songsApp.tracksNavigation();
if (topNavigationData[0] < (topNavigationData[1] - 1)) {
$('#search-results').animate({
top: (topNavigationData[2] * -1),
}, 1000, function() {
// Animation complete.
});
verticalIndex++
}
});
//handle "previous" click navigation:
$('#prv-track').on("click", function() {
var bottomNavigationData = songsApp.tracksNavigation();
if (bottomNavigationData[0] !== 0) {
$('#search-results').animate({
top: (bottomNavigationData[2]),
}, 1000, function() {
// Animation complete.
});
verticalIndex--
}
});
//triger player visibility when user clicks on track image
// songsApp.config.selectedTrack.on("click", function() {
// songsApp.config.tracksPlayer.show();
// });
});
})(jQuery);