-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent_handlers.js
More file actions
183 lines (166 loc) · 4.75 KB
/
event_handlers.js
File metadata and controls
183 lines (166 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// +++ EVENT HANDLERS +++ //
// [playlist]:click
listener($_playlist_open, "click", () => {
state.isPlaylist = true;
$_playlist.classList.add("music__playlist--on");
return generatePlaylist();
});
listener($_playlist_close, "click", () => {
state.isPlaylist = false;
$_playlist_tracks.innerHTML = ``;
$_playlist.classList.remove("music__playlist--on");
});
// [repeat-btn]:click
listener($_repeat, "click", () => {
state.repeatCount -= state.repeatCount - 1 < 0 ? -2 : 1;
updateRepeat(state);
});
// [shuffle-btn]:click
listener($_shuffle, "click", () => {
$_shuffle.classList.toggle("music__shuffle--on");
state.isShuffle = !state.isShuffle;
});
// [file]:change
listener($_file, "change", () => {
// $audio.pause();
[...Array($_file.files.length).keys()].forEach((index) => {
let file = $_file.files[index];
let src = URL.createObjectURL(file);
fetchMetadata(file, (tags) => {
let track = {
id: (() => trackList[trackList.length - 1].id + 1)(),
title: tags.title,
cover: tags.picture && fetchCover(tags.picture),
artist: tags.artist,
src
};
trackList.push(track);
});
updateMetaData(src);
});
$audio.play();
});
// ALT
/*
listener($_file, 'change', () => {
$audio.pause();
[...Array($_file.files.length).keys()].forEach((index) => {
let file = $_file.files[index];
let src = URL.createObjectURL(file);
fetchMetadata2(file, (tags) => {
let track = {
id: (() => trackList[trackList.length - 1].id + 1)(),
title: tags.title,
cover: tags.v2.APIC[0] && fetchCover(tags.v2.APIC[0]),
artist: tags.v2.TPE1,
src,
};
trackList.push(track);
});
updateMetaData(src);
});
$audio.play();
});
*/
// [file]:drag/drop
window.ondragenter = (e) => {
// $audio.pause();
$_file.classList.add("music__uploader--show");
$_player.classList.add("music--upload");
};
$_player.ondrop = () => {
$audio.play();
$_file.classList.remove("music__uploader--show");
$_player.classList.remove("music--upload");
};
// [audio]:play
listener($audio, "playing", () => updateMetaData());
// [audio]:canplaythrough
listener(
$audio,
"durationchange",
() => ($_duration.textContent = fixMoment($audio.duration))
);
// [audio]:time-update
listener(
$audio,
"timeupdate",
() => ($_currentTime.textContent = fixMoment($audio.currentTime))
);
// [auido]:time-update
listener($audio, "timeupdate", () => {
let percentage = fixPercentage(
parseFloat($audio.currentTime),
parseFloat($audio.duration)
);
fixVariable("seek_listener_percentage", `${percentage}%`);
});
// [seek]:seeked
listener($_seek, "mousedown", () => $audio.pause());
listener($_seek, "mouseup", () => $audio.play());
listener($_seek, "click", (ev) => {
let { offsetX: value } = ev;
let { offsetWidth: max } = $_seek;
// calc motion
let percentage = fixPercentage(value, max);
// TOGGLE
fixVariable("seek_listener_percentage", `${percentage}%`);
// calc value
let amount = fixFloat((percentage / 100) * parseFloat($audio.duration), 3);
$audio.currentTime = amount;
});
// [volume]:seeked
listener($_volume, "click", (ev) => {
let { offsetX: value } = ev;
let { offsetWidth: max } = $_volume;
// calc motion
let percentage = fixPercentage(value, max);
// TOGGLE
fixVariable("volume_listener_percentage", `${percentage}%`);
// calc value
let amount = fixFloat(percentage / 100, 3);
if ($audio.volume <= 0.1) return muteVolume();
$audio.volume = amount;
});
listener($audio, "volumechange", () => {
$_volume.setAttribute(`title`, `${fixFloat($audio.volume * 100, 3)}%`);
});
listener($_volume_btn, "click", () => {
$_volume_btn.firstElementChild.className.indexOf(`fa-volume-mute`) != -1
? unMuteVolume()
: (() => {
state.lastVolume = $audio.volume >= 0.1 ? $audio.volume : 0.1;
muteVolume();
})();
});
// [backward]:click
listener($_backward, "click", () => goBackward());
// [forward]:click
listener($_forward, "click", () => goForward());
listener(
$audio,
"ended",
() =>
// repeat-all-track
(state.repeatCount === 2 && $audio.play()) ||
// repeat-once-track
(state.repeatCount !== 1 && goForward()) ||
// dont-repeat
stopTrack()
);
// [play]:click
listener($_play, "click", () =>
$audio.paused ? $audio.play() : $audio.pause()
);
// [audio]:play
listener($audio, "play", () => {
$_play.setAttribute("title", "play");
$_play.classList.replace(`music__btn--pause`, `music__btn--play`);
$_play.children.item(0).classList.replace("fa-play", "fa-pause");
});
// [audio]:pause
listener($audio, "pause", () => {
$_play.setAttribute("title", "pause");
$_play.classList.replace(`music__btn--play`, `music__btn--pause`);
$_play.children.item(0).classList.replace("fa-pause", "fa-play");
});