-
Notifications
You must be signed in to change notification settings - Fork 0
/
ef.ymp.trackLoop.js
187 lines (146 loc) · 5.19 KB
/
ef.ymp.trackLoop.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
/**
Yahoo Media Player - trackLoop extension
Displays a visual indicator and loops tracks randomly
@author: Eric Fehrenbacher
@company: Yahoo!
*/
var TrackLoop = function() {
// reference to YMP
this.player = YAHOO.MediaPlayer;
// coookie handler
this.cookie = new CookieCutter();
// just some setup options
this.config = {
loop: 0,
cookieCrumb: {
loop : 'ympRPT'
}
};
// whether the playlist loops
this.loop = Number(this.cookie.get(this.config.cookieCrumb.loop) || this.config.loop);
// create playlist reference
this.playlist = YAHOO.mediaplayer.Controller.playlistmanager.playlistArray;
//
this.stack = [];
//
this.init();
};
TrackLoop.create = function(args) {
YAHOO.mediaplayer.TrackLoop = new TrackLoop(args[0]);
return YAHOO.mediaplayer.TrackLoop;
};
TrackLoop.prototype = {
/**
Attach some handlers for track play and progress
*/
init: function() {
//
this.setupUI();
//
this.player.onTrackComplete.subscribe(this.onComplete, this, true);
},
/**
We just setup the GUI here.
*/
setupUI: function() {
// re-style the transport controls
var list = document.getElementById('ymp-tray-list');
list.style.height = '160px';
// create panel for playlist controls
var playlistControlsContainerId = 'ymp-playlist-controls';
var playlistControlsContainer = YAHOO.ympyui.util.Dom.get(playlistControlsContainerId);
if (!playlistControlsContainer) {
playlistControlsContainer = document.createElement('div');
playlistControlsContainer.id = playlistControlsContainerId;
YAHOO.ympyui.util.Dom.setStyles(playlistControlsContainer, {
position: 'absolute',
top: '184px',
width: (parseInt(YAHOO.ympyui.util.Dom.getStyle('ymp-tray', 'width')) - 10) + 'px',
height: '14px',
zIndex: '2',
padding: '2px 5px 3px 5px',
borderTop: '1px solid #636363'
});
playlistControlsContainer.setAttribute('class', 'ymp-color-tray');
list.parentNode.appendChild(playlistControlsContainer);
}
// create loop button
var loopButton = document.createElement('div');
loopButton.id = 'ymp-loop-button';
YAHOO.ympyui.util.Dom.setStyles(loopButton, {
float: 'right',
height: '14px',
fontSize: '12px',
color: '#C5C5C5',
marginLeft: '10px',
cursor: 'pointer'
});
YAHOO.ympyui.util.Event.on(loopButton, 'click', this.toggleLoop, this, true);
playlistControlsContainer.appendChild(loopButton);
var loopIcon = document.createElement('div');
loopIcon.id = 'ymp-loop-button-icon';
YAHOO.ympyui.util.Dom.setStyles(loopIcon, {
float: 'left',
height: '10px',
width: '12px',
margin: '2px 2px 0px 0px',
backgroundImage: 'url(http://github.com/oxchronxo/YMP/raw/master/images/loop.gif)',
backgroundRepeat: 'no-repeat',
backgroundPosition: '0px ' + (this.loop ? -(10) : 0) + 'px'
});
YAHOO.ympyui.util.Event.on(loopButton, 'mouseover', function() {
YAHOO.ympyui.util.Dom.setStyle(loopIcon, 'backgroundPosition', '0px -20px');
}, this, true);
YAHOO.ympyui.util.Event.on(loopButton, 'mouseout', this.toggleLoopHighlight, this, true);
loopButton.appendChild(loopIcon);
var loopText = document.createTextNode('Loop');
loopButton.appendChild(loopText);
},
/**
*/
toggleLoop: function() {
// toggle local var
this.loop = this.loop ? 0 : 1;
// store token
this.cookie.set(this.config.cookieCrumb.loop, this.loop, 90);
// change appearance
this.toggleLoopHighlight();
},
/**
*/
toggleLoopHighlight: function() {
YAHOO.ympyui.util.Dom.setStyle('ymp-loop-button-icon', 'backgroundPosition', '0px ' + (this.loop ? -(10) : 0) + 'px');
},
/**
Provides a shortcut for stopping an event
*/
stopEvent: function(evt) {
YAHOO.ympyui.util.Event.stopEvent(evt);
},
/**
*/
onComplete: function(track) {
if (this.loop) {
if (!YAHOO.mediaplayer.TrackShuffle || (YAHOO.mediaplayer.TrackShuffle && !YAHOO.mediaplayer.TrackShuffle.shuffle)) {
var currentIndex = 0;
for (var i = 0; i < this.playlist.length; i++) {
if (this.playlist[i].id == track.mediaObject.id) {
var currentIndex = i;
break;
}
}
if (currentIndex == (this.playlist.length - 1)) {
// switch to new song
this.player.play(this.playlist[0], 0);
}
}
// cancel the event
return false;
} else {
return true;
}
}
};
/**
*/
(typeof YAHOO != 'undefined') && YAHOO.ympyui.util.Event.onAvailable('ymp-tray-list', TrackLoop.create, TrackLoop);