controller module
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import utils from './utils';
|
||||
import Icons from './icons';
|
||||
|
||||
class Controller {
|
||||
constructor (player) {
|
||||
this.player = player;
|
||||
|
||||
this.initPlayButton();
|
||||
this.initList();
|
||||
this.initPlayBar();
|
||||
this.initModeButton();
|
||||
this.initMenuButton();
|
||||
if (!utils.isMobile) {
|
||||
this.initVolumeButton();
|
||||
}
|
||||
}
|
||||
|
||||
initPlayButton () {
|
||||
this.player.template.button.addEventListener('click', () => {
|
||||
this.player.toggle();
|
||||
});
|
||||
}
|
||||
|
||||
initList () {
|
||||
this.player.template.list.addEventListener('click', (e) => {
|
||||
let target;
|
||||
if (e.target.tagName.toUpperCase() === 'LI') {
|
||||
target = e.target;
|
||||
}
|
||||
else {
|
||||
target = e.target.parentElement;
|
||||
}
|
||||
const musicIndex = parseInt(target.getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1;
|
||||
if (musicIndex !== this.player.playIndex) {
|
||||
this.player.setMusic(musicIndex);
|
||||
this.player.play();
|
||||
}
|
||||
else {
|
||||
this.player.toggle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initPlayBar () {
|
||||
let barWidth;
|
||||
this.player.template.barWrap.addEventListener('click', (event) => {
|
||||
const e = event || window.event;
|
||||
barWidth = this.player.template.barWrap.clientWidth;
|
||||
const percentage = (e.clientX - utils.getElementViewLeft(this.player.template.barWrap)) / barWidth;
|
||||
if (isNaN(this.player.audio.duration)) {
|
||||
this.player.bar.set('played', 0, 'width');
|
||||
}
|
||||
else {
|
||||
this.player.bar.set('played', percentage, 'width');
|
||||
this.player.template.ptime.innerHTML = utils.secondToTime(percentage * this.player.audio.duration);
|
||||
this.player.audio.currentTime = this.player.bar.get('played', 'width') * this.player.audio.duration;
|
||||
}
|
||||
});
|
||||
|
||||
this.player.template.thumb.addEventListener('mouseover', () => {
|
||||
this.player.template.thumb.style.background = this.player.options.theme;
|
||||
});
|
||||
this.player.template.thumb.addEventListener('mouseout', () => {
|
||||
this.player.template.thumb.style.background = '#fff';
|
||||
});
|
||||
|
||||
const thumbMove = (event) => {
|
||||
const e = event || window.event;
|
||||
let percentage = (e.clientX - utils.getElementViewLeft(this.player.template.barWrap)) / barWidth;
|
||||
percentage = percentage > 0 ? percentage : 0;
|
||||
percentage = percentage < 1 ? percentage : 1;
|
||||
this.player.bar.set('played', percentage, 'width');
|
||||
this.player.lrc && this.player.lrc.update(this.player.bar.get('played', 'width') * this.player.audio.duration);
|
||||
this.player.template.ptime.innerHTML = utils.secondToTime(percentage * this.player.audio.duration);
|
||||
};
|
||||
|
||||
const thumbUp = () => {
|
||||
document.removeEventListener('mouseup', thumbUp);
|
||||
document.removeEventListener('mousemove', thumbMove);
|
||||
if (isNaN(this.player.audio.duration)) {
|
||||
this.player.bar.set('played', 0, 'width');
|
||||
}
|
||||
else {
|
||||
this.player.audio.currentTime = this.player.bar.get('played', 'width') * this.player.audio.duration;
|
||||
this.player.playedTime = setInterval(() => {
|
||||
this.player.bar.set('played', this.player.audio.currentTime / this.player.audio.duration, 'width');
|
||||
this.player.lrc && this.player.lrc.update();
|
||||
this.player.template.ptime.innerHTML = utils.secondToTime(this.player.audio.currentTime);
|
||||
this.player.trigger('playing');
|
||||
}, 100);
|
||||
}
|
||||
};
|
||||
|
||||
this.player.template.thumb.addEventListener('mousedown', () => {
|
||||
barWidth = this.player.template.barWrap.clientWidth;
|
||||
clearInterval(this.player.playedTime);
|
||||
document.addEventListener('mousemove', thumbMove);
|
||||
document.addEventListener('mouseup', thumbUp);
|
||||
});
|
||||
}
|
||||
|
||||
initVolumeButton () {
|
||||
const barHeight = 35;
|
||||
this.player.template.volumeBarWrap.addEventListener('click', (event) => {
|
||||
const e = event || window.event;
|
||||
let percentage = (barHeight - e.clientY + utils.getElementViewTop(this.player.template.volumeBar)) / barHeight;
|
||||
percentage = percentage > 0 ? percentage : 0;
|
||||
percentage = percentage < 1 ? percentage : 1;
|
||||
this.player.volume(percentage);
|
||||
});
|
||||
this.player.template.volumeButton.addEventListener('click', () => {
|
||||
if (this.player.audio.muted) {
|
||||
this.player.audio.muted = false;
|
||||
this.player.switchVolumeIcon();
|
||||
this.player.bar.set('volume', this.player.volume(), 'height');
|
||||
}
|
||||
else {
|
||||
this.player.audio.muted = true;
|
||||
this.player.switchVolumeIcon();
|
||||
this.player.bar.set('volume', 0, 'height');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initModeButton () {
|
||||
this.player.template.mode.addEventListener('click', () => {
|
||||
if (this.player.isMultiple()) {
|
||||
if (this.player.mode === 'random') {
|
||||
this.player.mode = 'single';
|
||||
}
|
||||
else if (this.player.mode === 'single') {
|
||||
this.player.mode = 'order';
|
||||
}
|
||||
else if (this.player.mode === 'order') {
|
||||
this.player.mode = 'circulation';
|
||||
}
|
||||
else if (this.player.mode === 'circulation') {
|
||||
this.player.mode = 'random';
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this.player.mode === 'circulation') {
|
||||
this.player.mode = 'order';
|
||||
}
|
||||
else {
|
||||
this.player.mode = 'circulation';
|
||||
}
|
||||
}
|
||||
this.player.template.mode.innerHTML = Icons[this.player.mode];
|
||||
this.player.audio.loop = !(this.player.isMultiple() || this.player.mode === 'order');
|
||||
});
|
||||
}
|
||||
|
||||
initMenuButton () {
|
||||
this.player.template.list.style.height = this.player.template.list.offsetHeight + 'px';
|
||||
this.player.template.menu.addEventListener('click', () => {
|
||||
if (!this.player.template.list.classList.contains('aplayer-list-hide')) {
|
||||
this.player.template.list.classList.add('aplayer-list-hide');
|
||||
}
|
||||
else {
|
||||
this.player.template.list.classList.remove('aplayer-list-hide');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Controller;
|
||||
+2
-140
@@ -5,6 +5,7 @@ import Template from './template';
|
||||
import Bar from './bar';
|
||||
import User from './user';
|
||||
import Lrc from './lrc';
|
||||
import Controller from './controller';
|
||||
|
||||
const instances = [];
|
||||
|
||||
@@ -100,146 +101,7 @@ class APlayer {
|
||||
|
||||
this.bar = new Bar(this.template);
|
||||
|
||||
// play and pause button
|
||||
this.template.button.addEventListener('click', () => {
|
||||
this.toggle();
|
||||
});
|
||||
|
||||
// click music list: change music
|
||||
this.template.list.addEventListener('click', (e) => {
|
||||
let target;
|
||||
if (e.target.tagName.toUpperCase() === 'LI') {
|
||||
target = e.target;
|
||||
}
|
||||
else {
|
||||
target = e.target.parentElement;
|
||||
}
|
||||
const musicIndex = parseInt(target.getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1;
|
||||
if (musicIndex !== this.playIndex) {
|
||||
this.setMusic(musicIndex);
|
||||
this.play();
|
||||
}
|
||||
else {
|
||||
this.toggle();
|
||||
}
|
||||
});
|
||||
|
||||
let barWidth;
|
||||
this.template.barWrap.addEventListener('click', (event) => {
|
||||
const e = event || window.event;
|
||||
barWidth = this.template.barWrap.clientWidth;
|
||||
const percentage = (e.clientX - utils.getElementViewLeft(this.template.barWrap)) / barWidth;
|
||||
if (isNaN(this.audio.duration)) {
|
||||
this.bar.set('played', 0, 'width');
|
||||
}
|
||||
else {
|
||||
this.bar.set('played', percentage, 'width');
|
||||
this.template.ptime.innerHTML = utils.secondToTime(percentage * this.audio.duration);
|
||||
this.audio.currentTime = this.bar.get('played', 'width') * this.audio.duration;
|
||||
}
|
||||
});
|
||||
|
||||
this.template.thumb.addEventListener('mouseover', () => {
|
||||
this.template.thumb.style.background = this.options.theme;
|
||||
});
|
||||
this.template.thumb.addEventListener('mouseout', () => {
|
||||
this.template.thumb.style.background = '#fff';
|
||||
});
|
||||
|
||||
const thumbMove = (event) => {
|
||||
const e = event || window.event;
|
||||
let percentage = (e.clientX - utils.getElementViewLeft(this.template.barWrap)) / barWidth;
|
||||
percentage = percentage > 0 ? percentage : 0;
|
||||
percentage = percentage < 1 ? percentage : 1;
|
||||
this.bar.set('played', percentage, 'width');
|
||||
this.lrc && this.lrc.update(this.bar.get('played', 'width') * this.audio.duration);
|
||||
this.template.ptime.innerHTML = utils.secondToTime(percentage * this.audio.duration);
|
||||
};
|
||||
|
||||
const thumbUp = () => {
|
||||
document.removeEventListener('mouseup', thumbUp);
|
||||
document.removeEventListener('mousemove', thumbMove);
|
||||
if (isNaN(this.audio.duration)) {
|
||||
this.bar.set('played', 0, 'width');
|
||||
}
|
||||
else {
|
||||
this.audio.currentTime = this.bar.get('played', 'width') * this.audio.duration;
|
||||
this.playedTime = setInterval(() => {
|
||||
this.bar.set('played', this.audio.currentTime / this.audio.duration, 'width');
|
||||
this.lrc && this.lrc.update();
|
||||
this.template.ptime.innerHTML = utils.secondToTime(this.audio.currentTime);
|
||||
this.trigger('playing');
|
||||
}, 100);
|
||||
}
|
||||
};
|
||||
|
||||
this.template.thumb.addEventListener('mousedown', () => {
|
||||
barWidth = this.template.barWrap.clientWidth;
|
||||
clearInterval(this.playedTime);
|
||||
document.addEventListener('mousemove', thumbMove);
|
||||
document.addEventListener('mouseup', thumbUp);
|
||||
});
|
||||
|
||||
// control volume
|
||||
const barHeight = 35;
|
||||
this.template.volumeBarWrap.addEventListener('click', (event) => {
|
||||
const e = event || window.event;
|
||||
let percentage = (barHeight - e.clientY + utils.getElementViewTop(this.template.volumeBar)) / barHeight;
|
||||
percentage = percentage > 0 ? percentage : 0;
|
||||
percentage = percentage < 1 ? percentage : 1;
|
||||
this.volume(percentage);
|
||||
});
|
||||
this.template.volumeButton.addEventListener('click', () => {
|
||||
if (this.audio.muted) {
|
||||
this.audio.muted = false;
|
||||
this.switchVolumeIcon();
|
||||
this.bar.set('volume', this.volume(), 'height');
|
||||
}
|
||||
else {
|
||||
this.audio.muted = true;
|
||||
this.switchVolumeIcon();
|
||||
this.bar.set('volume', 0, 'height');
|
||||
}
|
||||
});
|
||||
|
||||
// mode control
|
||||
this.template.mode.addEventListener('click', () => {
|
||||
if (this.isMultiple()) {
|
||||
if (this.mode === 'random') {
|
||||
this.mode = 'single';
|
||||
}
|
||||
else if (this.mode === 'single') {
|
||||
this.mode = 'order';
|
||||
}
|
||||
else if (this.mode === 'order') {
|
||||
this.mode = 'circulation';
|
||||
}
|
||||
else if (this.mode === 'circulation') {
|
||||
this.mode = 'random';
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this.mode === 'circulation') {
|
||||
this.mode = 'order';
|
||||
}
|
||||
else {
|
||||
this.mode = 'circulation';
|
||||
}
|
||||
}
|
||||
this.template.mode.innerHTML = Icons[this.mode];
|
||||
this.audio.loop = !(this.isMultiple() || this.mode === 'order');
|
||||
});
|
||||
|
||||
// toggle menu control
|
||||
this.template.list.style.height = this.template.list.offsetHeight + 'px';
|
||||
this.template.menu.addEventListener('click', () => {
|
||||
if (!this.template.list.classList.contains('aplayer-list-hide')) {
|
||||
this.template.list.classList.add('aplayer-list-hide');
|
||||
}
|
||||
else {
|
||||
this.template.list.classList.remove('aplayer-list-hide');
|
||||
}
|
||||
});
|
||||
this.controller = new Controller(this);
|
||||
|
||||
if (this.mode === 'random') {
|
||||
this.setMusic(this.randomOrder[0]);
|
||||
|
||||
Reference in New Issue
Block a user