Skip to content

Commit

Permalink
32. jQuery: Next and Previous Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Colleen committed Sep 29, 2015
1 parent 58bfc91 commit ab49331
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 46 deletions.
7 changes: 4 additions & 3 deletions album.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ <h5 class="album-view-release-info">1909 Spanish Records</h5>
</a>
</div>
<div class="control-group currently-playing">
<h2 class="song-name">My dumb song</h2>
<h2 class="artist-song-mobile">My dumb song - Fallout Boy</h2>
<h3 class="artist-name">Fallout Boy</h3>
<h2 class="song-name"></h2>
<h2 class="artist-song-mobile"></h2>
<h3 class="artist-name"></h3>
<div class="seek-control">
<div class="seek-bar">
<div class="fill"></div>
Expand All @@ -71,6 +71,7 @@ <h3 class="artist-name">Fallout Boy</h3>
</div>
</section>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="scripts/fixtures.js"></script>
<script src="scripts/album.js"></script>
</body>
</html>
139 changes: 96 additions & 43 deletions scripts/album.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
var albumPicasso = {
name: 'The Colors',
artist: 'Pablo Picasso',
label: 'Cubism',
year: '1881',
albumArtUrl: 'assets/images/album_covers/01.png',
songs: [
{ name: 'Blue', length: '4:26' },
{ name: 'Green', length: '3:14' },
{ name: 'Red', length: '5:01' },
{ name: 'Pink', length: '3:21' },
{ name: 'Magenta', length: '2:15' }
]
};

var albumMarconi = {
name: 'The Telephone',
artist: 'Guglielmo Marconi',
label: 'EM',
year: '1909',
albumArtUrl: 'assets/images/album_covers/20.png',
songs: [
{ name: 'Hello, Operator?', length: '1:01' },
{ name: 'Ring, ring, ring', length: '5:01' },
{ name: 'Fits in your pocket', length: '3:21' },
{ name: 'Can you hear me now?', length: '3:14' },
{ name: 'Wrong phone number', length: '2:15' }
]
};

