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

var sound = null;

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

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

function preload_images() {
	var cache_images = new Array(
		'loading.gif',
		'music_playing.png'
	);
	$.each(cache_images, function() {
		(new Image()).src = '/static/icons/' + this;
	});
}

Handlebars.registerHelper('trackname', function() {
	var item = this;
	if(!item.metadata)
		return item.name;

	var s = '';
	if(item.metadata.title)
		s = item.metadata.title;
	if(item.metadata.artist) {
		if(s.length) {
			s = ' - ' + s;
			s = item.metadata.artist + s;
		}
	}
	if(!s.length)
		s = item.name;
	return s;
});

var templates = new (function Templates() {
	this.directory_item = Handlebars.compile('<li id="{{type}}-{{id}}" class="{{type}}{{#if nocache}} nocache{{/if}}"><a href="#">{{trackname}}</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) {
			if(item.type == "track")
				item.nocache = !item.cache;
			var el = $(templates.directory_item(item));
			var id = el.attr('id');
			if(item.type == "track") {
				$(el, 'a').click(function() {
					el.addClass('loading');
					if(sound) {
						sound.stop();
						sound.destruct();
					}
					sound = soundManager.createSound({
						id: 'audio',
						url: '/track/' + item.id,
						whileloading: function() {
							$('#status').text('Loading... ' + this.bytesLoaded);
						},
						onload: function(success) {
							el.removeClass('loading').removeClass('nocache');
							slider = $('#progress').slider({
								max: sound.duration,
								slide: function(event, ui) {
									if(event.originalEvent)
										sound.setPosition(ui.value);
								}
							});
						},
						whileplaying: function() {
							$('#progress').slider("value", sound.position);
							$('#' + id).addClass('playing');
							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);
						},
						onstop: function() {
							$('#' + id).removeClass('playing');
						},
						onfinish: function() {
							$('#' + id).removeClass('playing');
						}
					});
					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() {
	preload_images();
	load_directory(0);
	$('#progress').slider();
});