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

	this.get = function(path) {
		var li = this.pl.getElementsByTagName('li')[0];
		while(li) {
			var a = li.getElementsByTagName('a')[0];
			if(a.ml.path == path)
				return li;
			li = li.nextElementSibling;
		}
		return null;
	}

	this.add = function(ml) {
		var a = document.createElement('a');
		a.setAttribute('href', '#');
		a.appendChild(document.createTextNode(ml.path));
		a.ml = ml;
		var li = document.createElement('li');
		var pl = this;
		a.onclick = function() {
			while(li.previousElementSibling)
				li.previousElementSibling.parentElement.removeChild(li.previousElementSibling);
			a.setAttribute('class', 'playing');
			pl.current = li;
			ml.play();

			var nextsong = li.nextElementSibling;
			log('nextsong: ' + nextsong);
			if(nextsong) {
				var nexta = nextsong.getElementsByTagName('a')[0];
				log('nexta: ' + nexta);
				log('next ml: ' + nexta.ml);
				nexta.ml.recode();
			}

			return false;
		}
		log(a.click);

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

		if(this.pl.firstChild == li || (this.current && this.current.nextElementSibling == li))
			ml.recode();
	}

	this.next = function() {
		log('next');
		log('this: ' + this);
		log('current: ' + this.current);
		if(this.current) {
			var old = this.current;
			log('old: ' + old);
			this.current = this.current.nextElementSibling;
			old.parentElement.removeChild(old);
		}
		if(this.current) {
			var a = this.current.getElementsByTagName('a')[0];
			a.onclick();
		}
	}
}