var createSongRow = function(songNumber, songName, songLength) {
var template =
'<tr class="album-view-song-item">'
Expand All @@ -40,35 +10,39 @@ var createSongRow = function(songNumber, songName, songLength) {
var $row = $(template);

var clickHandler = function() {
var songNumber = $(this).attr('data-song-number');
var songNumber = parseInt($(this).attr('data-song-number'));

if (currentlyPlayingSong !== null) {
var currentlyPlayingCell = $('.song-item-number[data-song-number="' + currentlyPlayingSong + '"]');
currentlyPlayingCell.html(currentlyPlayingSong);
if (currentlyPlayingSongNumber !== null) {
var currentlyPlayingCell = $('.song-item-number[data-song-number="' + currentlyPlayingSongNumber + '"]');
currentlyPlayingCell.html(currentlyPlayingSongNumber);
}
if (currentlyPlayingSong !== songNumber) {
if (currentlyPlayingSongNumber !== songNumber) {
$(this).html(pauseButtonTemplate);
currentlyPlayingSong = songNumber;
} else if (currentlyPlayingSong === songNumber) {
currentlyPlayingSongNumber = songNumber;
currentSongFromAlbum = currentAlbum.songs[songNumber - 1];
updatePlayerBarSong();
} else if (currentlyPlayingSongNumber === songNumber) {
$(this).html(playButtonTemplate);
currentlyPlayingSong = null;
$('.main-controls .play-pause').html(playerBarPlayButton);
currentlyPlayingSongNumber = null;
currentSongFromAlbum = null;
}
};

var onHover = function(event) {
var songNumberCell = $(this).find('.song-item-number');
var songNumber = songNumberCell.attr('data-song-number');
var songNumber = parseInt(songNumberCell.attr('data-song-number'));

if (songNumber !== currentlyPlayingSong) {
if (songNumber !== currentlyPlayingSongNumber) {
songNumberCell.html(playButtonTemplate);
}
};

var offHover = function(event) {
var songNumberCell = $(this).find('.song-item-number');
var songNumber = songNumberCell.attr('data-song-number');
var songNumber = parseInt(songNumberCell.attr('data-song-number'));

if (songNumber !== currentlyPlayingSong) {
if (songNumber !== currentlyPlayingSongNumber) {
songNumberCell.html(songNumber);
}
};
Expand All @@ -79,6 +53,8 @@ var createSongRow = function(songNumber, songName, songLength) {
};

var setCurrentAlbum = function(album) {
currentAlbum = album;

var $albumTitle = $('.album-view-title');
var $albumArtist = $('.album-view-artist');
var $albumReleaseInfo = $('.album-view-release-info');
Expand All @@ -98,11 +74,88 @@ var setCurrentAlbum = function(album) {
}
};

var updatePlayerBarSong = function() {
$('.currently-playing .song-name').text(currentSongFromAlbum.name);
$('.currently-playing .artist-name').text(currentAlbum.artist);
$('.currently-playing .artist-song-mobile').text(currentSongFromAlbum.name + " - " + currentAlbum.artist);

$('.main-controls .play-pause').html(playerBarPauseButton);
};

var trackIndex = function(album, song) {
return album.songs.indexOf(song);
};

var nextSong = function() {
var getLastSongNumber = function(index) {
return index == 0 ? currentAlbum.songs.length : index;
};

var currentSongIndex = trackIndex(currentAlbum, currentSongFromAlbum);
currentSongIndex++;

if (currentSongIndex >= currentAlbum.songs.length) {
currentSongIndex = 0;
}

currentlyPlayingSongNumber = currentSongIndex + 1;
currentSongFromAlbum = currentAlbum.songs[currentSongIndex];

$('.currently-playing .song-name').text(currentSongFromAlbum.name);
$('.currently-playing .artist-name').text(currentAlbum.artist);
$('.currently-playing .artist-song-mobile').text(currentSongFromAlbum.name + " - " + currentAlbum.name);
$('.main-controls .play-pause').html(playerBarPauseButton);

var lastSongNumber = getLastSongNumber(currentSongIndex);
var $nextSongNumberCell = $('.song-item-number[data-song-number="' + currentlyPlayingSongNumber + '"]');
var $lastSongNumberCell = $('.song-item-number[data-song-number="' + lastSongNumber + '"]');

$nextSongNumberCell.html(pauseButtonTemplate);
$lastSongNumberCell.html(lastSongNumber);
};

var previousSong = function() {
var getLastSongNumber = function(index) {
return index == (currentAlbum.songs.length - 1) ? 1 : index + 2;
};

var currentSongIndex = trackIndex(currentAlbum, currentSongFromAlbum);
currentSongIndex--;

if (currentSongIndex < 0) {
currentSongIndex = currentAlbum.songs.length - 1;
}

currentlyPlayingSongNumber = currentSongIndex + 1;
currentSongFromAlbum = currentAlbum.songs[currentSongIndex];

$('.currently-playing .song-name').text(currentSongFromAlbum.name);
$('.currently-playing .artist-name').text(currentAlbum.artist);
$('.currently-playing .artist-song-mobile').text(currentSongFromAlbum.name + " - " + currentAlbum.name);
$('.main-controls .play-pause').html(playerBarPauseButton);

var lastSongNumber = getLastSongNumber(currentSongIndex);
var $previousSongNumberCell = $('.song-item-number[data-song-number="' + currentlyPlayingSongNumber + '"]');
var $lastSongNumberCell = $('.song-item-number[data-song-number="' + lastSongNumber + '"]');

$previousSongNumberCell.html(pauseButtonTemplate);
$lastSongNumberCell.html(lastSongNumber);
};

var playButtonTemplate = '<a class="album-song-button"><span class="ion-play"></span></a>';
var pauseButtonTemplate = '<a class="album-song-button"><span class="ion-pause"></span></a>';
var playerBarPlayButton = '<span class="ion-play"></span>';
var playerBarPauseButton = '<span class="ion-pause"></span>';

var currentAlbum = null;
var currentlyPlayingSongNumber = null;
var currentSongFromAlbum = null;

var currentlyPlayingSong = null;
var $previousButton = $('.main-controls .previous');
var $nextButton = $('.main-controls .next');

$(document).ready(function() {
setCurrentAlbum(albumPicasso);
$previousButton.click(previousSong);
$nextButton.click(nextSong);
});
29 changes: 29 additions & 0 deletions scripts/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var albumPicasso = {
name: 'The Colors',
artist: 'Pablo Picasso',
label: 'Cubism',
year: '1881',
albumArtUrl: 'assets/images/album_covers/01.png',
songs: [
{ name: 'Blue', length: '4:26' },
{ name: 'Green', length: '3:14' },
{ name: 'Red', length: '5:01' },
{ name: 'Pink', length: '3:21' },
{ name: 'Magenta', length: '2:15' }
]
};

var albumMarconi = {
name: 'The Telephone',
artist: 'Guglielmo Marconi',
label: 'EM',
year: '1909',
albumArtUrl: 'assets/images/album_covers/20.png',
songs: [
{ name: 'Hello, Operator?', length: '1:01' },
{ name: 'Ring, ring, ring', length: '5:01' },
{ name: 'Fits in your pocket', length: '3:21' },
{ name: 'Can you hear me now?', length: '3:14' },
{ name: 'Wrong phone number', length: '2:15' }
]
};

0 comments on commit ab49331

Please sign in to comment.