-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
361 lines (287 loc) · 10.6 KB
/
index.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
var audioPlayer = function() {
"use strict";
var _currentTrack = null;
var _elements = {
audio: document.getElementById("audio"),
playerButtons: {
largeToggleBtn: document.querySelector(".large-toggle-btn"),
nextTrackBtn: document.querySelector(".next-track-btn"),
previousTrackBtn: document.querySelector(".previous-track-btn"),
smallToggleBtn: document.getElementsByClassName("small-toggle-btn")
},
progressBar: document.querySelector(".progress-box"),
playListRows: document.getElementsByClassName("play-list-row"),
trackInfoBox: document.querySelector(".track-info-box")
};
var _playAHead = false;
var _progressCounter = 0;
var _progressBarIndicator = _elements.progressBar.children[0].children[0].children[1];
var _trackLoaded = false;
var _bufferProgress = function(audio) {
var bufferedTime = (audio.buffered.end(0) * 100) / audio.duration;
var progressBuffer = _elements.progressBar.children[0].children[0].children[0];
progressBuffer.style.width = bufferedTime + "%";
};
var _getXY = function(e) {
var containerX = _elements.progressBar.offsetLeft;
var containerY = _elements.progressBar.offsetTop;
var coords = {
x: null,
y: null
};
var isTouchSuopported = "ontouchstart" in window;
if (e.offsetX || e.offsetX === 0) {
coords.x = e.offsetX;
coords.y = e.offsetY;
return coords;
}
};
var _handleProgressIndicatorClick = function(e) {
var progressBar = document.querySelector(".progress-box");
var xCoords = _getXY(e).x;
return (xCoords - progressBar.offsetLeft) / progressBar.children[0].offsetWidth;
};
var initPlayer = function() {
if (_currentTrack === 1 || _currentTrack === null) {
_elements.playerButtons.previousTrackBtn.disabled = true;
}
for (var i = 0; i < _elements.playListRows.length; i++) {
var smallToggleBtn = _elements.playerButtons.smallToggleBtn[i];
var playListLink = _elements.playListRows[i].children[2].children[0];
playListLink.addEventListener("click", function(e) {
e.preventDefault();
var selectedTrack = parseInt(this.parentNode.parentNode.getAttribute("data-track-row"));
if (selectedTrack !== _currentTrack) {
_resetPlayStatus();
_currentTrack = null;
_trackLoaded = false;
}
if (_trackLoaded === false) {
_currentTrack = parseInt(selectedTrack);
_setTrack();
} else {
_playBack(this);
}
}, false);
//Small toggle button clicked.
smallToggleBtn.addEventListener("click", function(e) {
e.preventDefault();
var selectedTrack = parseInt(this.parentNode.getAttribute("data-track-row"));
if (selectedTrack !== _currentTrack) {
_resetPlayStatus();
_currentTrack = null;
_trackLoaded = false;
}
if (_trackLoaded === false) {
_currentTrack = parseInt(selectedTrack);
_setTrack();
} else {
_playBack(this);
}
}, false);
}
//Audio time has changed so update it.
_elements.audio.addEventListener("timeupdate", _trackTimeChanged, false);
//Audio track has ended playing.
_elements.audio.addEventListener("ended", function(e) {
_trackHasEnded();
}, false);
//Large toggle button clicked.
_elements.playerButtons.largeToggleBtn.addEventListener("click", function(e) {
if (_trackLoaded === false) {
_currentTrack = parseInt(1);
_setTrack()
} else {
_playBack();
}
}, false);
//Next track button clicked.
_elements.playerButtons.nextTrackBtn.addEventListener("click", function(e) {
if (this.disabled !== true) {
_currentTrack++;
_trackLoaded = false;
_resetPlayStatus();
_setTrack();
}
}, false);
//Previous track button clicked.
_elements.playerButtons.previousTrackBtn.addEventListener("click", function(e) {
if (this.disabled !== true) {
_currentTrack--;
_trackLoaded = false;
_resetPlayStatus();
_setTrack();
}
}, false);
//User is moving progress indicator.
_progressBarIndicator.addEventListener("mousedown", _mouseDown, false);
//User stops moving progress indicator.
window.addEventListener("mouseup", _mouseUp, false);
};
/**
Handles the mouseup event by a user.
**/
var _mouseUp = function(e) {
if (_playAHead === true) {
var duration = parseFloat(audio.duration);
var progressIndicatorClick = parseFloat(_handleProgressIndicatorClick(e));
window.removeEventListener("mousemove", _moveProgressIndicator, true);
audio.currentTime = duration * progressIndicatorClick;
audio.addEventListener("timeupdate", _trackTimeChanged, false);
_playAHead = false;
}
};
/**
Moves the progress indicator to a new point in the audio.
**/
var _moveProgressIndicator = function(e) {
var newPosition = 0;
var progressBarOffsetLeft = _elements.progressBar.offsetLeft;
var progressBarWidth = 0;
var progressBarIndicator = _elements.progressBar.children[0].children[0].children[1];
var progressBarIndicatorWidth = _progressBarIndicator.offsetWidth;
var xCoords = _getXY(e).x;
progressBarWidth = _elements.progressBar.children[0].offsetWidth - progressBarIndicatorWidth;
newPosition = xCoords - progressBarOffsetLeft;
if ((newPosition >= 1) && (newPosition <= progressBarWidth)) {
progressBarIndicator.style.left = newPosition + ".px";
}
if (newPosition < 0) {
progressBarIndicator.style.left = "0";
}
if (newPosition > progressBarWidth) {
progressBarIndicator.style.left = progressBarWidth + "px";
}
};
/**
Controls playback of the audio element.
**/
var _playBack = function() {
if (_elements.audio.paused) {
_elements.audio.play();
_updatePlayStatus(true);
document.title = "\u25B6 " + document.title;
} else {
_elements.audio.pause();
_updatePlayStatus(false);
document.title = document.title.substr(2);
}
};
/**
Sets the track if it hasn't already been loaded yet.
**/
var _setTrack = function() {
var songURL = _elements.audio.children[_currentTrack - 1].src;
_elements.audio.setAttribute("src", songURL);
_elements.audio.load();
_trackLoaded = true;
_setTrackTitle(_currentTrack, _elements.playListRows);
_setActiveItem(_currentTrack, _elements.playListRows);
_elements.trackInfoBox.style.visibility = "visible";
_playBack();
};
/**
Sets the activly playing item within the playlist.
**/
var _setActiveItem = function(currentTrack, playListRows) {
for (var i = 0; i < playListRows.length; i++) {
playListRows[i].children[2].className = "track-title";
}
playListRows[currentTrack - 1].children[2].className = "track-title active-track";
};
/**
Sets the text for the currently playing song.
**/
var _setTrackTitle = function(currentTrack, playListRows) {
var trackTitleBox = document.querySelector(".player .info-box .track-info-box .track-title-text");
var trackTitle = playListRows[currentTrack - 1].children[2].outerText;
trackTitleBox.innerHTML = null;
trackTitleBox.innerHTML = trackTitle;
document.title = trackTitle;
};
/**
Plays the next track when a track has ended playing.
**/
var _trackHasEnded = function() {
parseInt(_currentTrack);
_currentTrack = (_currentTrack === _elements.playListRows.length) ? 1 : _currentTrack + 1;
_trackLoaded = false;
_resetPlayStatus();
_setTrack();
};
/**
Updates the time for the song being played.
**/
var _trackTimeChanged = function() {
var currentTimeBox = document.querySelector(".player .info-box .track-info-box .audio-time .current-time");
var currentTime = audio.currentTime;
var duration = audio.duration;
var durationBox = document.querySelector(".player .info-box .track-info-box .audio-time .duration");
var trackCurrentTime = _trackTime(currentTime);
var trackDuration = _trackTime(duration);
currentTimeBox.innerHTML = null;
currentTimeBox.innerHTML = trackCurrentTime;
durationBox.innerHTML = null;
durationBox.innerHTML = trackDuration;
_updateProgressIndicator(audio);
_bufferProgress(audio);
};
/**
A utility function for converting a time in miliseconds to a readable time of minutes and seconds.
**/
var _trackTime = function(seconds) {
var min = 0;
var sec = Math.floor(seconds);
var time = 0;
min = Math.floor(sec / 60);
min = min >= 10 ? min : '0' + min;
sec = Math.floor(sec % 60);
sec = sec >= 10 ? sec : '0' + sec;
time = min + ':' + sec;
return time;
};
/**
* Updates both the large and small toggle buttons accordingly.
**/
var _updatePlayStatus = function(audioPlaying) {
if (audioPlaying) {
_elements.playerButtons.largeToggleBtn.children[0].className = "large-pause-btn";
_elements.playerButtons.smallToggleBtn[_currentTrack - 1].children[0].className = "small-pause-btn";
} else {
_elements.playerButtons.largeToggleBtn.children[0].className = "large-play-btn";
_elements.playerButtons.smallToggleBtn[_currentTrack - 1].children[0].className = "small-play-btn";
}
//Update next and previous buttons accordingly
if (_currentTrack === 1) {
_elements.playerButtons.previousTrackBtn.disabled = true;
_elements.playerButtons.previousTrackBtn.className = "previous-track-btn disabled";
} else if (_currentTrack > 1 && _currentTrack !== _elements.playListRows.length) {
_elements.playerButtons.previousTrackBtn.disabled = false;
_elements.playerButtons.previousTrackBtn.className = "previous-track-btn";
_elements.playerButtons.nextTrackBtn.disabled = false;
_elements.playerButtons.nextTrackBtn.className = "next-track-btn";
} else if (_currentTrack === _elements.playListRows.length) {
_elements.playerButtons.nextTrackBtn.disabled = true;
_elements.playerButtons.nextTrackBtn.className = "next-track-btn disabled";
}
};
/**
Resets all toggle buttons to be play buttons.
**/
var _resetPlayStatus = function() {
var smallToggleBtn = _elements.playerButtons.smallToggleBtn;
_elements.playerButtons.largeToggleBtn.children[0].className = "large-play-btn";
for (var i = 0; i < smallToggleBtn.length; i++) {
if (smallToggleBtn[i].children[0].className === "small-pause-btn") {
smallToggleBtn[i].children[0].className = "small-play-btn";
}
}
};
return {
initPlayer: initPlayer
};
};
(function() {
var player = new audioPlayer();
player.initPlayer();
})();