]> CyberLeo.Net >> Repos - CDN/metadata.git/blob - views/handler_js.erb
Even more screwing around
[CDN/metadata.git] / views / handler_js.erb
1 function format_track(index, title, length, bpm, notes) {
2   length = Math.round(length);
3   var minutes = Math.floor(length / 60);
4   var seconds = length % 60;
5   var length = minutes + ":" + ( "0" + seconds ).slice(-2);
6   if ( notes == null ) notes = '-';
7   entry = $('<div id="track_' + index + '" class="track" onClick="current(' + index + ');">');
8   entry.append($('<span class="index">' + index + '</span>'));
9   entry.append($('<span class="title">' + title + '</span>'));
10   entry.append($('<span class="length">' + length + '</span>'));
11   entry.append($('<span class="clear"></span>'));
12   entry.append($('<span class="bpm">' + bpm + ' BPM</span>'));
13   entry.append($('<span class="notes">' + notes + '</span>'));
14   return entry;
15 }
16
17 function init() {
18   for (var index in tracklist) {
19     track = tracklist[index];
20     entry = format_track(track['index'], track['title'], track['length'], track['bpm'], track['notes']);
21     $('#tracks').append(entry);
22   }
23 }
24
25 /* Track 0 is 'no current track' */
26 function current(index) {
27   if (index < 0 || index > maximum_index)
28     return;
29   current_index = index;
30   $("div.track").removeClass("current");
31   $("div#track_" + index).addClass("current");
32   scroll_to(index);
33 }
34
35 function running(index) {
36   if (index < 0 || index > maximum_index)
37     return;
38   running_index = index;
39   $("div.track").removeClass("running");
40   $("div#track_" + index).addClass("running");
41 }
42
43 function scroll_to(index) {
44   // Scroll so that this tile is at the top third. If beyond page boundaries, clamp.
45   var height = $(document).height();
46   var viewport = $(window).height() / 3;
47   var target = height * (index / maximum_index) - viewport;
48
49   $('html,body').animate({scrollTop: target}, 250);
50 }
51
52 function next() {
53   current(current_index + 1);
54 }
55
56 function prev() {
57   current(current_index - 1);
58 }
59
60 function commit() {
61   running(current_index);
62 }
63
64 maximum_index = tracklist.length;
65 current_index = 0;
66 running_index = 0;
67
68 init();