summaryrefslogtreecommitdiff
path: root/static/init.js
blob: e75f173850f2715f0fd3a0562e7a2742bdaa3a98 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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;
	});
}

function load_directory(dir_id, dir_item) {
	$.get('/json/list/' + dir_id, function(data) {
		var dir_list = $('#directory-dirs');
		var track_list = $('#directory-tracks');
		dir_list.html('');
		track_list.html('');
		if(dir_item && dir_item.parent) {
			dir_list.append($('<tr></tr>').append($('<td></td>').attr('colspan', 3)
				.append($('<a></a>')
					.addClass('dir')
					.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));
			var id = el.attr('id');
			if(item.type == "track") {
				el.data('track', item);
				// FIXME: This doesn't work with selectable
				/*$(el, 'a').dblclick(function() {
					console.log('track: ', item);
					playlist.add(item);
					return true;
				}).click(function() {
					return false;
				});*/
				track_list.append(el);
			} else if(item.type == "dir") {
				$(el).click(function() {
					load_directory(item.id, item);
					return false;
				});
				dir_list.append(el);
			}
		});
		track_list.selectable({
			filter: 'tr',
			stop: function(event, ui) {
				$('#directory-add').prop('disabled', track_list.find('.ui-selected').length == 0);
				return true;
			}
		});
	});
	$('#directory-add').prop('disabled', true);
}

function set_tracks(container, select, click) {
	return (function(tracks) {
		container.empty();
		$.each(tracks, function(i, track) {
			var el = $(templates.directory_item(track));
			el.data('track', track);
			if(click !== undefined) {
				el.find('a').click(function(event) {
					click(event, track);
				});
			}
			container.append(el);
		});
		if(select !== undefined) {
			container.selectable({
				filter: 'tr',
				stop: function(event, ui) {
					select.prop('disabled', $(container, ' .ui-selected').length == 0);
					return true;
				}
			});
			select.prop('disabled', true);
		}
	});
}

function show_album(album) {
	var tabs = $('#tabs');
	var tabid = '#album-tab-' + album.id;
	var tab = $(tabid);
	if(tab.length > 0) {
		tabs.tabs('select', tab.index()-1);
		return;
	}
	var tabli = $(templates.album_tabli({tabid: tabid, album: album}));
	tabs.find('.ui-tabs-nav').append(tabli);
	var tab = $(templates.album_tab(album));
	tabs.append(tab).tabs('refresh');
	var tracks = $(tabid + '-table tbody');
	var addbutton = $(tab).find('input');
	addbutton.click(function() {
		var tracks = $(tabid + '-table tbody tr.ui-selected');
		tracks.each(function(i, item) {
			var track = $(item).data('track');
			playlist.add(track);
		});
	});
	$.get('/json/album/' + album.id, set_tracks(tracks, addbutton), 'json');
	tabs.tabs('select', tab.index()-1);
}

function add_albums(data) {
	var div = $('#albums-list');
	$.each(data, function(i, album) {
		var el = $(templates.albums_item(album));
		el.find('a').click(function() {
			show_album(album);
			return false;
		});
		div.append(el);
	});
}

function setup_album_scrolling() {
	$('#albums-list').scroll(function(event) {
		if(albums_end)
			return;
		var scrolltop = event.target.scrollTop;
		var scrollheight = event.target.scrollHeight;
		var height = $(event.target).height();
		var remaining = (scrollheight - height) - scrolltop;
		if(remaining < 150 && !albums_loading) {
			load_albums();
		}
	});
}

var albums_page = 0;
var albums_loading = false;
var albums_end = false;
function load_albums(initiate_scrolling) {
	var page = albums_page;
	albums_page++;
	albums_loading = true;
	$.get('/json/albums/' + page, function(data) {
		if(data.length > 0)
			add_albums(data);
		else
			albums_end = true;
		if(initiate_scrolling == true)
			setup_album_scrolling();
		albums_loading = false;
	}, 'json');
}

$(document).ready(function() {
	var albums_initially_loaded = false;
	$('#tabs').tabs({
		activate: function(event, ui) {
			if(ui.newPanel.selector == '#albums-tab') {
				if(!albums_initially_loaded) {
					load_albums(true);
					albums_initially_loaded = true;
				}
			}
		}
	});
	// Shamlessly stolen from the tabs manipulation example: http://jqueryui.com/tabs/#manipulation
	$('#tabs span.ui-icon-close').live('click', function() {
		var panelid = $(this).closest('li').remove().attr('aria-controls');
		$('#' + panelid).remove();
		$('#tabs').tabs('refresh');
	});
	preload_images();
	load_directory(0);
	$('#progress').slider();
	$('#playlist tbody').sortable({
		items: 'tr:not(.playing)',
		cancel: '.playing',
		update: function() {
			$('#playlist tbody tr').each(function(i, tr) {
				var cid = $(tr).attr('id').match(/^cid-(c\d+)$/)[1];
				var model = items.getByCid(cid);
				model.attributes.order_id = i+1;
				model.save();
			});
			items.sort({silent: true});
			playlist.hintnext();
		}
	});
	$('#search_box').keypress(function(event) {
		if(event.keyCode == 13) {
			var val = $(this).val();
			$.get('/json/search?q=' + encodeURIComponent(val), set_tracks($('#search-results'), $('#search-add')), 'json');
		}
	});
	$('#search-add').click(function(event) {
		var tracks = $('#search-results tr.ui-selected');
		tracks.each(function(i, item) {
			var track = $(item).data('track');
			playlist.add(track);
		});
	}).prop('disabled', true);
	$('#directory-add').click(function(event) {
		var tracks = $('#directory-tracks tr.ui-selected');
		tracks.each(function(i, item) {
			var track = $(item).data('track');
			playlist.add(track);
		});
	}).prop('disabled', true);
});