summaryrefslogtreecommitdiff
path: root/static/init.js
blob: fc8e800e79659eda4bfcb205017709d4c53e84bf (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
soundManager.useHTML5Audio = true;
soundManager.preferFlash = false;

var sound = null;

function play() {
	if(sound)
		sound.play();
}

function pause() {
	if(sound)
		sound.togglePause();
}

function load_directory(dir_id, dir_item) {
	$.get('/json/list/' + dir_id, function(data) {
		var dir_list = $('#directory-list');
		dir_list.html('');
		if(dir_item && dir_item.parent) {
			dir_list.append($('<li></li>')
				.addClass('dir')
				.append($('<a></a>')
					.attr('href', '#')
					.text('..')
					.click(function() {
						load_directory(dir_item.parent.id, dir_item.parent);
						return false;
					})
				)
			);
		}
		$.each(data, function(i, item) {
			var a = $('<a></a>').attr('href', '#').text(item.name);
			if(item.type == "track") {
				a.click(function() {
					console.log(item);
					if(sound) {
						sound.destruct();
					}
					sound = soundManager.createSound({
						id: 'audio',
						url: '/track/' + item.id,
						whileloading: function() {
							$('#status').text('Loading... ' + this.bytesLoaded);
						},
						whileplaying: function() {
							var seconds = (this.position / 1000).toFixed(0);
							var minutes = Math.floor(seconds / 60).toFixed(0);
							seconds %= 60;
							if(seconds < 10)
								seconds = '0' + seconds;
							var pos = minutes + ':' + seconds;
							$('#status').text(pos);
						}
					});
					sound.play();
					return false;
				});
			} else if(item.type == "dir") {
				a.click(function() {
					load_directory(item.id, item);
					return false;
				});
			}
			dir_list.append($('<li></li>')
				.addClass(item.type)
				.append(a)
			);
		});
	});
}

$(document).ready(function() {
	load_directory(0);
});