Skip to content

Commit

Permalink
31. jQuery: Play/Pause
Browse files Browse the repository at this point in the history
  • Loading branch information
Colleen committed Sep 29, 2015
1 parent 448df31 commit 58bfc91
Showing 1 changed file with 41 additions and 73 deletions.
114 changes: 41 additions & 73 deletions scripts/album.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,45 @@ var createSongRow = function(songNumber, songName, songLength) {
+ '</tr>'
;

return $(template);
var $row = $(template);

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

if (currentlyPlayingSong !== null) {
var currentlyPlayingCell = $('.song-item-number[data-song-number="' + currentlyPlayingSong + '"]');
currentlyPlayingCell.html(currentlyPlayingSong);
}
if (currentlyPlayingSong !== songNumber) {
$(this).html(pauseButtonTemplate);
currentlyPlayingSong = songNumber;
} else if (currentlyPlayingSong === songNumber) {
$(this).html(playButtonTemplate);
currentlyPlayingSong = null;
}
};

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

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

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

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

$row.find('.song-item-number').click(clickHandler);
$row.hover(onHover, offHover);
return $row;
};

var setCurrentAlbum = function(album) {
Expand All @@ -60,81 +98,11 @@ var setCurrentAlbum = function(album) {
}
};

var findParentByClassName = function(element, targetClass) {
var currentParent = element.parentElement;
while (currentParent.className != targetClass) {
currentParent = currentParent.parentElement;
}
return currentParent;
};

var getSongItem = function(element) {
switch (element.className) {
case 'album-song-button':
case 'ion-play':
case 'ion-pause':
return findParentByClassName(element, 'song-item-number');
case 'album-view-song-item':
return element.querySelector('.song-item-number');
case 'song-item-title':
case 'song-item-duration':
return findParentByClassName(element, 'album-view-song-item').querySelector('.song-item-number');
case 'song-item-number':
return element;
default:
return;
}
};

var clickHandler = function(targetElement) {
var songItem = getSongItem(targetElement);

if (currentlyPlayingSong === null) {
songItem.innerHTML = pauseButtonTemplate;
currentlyPlayingSong = songItem.getAttribute('data-song-number');
} else if (currentlyPlayingSong === songItem.getAttribute('data-song-number')) {
songItem.innerHTML = playButtonTemplate;
currentlyPlayingSong = null;
} else if (currentlyPlayingSong !== songItem.getAttribute('data-song-number')) {
var currentlyPlayingSongElement = document.querySelector('[data-song-number="' + currentlyPlayingSong + '"]');
currentlyPlayingSongElement.innerHTML = currentlyPlayingSongElement.getAttribute('data-song-number');
songItem.innerHTML = pauseButtonTemplate;
currentlyPlayingSong = songItem.getAttribute('data-song-number');
}
};

var songListContainer = document.getElementsByClassName('album-view-song-list')[0];
var songRows = document.getElementsByClassName('album-view-song-item');

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 currentlyPlayingSong = null;

window.onload = function() {
$(document).ready(function() {
setCurrentAlbum(albumPicasso);

songListContainer.addEventListener('mouseover', function(event) {
if (event.target.parentElement.className === 'album-view-song-item') {
var songItem = getSongItem(event.target);

if (songItem.getAttribute('data-song-number') !== currentlyPlayingSong) {
songItem.innerHTML = playButtonTemplate;
}
}
});

for (i = 0; i < songRows.length; i++) {
songRows[i].addEventListener('mouseleave', function(event) {
var songItem = getSongItem(event.target);
var songItemNumber = songItem.getAttribute('data-song-number');

if (songItemNumber !== currentlyPlayingSong) {
songItem.innerHTML = songItemNumber;
}
});
songRows[i].addEventListener('click', function(event) {
clickHandler(event.target);
});
}
};
});

0 comments on commit 58bfc91

Please sign in to comment.