]> CyberLeo.Net >> Repos - CDN/metadata.git/blob - views/handler_js.erb
Update gems
[CDN/metadata.git] / views / handler_js.erb
1 function format_length(seconds) {
2   var length = Math.round(seconds);
3   var minutes = Math.floor(length / 60);
4   var seconds = length % 60;
5   return minutes + ":" + ( "0" + seconds ).slice(-2);
6 }
7
8 function format_track(index, title, length, bpm, notes, cue_start, cue_bkdn, cue_next, cue_last, bars_in, bars_out) {
9   length = format_length(length);
10   bpm = Math.round(bpm);
11   cue_start = format_length(cue_start);
12   cue_bkdn = format_length(cue_bkdn);
13   cue_next = '-' + format_length(cue_next);
14   cue_last = '-' + format_length(cue_last);
15   if ( notes == null ) notes = '-';
16   entry = $('<div id="track_' + index + '" class="track" onClick="current(' + index + ');">');
17   entry.append($('<span class="index">' + index + '</span>'));
18
19   tn = $('<div class="title-notes">');
20   tn.append($('<span class="title" title="' + title + '">' + title.slice(0,65) + '</span><br>'));
21   tn.append($('<span class="notes" title="' + notes + '">' + notes.slice(0,80) + '</span>'));
22   entry.append(tn);
23
24   if ( index > 0 ) {
25     lb = $('<div class="length-bpm">');
26     lb.append($('<span class="length">' + length + '</span><br>'));
27     lb.append($('<span class="bpm">' + bpm + '<span>BPM</span></span>'));
28     entry.append(lb);
29
30     cues = $('<div class="cues">');
31     cues.append($('<span class="next">Next: ' + cue_next + '</span><br>'));
32     cues.append($('<span class="last">Last: ' + cue_last + '</span><br>'));
33     cues.append($('<span class="out">(' + bars_out + ' bars)</span>'));
34     entry.append(cues);
35
36     cues = $('<div class="cues">');
37     cues.append($('<span class="start">Start: ' + cue_start + '</span><br>'));
38     cues.append($('<span class="bkdn">Bkdn: ' + cue_bkdn + '</span><br>'));
39     cues.append($('<span class="in">(' + bars_in + ' bars)</span><br>'));
40     entry.append(cues);
41   }
42
43   return entry;
44 }
45
46 function init() {
47   for (var index in tracklist) {
48     track = tracklist[index];
49     entry = format_track(
50       track['index'], track['title'], track['length'], track['bpm'],
51       track['notes'],
52       track['start'], track['bkdn'], track['next'], track['last'],
53       track['in'], track['out']
54     );
55     $('#tracks').append(entry);
56   }
57 }
58
59 /* Track 0 is 'no current track' */
60 function current(index) {
61   if (index < 0 || index > maximum_index)
62     return;
63   current_index = index;
64   $("div.track").removeClass("current");
65   $("div.track > .index").off('click.commit');
66   $("div#track_" + index).addClass("current");
67   $("div#track_" + index + " > .index").on('click.commit', commit);
68 }
69
70 function updating(index) {
71   if (index < 0 || index > maximum_index)
72     return;
73   $("div.track").removeClass("running sending");
74   $("div#track_" + index).addClass("sending");
75 }
76
77 function running(index) {
78   if (index < 0 || index > maximum_index)
79     return;
80   $("div.track").removeClass("running sending");
81   $("div#track_" + index).addClass("running");
82 }
83
84 function next() {
85   current(current_index + 1);
86 }
87
88 function prev() {
89   current(current_index - 1);
90 }
91
92 function commit() {
93   running_index = current_index;
94   commit_update();
95
96   params = {
97     url: "/commit",
98     type: "POST",
99     data: { index: current_index }
100   };
101
102   var req = $.ajax(params)
103     .done(function(data, textStatus, req) {
104       commit_ready();
105     })
106     .fail(function(req, textStatus, errorThrown) {
107       console.log("AjaxFailed:" + textStatus + " - " + errorThrown);
108       commit_failed();
109     });
110 }
111
112 function commit_ready() {
113   running(running_index);
114 }
115
116 function commit_update() {
117   updating(running_index);
118 }
119
120 function commit_failed() {
121   current(running_index);
122   $('div.track').removeClass("running sending");
123 }
124
125 maximum_index = tracklist.length;
126 current_index = 0;
127 running_index = 0;
128
129 $(document).ready(init);