From 58bfc91d174096f14bdded73f40c04023e26bd59 Mon Sep 17 00:00:00 2001 From: Colleen Date: Tue, 29 Sep 2015 12:16:46 -0700 Subject: [PATCH] 31. jQuery: Play/Pause --- scripts/album.js | 114 +++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 73 deletions(-) diff --git a/scripts/album.js b/scripts/album.js index e70901a..74edb08 100644 --- a/scripts/album.js +++ b/scripts/album.js @@ -37,7 +37,45 @@ var createSongRow = function(songNumber, songName, songLength) { + '' ; - 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) { @@ -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 = ''; var pauseButtonTemplate = ''; 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); - }); - } -}; \ No newline at end of file +}); \ No newline at end of file