2012-02-22 01:31:35 +04:00
|
|
|
var initScanned = false;
|
|
|
|
|
2011-07-30 01:53:22 +04:00
|
|
|
Collection={
|
|
|
|
artists:[],
|
2011-08-21 17:28:20 +04:00
|
|
|
albums:[],
|
|
|
|
songs:[],
|
|
|
|
artistsById:{},
|
|
|
|
albumsById:{},
|
2011-07-30 01:53:22 +04:00
|
|
|
loaded:false,
|
|
|
|
loading:false,
|
|
|
|
loadedListeners:[],
|
|
|
|
load:function(ready){
|
|
|
|
if(ready){
|
|
|
|
Collection.loadedListeners.push(ready);
|
|
|
|
}
|
|
|
|
if(!Collection.loading){
|
|
|
|
Collection.loading=true;
|
2012-04-15 17:18:45 +04:00
|
|
|
Collection.artists=[];
|
|
|
|
Collection.albums=[];
|
|
|
|
Collection.songs=[];
|
|
|
|
Collection.artistsById={};
|
|
|
|
Collection.albumsById={};
|
2011-07-30 01:53:22 +04:00
|
|
|
$.ajax({
|
|
|
|
url: OC.linkTo('media','ajax/api.php')+'?action=get_collection',
|
|
|
|
dataType: 'json',
|
2011-08-21 17:28:20 +04:00
|
|
|
success: function(data){
|
|
|
|
//normalize the data
|
|
|
|
for(var i=0;i<data.artists.length;i++){
|
|
|
|
var artist=data.artists[i];
|
|
|
|
var artistData={name:artist.artist_name,songs:[],albums:[]};
|
|
|
|
Collection.artistsById[artist.artist_id]=artistData;
|
|
|
|
Collection.artists.push(artistData);
|
|
|
|
}
|
|
|
|
for(var i=0;i<data.albums.length;i++){
|
|
|
|
var album=data.albums[i];
|
2011-10-17 03:25:11 +04:00
|
|
|
if(Collection.artistsById[album.album_artist]){
|
|
|
|
var artistName=Collection.artistsById[album.album_artist].name;
|
|
|
|
}else{
|
|
|
|
var artistName='unknown';
|
|
|
|
}
|
2011-08-21 17:28:20 +04:00
|
|
|
var albumData={name:album.album_name,artist:artistName,songs:[]};
|
|
|
|
Collection.albumsById[album.album_id]=albumData;
|
|
|
|
Collection.albums.push(albumData);
|
2011-10-17 03:25:11 +04:00
|
|
|
if(Collection.artistsById[album.album_artist]){
|
|
|
|
Collection.artistsById[album.album_artist].albums.push(albumData);
|
|
|
|
}
|
2011-08-21 17:28:20 +04:00
|
|
|
}
|
|
|
|
for(var i=0;i<data.songs.length;i++){
|
|
|
|
var song=data.songs[i];
|
|
|
|
if(Collection.artistsById[song.song_artist] && Collection.albumsById[song.song_album]){
|
|
|
|
var songData={
|
|
|
|
name:song.song_name,
|
|
|
|
artist:Collection.artistsById[song.song_artist].name,
|
|
|
|
album:Collection.albumsById[song.song_album].name,
|
|
|
|
lastPlayed:song.song_lastplayed,
|
|
|
|
length:song.song_length,
|
|
|
|
path:song.song_path,
|
|
|
|
playCount:song.song_playcount,
|
|
|
|
};
|
|
|
|
Collection.songs.push(songData);
|
|
|
|
Collection.artistsById[song.song_artist].songs.push(songData);
|
|
|
|
Collection.albumsById[song.song_album].songs.push(songData);
|
2011-07-30 01:53:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 01:49:55 +04:00
|
|
|
Collection.artists.sort(function(a,b){
|
2011-10-17 03:25:11 +04:00
|
|
|
if(!a.name){
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-29 01:49:55 +04:00
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
|
|
|
|
2011-07-30 01:53:22 +04:00
|
|
|
Collection.loaded=true;
|
|
|
|
Collection.loading=false;
|
|
|
|
for(var i=0;i<Collection.loadedListeners.length;i++){
|
|
|
|
Collection.loadedListeners[i]();
|
|
|
|
}
|
2012-02-22 01:31:35 +04:00
|
|
|
if(data.songs.length==0 && initScanned == false){
|
2011-08-13 19:41:34 +04:00
|
|
|
$('#scan input.start').click();
|
2012-02-22 01:31:35 +04:00
|
|
|
initScanned = true;
|
2011-08-02 01:53:01 +04:00
|
|
|
}
|
2011-07-30 01:53:22 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
display:function(){
|
|
|
|
if(Collection.parent){
|
|
|
|
Collection.parent.show();
|
|
|
|
}
|
|
|
|
if(!Collection.loaded){
|
2012-02-22 01:31:35 +04:00
|
|
|
Collection.load(Collection.display);
|
2011-07-30 01:53:22 +04:00
|
|
|
}else{
|
|
|
|
if(Collection.parent){
|
2011-08-08 19:53:03 +04:00
|
|
|
Collection.parent.find('tr:not(.template)').remove();
|
|
|
|
var template=Collection.parent.find('tr.template');
|
2011-08-21 17:28:20 +04:00
|
|
|
$.each(Collection.artists,function(i,artist){
|
|
|
|
if(artist.name && artist.songs.length>0){
|
|
|
|
var tr=template.clone().removeClass('template');
|
2011-09-29 02:42:30 +04:00
|
|
|
if(artist.songs.length>1){
|
|
|
|
tr.find('td.title a').text(artist.songs.length+' '+t('media','songs'));
|
|
|
|
tr.find('td.album a').text(artist.albums.length+' '+t('media','albums'));
|
|
|
|
}else{
|
|
|
|
tr.find('td.title a').text(artist.songs[0].name);
|
|
|
|
tr.find('td.album a').text(artist.albums[0].name);
|
|
|
|
}
|
2011-08-21 17:28:20 +04:00
|
|
|
tr.find('td.artist a').text(artist.name);
|
|
|
|
tr.data('artistData',artist);
|
|
|
|
tr.find('td.artist a').click(function(event){
|
|
|
|
event.preventDefault();
|
2011-08-25 22:57:17 +04:00
|
|
|
PlayList.add(artist);
|
2011-08-21 17:28:20 +04:00
|
|
|
PlayList.play(0);
|
|
|
|
Collection.parent.find('tr').removeClass('active');
|
|
|
|
$('tr[data-artist="'+artist.name+'"]').addClass('active');
|
|
|
|
});
|
2012-05-14 00:44:50 +04:00
|
|
|
var expander='';
|
2011-09-29 02:42:30 +04:00
|
|
|
if(artist.songs.length>1){
|
2012-02-22 01:31:35 +04:00
|
|
|
expander=$('<a class="expander">></a>');
|
2011-09-29 02:42:30 +04:00
|
|
|
expander.data('expanded',false);
|
|
|
|
expander.click(function(event){
|
|
|
|
var tr=$(this).parent().parent();
|
|
|
|
if(expander.data('expanded')){
|
|
|
|
Collection.hideArtist(tr.data('artist'));
|
|
|
|
}else{
|
|
|
|
Collection.showArtist(tr.data('artist'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tr.find('td.artist').addClass('buttons');
|
|
|
|
Collection.addButtons(tr,artist);
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.children('td.artist-expander').append(expander);
|
2011-08-21 17:28:20 +04:00
|
|
|
tr.attr('data-artist',artist.name);
|
|
|
|
Collection.parent.find('tbody').append(tr);
|
|
|
|
}
|
2011-08-08 19:53:03 +04:00
|
|
|
});
|
2011-07-30 01:53:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-08-08 19:53:03 +04:00
|
|
|
showArtist:function(artist){
|
2011-08-21 17:28:20 +04:00
|
|
|
var tr=Collection.parent.find('tr[data-artist="'+artist+'"]');
|
2012-01-12 04:14:16 +04:00
|
|
|
var lastRow=tr;
|
2011-08-21 17:28:20 +04:00
|
|
|
var artist=tr.data('artistData');
|
2011-08-25 22:57:17 +04:00
|
|
|
var first=true;
|
2011-09-29 02:42:30 +04:00
|
|
|
$.each(artist.albums,function(j,album){
|
2011-08-21 17:28:20 +04:00
|
|
|
$.each(album.songs,function(i,song){
|
2012-02-22 01:31:35 +04:00
|
|
|
var newRow;
|
2011-08-25 22:57:17 +04:00
|
|
|
if(first){
|
|
|
|
newRow=tr;
|
|
|
|
}else{
|
2012-02-22 01:31:35 +04:00
|
|
|
newRow=tr.clone();
|
2011-09-29 02:42:30 +04:00
|
|
|
newRow.find('td.artist').text('');
|
|
|
|
newRow.find('.expander').remove();
|
2011-08-25 22:57:17 +04:00
|
|
|
}
|
2012-01-19 18:14:24 +04:00
|
|
|
newRow.find('td.album-expander .expander').remove();
|
2011-08-25 22:57:17 +04:00
|
|
|
if(i==0){
|
|
|
|
newRow.find('td.album a').text(album.name);
|
|
|
|
newRow.find('td.album a').click(function(event){
|
|
|
|
event.preventDefault();
|
|
|
|
PlayList.add(album);
|
|
|
|
PlayList.play(0);
|
|
|
|
Collection.parent.find('tr').removeClass('active');
|
|
|
|
$('tr[data-album="'+album.name+'"]').addClass('active');
|
|
|
|
});
|
2011-09-29 02:42:30 +04:00
|
|
|
if(album.songs.length>1){
|
|
|
|
var expander=$('<a class="expander">v </a>');
|
|
|
|
expander.data('expanded',true);
|
|
|
|
expander.click(function(event){
|
|
|
|
var tr=$(this).parent().parent();
|
|
|
|
if(expander.data('expanded')) {
|
|
|
|
Collection.hideAlbum(tr.data('artist'),tr.data('album'));
|
|
|
|
} else {
|
|
|
|
Collection.showAlbum(tr.data('artist'),tr.data('album'));
|
|
|
|
}
|
|
|
|
});
|
2012-01-19 18:14:24 +04:00
|
|
|
newRow.children('td.album-expander').append(expander);
|
2011-09-29 02:42:30 +04:00
|
|
|
}
|
|
|
|
Collection.addButtons(newRow,album);
|
2011-09-01 20:31:09 +04:00
|
|
|
} else {
|
2011-08-25 22:57:17 +04:00
|
|
|
newRow.find('td.album a').text('');
|
2011-09-29 02:42:30 +04:00
|
|
|
Collection.addButtons(newRow,song);
|
2011-08-25 22:57:17 +04:00
|
|
|
}
|
|
|
|
newRow.find('td.title a').text(song.name);
|
|
|
|
newRow.find('td.title a').click(function(event){
|
2011-08-21 17:28:20 +04:00
|
|
|
event.preventDefault();
|
2011-08-25 22:57:17 +04:00
|
|
|
PlayList.add(song);
|
2011-08-21 17:28:20 +04:00
|
|
|
PlayList.play(0);
|
|
|
|
Collection.parent.find('tr').removeClass('active');
|
|
|
|
$('tr[data-title="'+song.name+'"]').addClass('active');
|
|
|
|
});
|
|
|
|
newRow.attr('data-album',album.name);
|
|
|
|
newRow.attr('data-title',song.name);
|
2011-08-25 22:57:17 +04:00
|
|
|
newRow.attr('data-artist',artist.name);
|
2011-09-29 02:42:30 +04:00
|
|
|
newRow.data('albumData',album);
|
2011-08-25 22:57:17 +04:00
|
|
|
if(!first){
|
2012-01-12 04:14:16 +04:00
|
|
|
lastRow.after(newRow);
|
2011-08-25 22:57:17 +04:00
|
|
|
}
|
|
|
|
first=false;
|
2012-01-12 04:14:16 +04:00
|
|
|
lastRow=newRow;
|
2011-08-21 17:28:20 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
tr.removeClass('collapsed');
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.find('td.artist-expander a.expander').data('expanded',true);
|
|
|
|
tr.find('td.artist-expander a.expander').addClass('expanded');
|
|
|
|
tr.find('td.artist-expander a.expander').text('v');
|
2011-08-08 19:53:03 +04:00
|
|
|
},
|
|
|
|
hideArtist:function(artist){
|
2011-08-21 17:28:20 +04:00
|
|
|
var tr=Collection.parent.find('tr[data-artist="'+artist+'"]');
|
2011-09-29 02:42:30 +04:00
|
|
|
var artist=tr.first().data('artistData');
|
|
|
|
tr.first().find('td.album a').first().text(artist.albums.length+' '+t('media','albums'));
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.first().find('td.album-expander a.expander').remove();
|
2011-09-29 02:42:30 +04:00
|
|
|
tr.first().find('td.title a').text(artist.songs.length+' '+t('media','songs'));
|
|
|
|
tr.first().find('td.album a').unbind('click');
|
|
|
|
tr.first().find('td.title a').unbind('click');
|
|
|
|
tr.each(function(i,row){
|
|
|
|
if(i>0){
|
|
|
|
$(row).remove();
|
|
|
|
}
|
|
|
|
});
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.find('td.artist-expander a.expander').data('expanded',false);
|
|
|
|
tr.find('td.artist-expander a.expander').removeClass('expanded');
|
|
|
|
tr.find('td.artist-expander a.expander').text('>');
|
2011-09-29 02:42:30 +04:00
|
|
|
Collection.addButtons(tr,artist);
|
2011-08-08 19:53:03 +04:00
|
|
|
},
|
|
|
|
showAlbum:function(artist,album){
|
2011-09-29 02:42:30 +04:00
|
|
|
var tr = Collection.parent.find('tr[data-artist="'+artist+'"][data-album="'+album+'"]');
|
2012-01-12 04:14:16 +04:00
|
|
|
var lastRow=tr;
|
2011-09-29 02:42:30 +04:00
|
|
|
var albumData=tr.data('albumData');
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.find('td.album-expander a.expander').data('expanded',true);
|
|
|
|
tr.find('td.album-expander a.expander').addClass('expanded');
|
|
|
|
tr.find('td.album-expander a.expander').text('v');
|
2011-09-29 02:42:30 +04:00
|
|
|
$.each(albumData.songs,function(i,song){
|
2012-02-22 01:31:35 +04:00
|
|
|
var newRow;
|
2011-09-29 02:42:30 +04:00
|
|
|
if(i>0){
|
2012-02-22 01:31:35 +04:00
|
|
|
newRow=tr.clone();
|
2011-09-29 02:42:30 +04:00
|
|
|
newRow.find('a.expander').remove();
|
|
|
|
newRow.find('td.album a').text('');
|
|
|
|
newRow.find('td.artist a').text('');
|
|
|
|
}else{
|
2012-02-22 01:31:35 +04:00
|
|
|
newRow=tr;
|
2011-09-29 02:42:30 +04:00
|
|
|
}
|
|
|
|
newRow.find('td.title a').text(song.name);
|
2012-01-12 04:18:27 +04:00
|
|
|
newRow.find('td.title a').click(function(event){
|
|
|
|
event.preventDefault();
|
|
|
|
PlayList.add(song);
|
|
|
|
PlayList.play(0);
|
|
|
|
Collection.parent.find('tr').removeClass('active');
|
|
|
|
$('tr[data-title="'+song.name+'"]').addClass('active');
|
|
|
|
});
|
2011-09-29 02:42:30 +04:00
|
|
|
if(i>0){
|
2012-01-12 04:14:16 +04:00
|
|
|
lastRow.after(newRow);
|
2011-09-29 02:42:30 +04:00
|
|
|
}
|
2012-01-12 04:14:16 +04:00
|
|
|
lastRow=newRow;
|
2011-09-29 02:42:30 +04:00
|
|
|
});
|
2011-08-08 19:53:03 +04:00
|
|
|
},
|
|
|
|
hideAlbum:function(artist,album){
|
2011-09-01 20:31:09 +04:00
|
|
|
var tr = Collection.parent.find('tr[data-artist="'+artist+'"][data-album="'+album+'"]');
|
2011-09-29 02:42:30 +04:00
|
|
|
var albumData=tr.data('albumData');
|
|
|
|
tr.first().find('td.title a').text(albumData.songs.length+' '+t('media','songs'));
|
2012-01-19 18:14:24 +04:00
|
|
|
tr.find('td.album-expander a.expander').data('expanded',false);
|
|
|
|
tr.find('td.album-expander a.expander').removeClass('expanded');
|
|
|
|
tr.find('td.album-expander a.expander').text('> ');
|
2011-09-29 02:42:30 +04:00
|
|
|
tr.each(function(i,row){
|
|
|
|
if(i>0){
|
|
|
|
$(row).remove();
|
|
|
|
}
|
|
|
|
});
|
2011-08-08 19:53:03 +04:00
|
|
|
},
|
2011-07-30 01:53:22 +04:00
|
|
|
parent:null,
|
|
|
|
hide:function(){
|
|
|
|
if(Collection.parent){
|
|
|
|
Collection.parent.hide();
|
|
|
|
}
|
|
|
|
},
|
2011-08-13 00:28:35 +04:00
|
|
|
registerPlay:function(item){
|
|
|
|
if(item){
|
|
|
|
var song=Collection.findSong(item.artist,item.album,item.name);
|
|
|
|
song.song_playcount++;
|
|
|
|
}
|
2011-07-30 06:15:09 +04:00
|
|
|
},
|
2011-08-25 22:57:17 +04:00
|
|
|
addButtons:function(parent,data){
|
2011-09-25 02:02:50 +04:00
|
|
|
buttons = parent.find('.buttons');
|
|
|
|
if(buttons.find('.add').length<=0) {
|
|
|
|
buttons.prepend('<img class="add action" src="'+OC.imagePath('core','actions/play-add')+'" title="Add to playlist" />');
|
|
|
|
}
|
2011-08-25 22:57:17 +04:00
|
|
|
buttons.find('.add').unbind('click');
|
|
|
|
buttons.find('.add').click(function(event){
|
2011-09-25 02:02:50 +04:00
|
|
|
event.preventDefault();
|
2011-08-25 22:57:17 +04:00
|
|
|
PlayList.add(data,true);
|
2011-09-25 02:02:50 +04:00
|
|
|
PlayList.render();
|
2011-07-30 06:15:09 +04:00
|
|
|
});
|
2011-07-31 02:50:04 +04:00
|
|
|
},
|
|
|
|
find:function(artistName,albumName,songName){
|
|
|
|
if(songName){
|
|
|
|
return Collection.findSong(artistName,albumName,songName);
|
|
|
|
}else if(albumName){
|
|
|
|
return Collection.findAlbum(artistName,albumName);
|
|
|
|
}else{
|
|
|
|
return Collection.findArtist(artistName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
findArtist:function(name){
|
|
|
|
for(var i=0;i<Collection.artists.length;i++){
|
2011-09-30 01:54:33 +04:00
|
|
|
if(Collection.artists[i].name==name){
|
2011-07-31 02:50:04 +04:00
|
|
|
return Collection.artists[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
findAlbum:function(artistName,albumName){
|
|
|
|
var artist=Collection.findArtist(artistName);
|
|
|
|
if(artist){
|
|
|
|
for(var i=0;i<artist.albums.length;i++){
|
2011-09-30 01:54:33 +04:00
|
|
|
if(artist.albums[i].name==albumName){
|
2011-07-31 02:50:04 +04:00
|
|
|
return artist.albums[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
findSong:function(artistName,albumName,songName){
|
|
|
|
var album=Collection.findAlbum(artistName,albumName);
|
|
|
|
if(album){
|
|
|
|
for(var i=0;i<album.songs.length;i++){
|
2011-09-30 01:54:33 +04:00
|
|
|
if(album.songs[i].name==songName){
|
2011-07-31 02:50:04 +04:00
|
|
|
return album.songs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-02 01:53:01 +04:00
|
|
|
},
|
|
|
|
addSong:function(song){
|
2012-01-20 04:41:37 +04:00
|
|
|
var artist=Collection.findArtist(song.artist);
|
2011-08-02 01:53:01 +04:00
|
|
|
if(!artist){
|
2012-01-20 04:41:37 +04:00
|
|
|
artist={name:song.artist,albums:[],songs:[]};
|
2011-08-02 01:53:01 +04:00
|
|
|
Collection.artists.push(artist);
|
2012-01-20 04:41:37 +04:00
|
|
|
Collection.artistsById[song.song_artist]=artist;
|
2011-08-02 01:53:01 +04:00
|
|
|
}
|
2012-01-20 04:41:37 +04:00
|
|
|
var album=Collection.findAlbum(song.artist,song.album);
|
2011-08-02 01:53:01 +04:00
|
|
|
if(!album){
|
2012-01-20 04:41:37 +04:00
|
|
|
album={name:song.album,artist:song.song_artist,songs:[]};
|
|
|
|
artist.albums.push(album);
|
|
|
|
Collection.albums.push(album);
|
|
|
|
Collection.albumsById[song.song_album]=album;
|
2011-08-02 01:53:01 +04:00
|
|
|
}
|
2012-01-20 04:41:37 +04:00
|
|
|
var songData={
|
|
|
|
name:song.song_name,
|
|
|
|
artist:Collection.artistsById[song.song_artist].name,
|
|
|
|
album:Collection.albumsById[song.song_album].name,
|
|
|
|
lastPlayed:song.song_lastplayed,
|
|
|
|
length:song.song_length,
|
|
|
|
path:song.song_path,
|
|
|
|
playCount:song.song_playcount,
|
|
|
|
};
|
2012-02-22 01:31:35 +04:00
|
|
|
album.songs.push(songData);
|
2012-01-20 04:41:37 +04:00
|
|
|
artist.songs.push(songData);
|
|
|
|
Collection.songs.push(songData);
|
2011-07-30 01:53:22 +04:00
|
|
|
}
|
2012-02-22 01:31:35 +04:00
|
|
|
};
|
2011-07-30 01:53:22 +04:00
|
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
Collection.parent=$('#collection');
|
|
|
|
Collection.load();
|
2011-08-02 01:53:01 +04:00
|
|
|
Collection.parent.hide();
|
2011-08-13 19:41:34 +04:00
|
|
|
$('#scan input.start').click(function(){
|
2011-08-02 01:53:01 +04:00
|
|
|
$('#scan input.start').hide();
|
|
|
|
$('#scan input.stop').show();
|
|
|
|
$('#scan input.stop').click(function(){
|
|
|
|
Scanner.toggle();
|
|
|
|
});
|
|
|
|
Scanner.scanCollection();
|
2011-08-13 19:41:34 +04:00
|
|
|
});
|
2011-07-30 01:53:22 +04:00
|
|
|
});
|