]> CyberLeo.Net >> Repos - CDN/metadata.git/blob - views/handler_js.erb
Remove controls; use index number region as commit control
[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   var bpm = Math.round(bpm);
7   if ( notes == null ) notes = '-';
8   entry = $('<div id="track_' + index + '" class="track" onClick="current(' + index + ');">');
9   entry.append($('<span class="index">' + index + '</span>'));
10   entry.append($('<span class="title" title="' + title + '">' + title.slice(0,65) + '</span>'));
11   entry.append($('<span class="length">' + length + '</span>'));
12   entry.append($('<span class="clear"></span>'));
13   entry.append($('<span class="bpm">' + bpm + ' BPM</span>'));
14   entry.append($('<span class="notes" title="' + notes + '">' + notes.slice(0,80) + '</span>'));
15   return entry;
16 }
17
18 function init() {
19   for (var index in tracklist) {
20     track = tracklist[index];
21     entry = format_track(track['index'], track['title'], track['length'], track['bpm'], track['notes']);
22     $('#tracks').append(entry);
23   }
24 }
25
26 /* Track 0 is 'no current track' */
27 function current(index) {
28   if (index < 0 || index > maximum_index)
29     return;
30   current_index = index;
31   $("div.track").removeClass("current");
32   $("div.track > .index").off('click.commit');
33   $("div#track_" + index).addClass("current");
34   $("div#track_" + index + " > .index").on('click.commit', commit);
35 }
36
37 function updating(index) {
38   if (index < 0 || index > maximum_index)
39     return;
40   $("div.track").removeClass("running sending");
41   $("div#track_" + index).addClass("sending");
42 }
43
44 function running(index) {
45   if (index < 0 || index > maximum_index)
46     return;
47   $("div.track").removeClass("running sending");
48   $("div#track_" + index).addClass("running");
49 }
50
51 function next() {
52   current(current_index + 1);
53 }
54
55 function prev() {
56   current(current_index - 1);
57 }
58
59 function commit() {
60   running_index = current_index;
61   commit_update();
62
63   params = {
64     url: "/commit",
65     type: "POST",
66     data: { index: current_index }
67   };
68
69   var req = $.ajax(params)
70     .done(function(data, textStatus, req) {
71       commit_ready();
72     })
73     .fail(function(req, textStatus, errorThrown) {
74       console.log("AjaxFailed:" + textStatus + " - " + errorThrown);
75       commit_failed();
76     });
77 }
78
79 function commit_ready() {
80   running(running_index);
81 }
82
83 function commit_update() {
84   updating(running_index);
85 }
86
87 function commit_failed() {
88   current(running_index);
89   $('div.track').removeClass("running sending");
90 }
91
92 maximum_index = tracklist.length;
93 current_index = 0;
94 running_index = 0;
95
96 $(document).ready(init);