]> CyberLeo.Net >> Repos - CDN/metadata.git/blob - views/handler_js.erb
Hopefully the last 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="' + title + '">' + title.slice(0,65) + '</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" title="' + notes + '">' + notes.slice(0,80) + '</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   commit_update();
63
64   params = {
65     url: "/commit",
66     type: "POST",
67     data: { index: current_index }
68   };
69
70   var req = $.ajax(params)
71     .done(function(data, textStatus, req) {
72       commit_ready();
73     })
74     .fail(function(req, textStatus, errorThrown) {
75       console.log("AjaxFailed:" + textStatus + " - " + errorThrown);
76       commit_failed();
77     });
78 }
79
80 function commit_ready() {
81   var e = $('#commit');
82   e.removeClass('update');
83   e.removeClass('failed');
84   e.html('&#x2713');
85 }
86
87 function commit_update() {
88   var e = $('#commit');
89   e.removeClass('failed');
90   e.addClass('update');
91   e.html('&#x2026');
92 }
93
94 function commit_failed() {
95   var e = $('#commit');
96   e.removeClass('update');
97   e.addClass('failed');
98   e.html('&#x2717');
99 }
100
101 maximum_index = tracklist.length;
102 current_index = 0;
103 running_index = 0;
104
105 init();