move getRandomOrder to utils

This commit is contained in:
DIYgod 2018-03-02 16:12:55 +08:00
parent 532dddc3b0
commit c590dd4080
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
2 changed files with 20 additions and 30 deletions

View File

@ -33,7 +33,7 @@ class APlayer {
if (!this.isMultiple() && this.mode !== 'circulation' && this.mode !== 'order') {
this.mode = 'circulation';
}
this.getRandomOrder();
this.randomOrder = utils.randomOrder(this.options.music.length);
if (this.options.showlrc) {
this.container.classList.add('aplayer-withlrc');
@ -348,34 +348,6 @@ class APlayer {
return this.options.music.length > 1;
}
/**
* get random order, using FisherYates shuffle
*/
getRandomOrder (length = this.options.music.length) {
function random (min, max) {
if (max) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
}
function shuffle (arr) {
const length = arr.length,
shuffled = new Array(length);
for (let index = 0, rand; index < length; index++) {
rand = random(0, index);
if (rand !== index) { shuffled[index] = shuffled[rand]; }
shuffled[rand] = arr[index];
}
return shuffled;
}
if (this.isMultiple()) {
this.randomOrder = shuffle([...Array(length)].map(function (item, i) {
return i;
}));
}
}
/**
* get next random number
*/
@ -423,7 +395,7 @@ class APlayer {
const songListLength = this.container.querySelectorAll('.aplayer-list li').length;
this.template.list.style.height = songListLength * 33 + 'px';
this.getRandomOrder();
this.randomOrder = utils.randomOrder(this.options.music.length);
}
/**

View File

@ -65,6 +65,24 @@ const utils = {
dragStart: isMobile ? 'touchstart' : 'mousedown',
dragMove: isMobile ? 'touchmove' : 'mousemove',
dragEnd: isMobile ? 'touchend' : 'mouseup'
},
/**
* get random order, using FisherYates shuffle
*/
randomOrder: (length = this.options.music.length) => {
function shuffle (arr) {
for (let i = arr.length - 1; i >= 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
const itemAtIndex = arr[randomIndex];
arr[randomIndex] = arr[i];
arr[i] = itemAtIndex;
}
return arr;
}
return shuffle([...Array(length)].map(function (item, i) {
return i;
}));
}
};