website/public/js/music.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-11-28 16:11:04 -05:00
var player = '';
2015-10-06 12:36:47 -04:00
$(document).ready(function() {
2016-11-28 16:36:14 -05:00
player = $('#music-player').get(0);
2016-11-28 16:11:04 -05:00
// on music track click
2016-11-28 16:36:14 -05:00
$('.now-playing-list .music-track').on('click', function() {
// change selected track
changeSelectedTrack(this);
2016-11-28 16:11:04 -05:00
});
// on album art click
$('.music-album .thumbnail').on('click', function() {
// change selected album
changeSelectedAlbum(this);
});
2016-11-28 11:17:28 -05:00
});
2016-11-28 16:11:04 -05:00
2016-11-28 20:09:37 -05:00
function changeSelectedAlbum(albumElem) {
2016-11-28 20:39:13 -05:00
var oldSelected = $('.music-album.selected');
var newSelected = $(albumElem).parent();
2016-11-28 20:09:37 -05:00
if (oldSelected.data('albumid') != newSelected.data('albumid')) {
// remove selected class from the old element and add it to the new one
oldSelected.removeClass('selected');
newSelected.addClass('selected');
2016-11-28 23:27:10 -05:00
// change album info
2016-11-28 21:26:47 -05:00
getAlbumInfo(albumElem);
2016-11-28 23:27:10 -05:00
// change available songs
getAlbumSongs(albumElem);
2016-11-28 20:09:37 -05:00
}
}
2016-11-28 21:23:55 -05:00
function getAlbumInfo(albumElem) {
2016-11-28 21:32:02 -05:00
albumElem = $(albumElem).parent();
2016-11-28 21:23:55 -05:00
$.get('/apiv1/music/album-info/' + albumElem.data('albumid'), function(data) {
var json = $.parseJSON(data);
// change album artwork
$('#album-artwork').attr('src', json.album_art);
// change album title
$('#album-title').text(json.title);
// change album description
$('#album-description').text(json.description);
});
}
function getAlbumSongs(albumElem) {
2016-11-28 23:28:04 -05:00
albumElem = $(albumElem).parent();
$.get('/apiv1/music/album-songs/' + albumElem.data('albumid'), function(data) {
var json = $.parseJSON(data);
var playlist = $('#now-playing-list');
playlist.empty();
for (var i = 0; i < json.songs.length; i++) {
var song = json.songs[i];
var classString = '';
if (i === 0) {
classString = 'music-track selected';
} else {
classString = 'music-track';
}
var songElem = '<li class="' + classString + '" data-trackid="' + song.id + '" data-title="' + song.title + '" data-album="' + song.album_id + '" data-order="' + song.track_order + '" data-path="' + song.audio_file + '">' +
'<span>' + song.track_order + '. ' + song.title + '</span>' +
'</li>';
playlist.append(song);
}
});
}
function changeSelectedTrack(trackElem) {
var oldSelected = $('.now-playing-list .music-track.selected');
var newSelected = $(trackElem);
if (oldSelected.data('trackid') != newSelected.data('trackid')) {
// remove selected class from the old element and add it to the new one
oldSelected.removeClass('selected');
newSelected.addClass('selected');
// now change the audio sources
changeAudioSources(trackElem);
}
}
2016-11-28 16:11:04 -05:00
function changeAudioSources(trackElem) {
trackElem = $(trackElem);
var playerSources = $('#music-player source');
// retrieve audio file path from selected element
var audioFilePath = trackElem.data('path');
// loop through the audio player source elements
2016-11-28 16:36:14 -05:00
playerSources.each(function() {
// jQuery-ize the element
2016-11-28 16:36:14 -05:00
source = $(this);
2016-11-28 16:11:04 -05:00
if (source.attr('type') == 'audio/ogg') {
// OGG source file
source.attr('src', audioFilePath + '.ogg');
} else if (source.attr('type') == 'audio/mpeg') {
// MP3 source file
source.attr('src', audioFilePath + '.mp3');
}
});
// reload music player
player.load();
2016-11-28 16:36:14 -05:00
// start playing new song
player.play();
2016-11-28 16:11:04 -05:00
}