Skip to content

Commit

Permalink
27. DOM Scripting: Play/Pause Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Colleen committed Sep 29, 2015
1 parent 3241dfb commit 116f17f
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions scripts/album.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,81 @@ 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() {
setCurrentAlbum(albumPicasso);

songListContainer.addEventListener('mouseover', function(event) {
if (event.target.parentElement.className === 'album-view-song-item') {
event.target.parentElement.querySelector('.song-item-number').innerHTML = playButtonTemplate;
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) {
this.children[0].innerHTML = this.children[0].getAttribute('data-song-number');
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 116f17f

Please sign in to comment.