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); 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).addClass("current"); scroll_to(index); } function running(index) { if (index < 0 || index > maximum_index) return; running_index = index; $("div.track").removeClass("running"); $("div#track_" + index).addClass("running"); } function scroll_to(index) { // Scroll so that this tile is at the top third. If beyond page boundaries, clamp. var height = $(document).height(); var viewport = $(window).height() / 3; var target = height * (index / maximum_index) - viewport; $('html,body').animate({scrollTop: target}, 250); } function next() { current(current_index + 1); } function prev() { current(current_index - 1); } function commit() { running(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() { var e = $('#commit'); e.removeClass('update'); e.removeClass('failed'); e.html('✓'); } function commit_update() { var e = $('#commit'); e.removeClass('failed'); e.addClass('update'); e.html('…'); } function commit_failed() { var e = $('#commit'); e.removeClass('update'); e.addClass('failed'); e.html('✗'); } maximum_index = tracklist.length; current_index = 0; running_index = 0; init();