Collection={ artists:[], albums:[], songs:[], artistsById:{}, albumsById:{}, loaded:false, loading:false, loadedListeners:[], load:function(ready){ if(ready){ Collection.loadedListeners.push(ready); } if(!Collection.loading){ Collection.loading=true; $.ajax({ url: OC.linkTo('media','ajax/api.php')+'?action=get_collection', dataType: 'json', success: function(data){ //normalize the data for(var i=0;i0){ var tr=template.clone().removeClass('template'); 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); } tr.find('td.artist a').text(artist.name); tr.data('artistData',artist); tr.find('td.artist a').click(function(event){ event.preventDefault(); PlayList.add(artist); PlayList.play(0); Collection.parent.find('tr').removeClass('active'); $('tr[data-artist="'+artist.name+'"]').addClass('active'); }); if(artist.songs.length>1){ var expander=$('>'); 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); tr.children('td.artist').append(expander); tr.attr('data-artist',artist.name); Collection.parent.find('tbody').append(tr); } }); } } }, showArtist:function(artist){ var tr=Collection.parent.find('tr[data-artist="'+artist+'"]'); var lastRow=tr; var artist=tr.data('artistData'); var first=true; $.each(artist.albums,function(j,album){ $.each(album.songs,function(i,song){ if(first){ newRow=tr; }else{ var newRow=tr.clone(); newRow.find('td.artist').text(''); newRow.find('.expander').remove(); } newRow.find('td.album .expander').remove(); 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'); }); if(album.songs.length>1){ var expander=$('v '); 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')); } }); newRow.children('td.album').append(expander); } Collection.addButtons(newRow,album); } else { newRow.find('td.album a').text(''); Collection.addButtons(newRow,song); } newRow.find('td.title a').text(song.name); 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'); }); newRow.attr('data-album',album.name); newRow.attr('data-title',song.name); newRow.attr('data-artist',artist.name); newRow.data('albumData',album); if(!first){ lastRow.after(newRow); } first=false; lastRow=newRow; }); }); tr.removeClass('collapsed'); tr.find('td.artist a.expander').data('expanded',true); tr.find('td.artist a.expander').addClass('expanded'); tr.find('td.artist a.expander').text('v'); }, hideArtist:function(artist){ var tr=Collection.parent.find('tr[data-artist="'+artist+'"]'); var artist=tr.first().data('artistData'); tr.first().find('td.album a').first().text(artist.albums.length+' '+t('media','albums')); tr.first().find('td.album a.expander').remove(); 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(); } }); tr.find('td.artist a.expander').data('expanded',false); tr.find('td.artist a.expander').removeClass('expanded'); tr.find('td.artist a.expander').text('>'); Collection.addButtons(tr,artist); }, showAlbum:function(artist,album){ var tr = Collection.parent.find('tr[data-artist="'+artist+'"][data-album="'+album+'"]'); var lastRow=tr; var albumData=tr.data('albumData'); tr.find('td.album a.expander').data('expanded',true); tr.find('td.album a.expander').addClass('expanded'); tr.find('td.album a.expander').text('v'); $.each(albumData.songs,function(i,song){ if(i>0){ var newRow=tr.clone(); newRow.find('a.expander').remove(); newRow.find('td.album a').text(''); newRow.find('td.artist a').text(''); }else{ var newRow=tr; } newRow.find('td.title a').text(song.name); 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'); }); if(i>0){ lastRow.after(newRow); } lastRow=newRow; }); }, hideAlbum:function(artist,album){ var tr = Collection.parent.find('tr[data-artist="'+artist+'"][data-album="'+album+'"]'); var albumData=tr.data('albumData'); tr.first().find('td.title a').text(albumData.songs.length+' '+t('media','songs')); tr.find('td.album a.expander').data('expanded',false); tr.find('td.album a.expander').removeClass('expanded'); tr.find('td.album a.expander').text('> '); tr.each(function(i,row){ if(i>0){ $(row).remove(); } }); }, parent:null, hide:function(){ if(Collection.parent){ Collection.parent.hide(); } }, registerPlay:function(item){ if(item){ var song=Collection.findSong(item.artist,item.album,item.name); song.song_playcount++; } }, addButtons:function(parent,data){ buttons = parent.find('.buttons'); if(buttons.find('.add').length<=0) { buttons.prepend(''); } buttons.find('.add').unbind('click'); buttons.find('.add').click(function(event){ event.preventDefault(); PlayList.add(data,true); PlayList.render(); }); }, 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