function format_track(index, title, length, bpm, notes) { length = Math.round(length); var minutes = Math.floor(length / 60); var seconds = length % 60; var length = minutes + ":" + ( "0" + seconds ).slice(-2); var bpm = Math.round(bpm); if ( notes == null ) notes = '-'; entry = $('
'); entry.append($('' + index + '')); entry.append($('' + title.slice(0,65) + '')); entry.append($('' + length + '')); entry.append($('')); entry.append($('' + bpm + ' BPM')); entry.append($('' + notes.slice(0,80) + '')); return entry; } function init() { for (var index in tracklist) { track = tracklist[index]; entry = format_track(track['index'], track['title'], track['length'], track['bpm'], track['notes']); $('#tracks').append(entry); } } /* Track 0 is 'no current track' */ function current(index) { if (index < 0 || index > maximum_index) return; current_index = index; $("div.track").removeClass("current"); $("div.track > .index").off('click.commit'); $("div#track_" + index).addClass("current"); $("div#track_" + index + " > .index").on('click.commit', commit); } function updating(index) { if (index < 0 || index > maximum_index) return; $("div.track").removeClass("running sending"); $("div#track_" + index).addClass("sending"); } function running(index) { if (index < 0 || index > maximum_index) return; $("div.track").removeClass("running sending"); $("div#track_" + index).addClass("running"); } function next() { current(current_index + 1); } function prev() { current(current_index - 1); } function commit() { running_index = current_index; commit_update(); params = { url: "/commit", type: "POST", data: { index: current_index } }; var req = $.ajax(params) .done(function(data, textStatus, req) { commit_ready(); }) .fail(function(req, textStatus, errorThrown) { console.log("AjaxFailed:" + textStatus + " - " + errorThrown); commit_failed(); }); } function commit_ready() { running(running_index); } function commit_update() { updating(running_index); } function commit_failed() { current(running_index); $('div.track').removeClass("running sending"); } maximum_index = tracklist.length; current_index = 0; running_index = 0; $(document).ready(init);