Merge "add progressive interval delay, extra checks in ajax row update polling"

This commit is contained in:
Jenkins 2012-03-13 19:28:37 +00:00 committed by Gerrit Code Review
commit d3f2552547
2 changed files with 37 additions and 4 deletions

View File

@ -45,17 +45,40 @@ var Horizon = function() {
update: function () {
var rows_to_update = $('tr.status_unknown');
if (rows_to_update.length) {
// Trigger the update handler.
var $updaters = rows_to_update.find('.ajax-update');
var interval = $updaters.attr('data-update-interval');
var $table = rows_to_update.closest('table');
var decay_constant = $table.attr('decay_constant');
// Do not update this row if the action column is expanded
if (rows_to_update.find('.actions_column .btn-group.open').length) {
// Wait and try to update again in next interval instead
setTimeout(horizon.datatables.update, interval);
// Remove interval decay, since this will not hit server
$table.removeAttr('decay_constant');
return;
}
// Trigger the update handler.
$updaters.click();
// Set interval decay to this table, and increase if it already exist
if(decay_constant === undefined) {
decay_constant = 1;
} else {
decay_constant++;
}
$table.attr('decay_constant', decay_constant);
// Poll until there are no rows in an "unknown" state on the page.
setTimeout(horizon.datatables.update, $updaters.attr('data-update-interval'));
next_poll = interval*decay_constant;
// Limit the interval to 30 secs
if(next_poll > 30*1000) next_poll = 30*1000;
setTimeout(horizon.datatables.update, next_poll);
}
},
validate_button: function () {
// Disable form button if checkbox are not checked
$("form").each(function (i) {
var checkboxes = $(this).find(":checkbox")
var checkboxes = $(this).find(":checkbox");
if(checkboxes.length == 0) {
// Do nothing if no checkboxes in this form
return;

View File

@ -28,11 +28,21 @@ horizon.addInitFunction(function() {
$('table').on('click', 'tr .ajax-update', function (evt) {
var $this = $(this);
var $table = $this.closest('table');
$.ajax($this.attr('href'), {
complete: function (jqXHR, status) {
var $new_row = $(jqXHR.responseText);
var $old_row = $this.closest('tr');
$new_row.find("td.status_unknown").prepend('<i class="icon-updating ajax-updating"></i>');
$this.closest('tr').replaceWith($new_row);
// Only replace row if the html content has changed
if($new_row.html() != $old_row.html()) {
if($old_row.find(':checkbox').is(':checked')) {
// Preserve the checkbox if it's already clicked
$new_row.find(':checkbox').prop('checked', true);
}
$old_row.replaceWith($new_row);
$table.removeAttr('decay_constant');
}
// Revalidate the button check for updated table
horizon.datatables.validate_button();
}