Last active
December 15, 2015 23:09
-
-
Save ChillPenguinX/5337925 to your computer and use it in GitHub Desktop.
tracked_requests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
State = { | |
locked: false, | |
push: function() { | |
if (State.locked) { return; } | |
var data = {}; | |
var current_tab = $('#main_tab_list .current-tab'); | |
var suffix = current_tab.attr('search_string'); | |
data['current_tab'] = current_tab.attr('id'); | |
data['exact_match'] = current_tab.attr('exact_match'); | |
data['workspace_id'] = current_tab.attr('workspace_id'); | |
data['search_string'] = suffix; | |
var visible_request_details = []; | |
$('.details-row:visible').each(function(){visible_request_details.push($(this).attr('request_id'))}); | |
data['visible_request_details'] = visible_request_details; | |
var url = (current_tab.hasClass('id') || current_tab.hasClass('most-recent') ? | |
$('#show_path').text() : $('#search_form').attr('action') + '?id=') + suffix; | |
window.history.pushState(data, 'Tracked Requests', url); | |
}, | |
restore: function(e) { | |
console.log('restore'); | |
var data = e.state; | |
if (data == null) { return; } | |
State.locked = true; | |
if (data['current_tab'] == 'tab_most_recent') { | |
Ticker.show(); | |
} else { | |
Search.auto_fill(data['search_string'], data['exact_match'], data['workspace_id']); | |
} | |
$('#details_dump').append($('.details-row:visible')); | |
var visible_request_details = data['visible_request_details']; | |
for (var i=0; i < visible_request_details.length; i++) { | |
show_details_for_request_by_id(visible_request_details[i]); | |
} | |
$('#search_string').val(data['search_string']); | |
State.locked = false; | |
} | |
} | |
Search = { | |
perform: function(data) { | |
var div_id = search_string_to_class_name($.trim($('#search_string').val()) + $('#exact_match').val()); | |
var workspace = $('#' + div_id + '.workspace'); | |
$('#search_fail_container').html(''); | |
if (workspace.size() == 0) { | |
$(this).addClass('mouse-disabled'); | |
$('#search_loader').removeClass('hide'); | |
$.get($(this).attr('action') + '?' + $(this).serialize() + '&ajax=true') | |
.success(Search.on_success) | |
.error(Search.on_error) | |
.complete(Search.on_complete); | |
} else { | |
$('.workspace').addClass('hide'); | |
workspace.removeClass('hide'); | |
$('#main_tab_list .current-tab').removeClass('current-tab'); | |
$('#tab_' + div_id).addClass('current-tab'); | |
} | |
return false; //keeps the form from making page refresh | |
}, | |
on_complete: function() { | |
$('#search_form').removeClass('mouse-disabled'); | |
$('#search_loader').addClass('hide'); | |
$('#exact_match').val(''); | |
State.push(); | |
Ticker.activate(); | |
}, | |
on_error: function() { | |
display_error('There was an error') | |
}, | |
on_success: function(data) { | |
var new_result = $(data); | |
var search_fail = new_result.find('.alert-error'); | |
if (search_fail.size() > 0) { | |
$('#search_fail_container').append(search_fail); | |
} else { | |
$('.workspace').addClass('hide'); | |
$('#main_tab_list .current-tab').removeClass('current-tab'); | |
var workspace = new_result.find('.workspace'); | |
var id = workspace.attr('id'); | |
if ($('#' + id + '.workspace').size() == 0) { | |
$('#workspace_container').append(workspace); | |
$('#main_tab_list').append(new_result.find('.current-tab')); | |
set_up_table_sorting(); | |
} else { | |
$('#' + id + '.workspace').removeClass('hide'); | |
$('#tab_' + id).addClass('current-tab'); | |
} | |
update_all_loaded_details(); | |
} | |
}, | |
auto_fill: function(search_string, exact_match, div_id) { | |
div_id = div_id || search_string_to_class_name(search_string + exact_match); | |
console.log(div_id); | |
var div = $('#' + div_id + '.workspace'); | |
if (div.size() == 0) { | |
$('#search_string').val(search_string); | |
$('#exact_match').val(exact_match); | |
$('#search_form').submit(); | |
if (exact_match == 'id') { | |
$('#search_string').val(''); | |
} | |
} else { | |
$('.workspace').addClass('hide'); | |
div.removeClass('hide'); | |
$('#main_tab_list .current-tab').removeClass('current-tab'); | |
$('#tab_' + div_id).addClass('current-tab'); | |
State.push(); | |
} | |
} | |
} | |
Ticker = { | |
is_active: false, | |
show: function() { | |
var ticker_tab = $('#tab_most_recent'); | |
if (ticker_tab.size() > 0) { | |
ticker_tab.click(); | |
} else { | |
$.get($('#button_show_most_recent').attr('action')) | |
.success(Search.on_success) | |
.error(Search.on_error) | |
.complete(Search.on_complete); | |
} | |
}, | |
activate: function() { | |
if ($('#most_recent').size() > 0 && !Ticker.is_active) { | |
setTimeout(Ticker.update, 5000); | |
Ticker.is_active = true; | |
} | |
}, | |
update: function() { | |
var ticker = $('#most_recent'); | |
if (ticker.size() > 0) { | |
$.get($('#update_path').text() + '?start_time=' + ticker.find('tbody tr:first .request-time').attr('rb_time')) | |
.success(Ticker.on_success) | |
.error(Ticker.on_error); | |
} else { | |
Ticker.is_active = false; | |
} | |
}, | |
on_success: function(data) { | |
Ticker.is_active = false; | |
var ticker = $('#most_recent'); | |
if (ticker.size() > 0) { | |
ticker.find('.request-row:first').before(data); | |
Ticker.activate(); | |
ticker.find('.request-row:gt(50)').remove(); | |
} | |
}, | |
on_error: function() { | |
Ticker.is_active = false; | |
display_error('There was an error updating the Live Ticker'); | |
} | |
} | |
var display_error = function(msg) { | |
var alert = $('<div>').addClass('alert alert-error'); | |
alert.text(msg); | |
alert.prepend($('<a>').addClass('close').attr('data-dismiss', 'alert').text('×')); | |
$('#search_fail_container').append(alert); | |
} | |
var open_selected_workspace = function() { | |
var tab = $(this); | |
if (!tab.hasClass('current-tab')) { | |
$('.workspace').addClass('hide'); | |
$('#' + tab.attr('workspace_id') + '.workspace').removeClass('hide'); | |
$('#main_tab_list .current-tab').removeClass('current-tab'); | |
tab.addClass('current-tab'); | |
$('#search_fail_container').html(''); | |
State.push(); | |
} | |
} | |
var search_string_to_class_name = function(string) { | |
return string.replace('.', '_dot_'); | |
} | |
var class_name_to_search_string = function(string) { | |
return string.replace('_dot_', '.'); | |
} | |
var remove_workspace = function() { | |
var tab_div = $(this).parents('.tab'); | |
$('#' + tab_div.attr('workspace_id') + '.workspace').remove(); | |
tab_div.remove(); | |
if ($('#main_tab_list .current-tab').size() == 0) { | |
$('#main_tab_list li:first').click(); | |
} | |
return false; //keeps the event from bubbling up, which would get to open_selected_workspace | |
} | |
var close_flash_alert = function() { | |
$(this).parent().remove(); | |
} | |
var go_to_workspace_for_searchable = function() { | |
var div = $(this); | |
Search.auto_fill(div.attr('search_string'), div.attr('exact_match')); | |
} | |
var show_clicked_details = function() { | |
show_details_for_request_by_id($(this).parents('tr').attr('request_id')); | |
} | |
var show_details_for_request_by_id = function(id) { | |
var request_row = $('#' + $('#main_tab_list .current-tab').attr('workspace_id') + | |
" .request-row[request_id='" + id + "']"); | |
var details_row = $('#request_' + id); | |
if (details_row.size() > 0) { | |
request_row.after(details_row); | |
State.push(); | |
} else { | |
$.get($('#show_path').text() + id + '?preview=true&ajax=true') | |
.success(on_request_details_success) | |
.error(on_request_details_error); | |
} | |
} | |
var on_request_details_success = function(data) { | |
var html = $(data); | |
var request_id = html.attr('request_id'); | |
if ($('#' + html.attr('id')).size() == 0) { | |
var request_row = $('#' + $('#main_tab_list .current-tab').attr('workspace_id') + | |
" .request-row[request_id='" + html.attr('request_id') + "']"); | |
if (request_row.size() > 0) { | |
request_row.after(html); | |
State.push(); | |
} else { | |
$('#details_dump').append(html); | |
} | |
update_loaded_details_for_id(request_id); | |
} else { | |
show_details_for_request_by_id(request_id); //we already have this somehow, so just use the one we already have | |
} | |
} | |
var on_request_details_error = function() { | |
display_error('error loading details'); | |
} | |
var hide_details = function() { | |
$('#details_dump').append($(this).parents('.details-row')); | |
} | |
var set_up_table_sorting = function() { | |
$('.tablesorter:visible').tablesorter({ | |
headers: { | |
5: {sorter: 'date-column'}, | |
6: {sorter: false} | |
}}); | |
} | |
var hide_visible_details = function() { | |
$('#details_dump').append($('.details-row:visible')); //detail rows don't sort | |
} | |
var update_all_loaded_details = function() { | |
//makes sure all the [details] links that have been loaded show that | |
$('.details-row').each(function() { | |
update_loaded_details_for_id($(this).attr('request_id')); | |
}); | |
} | |
var update_loaded_details_for_id = function(id) { | |
$(".request-row[request_id='" + id + "'] .show-details").addClass('details-loaded'); | |
} | |
var switch_request_tab = function() { | |
var tab = $(this); | |
var workspace = tab.parents('.workspace'); | |
workspace.find('.request-workspace').addClass('hide'); | |
workspace.find(".request-workspace[data_type='" + tab.attr('data_type') + "']").removeClass('hide'); | |
workspace.find('.current-tab').removeClass('current-tab'); | |
tab.addClass('current-tab'); | |
} | |
$(document).ready(function() { | |
if ($('#workspace_container').size() == 0) { return; } | |
$('#search_form').submit(Search.perform); | |
$('#workspace_container').on('click', '.searchable', go_to_workspace_for_searchable) | |
.on('click', '.show-details', show_clicked_details) | |
.on('click', '.hide-details', hide_details) | |
.on('click', '.tab', switch_request_tab) | |
.on('sortStart', '.tablesorter', hide_visible_details) | |
.on('click', '.hash-toggle', function(){ | |
$(this).parents('.recursion').toggleClass('show-json');}); | |
$('#main_tab_list').on('click', '.tab', open_selected_workspace) | |
.on('click', '.close', remove_workspace); | |
$('#search_fail_container').on('click', '.close', close_flash_alert); | |
$('#button_show_most_recent').click(Ticker.show); | |
window.onpopstate = State.restore; | |
$.tablesorter.addParser({ | |
id: 'date-column', | |
is: function(s) { return false; }, | |
type: 'numeric', | |
format: function(s) { | |
return new Date($($.trim(s)).attr('js_time')).getTime(); | |
} | |
}); | |
State.push(); | |
Ticker.activate(); | |
set_up_table_sorting(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment