-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathef.ymp.trackSeek.js
369 lines (309 loc) · 11 KB
/
ef.ymp.trackSeek.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
362
363
364
365
366
367
368
369
/**
Yahoo Media Player - TrackSeek extension
Displays a visual position indicator and allows the user to control the seek position
@author: Eric Fehrenbacher
@company: Yahoo!
@version 0.1.2
*/
var TrackSeek = function() {
// reference to YMP
this.player = YAHOO.MediaPlayer;
// position of the left edge of the slider relative to the page
this.seekControlX = 0;
// reference to the currently playing track
this.track = null;
// the elapsed time of the currently playing track
this.elapsed = 0;
// the duration of the currently playing track
this.duration = 0;
// tells us if the track is playing or not
this.paused = true;
// tells us if we are dragging the slider or not
this.dragging = false;
// tracks the percentage of movement from the left edge of the control
this.position = 0;
// setup of delegates, subscribers, and UI
this.init();
};
TrackSeek.create = function(args) {
YAHOO.mediaplayer.TrackSeek = new TrackSeek(args[0]);
return YAHOO.mediaplayer.TrackSeek;
};
TrackSeek.attachElement = 'ymp-meta-album-title';
TrackSeek.prototype = {
/**
Fires during the contruction of this object
@method init
*/
init: function() {
//
this.setupUI();
// some ui specs
this.ui = {
// define the total width
width: function() {
return parseInt(YAHOO.ympyui.util.Dom.getStyle('ymp-seek', 'width'));
},
// define the left edge
left: function() {
return Math.round(parseInt(YAHOO.ympyui.util.Dom.getStyle('ymp-seek-thumb', 'width')) / 2);
},
// define the right edge
right: function() {
return this.width() - this.left();
}
};
// initialize default seek position...
this.onPositionChange(1);
// handle mousedown on the position control
YAHOO.ympyui.util.Event.on('ymp-seek', 'mousedown', this.seekStartDrag, this, true);
// handle clicks on the position control
YAHOO.ympyui.util.Event.on('ymp-seek', 'click', this.stopEvent);
// track hash of active track when play is clicked
this.player.onTrackStart.subscribe(this.onPlay, this, true);
// update position based on elapsed time of active track
this.player.onProgress.subscribe(this.onProgress, this, true);
//
this.player.onTrackPause.subscribe(this.onPause, this, true);
//
YAHOO.ympyui.util.Event.addListener(['ymp-next','ymp-prev'], 'click', function() {
this.onPositionChange(1);
}, this, true);
},
/**
We just setup the GUI here.
@method setupUI
*/
setupUI: function() {
// grab the volume control
var displaced = document.getElementById(TrackSeek.attachElement);
YAHOO.ympyui.util.Dom.setStyles(displaced, {display: 'none'});
// create slider
var seekContainer = document.createElement('div');
seekContainer.id = 'ymp-seek';
YAHOO.ympyui.util.Dom.setStyles(seekContainer, {
position: 'absolute',
width: '190px',
height: '15px',
zIndex: '2'
});
displaced.parentNode.insertBefore(seekContainer, displaced);
/*
// we need to be notified when the timer changes size so we can readjust
// or better yet, why do we need to readjust the timer should be doing that
var seekRegion = YAHOO.ympyui.util.Dom.getRegion(seekContainer);
var seekWidth = seekRegion.right - seekRegion.left;
YAHOO.ympyui.util.Dom.setStyle(seekContainer, 'width', (seekWidth - 10) + 'px');
*/
var seekCover = document.createElement('div');
seekCover.id = 'ymp-seek-cover';
YAHOO.ympyui.util.Dom.setStyles(seekCover, {
display: 'block',
position: 'absolute',
overflow: 'hidden',
top: '5px',
left: '0px',
width: '100%',
height: '4px',
backgroundColor: '#D6D6D6'
});
seekContainer.appendChild(seekCover);
var seekCoverElapsed = document.createElement('div');
seekCoverElapsed.id = 'ymp-seek-cover-elapsed';
YAHOO.ympyui.util.Dom.setStyles(seekCoverElapsed, {
display: 'block',
position: 'absolute',
visibility: 'hidden',
overflow: 'hidden',
top: '0px',
left: '0px',
width: '100%',
height: '4px',
backgroundColor: '#929392'
});
seekCover.appendChild(seekCoverElapsed);
YAHOO.ympyui.util.Event.addListener(seekContainer, 'mouseover', function() {
this.style.backgroundColor = '#CEFD0D';
}, seekCoverElapsed, true);
YAHOO.ympyui.util.Event.addListener(seekContainer, 'mouseout', function() {
this.style.backgroundColor = '#929392';
}, seekCoverElapsed, true);
var seekThumb = document.createElement('div');
seekThumb.id = 'ymp-seek-thumb';
YAHOO.ympyui.util.Event.addListener(seekThumb, 'mouseover', function() {
this.style.borderColor = '#CEFD0D';
}, seekThumb, true);
YAHOO.ympyui.util.Event.addListener(seekThumb, 'mouseout', function() {
this.style.borderColor = '#DDDDDD #B2B2B2 #B2B2B2 #DDDDDD';
}, seekThumb, true);
YAHOO.ympyui.util.Dom.setStyles(seekThumb, {
display: 'block',
position: 'absolute',
overflow: 'hidden',
top: '3px',
left: '0px',
width: '4px',
height: '7px',
cursor: 'pointer',
backgroundColor: '#E9E8E8',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: '#DDDDDD #B2B2B2 #B2B2B2 #DDDDDD'
});
seekContainer.appendChild(seekThumb);
},
/**
Provides a shortcut for stopping an event
@method stopEvent
@param {DOMEvent} evt
*/
stopEvent: function(evt) {
YAHOO.ympyui.util.Event.stopEvent(evt);
},
/**
Event handler for when the user starts to drag the position slider
@method seekStartDrag
@param {Object} eventObj The HTML event object.
*/
seekStartDrag: function(evt) {
if (this.paused) {
return;
}
this.stopEvent(evt);
this.dragging = true;
// get the current position of the left edge of the slider relative to the page
this.seekControlX = YAHOO.ympyui.util.Dom.getX('ymp-seek');
// notify everyone about position change
this.notifySeekChange(evt);
YAHOO.ympyui.util.Event.on(document, 'mousemove', this.notifySeekChange, this, true);
YAHOO.ympyui.util.Event.on(document, 'mouseup', this.seekMouseUp, this, true);
},
/**
Event handler when the user releases the mouse after dragging the position slider. Remove the appropriate event listeners.
@method seekMouseUp
@param {Object} eventObj The HTML event object
*/
seekMouseUp: function(evt) {
this.dragging = false;
this.stopEvent(evt);
this.elapsed = this.duration - (this.position * this.duration);
YAHOO.ympyui.util.Event.removeListener(document, 'mousemove', this.notifySeekChange);
YAHOO.ympyui.util.Event.removeListener(document, 'mouseup', this.seekMouseUp);
if (this.paused) {
this.player.pause();
return;
// reduce the volume before we play so that no one here's the brief noise
var volume = this.player.getVolume();
this.player.setVolume(0);
this.player.play(this.track, this.elapsed);
/*
it would be nice to be able to move the position while paused, but...
for some reason this hack won't work here, haven't figured out why yet
*/
/*
this.playCheck = YAHOO.ympyui.lang.later(100, this, function(volume) {
if (this.player.getPlayerState() == 2) {
this.playCheck.cancel();
//this.updateSeekPosition();
this.onProgress({elapsed: this.elapsed, duration: this.duration})
console.log("asddddddddd");
this.player.pause();
//this.player.setVolume(volume);
}
}, [volume], true);
*/
} else {
this.player.play(this.track, this.elapsed);
}
},
/**
Get the right requested position based on where the user has dragged the position slider and then fire the position change request event for that position
@method notifySeekChange
@param {DOMEvent} evt
*/
notifySeekChange: function(evt) {
this.stopEvent(evt);
var newMouseX = YAHOO.ympyui.util.Event.getPageX(evt);
var xDiff = (newMouseX - this.seekControlX);
xDiff -= 0;
var thumbLeft;
// calculate position percentage (0 - 1) and fire the event to notify whoever is listening
if ((xDiff >= this.ui.left()) && (xDiff < this.ui.right())) {
// mouse is within the constraint
thumbLeft = xDiff - this.ui.left();
} else if (xDiff >= this.ui.right()) {
// mouse is way below the position, so cap the thumb at the maximum x it is allowed to go
thumbLeft = this.ui.right() - this.ui.left();
} else if (xDiff < this.ui.left()) {
// mouse is way above the position, so cap the x at 0
thumbLeft = 0;
}
// percentage of movement from the left edge of the control
this.position = 1 - (thumbLeft / (this.ui.right() - this.ui.left()));
this.onPositionChange(this.position);
},
/**
Event handler for onPlay. Record the track as a mediaObject for reference.
@method onPlay
@param {Object} track The currently playing track
*/
onPlay: function(track) {
this.paused = false;
this.track = track.mediaObject;
},
/**
This function updates the seek position based on the elapsed time of the currently playing track
@method onProgress
@param {Object} options
*/
onProgress: function(options) {
this.elapsed = options.elapsed;
this.duration = options.duration || (YAHOO.mediaplayer.TrackResume && YAHOO.mediaplayer.TrackResume.duration) || 0;
if (!this.dragging && (this.duration != 0)) {
var thumbLeft;
// convert thumb position into seek position
var xDiff = Math.ceil((Math.ceil(this.elapsed) * this.ui.right() - this.ui.left()) / Math.ceil(this.duration));
xDiff -= 0;
// calculate position percentage (0 - 1) and fire the event to notify whoever is listening
if ((xDiff >= this.ui.left()) && (xDiff < this.ui.right())) {
// seek is within the constraint
thumbLeft = xDiff - this.ui.left();
} else if (xDiff >= this.ui.right()) {
// seek is way below the position, so cap the thumb at the maximum x it is allowed to go
thumbLeft = this.ui.right() - this.ui.left();
} else if (xDiff < this.ui.left()) {
// seek is way above the position, so cap the x at 0
thumbLeft = 0;
}
// percentage of seek position from the left edge of the controle
var position = 1 - (thumbLeft / (this.ui.right() - this.ui.left()));
this.onPositionChange(position);
}
},
/**
Event handler for onPlay. Record the track as a mediaObject for reference.
@method onPause
@param {Object} track The currently playing track
*/
onPause: function(track) {
this.paused = true;
},
/**
Event handler for onPositionChange. If the seek position is changed through API update the view appropriately
@method onPositionChange
@param {Object} seek The new position [0-1]
*/
onPositionChange: function(position) {
// convert seek position into thumb position
var thumbLeft = (1 - position) * (this.ui.right() - this.ui.left());
// adjust seek thumb position
YAHOO.ympyui.util.Dom.setStyle('ymp-seek-thumb', 'left', thumbLeft + 'px');
// adjust elapsed position
YAHOO.ympyui.util.Dom.setStyle('ymp-seek-cover-elapsed', 'visibility', 'visible');
YAHOO.ympyui.util.Dom.setStyle('ymp-seek-cover-elapsed', 'left', -((this.ui.right() - this.ui.left()) - thumbLeft) + 'px');
}
};
/**
This creates a new TrackSeek object and attaches to the MediaPlayer
*/
(typeof YAHOO != 'undefined') && YAHOO.ympyui.util.Event.onAvailable(TrackSeek.attachElement, TrackSeek.create, TrackSeek);