summaryrefslogtreecommitdiff
path: root/static/playlist.js
blob: a84e3d597c53ae3f2360844a4caa0aa7b3a3c448 (plain)
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
function Playlist(pl) {
	this.pl = pl;
	this.current = null;
	this.lastalbum = null;

	this.get = function(path, track) {
		var li = this.pl.getElementsByTagName('li')[0];
		while(li) {
			var as = li.getElementsByTagName('a');
			if(as.length == 2) {
				var a = as[0];
				if(a.ml.path == path && (!track || a.ml.track == track))
					return li;
			}
			li = li.nextElementSibling;
		}
		return null;
	}

	this.add = function(ml) {
		var a = document.createElement('a');
		a.setAttribute('class', 'song');
		a.setAttribute('href', '#');
		var name = ml.get_text();
		a.appendChild(document.createTextNode(name));
		a.ml = ml;
		var li = document.createElement('li');
		var pl = this;
		a.onclick = function() {
			while(li.previousElementSibling && li.previousElementSibling.getAttribute('class') != 'album')
				li.previousElementSibling.parentElement.removeChild(li.previousElementSibling);
			// Remove children above the album marker.
			// This could probably be done in a more elegant way.
			if(li.previousElementSibling && li.previousElementSibling.previousElementSibling)
				while(li.previousElementSibling.previousElementSibling)
					li.previousElementSibling.previousElementSibling.parentElement.removeChild(li.previousElementSibling.previousElementSibling);
			if(li.previousElementSibling && pl.pl.firstChild != li.previousElementSibling)
				pl.pl.removeChild(pl.pl.firstChild);
			a.setAttribute('class', 'song playing');
			pl.current = li;
			ml.play();

			var nextsong = li.nextElementSibling;
			// avoid breaking when a track has been removed
			while(nextsong && nextsong.childElementCount == 0)
				nextsong = nextsong.nextElementSibling;
			if(nextsong && nextsong.getAttribute('class') == 'album')
				nextsong = nextsong.nextElementSibling;
			if(nextsong) {
				var nexta = nextsong.getElementsByTagName('a')[0];
				nexta.ml.recode();
			}

			return false;
		}

		// album header
		if(ml.metadata.album != this.lastalbum) {
			var album_li = document.createElement('li');
			album_li.appendChild(document.createTextNode(ml.metadata.album));
			album_li.setAttribute('class', 'album');

			var a_del = document.createElement('a');
			a_del.setAttribute('class', 'delete');
			a_del.setAttribute('href', '#');
			a_del.setAttribute('title', 'Remove');
			var del_img = new Image();
			del_img.src = '/static/icons/delete.png';
			a_del.appendChild(del_img);
			a_del.onclick = (function(album_li) {
				return function() {
					while(album_li.nextElementSibling && album_li.nextElementSibling.getAttribute('class') != 'album')
						pl.pl.removeChild(album_li.nextElementSibling);
					pl.pl.removeChild(album_li);
					return false;
				}
			})(album_li);

			var span = document.createElement('span');
			span.style.display = 'none';

			album_li.onmouseover = (function(span) {
				return function() {
					span.style.display = '';
				}
			})(span);
			album_li.onmouseout = (function(span) {
				return function() {
					span.style.display = 'none';
				}
			})(span);
			span.appendChild(a_del);
			album_li.appendChild(span);

			this.pl.appendChild(album_li);
			this.lastalbum = ml.metadata.album;
		}

		var div = document.createElement('div');

		// anchor to remove track from playlist
		var a_del = document.createElement('a');
		a_del.setAttribute('class', 'delete');
		a_del.setAttribute('href', '#');
		a_del.setAttribute('title', 'Remove');
		var del_img = new Image();
		del_img.src = '/static/icons/delete.png';
		a_del.appendChild(del_img);
		a_del.onclick = function() {
			// remove previous element if it is an album header and we're at end of queue,
			// or next element is an album header
			if(li.previousElementSibling && li.previousElementSibling.getAttribute('class') == 'album'
					&& (!li.nextElementSibling || li.nextElementSibling.getAttribute('class') == 'album'))
				pl.pl.removeChild(li.previousElementSibling);
			pl.pl.removeChild(li);
			if(pl.pl.childElementCount == 0)
				pl.lastalbum = null;

			// recode next
			var next = pl.get_next();
			if(next)
				next.getElementsByTagName('a')[0].ml.recode();

			return false;
		}
		var span = document.createElement('span');
		span.style.display = 'none';

		div.onmouseover = (function(span) {
			return function() {
				span.style.display = '';
			}
		})(span);
		div.onmouseout = (function(span) {
			return function() {
				span.style.display = 'none';
			}
		})(span);

		div.appendChild(a);
		div.appendChild(document.createTextNode(' '));

		span.appendChild(a_del);
		span.setAttribute('class', 'right');

		div.appendChild(span);

		li.appendChild(div);
		this.pl.appendChild(li);

		if(this.get_next() == li)
			ml.recode();
	}

	// get the next queued song
	this.get_next = function() {
		var next = null;
		if(this.current)
			next = this.current.nextElementSibling;
		else if(this.pl.firstChild)
			next = this.pl.firstChild;

		if(!next)
			return null;

		if(next.getAttribute('class') == 'album')
			next = next.nextElementSibling;

		return next;
	}

	// play the next queued song
	this.next = function() {
		if(this.current) {
			var old = this.current;
			this.current = this.current.nextElementSibling;
			old.parentElement.removeChild(old);
		}
		// avoid breaking when a track has been removed
		while(this.current && this.current.getAttribute('class') == 'album') {
			this.current = this.current.nextElementSibling;
		}
		if(this.current) {
			var a = this.current.getElementsByTagName('a')[0];
			a.onclick();
		} else {
			this.lastalbum = null;
			this.pl.removeChild(this.pl.firstChild);
		}
	}
}