summaryrefslogtreecommitdiff
path: root/static/player.js
blob: c0cf6ff6b1c7ac1d1ac9853c84bec2073c2cc1d0 (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
audio = null;

// pre-load some icons
cache_images = new Array(
	'music-cached.png',
	'music-queued.png',
	'loading.gif'
);
var img = new Image();
for(var i = 0; i < cache_images.length; i++) {
	img.src = '/static/icons/' + cache_images[i];
}

function MusicListing(type, path, name, cached) {
	this.type = type;
	this.path = path;
	this.name = name ? name : path.split('/').pop();
	this.a = document.createElement('a');
	this.a.tag = path;

	this.play = function() {
		log('playing ' + path);
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open('GET', '/play?path=' + encodeURIComponent(path));
		xmlhttp.send(null);
	}

	this.recode = function() {
		var path = '/recode?path=' + encodeURIComponent(this.path) +
			'&decoder=' + document.getElementById('trans_from').value +
			'&encoder=' + document.getElementById('trans_to').value;
		var a = this.a;
		var ml = this;
		var xmlhttp = new XMLHttpRequest();
		a.setAttribute('class', 'file file-queued');
		xmlhttp.open('GET', path);
		xmlhttp.send(null);
	}

	this.get_anchor = function() {
		var a = this.a;
		var className = type;
		if(cached)
			className += ' file-cached'
		a.setAttribute('class', className);
		a.setAttribute('href', '#');
		a.appendChild(document.createTextNode(this.name));
		var ml = this;

		a.onclick = function() {
			if(type == 'dir') {
				list(path);
			} else if(type == 'file') {
				ml.play();
			}
			return false;
		}
		return a;
	}
}

function set_current(path) {
	document.getElementById('current-dir').innerHTML = 'Directory: ' + path;
}

function log(s) {
	logbox = document.getElementById('logbox');
	logbox.value += s + '\n';
	logbox.scrollTop = logbox.scrollHeight;
}

function change_url(url) {
	audio.set_src(url);

	var audio_src_url = document.getElementById('audio-src-url');
	audio_src_url.innerHTML = 'Playing: ' + url.replace('&', '&amp;');
}

function output_link(obj) {
	var a = obj.get_anchor();

	var li = document.createElement('li');
	li.appendChild(a);

	var song_links = document.getElementById('song-links');
	song_links.appendChild(li);
}

function list(root) {
	log('listing ' + root);
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			set_current(root);
			var json = JSON.parse(xmlhttp.responseText);
			document.getElementById('song-links').innerHTML = '';
			// add "up" link
			if(root.length > 1) {
				up = root.substr(0, root.lastIndexOf('/'));
				if(up.length == 0)
					up = '/';
				l = new MusicListing('dir', up, '..');
				output_link(l);
			}
			for(var i = 0; i < json.length; i++) {
				var type = json[i]["type"];
				var path = json[i]["name"];
				var name = path.substring(path.lastIndexOf('/')+1);
				var cached = type == "file" ? json[i]["cached"] : false;
				var l = new MusicListing(type, path, name, cached);
				output_link(l);
			}
		}
	}

	path = '/list?directory=' + encodeURIComponent(root);
	xmlhttp.open('GET', path);
	xmlhttp.send();
}

var source = null;

function get_a(path) {
	var as = document.getElementsByTagName('a');
	for(var i = 0; i < as.length; i++) {
		var a = as[i];
		if(a.tag == path)
			return a;
	}
}

function event_handler(event) {
	data = JSON.parse(event.data);
	switch(data['type']) {
		case 'cached':
		case 'recoding':
			log('[' + data['type'] + '] ' + data['path']);
			var a = get_a(data['path']);
			if(a)
				a.setAttribute('class', 'file file-' + data['type']);
			break;
		case 'play':
			log('[play] ' + data['path']);
			change_url(data['path']);
			audio.play();
			break;
		default:
			log('[event] unknown type: ' + data['type']);
	}
}

function event_open() {
	if(source)
		source.close();
	source = new EventSource('/events');
	source.onopen = function() { log('event source opened'); }
	source.onmessage = event_handler;
	source.onerror = function(event) { log('event source error'); }
	log('event source status: ' + source.readyState);
}

window.onload = function() {
	event_open();

	audio = new Audio();
	list('/');
}