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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
var audioPlaylist;
var URLBASE='ajax/api.php?action=play&path=';
$(document).ready(function() {
if(typeof FileActions!=='undefined'){
URLBASE='../apps/media/ajax/api.php?action=play&path=';
var playerLoaded=false;
function playFile(filename){
audioPlaylist.playlist=[];
audioPlaylist.addToPlaylist({
song_name:filename,
song_path:$('#dir').val()+'/'+filename
},true);
audioPlaylist.playlistChange(audioPlaylist.playlist.length-1);
}
function playAudio(filename){
if(!playerLoaded){
var parent=$('body').append('<div id="media_container"/>');
$('#media_container').load('../apps/media/templates/music.php',function(){
playerLoaded=true;
//remove playlist and collection view
$('#jp_playlist_1').remove();
$('collection').remove();
//init the audio player
audioPlaylist =initPlayList(false,false,function(){
//play the file
playFile(filename);
});
});
}else{
playFile(filename);
}
}
FileActions.register('audio','Play',playAudio);
FileActions.register('application/ogg','Play',playAudio);
FileActions.setDefault('audio','Play');
FileActions.setDefault('application/ogg','Play');
}
Playlist = function(instance, playlist, options) {
var self = this;
this.instance = instance; // String: To associate specific HTML with this playlist
this.playlist = playlist; // Array of Objects: The playlist
this.options = options; // Object: The jPlayer constructor options for this playlist
this.current = -1;
this.cssId = {
jPlayer: "jplayer_",
interface: "jp_interface_",
playlist: "jp_playlist_"
};
this.cssSelector = {};
$.each(this.cssId, function(entity, id) {
self.cssSelector[entity] = "#" + id + self.instance;
});
if(!this.options.cssSelectorAncestor) {
this.options.cssSelectorAncestor = this.cssSelector.interface;
}
$(this.cssSelector.jPlayer).jPlayer(this.options);
$(this.cssSelector.interface + " .jp-previous").click(function() {
self.playlistPrev();
$(this).blur();
return false;
});
$(this.cssSelector.interface + " .jp-next").click(function() {
self.playlistNext();
$(this).blur();
return false;
});
};
Playlist.prototype = {
displayPlaylist: function() {
var self = this;
$(this.cssSelector.playlist + " ul").empty();
for (i=0; i < this.playlist.length; i++) {
var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";
// Create links to free media
if(this.playlist[i].free) {
var first = true;
listItem += "<div class='jp-free-media'>(";
$.each(this.playlist[i], function(property,value) {
if($.jPlayer.prototype.format[property]) { // Check property is a media format.
if(first) {
first = false;
} else {
listItem += " | ";
}
listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
}
});
listItem += ")</span>";
}
listItem += "<button class='right prettybutton remove'>Remove</button>";
listItem += "</li>";
// Associate playlist items with their media
$(this.cssSelector.playlist + " ul").append(listItem);
$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
var index = $(this).data("index");
if(self.current !== index) {
self.playlistChange(index);
} else {
$(self.cssSelector.jPlayer).jPlayer("play");
}
$(this).blur();
return false;
});
$(this.cssSelector.playlist + "_item_" + i).parent().children('button').data("index", i).click(function() {
var index = $(this).data("index");
self.removeFromPlaylist(index);
});
// Disable free media links to force access via right click
if(this.playlist[i].free) {
$.each(this.playlist[i], function(property,value) {
if($.jPlayer.prototype.format[property]) { // Check property is a media format.
$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
var index = $(this).data("index");
$(self.cssSelector.playlist + "_item_" + index).click();
$(this).blur();
return false;
});
}
});
}
}
},
playlistInit: function(autoplay) {
if(autoplay) {
this.playlistChange(this.current);
} else {
this.playlistConfig(this.current);
}
},
playlistConfig: function(index,play) {
$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
this.current = index;
var that=this;
if(this.playlist[this.current]){
if($(this.cssSelector.jPlayer).data('jPlayer').options.supplied!=this.playlist[this.current].type){//the the audio type changes we need to reinitialize jplayer
$(this.cssSelector.jPlayer).jPlayer("destroy");
$(this.cssSelector.jPlayer).jPlayer({
ended:this.options.ended,
play:this.options.play,
supplied:this.playlist[this.current].type,
ready:function(){
that.playlistConfig(index);
if(play){
$(that.cssSelector.jPlayer).jPlayer("play");
}
}
});
}else{
$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
}
}
},
playlistChange: function(index) {
this.playlistConfig(index,true);
$(this.cssSelector.jPlayer).jPlayer("play");
},
playlistNext: function() {
var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
this.playlistChange(index);
},
playlistPrev: function() {
var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
this.playlistChange(index);
},
removeFromPlaylist: function(index){
this.playlist.splice(index,1);
this.displayPlaylist();
if(index==this.current){
this.playlistConfig((index<this.playlist.length)?index:0);
}else{
$(this.cssSelector.playlist + "_item_" + this.current).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
}
},
addToPlaylist : function(stuff,dontRedraw){
var self=this;
if(!stuff){
return;
}
if(stuff.artist_name){
$.each(stuff.albums,function(index,album){
self.addToPlaylist(album,true);
});
}
if(stuff.album_name){
$.each(stuff.songs,function(index,song){
self.addToPlaylist(song,true);
});
}
if(stuff.song_name){
var extention=stuff.song_path.split('.').pop();
var type=musicTypeFromExtention(extention);
var item={name:stuff.song_name,type:type};
item[type]=URLBASE+stuff.song_path;
this.playlist.push(item);
}
if(!dontRedraw){
this.displayPlaylist();
}
}
};
if($('#jp-audio')){//only do this when we're actually in the media player
//load the collection
$.ajax({
url: 'ajax/api.php?action=get_collection',
dataType: 'json',
success: function(collection){
var playlist=[];
var types=[];
for(var i=0;i<collection.length;i++){
var artist=collection[i];
for(var j=0;j<artist.albums.length;j++){
var album=artist.albums[j];
for(var n=0;n<album.songs.length;n++){
var song=album.songs[n];
var extention=song.song_path.split('.').pop();
var type=musicTypeFromExtention(extention);
if(types.indexOf(type)==-1){
types.push(type);
}
}
}
}
displayCollection(collection);
audioPlaylist =initPlayList(true,true);
}
});
}
});
function initPlayList(display,enableAutoPlay,ready){
return new Playlist('1', [],{
ready: function() {
if(display){
audioPlaylist.displayPlaylist();
}
if(enableAutoPlay){
if(window.location.href.indexOf('#')>-1){//autoplay passed arist/album/song
var vars=getUrlVars();
var play;
if(vars['artist']){
$.each(collection,function(index,artist){
if(artist.artist_name==vars['artist']){
play=artist;
if(vars['album']){
$.each(artist.albums,function(index,album){
if(album.album_name==vars['album']){
play=album;
if(vars['song']){
$.each(album.songs,function(index,song){
if(song.song_name==vars['song']){
play=song;
}
});
}
}
});
}
}
});
}
audioPlaylist.addToPlaylist(play);
audioPlaylist.playlistInit(true);
}else{
audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
}
}else{
audioPlaylist.playlistInit(false);
}
if(ready){
ready();
}
},
ended: function() {
audioPlaylist.playlistNext();
},
play: function() {
$(this).jPlayer("pauseOthers");
},
});
}
function musicTypeFromExtention(extention){
if(extention=='ogg'){
return 'oga'
}
//TODO check for more specific cases
return extention;
}
// indexOf implemententation for browsers that don't support it
if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(elt /*, from*/) {
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
function displayCollection(collection){
$('#collection').data('collection',collection);
$.each(collection,function(index,artist){
var artistNode=$('<li class="artist">'+artist.artist_name+'<button class="add">Add</button><ul/></li>');
artistNode.data('name',artist.artist_name);
artistNode.data('stuff',artist);
$('#collection>ul').append(artistNode);
$.each(artist.albums,function(index,album){
var albumNode=$('<li class="album">'+album.album_name+'<button class="add">Add</button><ul/></li>');
albumNode.data('name',album.album_name);
albumNode.data('stuff',album);
artistNode.children('ul').append(albumNode);
$.each(album.songs,function(index,song){
var songNode=$('<li class="song">'+song.song_name+'<button class="add">Add</button></li>');
songNode.data('name',song.song_name);
songNode.data('stuff',song);
albumNode.children('ul').append(songNode);
});
});
});
$('li.album').hide();
$('li.song').hide();
$('li.artist').click(function(){
$(this).children().children().slideToggle();
return false;
});
$('li.album').click(function(){
$(this).children().children().slideToggle();
return false;
});
$('li.song').click(function(){
return false;
});
$('li>button.add').click(function(){
audioPlaylist.addToPlaylist($(this).parent().data('stuff'));
return false;
})
}
function getUrlVars(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = decodeURIComponent(hash[1]).replace(/\+/g,' ');
}
return vars;
}
|