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

var sound = null;

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

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

var templates = new (function Templates() {
	this.directory_item = Handlebars.compile('<li class="{{type}}"><a href="#">{{name}}</a>');
})();

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 el = $(templates.directory_item(item));
			if(item.type == "track") {
				$(el, '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") {
				$(el).click(function() {
					load_directory(item.id, item);
					return false;
				});
			}
			$(el, 'a').click
			dir_list.append(el);
		});
	});
}

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