Skip to content

Commit ea7a97a

Browse files
Merge pull request DIYgod#1 from ericdrobinson/simple-fixes
Simple fixes
2 parents 275bbfc + a6705d0 commit ea7a97a

3 files changed

Lines changed: 23 additions & 19 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: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ class APlayer {
135135

136136
// multiple music
137137
this.playIndex = 0;
138-
this.multiple = Object.prototype.toString.call(option.music) === '[object Array]';
139-
if (!this.multiple) {
138+
if (Object.prototype.toString.call(option.music) !== '[object Array]') {
140139
this.option.music = [this.option.music];
141140
}
142141
this.music = this.option.music[this.playIndex];
@@ -149,7 +148,8 @@ class APlayer {
149148
this.element.classList.add('aplayer-withlist');
150149
}
151150

152-
if (!this.multiple && this.mode !== 'circulation' && this.mode !== 'order') {
151+
// Assume "circulation" mode if single music is loaded and mode isn't already "circulation" or "order".
152+
if (!this.isMultiple() && this.mode !== 'circulation' && this.mode !== 'order') {
153153
this.mode = 'circulation';
154154
}
155155
this.getRandomOrder();
@@ -390,7 +390,7 @@ class APlayer {
390390
// mode control
391391
const modeEle = this.element.getElementsByClassName('aplayer-icon-mode')[0];
392392
modeEle.addEventListener('click', () => {
393-
if (this.multiple) {
393+
if (this.isMultiple()) {
394394
if (this.mode === 'random') {
395395
this.mode = 'single';
396396
}
@@ -413,7 +413,7 @@ class APlayer {
413413
}
414414
}
415415
modeEle.innerHTML = this.getSVG(this.mode);
416-
this.audio.loop = !(this.multiple || this.mode === 'order');
416+
this.audio.loop = !(this.isMultiple() || this.mode === 'order');
417417
});
418418

419419
// toggle menu control
@@ -564,7 +564,7 @@ class APlayer {
564564
// multiple music play
565565
this.ended = false;
566566
this.audio.addEventListener('ended', () => {
567-
if (this.multiple) {
567+
if (this.isMultiple()) {
568568
if (this.audio.currentTime !== 0) {
569569
if (this.mode === 'random') {
570570
this.setMusic(this.nextRandomNum());
@@ -583,12 +583,8 @@ class APlayer {
583583
}
584584
}
585585
else if (this.mode === 'circulation') {
586-
if (this.playIndex < this.option.music.length - 1) {
587-
this.setMusic(++this.playIndex);
588-
}
589-
else {
590-
this.setMusic(0);
591-
}
586+
this.playIndex = (this.playIndex + 1) % this.option.music.length;
587+
this.setMusic(this.playIndex);
592588
}
593589
}
594590
}
@@ -605,7 +601,7 @@ class APlayer {
605601
this.audio.volume = parseInt(this.element.getElementsByClassName('aplayer-volume')[0].style.height) / 100;
606602

607603
// loop
608-
this.audio.loop = !(this.multiple || this.mode === 'order');
604+
this.audio.loop = !(this.isMultiple() || this.mode === 'order');
609605

610606
this.audios[indexMusic] = this.audio;
611607
}
@@ -790,6 +786,13 @@ class APlayer {
790786
}
791787
}
792788

789+
/**
790+
* get whether multiple music definitions are loaded
791+
*/
792+
isMultiple() {
793+
return this.option.music.length > 1;
794+
}
795+
793796
/**
794797
* get random order, using Fisher–Yates shuffle
795798
*/
@@ -811,7 +814,7 @@ class APlayer {
811814
}
812815
return shuffled;
813816
}
814-
if (this.multiple) {
817+
if (this.isMultiple()) {
815818
this.randomOrder = shuffle([...Array(this.option.music.length)].map(function(item, i) {
816819
return i;
817820
}));
@@ -822,7 +825,7 @@ class APlayer {
822825
* get next random number
823826
*/
824827
nextRandomNum() {
825-
if (this.multiple) {
828+
if (this.isMultiple()) {
826829
let index = this.randomOrder.indexOf(this.playIndex);
827830
if (index === this.randomOrder.length - 1) {
828831
return this.randomOrder[0];
@@ -857,6 +860,8 @@ class APlayer {
857860
* @param {Array} newMusic
858861
*/
859862
addMusic(newMusic) {
863+
let wasSingle = !this.isMultiple();
864+
860865
this.option.music = this.option.music.concat(newMusic);
861866

862867
const list = this.element.getElementsByClassName('aplayer-list')[0];
@@ -873,8 +878,7 @@ class APlayer {
873878
}
874879
listEle.innerHTML += newItemHTML;
875880

876-
if (!this.multiple) {
877-
this.multiple = true;
881+
if (wasSingle && this.isMultiple()) {
878882
this.element.classList.add('aplayer-withlist');
879883
this.audio.loop = false;
880884
}

0 commit comments

Comments
 (0)