Skip to content

Commit 82ca11d

Browse files
committed
New remove song function
1 parent fa59380 commit 82ca11d

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

dist/APlayer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/APlayer.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/APlayer.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,66 @@ class APlayer {
846846
}
847847
}
848848

849+
/**
850+
* Remove song from playlist
851+
*/
852+
removeSong(indexOfSong) {
853+
854+
if (this.option.music[indexOfSong] != null) { // Check if song exists
855+
const list = this.element.getElementsByClassName('aplayer-list')[0];
856+
var oList = list.firstElementChild; // OL tag
857+
var liList = []; // Holds the index of the LI tags
858+
859+
for (let i = 0; i < oList.childNodes.length; i++) {
860+
if (oList.childNodes[i].tagName == 'LI') {
861+
liList.push(i); //Adds the LI tag indexes to array
862+
}
863+
}
864+
if (this.option.music[indexOfSong + 1] != null || this.option.music[indexOfSong - 1] != null) {
865+
if (indexOfSong == this.playIndex) {
866+
867+
if (this.option.music[indexOfSong + 1] != null) { // Play next song if it exists. If not paused.
868+
this.setMusic(indexOfSong + 1);
869+
} else if (this.option.music[indexOfSong + 1] == null) { // Play previous song if it exists. If not paused.
870+
this.setMusic(indexOfSong - 1);
871+
}
872+
873+
this.playIndex = this.playIndex - 1
874+
875+
} else {
876+
if (indexOfSong < this.playIndex) {
877+
this.playIndex = this.playIndex - 1;
878+
}
879+
}
880+
881+
if (oList.childNodes[liList[indexOfSong + 1]] == null) {
882+
var targetSong = oList.childNodes[liList[indexOfSong - 1]];
883+
targetSong.childNodes[3].textContent = indexOfSong;
884+
} else {
885+
for (let i = 1; i < liList.length; i++) {
886+
if (oList.childNodes[liList[indexOfSong + i]] != null) {
887+
var targetSong = oList.childNodes[liList[indexOfSong + i]];
888+
targetSong.childNodes[3].textContent = indexOfSong + i;
889+
}
890+
}
891+
}
892+
this.option.music.splice(indexOfSong, 1); // Delete song from music array
893+
this.audios.splice(indexOfSong, 1); // Delete song from audios array (Has to be)
894+
oList.childNodes[liList[indexOfSong]].remove();
895+
896+
if (this.option.music[0] != null && this.option.music[1] == null) {
897+
this.multiple = false;
898+
this.element.classList.remove('aplayer-withlist');
899+
}
900+
}
901+
902+
list.style.height = "";
903+
904+
} else {
905+
console.error("ERROR: Song does not exist");
906+
}
907+
908+
}
849909
/**
850910
* destroy this player
851911
*/

0 commit comments

Comments
 (0)